diff --git a/src/main/java/com/lishid/openinv/ConfigUpdater.java b/src/main/java/com/lishid/openinv/ConfigUpdater.java index 6265210..eff633e 100644 --- a/src/main/java/com/lishid/openinv/ConfigUpdater.java +++ b/src/main/java/com/lishid/openinv/ConfigUpdater.java @@ -7,6 +7,7 @@ import java.util.UUID; import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; import com.lishid.openinv.utils.UUIDUtil; @@ -30,119 +31,92 @@ public class ConfigUpdater { public void checkForUpdates() { if (isConfigOutdated()) { plugin.getLogger().info("[Config] Update found! Performing update..."); - updateConfig(); + performUpdate(); } else { plugin.getLogger().info("[Config] Update not found. Config is already up-to-date."); } } - private void updateConfig() { + private void performUpdate() { + // Update according to the right version + switch (getConfigVersion()) { + case 1: + updateConfig1To2(); + break; + } + } + + private void updateConfig1To2() { // Get the old config settings - int itemOpenInvItemId = plugin.getConfig().getInt("ItemOpenInvItemID", 280); - boolean checkForUpdates = plugin.getConfig().getBoolean("CheckForUpdates", true); - boolean notifySilentChest = plugin.getConfig().getBoolean("NotifySilentChest", true); - boolean notifyAnyChest = plugin.getConfig().getBoolean("NotifyAnyChest", true); + FileConfiguration config = plugin.getConfig(); + + int itemOpenInvItemId = config.getInt("ItemOpenInvItemID", 280); + boolean checkForUpdates = config.getBoolean("CheckForUpdates", true); + boolean notifySilentChest = config.getBoolean("NotifySilentChest", true); + boolean notifyAnyChest = config.getBoolean("NotifyAnyChest", true); Map anyChestToggles = null; Map itemOpenInvToggles = null; Map silentChestToggles = null; - if (plugin.getConfig().isSet("AnyChest")) { - anyChestToggles = updateAnyChestToggles(); + if (config.isSet("AnyChest")) { + anyChestToggles = updateToggles("AnyChest"); } - if (plugin.getConfig().isSet("ItemOpenInv")) { - itemOpenInvToggles = updateItemOpenInvToggles(); + if (config.isSet("ItemOpenInv")) { + itemOpenInvToggles = updateToggles("ItemOpenInv"); } - if (plugin.getConfig().isSet("SilentChest")) { - silentChestToggles = updateSilentChestToggles(); + if (config.isSet("SilentChest")) { + silentChestToggles = updateToggles("SilentChest"); } // Clear the old config - for (String key : plugin.getConfig().getKeys(false)) { + for (String key : config.getKeys(false)) { plugin.getConfig().set(key, null); } // Set the new config options - plugin.getConfig().set("config-version", CONFIG_VERSION); - plugin.getConfig().set("check-for-updates", checkForUpdates); - plugin.getConfig().set("items.open-inv", getMaterialById(itemOpenInvItemId).toString()); - plugin.getConfig().set("notify.any-chest", notifyAnyChest); - plugin.getConfig().set("notify.silent-chest", notifySilentChest); + config.set("config-version", "2"); + config.set("check-for-updates", checkForUpdates); + config.set("items.open-inv", getMaterialById(itemOpenInvItemId).toString()); + config.set("notify.any-chest", notifyAnyChest); + config.set("notify.silent-chest", notifySilentChest); if (anyChestToggles != null && !anyChestToggles.isEmpty()) { for (Map.Entry entry : anyChestToggles.entrySet()) { - plugin.getConfig().set("toggles.any-chest." + entry.getKey(), entry.getValue()); + config.set("toggles.any-chest." + entry.getKey(), entry.getValue()); } } if (itemOpenInvToggles != null && !itemOpenInvToggles.isEmpty()) { for (Map.Entry entry : itemOpenInvToggles.entrySet()) { - plugin.getConfig().set("toggles.items.open-inv." + entry.getKey(), entry.getValue()); + config.set("toggles.items.open-inv." + entry.getKey(), entry.getValue()); } } if (silentChestToggles != null && !silentChestToggles.isEmpty()) { for (Map.Entry entry : silentChestToggles.entrySet()) { - plugin.getConfig().set("toggles.silent-chest." + entry.getKey(), entry.getValue()); + config.set("toggles.silent-chest." + entry.getKey(), entry.getValue()); } } // Save the new config plugin.saveConfig(); - plugin.getLogger().info("[Config] Update complete."); - } - private Map updateAnyChestToggles() { + private Map updateToggles(String sectionName) { Map toggles = new HashMap(); - ConfigurationSection anyChestSection = plugin.getConfig().getConfigurationSection("AnyChest"); - Set keys = anyChestSection.getKeys(false); + ConfigurationSection section = plugin.getConfig().getConfigurationSection(sectionName); + Set keys = section.getKeys(false); if (keys == null || keys.isEmpty()) return null; for (String playerName : keys) { UUID uuid = UUIDUtil.getUUIDOf(playerName); if (uuid != null) { - boolean toggled = anyChestSection.getBoolean(playerName + ".toggle", false); - toggles.put(uuid, toggled); - } - } - - return toggles; - } - - private Map updateItemOpenInvToggles() { - Map toggles = new HashMap(); - - ConfigurationSection anyChestSection = plugin.getConfig().getConfigurationSection("ItemOpenInv"); - Set keys = anyChestSection.getKeys(false); - if (keys == null || keys.isEmpty()) return null; - - for (String playerName : keys) { - UUID uuid = UUIDUtil.getUUIDOf(playerName); - if (uuid != null) { - boolean toggled = anyChestSection.getBoolean(playerName + ".toggle", false); - toggles.put(uuid, toggled); - } - } - - return toggles; - } - - private Map updateSilentChestToggles() { - Map toggles = new HashMap(); - - ConfigurationSection silentChestSection = plugin.getConfig().getConfigurationSection("SilentChest"); - Set keys = silentChestSection.getKeys(false); - if (keys == null || keys.isEmpty()) return null; - - for (String playerName : keys) { - UUID uuid = UUIDUtil.getUUIDOf(playerName); - if (uuid != null) { - boolean toggled = silentChestSection.getBoolean(playerName + ".toggle", false); + boolean toggled = section.getBoolean(playerName + ".toggle", false); toggles.put(uuid, toggled); } } diff --git a/src/main/java/com/lishid/openinv/OpenInv.java b/src/main/java/com/lishid/openinv/OpenInv.java index e300fc0..ae96043 100644 --- a/src/main/java/com/lishid/openinv/OpenInv.java +++ b/src/main/java/com/lishid/openinv/OpenInv.java @@ -131,9 +131,9 @@ public class OpenInv extends JavaPlugin { if (!mainPlugin.getConfig().isSet("items.open-inv")) { saveToConfig("items.open-inv", "STICK"); } - String itemName = mainPlugin.getConfig().getString("items.open-inv", "STICK"); - return Material.getMaterial(itemName); + Material material = Material.getMaterial(itemName); + return material != null ? material : Material.STICK; } public static boolean notifySilentChest() { @@ -198,13 +198,13 @@ public class OpenInv extends JavaPlugin { } public static void showHelp(Player player) { - player.sendMessage(ChatColor.GREEN + "/openinv - Open a player's inventory"); + player.sendMessage(ChatColor.GREEN + "/openinv - Open a player's inventory"); player.sendMessage(ChatColor.GREEN + " (aliases: oi, inv, open)"); - player.sendMessage(ChatColor.GREEN + "/openender - Open a player's enderchest"); + player.sendMessage(ChatColor.GREEN + "/openender - Open a player's ender chest"); player.sendMessage(ChatColor.GREEN + " (aliases: oe, enderchest)"); player.sendMessage(ChatColor.GREEN + "/toggleopeninv - Toggle item openinv function"); player.sendMessage(ChatColor.GREEN + " (aliases: toi, toggleoi, toggleinv)"); - player.sendMessage(ChatColor.GREEN + "/searchinv [MinAmount] - "); + player.sendMessage(ChatColor.GREEN + "/searchinv [minAmount] - "); player.sendMessage(ChatColor.GREEN + " Search and list players having a specific item."); player.sendMessage(ChatColor.GREEN + " (aliases: si, search)"); player.sendMessage(ChatColor.GREEN + "/anychest - Toggle anychest function");