diff --git a/plugin/src/main/java/com/lishid/openinv/ConfigUpdater.java b/plugin/src/main/java/com/lishid/openinv/ConfigUpdater.java index ba299a8..ee0a826 100644 --- a/plugin/src/main/java/com/lishid/openinv/ConfigUpdater.java +++ b/plugin/src/main/java/com/lishid/openinv/ConfigUpdater.java @@ -9,6 +9,7 @@ import java.util.Set; import org.bukkit.Material; import org.bukkit.OfflinePlayer; import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.scheduler.BukkitRunnable; public class ConfigUpdater { @@ -20,44 +21,14 @@ public class ConfigUpdater { 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!"); + final int version = plugin.getConfig().getInt("config-version", 1); + if (version >= CONFIG_VERSION) { + return; } - } - private void performUpdate() { - // Update according to the right version - switch (getConfigVersion()) { - case 1: - updateConfig1To2(); - case 2: - updateConfig2To3(); - break; - } - } + plugin.getLogger().info("Configuration update found! Performing update..."); - private void updateConfig2To3() { - plugin.getConfig().set("config-version", 3); - plugin.getConfig().set("items.open-inv", null); - plugin.getConfig().set("toggles.items.open-inv", null); - plugin.getConfig().set("settings.disable-saving", plugin.getConfig().getBoolean("DisableSaving", false)); - plugin.getConfig().set("DisableSaving", null); - // Save the new config - plugin.saveConfig(); - } - - private void updateConfig1To2() { // Backup the old config file try { plugin.getConfig().save(new File(plugin.getDataFolder(), "config_old.yml")); @@ -66,28 +37,67 @@ public class ConfigUpdater { 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); + new BukkitRunnable() { + @Override + public void run() { + switch (version) { + case 1: + updateConfig1To2(); + case 2: + updateConfig2To3(); + break; + } + + new BukkitRunnable() { + @Override + public void run() { + plugin.saveConfig(); + plugin.getLogger().info("Configuration update complete!"); + } + }.runTaskLater(plugin, 1L); // Run on 1 tick delay; on older versions Bukkit's scheduler is not guaranteed FIFO + } + }.runTaskAsynchronously(plugin); + } + + private void updateConfig2To3() { + new BukkitRunnable() { + @Override + public void run() { + plugin.getConfig().set("config-version", 3); + plugin.getConfig().set("items.open-inv", null); + plugin.getConfig().set("toggles.items.open-inv", null); + plugin.getConfig().set("settings.disable-saving", + plugin.getConfig().getBoolean("DisableSaving", false)); + plugin.getConfig().set("DisableSaving", null); + } + }.runTask(plugin); + } + + private void updateConfig1To2() { + new BukkitRunnable() { + @Override + public void run() { + // Get the old config settings + int itemOpenInvItemId = plugin.getConfig().getInt("ItemOpenInvItemID", 280); + boolean notifySilentChest = plugin.getConfig().getBoolean("NotifySilentChest", true); + boolean notifyAnyChest = plugin.getConfig().getBoolean("NotifyAnyChest", true); + plugin.getConfig().set("ItemOpenInvItemID", null); + plugin.getConfig().set("NotifySilentChest", null); + plugin.getConfig().set("NotifyAnyChest", null); + 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); + } + }.runTask(plugin); 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) { + private void updateToggles(final String sectionName, String suffix, final String newSectionName) { // Ensure section exists if (!plugin.getConfig().isConfigurationSection(sectionName)) { return; @@ -101,7 +111,7 @@ public class ConfigUpdater { return; } - Map toggles = new HashMap(); + final Map toggles = new HashMap(); for (String playerName : keys) { OfflinePlayer player = plugin.matchPlayer(playerName); @@ -109,19 +119,25 @@ public class ConfigUpdater { 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); - } + new BukkitRunnable() { + @Override + public void run() { + // Wipe old ConfigurationSection + plugin.getConfig().set(sectionName, null); - // Set new values - for (Map.Entry entry : toggles.entrySet()) { - section.set(entry.getKey(), entry.getValue()); - } + // Prepare new ConfigurationSection + ConfigurationSection newSection; + if (plugin.getConfig().isConfigurationSection(newSectionName)) { + newSection = plugin.getConfig().getConfigurationSection(newSectionName); + } else { + newSection = plugin.getConfig().createSection(newSectionName); + } + // Set new values + for (Map.Entry entry : toggles.entrySet()) { + newSection.set(entry.getKey(), entry.getValue()); + } + } + }.runTask(plugin); } @SuppressWarnings("deprecation") diff --git a/pom.xml b/pom.xml index 86e93ae..9214a37 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ UTF-8 - 3.0.2 + 3.0.3-SNAPSHOT