Don't convert names in config to UUIDs on the main thread

This is pretty messy, but I can't think of a better way to avoid saving about 5 times in a row. Then again, I did just wake up, so my brain may not be on point yet.
This commit is contained in:
Jikoo 2016-11-27 17:30:39 -05:00
parent 718b4bb5dd
commit db2cade4e2
2 changed files with 80 additions and 64 deletions

View file

@ -9,6 +9,7 @@ import java.util.Set;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.scheduler.BukkitRunnable;
public class ConfigUpdater { public class ConfigUpdater {
@ -20,44 +21,14 @@ public class ConfigUpdater {
this.plugin = plugin; this.plugin = plugin;
} }
private int getConfigVersion() {
return plugin.getConfig().getInt("config-version", 1);
}
private boolean isConfigOutdated() {
return getConfigVersion() < CONFIG_VERSION;
}
public void checkForUpdates() { public void checkForUpdates() {
if (isConfigOutdated()) { final int version = plugin.getConfig().getInt("config-version", 1);
plugin.getLogger().info("Configuration update found! Performing update..."); if (version >= CONFIG_VERSION) {
performUpdate(); return;
plugin.getLogger().info("Configuration update complete!");
} }
}
private void performUpdate() { plugin.getLogger().info("Configuration update found! Performing update...");
// Update according to the right version
switch (getConfigVersion()) {
case 1:
updateConfig1To2();
case 2:
updateConfig2To3();
break;
}
}
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 // Backup the old config file
try { try {
plugin.getConfig().save(new File(plugin.getDataFolder(), "config_old.yml")); 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!"); plugin.getLogger().warning("Could not back up config.yml before updating!");
} }
// Get the old config settings new BukkitRunnable() {
int itemOpenInvItemId = plugin.getConfig().getInt("ItemOpenInvItemID", 280); @Override
plugin.getConfig().set("ItemOpenInvItemID", null); public void run() {
boolean notifySilentChest = plugin.getConfig().getBoolean("NotifySilentChest", true); switch (version) {
plugin.getConfig().set("NotifySilentChest", null); case 1:
boolean notifyAnyChest = plugin.getConfig().getBoolean("NotifyAnyChest", true); updateConfig1To2();
plugin.getConfig().set("NotifyAnyChest", null); 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("AnyChest", ".toggle", "toggles.any-chest");
updateToggles("ItemOpenInv", ".toggle", "toggles.items.open-inv"); updateToggles("ItemOpenInv", ".toggle", "toggles.items.open-inv");
updateToggles("SilentChest", ".toggle", "toggles.silent-chest"); 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 // Ensure section exists
if (!plugin.getConfig().isConfigurationSection(sectionName)) { if (!plugin.getConfig().isConfigurationSection(sectionName)) {
return; return;
@ -101,7 +111,7 @@ public class ConfigUpdater {
return; return;
} }
Map<String, Boolean> toggles = new HashMap<String, Boolean>(); final Map<String, Boolean> toggles = new HashMap<String, Boolean>();
for (String playerName : keys) { for (String playerName : keys) {
OfflinePlayer player = plugin.matchPlayer(playerName); OfflinePlayer player = plugin.matchPlayer(playerName);
@ -109,19 +119,25 @@ public class ConfigUpdater {
toggles.put(dataID, section.getBoolean(playerName + suffix, false)); toggles.put(dataID, section.getBoolean(playerName + suffix, false));
} }
// Wipe old ConfigurationSection new BukkitRunnable() {
plugin.getConfig().set(sectionName, null); @Override
// Prepare new ConfigurationSection public void run() {
if (plugin.getConfig().isConfigurationSection(newSectionName)) { // Wipe old ConfigurationSection
section = plugin.getConfig().getConfigurationSection(newSectionName); plugin.getConfig().set(sectionName, null);
} else {
section = plugin.getConfig().createSection(newSectionName);
}
// Set new values // Prepare new ConfigurationSection
for (Map.Entry<String, Boolean> entry : toggles.entrySet()) { ConfigurationSection newSection;
section.set(entry.getKey(), entry.getValue()); if (plugin.getConfig().isConfigurationSection(newSectionName)) {
} newSection = plugin.getConfig().getConfigurationSection(newSectionName);
} else {
newSection = plugin.getConfig().createSection(newSectionName);
}
// Set new values
for (Map.Entry<String, Boolean> entry : toggles.entrySet()) {
newSection.set(entry.getKey(), entry.getValue());
}
}
}.runTask(plugin);
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")

View file

@ -10,7 +10,7 @@
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<openinv.version>3.0.2</openinv.version> <openinv.version>3.0.3-SNAPSHOT</openinv.version>
</properties> </properties>
<profiles> <profiles>