diff --git a/src/main/java/org/shanerx/tradeshop/data/config/ConfigManager.java b/src/main/java/org/shanerx/tradeshop/data/config/ConfigManager.java index b8241e72..44537f60 100644 --- a/src/main/java/org/shanerx/tradeshop/data/config/ConfigManager.java +++ b/src/main/java/org/shanerx/tradeshop/data/config/ConfigManager.java @@ -29,6 +29,7 @@ import com.google.common.base.Charsets; import com.google.common.io.Files; import org.apache.commons.lang.Validate; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; @@ -41,8 +42,10 @@ import java.io.Writer; import java.util.Arrays; import java.util.Comparator; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Set; import java.util.logging.Level; public class ConfigManager { @@ -78,7 +81,7 @@ public String colour(String toColour) { return ChatColor.translateAlternateColorCodes('&', toColour); } - public void load() { + public boolean load() { try { if (!PLUGIN.getDataFolder().isDirectory()) { PLUGIN.getDataFolder().mkdirs(); @@ -93,23 +96,24 @@ public void load() { config = YamlConfiguration.loadConfiguration(file); - setDefaults(); + return setDefaults(); } public void reload() { - load(); + Set hasUpgraded = new HashSet<>(); + hasUpgraded.add(load()); switch (configType) { case CONFIG: - Setting.upgrade(); + hasUpgraded.add(Setting.upgrade()); PLUGIN.setUseInternalPerms(Setting.USE_INTERNAL_PERMISSIONS.getBoolean()); break; case MESSAGES: - Message.upgrade(); + hasUpgraded.add(Message.upgrade()); break; } - save(); + save(hasUpgraded.contains(true)); PLUGIN.setSkipHopperProtection( Setting.BITRADESHOP_HOPPER_EXPORT.getBoolean() && @@ -118,15 +122,28 @@ public void reload() { Setting.TRADESHOP_HOPPER_EXPORT.getBoolean()); } - public void setDefaults() { + public boolean setDefaults() { + Set hasUpgraded = new HashSet<>(); + switch (configType) { case CONFIG: - Arrays.stream(Setting.values()).forEach((setting) -> addKeyValue(setting.getPath(), setting.getDefaultValue())); + Arrays.stream(Setting.values()).forEach((setting) -> hasUpgraded.add(addKeyValue(setting.getPath(), setting.getDefaultValue()))); break; case MESSAGES: - Arrays.stream(Message.values()).forEach((message) -> addKeyValue(message.getPath(), message.getDefaultValue())); + Arrays.stream(Message.values()).forEach((message) -> hasUpgraded.add(addKeyValue(message.getPath(), message.getDefaultValue()))); break; } + + return hasUpgraded.contains(true); + } + + /** + * Saves the file if passed boolean is true + * + * @param shouldSave true if save should proceed + */ + public void save(boolean shouldSave) { + if (shouldSave) save(); } public void save() { @@ -181,17 +198,22 @@ public void save() { config = YamlConfiguration.loadConfiguration(file); } - private void addKeyValue(String node, Object value) { + private boolean addKeyValue(String node, Object value) { if (value instanceof Map) { for (Map.Entry entry : ((Map) value).entrySet()) { String newNode = node + "." + entry.getKey().toString(); if (config.get(newNode) == null || (config.get(newNode) != null && config.get(newNode).toString().isEmpty())) { + Bukkit.getLogger().log(Level.WARNING, "TS reload issue ->\n" + newNode + " = " + config.get(newNode).toString() + " | " + entry.getValue().toString()); config.set(newNode, entry.getValue().toString()); + return true; } } } else if (config.get(node) == null || (config.get(node) != null && config.get(node).toString().isEmpty())) { config.set(node, value); + return true; } + + return false; } public enum ConfigType { diff --git a/src/main/java/org/shanerx/tradeshop/data/config/Message.java b/src/main/java/org/shanerx/tradeshop/data/config/Message.java index 8028966a..f2de16e1 100644 --- a/src/main/java/org/shanerx/tradeshop/data/config/Message.java +++ b/src/main/java/org/shanerx/tradeshop/data/config/Message.java @@ -37,9 +37,11 @@ import org.yaml.snakeyaml.Yaml; import java.util.Arrays; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -117,13 +119,15 @@ public enum Message { } // Method to fix any values that have changed with updates - static void upgrade() { + static boolean upgrade() { double version = MESSAGE_VERSION.getDouble(); + Set hasUpgraded = new HashSet<>(); // Uses this instead of a boolean to later replace below ifs with boolean return methods... //Changes if CONFIG_VERSION is below 1.1, then update to 1.1 if (checkVersion(version, 1.1)) { if (TOO_MANY_ITEMS.getString().equals("&cThis trade can not take any more %side%!")) { TOO_MANY_ITEMS.setValue(PLUGIN.getLanguage().getDefault(Language.LangSection.MESSAGE, TOO_MANY_ITEMS.getPath())); + hasUpgraded.add(true); } version = 1.1; } @@ -133,8 +137,10 @@ static void upgrade() { Arrays.stream(values()).forEach((message) -> { String str = message.getString().replace("{", "%").replace("}", "%"); - if (!str.equals(message.getString())) + if (!str.equals(message.getString())) { message.setValue(str); + hasUpgraded.add(true); + } }); version = 1.2; @@ -144,12 +150,15 @@ static void upgrade() { if (checkVersion(version, 1.3)) { if (INSUFFICIENT_ITEMS.getString().equals("&cYou do not have &e%AMOUNT% %ITEM%&c!")) { INSUFFICIENT_ITEMS.setValue(PLUGIN.getLanguage().getDefault(Language.LangSection.MESSAGE, INSUFFICIENT_ITEMS.getPath())); + hasUpgraded.add(true); } if (SHOP_INSUFFICIENT_ITEMS.getString().equals("&cThis shop does not have enough &e%AMOUNT% %ITEM%&c to trade!")) { SHOP_INSUFFICIENT_ITEMS.setValue(PLUGIN.getLanguage().getDefault(Language.LangSection.MESSAGE, SHOP_INSUFFICIENT_ITEMS.getPath())); + hasUpgraded.add(true); } if (ON_TRADE.getString().equals("&aYou have traded your &e%AMOUNT2% %ITEM2% &afor &e%AMOUNT1% %ITEM1% &awith %SELLER%")) { ON_TRADE.setValue(PLUGIN.getLanguage().getDefault(Language.LangSection.MESSAGE, ON_TRADE.getPath())); + hasUpgraded.add(true); } @@ -157,6 +166,8 @@ static void upgrade() { } MESSAGE_VERSION.setValue(version != 0.0 ? version : 1.3); + + return hasUpgraded.contains(true); } private static boolean checkVersion(double version, double maxVersion) { diff --git a/src/main/java/org/shanerx/tradeshop/data/config/Setting.java b/src/main/java/org/shanerx/tradeshop/data/config/Setting.java index 79e24074..9a4f6a53 100644 --- a/src/main/java/org/shanerx/tradeshop/data/config/Setting.java +++ b/src/main/java/org/shanerx/tradeshop/data/config/Setting.java @@ -32,8 +32,10 @@ import org.yaml.snakeyaml.Yaml; import java.util.Arrays; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; public enum Setting { @@ -170,8 +172,9 @@ public static Setting findSetting(String search) { } // Method to fix any values that have changed with updates - static void upgrade() { + static boolean upgrade() { double version = CONFIG_VERSION.getDouble(); + Set hasUpgraded = new HashSet<>(); // Uses this instead of a boolean to later replace below ifs with boolean return methods... ConfigManager configManager = PLUGIN.getSettingManager(); // 2.2.2 Changed enable debug from true/false to integer @@ -184,46 +187,55 @@ static void upgrade() { if (configManager.getConfig().contains("itradeshop.owner")) { configManager.getConfig().set(ITRADESHOP_OWNER.path, configManager.getConfig().get("itradeshop.owner")); configManager.getConfig().set("itradeshop.owner", null); + hasUpgraded.add(true); } if (configManager.getConfig().contains("itradeshop.header")) { configManager.getConfig().set(ITRADESHOP_HEADER.path, configManager.getConfig().get("itradeshop.header")); configManager.getConfig().set("itradeshop.header", null); + hasUpgraded.add(true); } if (configManager.getConfig().contains("itradeshop.allow-explode")) { configManager.getConfig().set(ITRADESHOP_EXPLODE.path, configManager.getConfig().get("itradeshop.allow-explode")); configManager.getConfig().set("itradeshop.allow-explode", null); + hasUpgraded.add(true); } if (configManager.getConfig().contains("tradeshop.header")) { configManager.getConfig().set(TRADESHOP_HEADER.path, configManager.getConfig().get("tradeshop.header")); configManager.getConfig().set("tradeshop.header", null); + hasUpgraded.add(true); } if (configManager.getConfig().contains("tradeshop.allow-explode")) { configManager.getConfig().set(TRADESHOP_EXPLODE.path, configManager.getConfig().get("tradeshop.allow-explode")); configManager.getConfig().set("tradeshop.allow-explode", null); + hasUpgraded.add(true); } if (configManager.getConfig().contains("tradeshop.allow-hopper-export")) { configManager.getConfig().set(TRADESHOP_HOPPER_EXPORT.path, configManager.getConfig().get("tradeshop.allow-hopper-export")); configManager.getConfig().set("tradeshop.allow-hopper-export", null); + hasUpgraded.add(true); } if (configManager.getConfig().contains("bitradeshop.header")) { configManager.getConfig().set(BITRADESHOP_HEADER.path, configManager.getConfig().get("bitradeshop.header")); configManager.getConfig().set("bitradeshop.header", null); + hasUpgraded.add(true); } if (configManager.getConfig().contains("bitradeshop.allow-explode")) { configManager.getConfig().set(BITRADESHOP_EXPLODE.path, configManager.getConfig().get("bitradeshop.allow-explode")); configManager.getConfig().set("bitradeshop.allow-explode", null); + hasUpgraded.add(true); } if (configManager.getConfig().contains("bitradeshop.allow-hopper-export")) { configManager.getConfig().set(BITRADESHOP_HOPPER_EXPORT.path, configManager.getConfig().get("bitradeshop.allow-hopper-export")); configManager.getConfig().set("bitradeshop.allow-hopper-export", null); + hasUpgraded.add(true); } @@ -235,12 +247,15 @@ static void upgrade() { configManager.getConfig().set(GLOBAL_ILLEGAL_ITEMS_LIST.path, configManager.getConfig().get("global-options.illegal-items")); GLOBAL_ILLEGAL_ITEMS_LIST.setValue(configManager.getConfig().getStringList("global-options.illegal-items").removeAll(Arrays.asList("Air", "Void_Air", "Cave_Air"))); configManager.getConfig().set("global-options.illegal-items", null); + hasUpgraded.add(true); } version = 1.2; } CONFIG_VERSION.setValue(version); + + return hasUpgraded.contains(true); } public String getKey() { @@ -255,6 +270,10 @@ public String getMappedString(String subKey) { return PLUGIN.getSettingManager().getConfig().getConfigurationSection(getPath()).getString(subKey.toLowerCase().replace("_", "-")); } + public boolean getMappedBoolean(String subKey) { + return PLUGIN.getSettingManager().getConfig().getConfigurationSection(getPath()).getBoolean(subKey.toLowerCase().replace("_", "-")); + } + public String getPostComment() { return PLUGIN.getLanguage().getPostComment(Language.LangSection.SETTING, path); } @@ -281,7 +300,7 @@ public String getFileString() { if (defaultValue instanceof Map) { keyOutput.append(section.getSectionLead()).append(getKey()).append(":\n"); for (Map.Entry entry : ((Map) defaultValue).entrySet()) { - keyOutput.append(section.getSectionLead() + " ").append(entry.getKey().toString()).append(": ").append(new Yaml().dump(entry.getValue())); + keyOutput.append(section.getSectionLead() + " ").append(entry.getKey().toString()).append(": ").append(new Yaml().dump(entry.getValue())); } } else { keyOutput.append(section.getSectionLead()).append(getKey()).append(": ").append(new Yaml().dump(getSetting())); @@ -301,6 +320,11 @@ public void setValue(Object obj) { PLUGIN.getSettingManager().getConfig().set(getPath(), obj); } + public void setMappedValue(String subKey, Object obj) { + String newNode = getPath() + "." + subKey; + PLUGIN.getSettingManager().getConfig().set(newNode, obj); + } + public void clearSetting() { PLUGIN.getSettingManager().getConfig().set(getPath(), null); } @@ -309,7 +333,7 @@ public Object getSetting() { return PLUGIN.getSettingManager().getConfig().get(getPath()); } - public String getString() { + public String getString() { return PLUGIN.getSettingManager().getConfig().getString(getPath()); } diff --git a/src/main/java/org/shanerx/tradeshop/shop/Shop.java b/src/main/java/org/shanerx/tradeshop/shop/Shop.java index 3c7cf51a..6bdc4d44 100644 --- a/src/main/java/org/shanerx/tradeshop/shop/Shop.java +++ b/src/main/java/org/shanerx/tradeshop/shop/Shop.java @@ -725,7 +725,8 @@ public void setOwner(ShopUser owner) { * @return true if player has been set */ public boolean setUser(UUID newUser, ShopRole role) { - return removeUser(newUser) && addUser(newUser, role); + removeUser(newUser); + return addUser(newUser, role); } /** diff --git a/src/main/java/org/shanerx/tradeshop/shop/listeners/ShopTradeListener.java b/src/main/java/org/shanerx/tradeshop/shop/listeners/ShopTradeListener.java index 43d547f7..c4bba16c 100644 --- a/src/main/java/org/shanerx/tradeshop/shop/listeners/ShopTradeListener.java +++ b/src/main/java/org/shanerx/tradeshop/shop/listeners/ShopTradeListener.java @@ -134,6 +134,7 @@ public void onBlockInteract(PlayerInteractEvent e) { return; case INCOMPLETE: Message.SHOP_EMPTY.sendMessage(buyer); + return; case OUT_OF_STOCK: if (shop.getShopType() == ShopType.ITRADE) { break; @@ -142,8 +143,6 @@ public void onBlockInteract(PlayerInteractEvent e) { if (shop.hasSide(ShopItemSide.PRODUCT)) { List searchResult = getItems(shop.getChestAsSC().getInventory().getStorageContents(), shop.getSideList(ShopItemSide.PRODUCT, doBiTradeAlternate), multiplier); Message.SHOP_INSUFFICIENT_ITEMS.sendItemMultiLineMessage(buyer, Collections.singletonMap(Variable.MISSING_ITEMS, searchResult)); - } else { - Message.SHOP_EMPTY.sendMessage(buyer); } return; case OPEN: