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.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<String, Boolean> toggles = new HashMap<String, Boolean>();
final Map<String, Boolean> toggles = new HashMap<String, Boolean>();
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<String, Boolean> 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<String, Boolean> entry : toggles.entrySet()) {
newSection.set(entry.getKey(), entry.getValue());
}
}
}.runTask(plugin);
}
@SuppressWarnings("deprecation")

View file

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