From b19f75277df1cdf346fdbc1a72dbded0b25a5653 Mon Sep 17 00:00:00 2001 From: evyatar Date: Sun, 30 Jul 2023 23:39:13 +0100 Subject: [PATCH] Fixed possible performance issue when reading messages. see changes in INeedTheNight.java --- .../com/shinybunny/intn/INeedTheNight.java | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/shinybunny/intn/INeedTheNight.java b/src/main/java/com/shinybunny/intn/INeedTheNight.java index 1ff0378..7ec23f0 100644 --- a/src/main/java/com/shinybunny/intn/INeedTheNight.java +++ b/src/main/java/com/shinybunny/intn/INeedTheNight.java @@ -5,12 +5,12 @@ import org.bukkit.plugin.java.JavaPlugin; import revxrsal.commands.bukkit.BukkitCommandHandler; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import javax.annotation.Nullable; +import java.util.*; public final class INeedTheNight extends JavaPlugin { public final Set needTheNight = new HashSet<>(); + private Map messages; @Override public void onEnable() { @@ -20,6 +20,8 @@ public void onEnable() { Bukkit.getPluginManager().registerEvents(new SleepingListener(this), this); TimeChecker timeChecker = new TimeChecker(this); timeChecker.start(); + + loadMessages(); } @Override @@ -27,8 +29,13 @@ public void onDisable() { // Plugin shutdown logic } - public String getMessage(String key) { - return getConfig().getString(key, "Error: missing message " + key); + /** + * Gets the requested message from the config file. Might return null if key does not exist. + * @param key The Key leading to the message. Sections are seperated with a dot. + * @return The message stored on the given key, or alternatively null if there is no value for the given key. + */ + public @Nullable String getMessage(String key) { + return messages.get(key); } public static String combineNames(List items) { @@ -53,4 +60,13 @@ public static String combineNames(List items) { } return str.toString(); } + + private Map loadMessages() { + Map messages = new HashMap<>(); + + getConfig().getKeys(true).forEach(key -> messages.put(key, getConfig().getString(key))); + + return messages; + } + }