Make configuration values static

This commit is contained in:
filoghost 2020-08-15 10:53:37 +02:00
parent 94ab7624f7
commit c89cda66e5
15 changed files with 50 additions and 58 deletions

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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()));
}
}

View File

@ -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();
}
}

View File

@ -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
}

View File

@ -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(

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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));
}
}

View File

@ -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) {

View File

@ -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");
}

View File

@ -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;
}

View File

@ -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));
}