From f11d60f78c925b583ab9500dd76368a7560286e2 Mon Sep 17 00:00:00 2001 From: ShadowRanger Date: Sat, 26 Nov 2016 16:57:03 -0500 Subject: [PATCH] Reimplement ShadowRanger's configuration updater and path changes Bumped version for release --- .../com/lishid/openinv/ConfigUpdater.java | 125 ++++++++++++++++++ .../main/java/com/lishid/openinv/OpenInv.java | 35 +---- pom.xml | 2 +- 3 files changed, 133 insertions(+), 29 deletions(-) create mode 100644 plugin/src/main/java/com/lishid/openinv/ConfigUpdater.java diff --git a/plugin/src/main/java/com/lishid/openinv/ConfigUpdater.java b/plugin/src/main/java/com/lishid/openinv/ConfigUpdater.java new file mode 100644 index 0000000..2f520cc --- /dev/null +++ b/plugin/src/main/java/com/lishid/openinv/ConfigUpdater.java @@ -0,0 +1,125 @@ +package com.lishid.openinv; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import org.bukkit.Material; +import org.bukkit.OfflinePlayer; +import org.bukkit.configuration.ConfigurationSection; + +public class ConfigUpdater { + + private static final int CONFIG_VERSION = 2; + + private final OpenInv plugin; + + public ConfigUpdater(OpenInv plugin) { + this.plugin = plugin; + } + + private int getConfigVersion() { + return plugin.getConfig().getInt("config-version", 1); + } + + private boolean isConfigOutdated() { + return getConfigVersion() < CONFIG_VERSION; + } + + public void checkForUpdates() { + if (isConfigOutdated()) { + plugin.getLogger().info("Configuration update found! Performing update..."); + performUpdate(); + plugin.getLogger().info("Configuration update complete!"); + } + } + + private void performUpdate() { + // Update according to the right version + switch (getConfigVersion()) { + case 1: + updateConfig1To2(); + break; + } + } + + private void updateConfig1To2() { + // Backup the old config file + try { + plugin.getConfig().save(new File(plugin.getDataFolder(), "config_old.yml")); + plugin.getLogger().info("Backed up config.yml to config_old.yml before updating."); + } catch (IOException e) { + plugin.getLogger().warning("Could not back up config.yml before updating!"); + } + + // Get the old config settings + int itemOpenInvItemId = plugin.getConfig().getInt("ItemOpenInvItemID", 280); + plugin.getConfig().set("ItemOpenInvItemID", null); + boolean notifySilentChest = plugin.getConfig().getBoolean("NotifySilentChest", true); + plugin.getConfig().set("NotifySilentChest", null); + boolean notifyAnyChest = plugin.getConfig().getBoolean("NotifyAnyChest", true); + plugin.getConfig().set("NotifyAnyChest", null); + + updateToggles("AnyChest", ".toggle", "toggles.any-chest"); + updateToggles("ItemOpenInv", ".toggle", "toggles.items.open-inv"); + updateToggles("SilentChest", ".toggle", "toggles.silent-chest"); + + plugin.getConfig().set("config-version", 2); + plugin.getConfig().set("items.open-inv", getMaterialById(itemOpenInvItemId).toString()); + plugin.getConfig().set("notify.any-chest", notifyAnyChest); + plugin.getConfig().set("notify.silent-chest", notifySilentChest); + + // Save the new config + plugin.saveConfig(); + } + + private void updateToggles(String sectionName, String suffix, String newSectionName) { + // Ensure section exists + if (!plugin.getConfig().isConfigurationSection(sectionName)) { + return; + } + + ConfigurationSection section = plugin.getConfig().getConfigurationSection(sectionName); + Set keys = section.getKeys(false); + + // Ensure section has content + if (keys == null || keys.isEmpty()) { + return; + } + + Map toggles = new HashMap(); + + for (String playerName : keys) { + OfflinePlayer player = plugin.matchPlayer(playerName); + String dataID = plugin.getPlayerID(player); + toggles.put(dataID, section.getBoolean(playerName + suffix, false)); + } + + // Wipe old ConfigurationSection + plugin.getConfig().set(sectionName, null); + // Prepare new ConfigurationSection + if (plugin.getConfig().isConfigurationSection(newSectionName)) { + section = plugin.getConfig().getConfigurationSection(newSectionName); + } else { + section = plugin.getConfig().createSection(newSectionName); + } + + // Set new values + for (Map.Entry entry : toggles.entrySet()) { + section.set(entry.getKey(), entry.getValue()); + } + } + + @SuppressWarnings("deprecation") + private Material getMaterialById(int id) { + Material material = Material.getMaterial(id); + + if (material == null) { + material = Material.STICK; + } + + return material; + } +} diff --git a/plugin/src/main/java/com/lishid/openinv/OpenInv.java b/plugin/src/main/java/com/lishid/openinv/OpenInv.java index 2cb85c9..ed7dcce 100644 --- a/plugin/src/main/java/com/lishid/openinv/OpenInv.java +++ b/plugin/src/main/java/com/lishid/openinv/OpenInv.java @@ -41,7 +41,6 @@ import com.lishid.openinv.util.Function; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.command.PluginCommand; -import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.HumanEntity; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; @@ -118,27 +117,7 @@ public class OpenInv extends JavaPlugin { inventoryAccess = accessor.newInventoryAccess(); anySilentContainer = accessor.newAnySilentContainer(); - FileConfiguration config = getConfig(); - boolean dirtyConfig = false; - if (!config.isBoolean("NotifySilentChest")) { - config.set("NotifySilentChest", true); - dirtyConfig = true; - } - if (!config.isBoolean("NotifyAnyChest")) { - config.set("NotifyAnyChest", true); - dirtyConfig = true; - } - if (!config.isBoolean("DisableSaving")) { - config.set("DisableSaving", false); - dirtyConfig = true; - } - config.addDefault("NotifySilentChest", true); - config.addDefault("NotifyAnyChest", true); - config.addDefault("DisableSaving", false); - config.options().copyDefaults(true); - if (dirtyConfig) { - saveConfig(); - } + new ConfigUpdater(this).checkForUpdates(); pm.registerEvents(new OpenInvPlayerListener(this), this); pm.registerEvents(new OpenInvInventoryListener(this), this); @@ -256,7 +235,7 @@ public class OpenInv extends JavaPlugin { * @return true unless configured otherwise */ public boolean notifySilentChest() { - return getConfig().getBoolean("NotifySilentChest", true); + return getConfig().getBoolean("notify.silent-chest", true); } /** @@ -266,7 +245,7 @@ public class OpenInv extends JavaPlugin { * @return true unless configured otherwise */ public boolean notifyAnyChest() { - return getConfig().getBoolean("NotifyAnyChest", true); + return getConfig().getBoolean("notify.any-chest", true); } /** @@ -276,7 +255,7 @@ public class OpenInv extends JavaPlugin { * @return true if SilentChest is enabled */ public boolean getPlayerSilentChestStatus(OfflinePlayer player) { - return getConfig().getBoolean("SilentChest." + playerLoader.getPlayerDataID(player) + ".toggle", false); + return getConfig().getBoolean("toggles.silent-chest." + playerLoader.getPlayerDataID(player), false); } /** @@ -286,7 +265,7 @@ public class OpenInv extends JavaPlugin { * @param status the status */ public void setPlayerSilentChestStatus(OfflinePlayer player, boolean status) { - getConfig().set("SilentChest." + playerLoader.getPlayerDataID(player) + ".toggle", status); + getConfig().set("toggles.silent-chest." + playerLoader.getPlayerDataID(player), status); saveConfig(); } @@ -297,7 +276,7 @@ public class OpenInv extends JavaPlugin { * @return true if AnyChest is enabled */ public boolean getPlayerAnyChestStatus(OfflinePlayer player) { - return getConfig().getBoolean("AnyChest." + playerLoader.getPlayerDataID(player) + ".toggle", true); + return getConfig().getBoolean("toggles.any-chest." + playerLoader.getPlayerDataID(player), false); } /** @@ -307,7 +286,7 @@ public class OpenInv extends JavaPlugin { * @param status the status */ public void setPlayerAnyChestStatus(OfflinePlayer player, boolean status) { - getConfig().set("AnyChest." + playerLoader.getPlayerDataID(player) + ".toggle", status); + getConfig().set("toggles.silent-chest." + playerLoader.getPlayerDataID(player), status); saveConfig(); } diff --git a/pom.xml b/pom.xml index 97b1381..8bd49bd 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ UTF-8 - 3.0.0-SNAPSHOT + 3.0.1