From 78df061bc92e932fb5c0356e934f9071e0a8663d Mon Sep 17 00:00:00 2001 From: TechnicJelle <22576047+TechnicJelle@users.noreply.github.com> Date: Sat, 6 May 2023 03:42:08 +0200 Subject: [PATCH] Config for marker set properties and duration Finishes point 2. in #1 --- .../BlueMapChatMarkers.java | 20 +++++----- .../bluemapchatmarkers/Config.java | 40 +++++++++++++++++++ src/main/resources/config.yml | 8 ++++ 3 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/technicjelle/bluemapchatmarkers/Config.java create mode 100644 src/main/resources/config.yml diff --git a/src/main/java/com/technicjelle/bluemapchatmarkers/BlueMapChatMarkers.java b/src/main/java/com/technicjelle/bluemapchatmarkers/BlueMapChatMarkers.java index c37384a..26bdd01 100644 --- a/src/main/java/com/technicjelle/bluemapchatmarkers/BlueMapChatMarkers.java +++ b/src/main/java/com/technicjelle/bluemapchatmarkers/BlueMapChatMarkers.java @@ -22,11 +22,9 @@ import java.util.function.Consumer; public final class BlueMapChatMarkers extends JavaPlugin implements Listener { - private final String MARKERSET_ID = "chat-markers"; + private Config config; private UpdateChecker updateChecker; - private final int seconds = 60; //TODO: Make this configurable - @Override public void onEnable() { new Metrics(this, 16424); @@ -42,6 +40,8 @@ public void onEnable() { Consumer onEnableListener = api -> { updateChecker.logUpdateMessage(getLogger()); + config = new Config(this); + String styleFile = "textStyle.css"; try { MCUtils.copyPluginResourceToConfigDir(this, styleFile, styleFile, false); @@ -52,14 +52,12 @@ public void onEnable() { }; private void createMarkerSet(BlueMapWorld bmWorld) { - getLogger().info("Creating MarkerSet for BlueMap World " + bmWorld.getId()); - MarkerSet markerSet = new MarkerSet("Chat Markers"); - markerSet.setDefaultHidden(false); - markerSet.setToggleable(true); + getLogger().info("Creating MarkerSet for BlueMap World '" + bmWorld.getSaveFolder().getFileName() + "'"); + MarkerSet markerSet = new MarkerSet(config.markerSetName, config.toggleable, config.defaultHidden); //add the markerset to all BlueMap maps of this world for (BlueMapMap map : bmWorld.getMaps()) { - map.getMarkerSets().put(MARKERSET_ID, markerSet); + map.getMarkerSets().put(Config.MARKER_SET_ID, markerSet); } } @@ -83,10 +81,10 @@ public void onPlayerChat(AsyncPlayerChatEvent event) { //for all BlueMap Maps belonging to the BlueMap World the Player is in, add the Marker to the MarkerSet of that BlueMap World bmWorld.getMaps().forEach(map -> { - if (!map.getMarkerSets().containsKey(MARKERSET_ID)) //if this world doesn't have a MarkerSet yet, create it + if (!map.getMarkerSets().containsKey(Config.MARKER_SET_ID)) //if this world doesn't have a MarkerSet yet, create it createMarkerSet(bmWorld); //creates a new MarkerSet, and assigns it to each Map of this World - MarkerSet markerSet = map.getMarkerSets().get(MARKERSET_ID); + MarkerSet markerSet = map.getMarkerSets().get(Config.MARKER_SET_ID); String key = "chat-marker_" + event.hashCode(); @@ -96,7 +94,7 @@ public void onPlayerChat(AsyncPlayerChatEvent event) { //wait Seconds and remove the Marker Bukkit.getScheduler().runTaskLater(this, () -> markerSet.remove(key), - seconds * 20); + config.markerDuration * 20L); }); }); } diff --git a/src/main/java/com/technicjelle/bluemapchatmarkers/Config.java b/src/main/java/com/technicjelle/bluemapchatmarkers/Config.java new file mode 100644 index 0000000..0f65532 --- /dev/null +++ b/src/main/java/com/technicjelle/bluemapchatmarkers/Config.java @@ -0,0 +1,40 @@ +package com.technicjelle.bluemapchatmarkers; + +import com.technicjelle.MCUtils; +import org.bukkit.configuration.file.FileConfiguration; + +import java.io.IOException; + +public class Config { + public static final String MARKER_SET_ID = "chat-markers"; + + private final BlueMapChatMarkers plugin; + + public String markerSetName; + public boolean toggleable; + public boolean defaultHidden; + public long markerDuration; + + public Config(BlueMapChatMarkers plugin) { + this.plugin = plugin; + + try { + MCUtils.copyPluginResourceToConfigDir(plugin, "config.yml", "config.yml", false); + } catch (IOException e) { + throw new RuntimeException(e); + } + + //Load config from disk + plugin.reloadConfig(); + + //Load config values into variables + markerSetName = configFile().getString("MarkerSetName"); + toggleable = configFile().getBoolean("Toggleable"); + defaultHidden = configFile().getBoolean("DefaultHidden"); + markerDuration = configFile().getLong("MarkerDuration"); + } + + private FileConfiguration configFile() { + return plugin.getConfig(); + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..6d4e57f --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,8 @@ +# Marker Set Options +# Documentation: https://bluemap.bluecolored.de/wiki/customization/Markers.html#marker-sets +MarkerSetName: "Chat Messages" +Toggleable: true +DefaultHidden: false + +# Amount of time a chat marker stays on the map (in seconds) +MarkerDuration: 60