From c89cda66e587935b07d8c98cbd92e41bfe0d6ade Mon Sep 17 00:00:00 2001 From: filoghost Date: Sat, 15 Aug 2020 10:53:37 +0200 Subject: [PATCH] Make configuration values static --- .../chestcommands/ChestCommands.java | 19 +++--------------- .../attribute/LoreAttribute.java | 4 ++-- .../attribute/NameAttribute.java | 4 ++-- .../chestcommands/command/CommandHandler.java | 9 +++++---- .../chestcommands/config/ConfigManager.java | 10 ++++------ .../filoghost/chestcommands/config/Lang.java | 20 ++++++++++--------- .../chestcommands/config/Settings.java | 10 ++++++---- .../icon/requirement/RequiredExpLevel.java | 4 ++-- .../icon/requirement/RequiredMoney.java | 4 ++-- .../icon/requirement/RequiredPermission.java | 4 ++-- .../icon/requirement/item/RequiredItems.java | 6 +++--- .../listener/InventoryListener.java | 3 ++- .../chestcommands/listener/JoinListener.java | 3 ++- .../chestcommands/listener/SignListener.java | 4 ++-- .../chestcommands/menu/InternalIconMenu.java | 4 ++-- 15 files changed, 50 insertions(+), 58 deletions(-) diff --git a/plugin/src/main/java/me/filoghost/chestcommands/ChestCommands.java b/plugin/src/main/java/me/filoghost/chestcommands/ChestCommands.java index 002c5bd..12033b9 100644 --- a/plugin/src/main/java/me/filoghost/chestcommands/ChestCommands.java +++ b/plugin/src/main/java/me/filoghost/chestcommands/ChestCommands.java @@ -9,7 +9,6 @@ import me.filoghost.chestcommands.api.internal.BackendAPI; import me.filoghost.chestcommands.command.CommandHandler; import me.filoghost.chestcommands.config.ConfigManager; import me.filoghost.chestcommands.config.CustomPlaceholders; -import me.filoghost.chestcommands.config.Lang; import me.filoghost.chestcommands.config.Settings; import me.filoghost.chestcommands.hook.BarAPIHook; import me.filoghost.chestcommands.hook.BungeeCordHook; @@ -55,8 +54,6 @@ public class ChestCommands extends BaseJavaPlugin { private static ConfigManager configManager; private static MenuManager menuManager; - private static Settings settings; - private static Lang lang; private static CustomPlaceholders placeholders; private static ErrorCollector lastLoadErrors; @@ -84,8 +81,6 @@ public class ChestCommands extends BaseJavaPlugin { Log.setLogger(getLogger()); configManager = new ConfigManager(getDataFolderPath()); menuManager = new MenuManager(); - settings = new Settings(); - lang = new Lang(); placeholders = new CustomPlaceholders(); BackendAPI.setImplementation(new DefaultBackendAPI()); @@ -109,7 +104,7 @@ public class ChestCommands extends BaseJavaPlugin { Log.info("Hooked PlaceholderAPI"); } - if (settings.update_notifications) { + if (Settings.update_notifications) { UpdateChecker.run(this, 56919, (String newVersion) -> { ChestCommands.newVersion = newVersion; @@ -172,8 +167,8 @@ public class ChestCommands extends BaseJavaPlugin { errorCollector.add(Errors.Upgrade.failedSomeUpgrades); } - settings = configManager.tryLoadSettings(errorCollector); - lang = configManager.tryLoadLang(errorCollector); + configManager.tryLoadSettings(errorCollector); + configManager.tryLoadLang(errorCollector); placeholders = configManager.tryLoadCustomPlaceholders(errorCollector); PlaceholderManager.setStaticPlaceholders(placeholders.getPlaceholders()); @@ -213,14 +208,6 @@ public class ChestCommands extends BaseJavaPlugin { return menuManager; } - public static Settings getSettings() { - return settings; - } - - public static Lang getLang() { - return lang; - } - public static boolean hasNewVersion() { return newVersion != null; } diff --git a/plugin/src/main/java/me/filoghost/chestcommands/attribute/LoreAttribute.java b/plugin/src/main/java/me/filoghost/chestcommands/attribute/LoreAttribute.java index d1c51fb..4411d05 100644 --- a/plugin/src/main/java/me/filoghost/chestcommands/attribute/LoreAttribute.java +++ b/plugin/src/main/java/me/filoghost/chestcommands/attribute/LoreAttribute.java @@ -5,7 +5,7 @@ */ package me.filoghost.chestcommands.attribute; -import me.filoghost.chestcommands.ChestCommands; +import me.filoghost.chestcommands.config.Settings; import me.filoghost.chestcommands.icon.InternalConfigurableIcon; import me.filoghost.commons.Colors; import me.filoghost.commons.collection.CollectionUtils; @@ -26,7 +26,7 @@ public class LoreAttribute implements IconAttribute { if (line.isEmpty()) { return line; } else if (line.charAt(0) != ChatColor.COLOR_CHAR) { - return ChestCommands.getSettings().default_color__lore + Colors.addColors(line); + return Settings.default_color__lore + Colors.addColors(line); } else { return Colors.addColors(line); } diff --git a/plugin/src/main/java/me/filoghost/chestcommands/attribute/NameAttribute.java b/plugin/src/main/java/me/filoghost/chestcommands/attribute/NameAttribute.java index 637dbe8..2fdef46 100644 --- a/plugin/src/main/java/me/filoghost/chestcommands/attribute/NameAttribute.java +++ b/plugin/src/main/java/me/filoghost/chestcommands/attribute/NameAttribute.java @@ -5,7 +5,7 @@ */ package me.filoghost.chestcommands.attribute; -import me.filoghost.chestcommands.ChestCommands; +import me.filoghost.chestcommands.config.Settings; import me.filoghost.chestcommands.icon.InternalConfigurableIcon; import me.filoghost.commons.Colors; import org.bukkit.ChatColor; @@ -24,7 +24,7 @@ public class NameAttribute implements IconAttribute { } if (input.charAt(0) != ChatColor.COLOR_CHAR) { - return ChestCommands.getSettings().default_color__name + Colors.addColors(input); + return Settings.default_color__name + Colors.addColors(input); } else { return Colors.addColors(input); } diff --git a/plugin/src/main/java/me/filoghost/chestcommands/command/CommandHandler.java b/plugin/src/main/java/me/filoghost/chestcommands/command/CommandHandler.java index 99ec5a9..b4e44c0 100644 --- a/plugin/src/main/java/me/filoghost/chestcommands/command/CommandHandler.java +++ b/plugin/src/main/java/me/filoghost/chestcommands/command/CommandHandler.java @@ -7,6 +7,7 @@ package me.filoghost.chestcommands.command; import me.filoghost.chestcommands.ChestCommands; import me.filoghost.chestcommands.Permissions; +import me.filoghost.chestcommands.config.Lang; import me.filoghost.chestcommands.menu.InternalIconMenu; import me.filoghost.chestcommands.menu.MenuManager; import me.filoghost.chestcommands.util.Utils; @@ -119,12 +120,12 @@ public class CommandHandler extends CommandFramework { } if (sender.getName().equalsIgnoreCase(target.getName())) { - if (!ChestCommands.getLang().open_menu.isEmpty()) { - sender.sendMessage(ChestCommands.getLang().open_menu.replace("{menu}", menuName)); + if (!Lang.open_menu.isEmpty()) { + sender.sendMessage(Lang.open_menu.replace("{menu}", menuName)); } } else { - if (!ChestCommands.getLang().open_menu_others.isEmpty()) { - sender.sendMessage(ChestCommands.getLang().open_menu_others.replace("{menu}", menuName).replace("{player}", target.getName())); + if (!Lang.open_menu_others.isEmpty()) { + sender.sendMessage(Lang.open_menu_others.replace("{menu}", menuName).replace("{player}", target.getName())); } } diff --git a/plugin/src/main/java/me/filoghost/chestcommands/config/ConfigManager.java b/plugin/src/main/java/me/filoghost/chestcommands/config/ConfigManager.java index 54b5fa0..77642c4 100644 --- a/plugin/src/main/java/me/filoghost/chestcommands/config/ConfigManager.java +++ b/plugin/src/main/java/me/filoghost/chestcommands/config/ConfigManager.java @@ -40,21 +40,19 @@ public class ConfigManager extends BaseConfigManager { langConfigLoader = getMappedConfigLoader("lang.yml", Lang::new); } - public Settings tryLoadSettings(ErrorCollector errorCollector) { + public void tryLoadSettings(ErrorCollector errorCollector) { try { - return settingsConfigLoader.init(); + settingsConfigLoader.init(); } catch (ConfigException e) { logConfigInitException(errorCollector, settingsConfigLoader.getFile(), e); - return new Settings(); } } - public Lang tryLoadLang(ErrorCollector errorCollector) { + public void tryLoadLang(ErrorCollector errorCollector) { try { - return langConfigLoader.init(); + langConfigLoader.init(); } catch (ConfigException e) { logConfigInitException(errorCollector, langConfigLoader.getFile(), e); - return new Lang(); } } diff --git a/plugin/src/main/java/me/filoghost/chestcommands/config/Lang.java b/plugin/src/main/java/me/filoghost/chestcommands/config/Lang.java index 87b164b..0f30b8b 100644 --- a/plugin/src/main/java/me/filoghost/chestcommands/config/Lang.java +++ b/plugin/src/main/java/me/filoghost/chestcommands/config/Lang.java @@ -6,20 +6,22 @@ package me.filoghost.chestcommands.config; import me.filoghost.chestcommands.logging.Errors; +import me.filoghost.commons.config.mapped.IncludeStatic; import me.filoghost.commons.config.mapped.MappedConfig; import me.filoghost.commons.config.mapped.modifier.ChatColors; @ChatColors +@IncludeStatic public class Lang extends MappedConfig { - public String no_open_permission = "&cYou don't have permission &e{permission} &cto use this menu."; - public String default_no_icon_permission = "&cYou don't have permission for this icon."; - public String no_required_item = "&cYou must have &e{amount}x {material} &c(durability: {durability}) for this."; - public String no_money = "&cYou need {money}$ for this."; - public String no_exp = "&cYou need {levels} XP levels for this."; - public String menu_not_found = "&cMenu not found! " + Errors.User.notifyStaffRequest; - public String open_menu = "&aOpening the menu \"{menu}\"."; - public String open_menu_others = "&aOpening the menu \"{menu}\" to {player}."; - public String any = "any"; // Used in no_required_item when durability is not restrictive + public static String no_open_permission = "&cYou don't have permission &e{permission} &cto use this menu."; + public static String default_no_icon_permission = "&cYou don't have permission for this icon."; + public static String no_required_item = "&cYou must have &e{amount}x {material} &c(durability: {durability}) for this."; + public static String no_money = "&cYou need {money}$ for this."; + public static String no_exp = "&cYou need {levels} XP levels for this."; + public static String menu_not_found = "&cMenu not found! " + Errors.User.notifyStaffRequest; + public static String open_menu = "&aOpening the menu \"{menu}\"."; + public static String open_menu_others = "&aOpening the menu \"{menu}\" to {player}."; + public static String any = "any"; // Used in no_required_item when durability is not restrictive } diff --git a/plugin/src/main/java/me/filoghost/chestcommands/config/Settings.java b/plugin/src/main/java/me/filoghost/chestcommands/config/Settings.java index 7a8aa41..86fc1c5 100644 --- a/plugin/src/main/java/me/filoghost/chestcommands/config/Settings.java +++ b/plugin/src/main/java/me/filoghost/chestcommands/config/Settings.java @@ -5,16 +5,18 @@ */ package me.filoghost.chestcommands.config; +import me.filoghost.commons.config.mapped.IncludeStatic; import me.filoghost.commons.config.mapped.MappedConfig; import me.filoghost.commons.config.mapped.modifier.ChatColors; @ChatColors +@IncludeStatic public class Settings extends MappedConfig { - public String default_color__name = "&f"; - public String default_color__lore = "&7"; - public boolean update_notifications = true; - public int anti_click_spam_delay = 200; + public static String default_color__name = "&f"; + public static String default_color__lore = "&7"; + public static boolean update_notifications = true; + public static int anti_click_spam_delay = 200; public Settings() { setHeader( diff --git a/plugin/src/main/java/me/filoghost/chestcommands/icon/requirement/RequiredExpLevel.java b/plugin/src/main/java/me/filoghost/chestcommands/icon/requirement/RequiredExpLevel.java index 5ecf235..e2b2d0c 100644 --- a/plugin/src/main/java/me/filoghost/chestcommands/icon/requirement/RequiredExpLevel.java +++ b/plugin/src/main/java/me/filoghost/chestcommands/icon/requirement/RequiredExpLevel.java @@ -6,7 +6,7 @@ package me.filoghost.chestcommands.icon.requirement; import com.google.common.base.Preconditions; -import me.filoghost.chestcommands.ChestCommands; +import me.filoghost.chestcommands.config.Lang; import org.bukkit.entity.Player; public class RequiredExpLevel implements Requirement { @@ -21,7 +21,7 @@ public class RequiredExpLevel implements Requirement { @Override public boolean hasCost(Player player) { if (player.getLevel() < levels) { - player.sendMessage(ChestCommands.getLang().no_exp.replace("{levels}", Integer.toString(levels))); + player.sendMessage(Lang.no_exp.replace("{levels}", Integer.toString(levels))); return false; } diff --git a/plugin/src/main/java/me/filoghost/chestcommands/icon/requirement/RequiredMoney.java b/plugin/src/main/java/me/filoghost/chestcommands/icon/requirement/RequiredMoney.java index 21c911b..7a60e5f 100644 --- a/plugin/src/main/java/me/filoghost/chestcommands/icon/requirement/RequiredMoney.java +++ b/plugin/src/main/java/me/filoghost/chestcommands/icon/requirement/RequiredMoney.java @@ -6,7 +6,7 @@ package me.filoghost.chestcommands.icon.requirement; import com.google.common.base.Preconditions; -import me.filoghost.chestcommands.ChestCommands; +import me.filoghost.chestcommands.config.Lang; import me.filoghost.chestcommands.hook.VaultEconomyHook; import me.filoghost.chestcommands.logging.Errors; import org.bukkit.entity.Player; @@ -30,7 +30,7 @@ public class RequiredMoney implements Requirement { } if (!VaultEconomyHook.hasMoney(player, moneyAmount)) { - player.sendMessage(ChestCommands.getLang().no_money.replace("{money}", VaultEconomyHook.formatMoney(moneyAmount))); + player.sendMessage(Lang.no_money.replace("{money}", VaultEconomyHook.formatMoney(moneyAmount))); return false; } diff --git a/plugin/src/main/java/me/filoghost/chestcommands/icon/requirement/RequiredPermission.java b/plugin/src/main/java/me/filoghost/chestcommands/icon/requirement/RequiredPermission.java index 8337aae..afd5eee 100644 --- a/plugin/src/main/java/me/filoghost/chestcommands/icon/requirement/RequiredPermission.java +++ b/plugin/src/main/java/me/filoghost/chestcommands/icon/requirement/RequiredPermission.java @@ -5,7 +5,7 @@ */ package me.filoghost.chestcommands.icon.requirement; -import me.filoghost.chestcommands.ChestCommands; +import me.filoghost.chestcommands.config.Lang; import me.filoghost.commons.Strings; import org.bukkit.entity.Player; @@ -48,7 +48,7 @@ public class RequiredPermission implements Requirement { if (noPermissionMessage != null) { player.sendMessage(noPermissionMessage); } else { - player.sendMessage(ChestCommands.getLang().default_no_icon_permission); + player.sendMessage(Lang.default_no_icon_permission); } return false; } diff --git a/plugin/src/main/java/me/filoghost/chestcommands/icon/requirement/item/RequiredItems.java b/plugin/src/main/java/me/filoghost/chestcommands/icon/requirement/item/RequiredItems.java index 7fbafa3..ce696f2 100644 --- a/plugin/src/main/java/me/filoghost/chestcommands/icon/requirement/item/RequiredItems.java +++ b/plugin/src/main/java/me/filoghost/chestcommands/icon/requirement/item/RequiredItems.java @@ -6,7 +6,7 @@ package me.filoghost.chestcommands.icon.requirement.item; import com.google.common.collect.ImmutableList; -import me.filoghost.chestcommands.ChestCommands; +import me.filoghost.chestcommands.config.Lang; import me.filoghost.chestcommands.icon.requirement.Requirement; import me.filoghost.chestcommands.util.Utils; import org.bukkit.entity.Player; @@ -28,10 +28,10 @@ public class RequiredItems implements Requirement { if (!hasItems) { for (RequiredItem item : items) { - player.sendMessage(ChestCommands.getLang().no_required_item + player.sendMessage(Lang.no_required_item .replace("{material}", Utils.formatEnum(item.getMaterial())) .replace("{amount}", Integer.toString(item.getAmount())) - .replace("{durability}", item.hasRestrictiveDurability() ? Short.toString(item.getDurability()) : ChestCommands.getLang().any)); + .replace("{durability}", item.hasRestrictiveDurability() ? Short.toString(item.getDurability()) : Lang.any)); } } diff --git a/plugin/src/main/java/me/filoghost/chestcommands/listener/InventoryListener.java b/plugin/src/main/java/me/filoghost/chestcommands/listener/InventoryListener.java index 4ac8ba4..6efb09a 100644 --- a/plugin/src/main/java/me/filoghost/chestcommands/listener/InventoryListener.java +++ b/plugin/src/main/java/me/filoghost/chestcommands/listener/InventoryListener.java @@ -7,6 +7,7 @@ package me.filoghost.chestcommands.listener; import me.filoghost.chestcommands.ChestCommands; import me.filoghost.chestcommands.api.ClickResult; +import me.filoghost.chestcommands.config.Settings; import me.filoghost.chestcommands.inventory.DefaultMenuInventory; import me.filoghost.chestcommands.inventory.DefaultMenuInventory.SlotClickHandler; import me.filoghost.chestcommands.menu.MenuManager; @@ -69,7 +70,7 @@ public class InventoryListener implements Listener { Long cooldownUntil = antiClickSpam.get(clicker); long now = System.currentTimeMillis(); - int minDelay = ChestCommands.getSettings().anti_click_spam_delay; + int minDelay = Settings.anti_click_spam_delay; if (minDelay > 0) { if (cooldownUntil != null && cooldownUntil > now) { diff --git a/plugin/src/main/java/me/filoghost/chestcommands/listener/JoinListener.java b/plugin/src/main/java/me/filoghost/chestcommands/listener/JoinListener.java index 166597c..61b986f 100644 --- a/plugin/src/main/java/me/filoghost/chestcommands/listener/JoinListener.java +++ b/plugin/src/main/java/me/filoghost/chestcommands/listener/JoinListener.java @@ -7,6 +7,7 @@ package me.filoghost.chestcommands.listener; import me.filoghost.chestcommands.ChestCommands; import me.filoghost.chestcommands.Permissions; +import me.filoghost.chestcommands.config.Settings; import org.bukkit.ChatColor; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -25,7 +26,7 @@ public class JoinListener implements Listener { + " error(s) last time it was loaded. You can see them by doing \"/cc reload\" in the console."); } - if (ChestCommands.hasNewVersion() && ChestCommands.getSettings().update_notifications && player.hasPermission(Permissions.UPDATE_NOTIFICATIONS)) { + if (ChestCommands.hasNewVersion() && Settings.update_notifications && player.hasPermission(Permissions.UPDATE_NOTIFICATIONS)) { player.sendMessage(ChestCommands.CHAT_PREFIX + "Found an update: " + ChestCommands.getNewVersion() + ". Download:"); player.sendMessage(ChatColor.DARK_GREEN + ">> " + ChatColor.GREEN + "http://dev.bukkit.org/bukkit-plugins/chest-commands"); } diff --git a/plugin/src/main/java/me/filoghost/chestcommands/listener/SignListener.java b/plugin/src/main/java/me/filoghost/chestcommands/listener/SignListener.java index ba74942..01a71c7 100644 --- a/plugin/src/main/java/me/filoghost/chestcommands/listener/SignListener.java +++ b/plugin/src/main/java/me/filoghost/chestcommands/listener/SignListener.java @@ -5,8 +5,8 @@ */ package me.filoghost.chestcommands.listener; -import me.filoghost.chestcommands.ChestCommands; import me.filoghost.chestcommands.Permissions; +import me.filoghost.chestcommands.config.Lang; import me.filoghost.chestcommands.menu.InternalIconMenu; import me.filoghost.chestcommands.menu.MenuManager; import me.filoghost.chestcommands.util.Utils; @@ -59,7 +59,7 @@ public class SignListener implements Listener { InternalIconMenu menu = menuManager.getMenuByFileName(menuFileName); if (menu == null) { - event.getPlayer().sendMessage(ChestCommands.getLang().menu_not_found); + event.getPlayer().sendMessage(Lang.menu_not_found); return; } diff --git a/plugin/src/main/java/me/filoghost/chestcommands/menu/InternalIconMenu.java b/plugin/src/main/java/me/filoghost/chestcommands/menu/InternalIconMenu.java index da10034..3d52714 100644 --- a/plugin/src/main/java/me/filoghost/chestcommands/menu/InternalIconMenu.java +++ b/plugin/src/main/java/me/filoghost/chestcommands/menu/InternalIconMenu.java @@ -6,10 +6,10 @@ package me.filoghost.chestcommands.menu; import com.google.common.collect.ImmutableList; -import me.filoghost.chestcommands.ChestCommands; import me.filoghost.chestcommands.Permissions; import me.filoghost.chestcommands.action.Action; import me.filoghost.chestcommands.api.MenuInventory; +import me.filoghost.chestcommands.config.Lang; import me.filoghost.commons.collection.CollectionUtils; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -71,7 +71,7 @@ public class InternalIconMenu extends BaseIconMenu { } public void sendNoOpenPermissionMessage(CommandSender sender) { - String noPermMessage = ChestCommands.getLang().no_open_permission; + String noPermMessage = Lang.no_open_permission; if (noPermMessage != null && !noPermMessage.isEmpty()) { sender.sendMessage(noPermMessage.replace("{permission}", this.openPermission)); }