Skip to content

Commit

Permalink
Remove old config toggles
Browse files Browse the repository at this point in the history
Removes the need for scheduling config updates async because we no longer need to migrate legacy player name toggles
  • Loading branch information
Jikoo committed Jul 28, 2024
1 parent 3b62db4 commit 8ca6a01
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 106 deletions.
165 changes: 61 additions & 104 deletions plugin/src/main/java/com/lishid/openinv/util/config/ConfigUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@

package com.lishid.openinv.util.config;

import com.lishid.openinv.OpenInv;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public record ConfigUpdater(OpenInv plugin) {
public record ConfigUpdater(@NotNull Plugin plugin) {

public void checkForUpdates() {
final int version = plugin.getConfig().getInt("config-version", 1);
Expand All @@ -45,119 +43,78 @@ public void checkForUpdates() {
plugin.getLogger().warning("Could not back up config.yml before updating!");
}

plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> {
if (version < 2) {
updateConfig1To2();
}
if (version < 3) {
updateConfig2To3();
}
if (version < 4) {
updateConfig3To4();
}
if (version < 5) {
updateConfig4To5();
}
if (version < 6) {
updateConfig5To6();
}

plugin.getServer().getScheduler().runTask(plugin, () -> {
plugin.saveConfig();
plugin.getLogger().info("Configuration update complete!");
});
});
if (version < 2) {
updateConfig1To2();
}
if (version < 3) {
updateConfig2To3();
}
if (version < 4) {
updateConfig3To4();
}
if (version < 5) {
updateConfig4To5();
}
if (version < 6) {
updateConfig5To6();
}
if (version < 7) {
updateConfig6To7();
}

plugin.saveConfig();
plugin.getLogger().info("Configuration update complete!");
}

private void updateConfig6To7() {
FileConfiguration config = plugin.getConfig();
config.set("toggles", null);
String consoleLocale = config.getString("settings.locale", "en");
if (consoleLocale.isBlank() || consoleLocale.equalsIgnoreCase("en_us")) {
consoleLocale = "en";
}
config.set("settings.console-locale", consoleLocale);
config.set("settings.locale", null);
config.set("config-version", 7);
}

private void updateConfig5To6() {
plugin.getServer().getScheduler().runTask(plugin, () -> {
plugin.getConfig().set("settings.command.open.no-args-opens-self", false);
plugin.getConfig().set("settings.command.searchcontainer.max-radius", 10);
plugin.getConfig().set("config-version", 6);
});
FileConfiguration config = plugin.getConfig();
config.set("settings.command.open.no-args-opens-self", false);
config.set("settings.command.searchcontainer.max-radius", 10);
config.set("config-version", 6);
}

private void updateConfig4To5() {
plugin.getServer().getScheduler().runTask(plugin, () -> {
plugin.getConfig().set("settings.disable-offline-access", false);
plugin.getConfig().set("config-version", 5);
});
FileConfiguration config = plugin.getConfig();
config.set("settings.disable-offline-access", false);
config.set("config-version", 5);
}

private void updateConfig3To4() {
plugin.getServer().getScheduler().runTask(plugin, () -> {
plugin.getConfig().set("notify", null);
plugin.getConfig().set("settings.locale", "en_US");
plugin.getConfig().set("config-version", 4);
});
FileConfiguration config = plugin.getConfig();
config.set("notify", null);
config.set("config-version", 4);
}

private void updateConfig2To3() {
plugin.getServer().getScheduler().runTask(plugin, () -> {
plugin.getConfig().set("config-version", 3);
plugin.getConfig().set("items.open-inv", null);
plugin.getConfig().set("ItemOpenInv", 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);
});
FileConfiguration config = plugin.getConfig();
config.set("items", null);
config.set("ItemOpenInv", null);
config.set("toggles", null);
config.set("settings.disable-saving", config.getBoolean("DisableSaving", false));
config.set("DisableSaving", null);
config.set("config-version", 3);
}

private void updateConfig1To2() {
plugin.getServer().getScheduler().runTask(plugin, () -> {
// Get the old config settings
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("notify.any-chest", notifyAnyChest);
plugin.getConfig().set("notify.silent-chest", notifySilentChest);
});

updateToggles("AnyChest", "toggles.any-chest");
updateToggles("SilentChest", "toggles.silent-chest");
}

private void updateToggles(final String sectionName, final String newSectionName) {
ConfigurationSection section = plugin.getConfig().getConfigurationSection(sectionName);
// Ensure section exists
if (section == null) {
return;
}

Set<String> keys = section.getKeys(false);

// Ensure section has content
if (keys.isEmpty()) {
return;
}

final Map<String, Boolean> toggles = new HashMap<>();

for (String playerName : keys) {
OfflinePlayer player = plugin.matchPlayer(playerName);
if (player != null) {
toggles.put(player.getUniqueId().toString(), section.getBoolean(playerName + ".toggle", false));
}
}

plugin.getServer().getScheduler().runTask(plugin, () -> {
// Wipe old ConfigurationSection
plugin.getConfig().set(sectionName, null);

// Prepare new ConfigurationSection
ConfigurationSection newSection = plugin.getConfig().getConfigurationSection(newSectionName);
if (newSection == null) {
newSection = plugin.getConfig().createSection(newSectionName);
}
// Set new values
for (Map.Entry<String, Boolean> entry : toggles.entrySet()) {
newSection.set(entry.getKey(), entry.getValue());
}
});
FileConfiguration config = plugin.getConfig();
config.set("ItemOpenInvItemID", null);
config.set("NotifySilentChest", null);
config.set("NotifyAnyChest", null);
config.set("AnyChest", null);
config.set("SilentChest", null);
config.set("config-version", 2);
}

}
4 changes: 2 additions & 2 deletions plugin/src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
config-version: 6
config-version: 7
settings:
command:
open:
Expand All @@ -7,4 +7,4 @@ settings:
max-radius: 10
disable-offline-access: false
disable-saving: false
locale: 'en_us'
console-locale: 'en'

0 comments on commit 8ca6a01

Please sign in to comment.