mirror of
https://github.com/TotalFreedomMC/OpenInv.git
synced 2024-12-23 00:15:08 +00:00
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:
parent
718b4bb5dd
commit
db2cade4e2
2 changed files with 80 additions and 64 deletions
|
@ -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);
|
||||||
|
if (version >= CONFIG_VERSION) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
plugin.getLogger().info("Configuration update found! Performing update...");
|
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();
|
|
||||||
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!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
// Get the old config settings
|
||||||
int itemOpenInvItemId = plugin.getConfig().getInt("ItemOpenInvItemID", 280);
|
int itemOpenInvItemId = plugin.getConfig().getInt("ItemOpenInvItemID", 280);
|
||||||
plugin.getConfig().set("ItemOpenInvItemID", null);
|
|
||||||
boolean notifySilentChest = plugin.getConfig().getBoolean("NotifySilentChest", true);
|
boolean notifySilentChest = plugin.getConfig().getBoolean("NotifySilentChest", true);
|
||||||
plugin.getConfig().set("NotifySilentChest", null);
|
|
||||||
boolean notifyAnyChest = plugin.getConfig().getBoolean("NotifyAnyChest", 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("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,20 +119,26 @@ public class ConfigUpdater {
|
||||||
toggles.put(dataID, section.getBoolean(playerName + suffix, false));
|
toggles.put(dataID, section.getBoolean(playerName + suffix, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
new BukkitRunnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
// Wipe old ConfigurationSection
|
// Wipe old ConfigurationSection
|
||||||
plugin.getConfig().set(sectionName, null);
|
plugin.getConfig().set(sectionName, null);
|
||||||
// Prepare new ConfigurationSection
|
|
||||||
if (plugin.getConfig().isConfigurationSection(newSectionName)) {
|
|
||||||
section = plugin.getConfig().getConfigurationSection(newSectionName);
|
|
||||||
} else {
|
|
||||||
section = plugin.getConfig().createSection(newSectionName);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Prepare new ConfigurationSection
|
||||||
|
ConfigurationSection newSection;
|
||||||
|
if (plugin.getConfig().isConfigurationSection(newSectionName)) {
|
||||||
|
newSection = plugin.getConfig().getConfigurationSection(newSectionName);
|
||||||
|
} else {
|
||||||
|
newSection = plugin.getConfig().createSection(newSectionName);
|
||||||
|
}
|
||||||
// Set new values
|
// Set new values
|
||||||
for (Map.Entry<String, Boolean> entry : toggles.entrySet()) {
|
for (Map.Entry<String, Boolean> entry : toggles.entrySet()) {
|
||||||
section.set(entry.getKey(), entry.getValue());
|
newSection.set(entry.getKey(), entry.getValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}.runTask(plugin);
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
private Material getMaterialById(int id) {
|
private Material getMaterialById(int id) {
|
||||||
|
|
2
pom.xml
2
pom.xml
|
@ -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>
|
||||||
|
|
Loading…
Reference in a new issue