mirror of
https://github.com/TotalFreedomMC/OpenInv.git
synced 2024-12-23 00:15:08 +00:00
Reimplement ShadowRanger's configuration updater and path changes
Bumped version for release
This commit is contained in:
parent
8a6b98614f
commit
f11d60f78c
3 changed files with 133 additions and 29 deletions
125
plugin/src/main/java/com/lishid/openinv/ConfigUpdater.java
Normal file
125
plugin/src/main/java/com/lishid/openinv/ConfigUpdater.java
Normal file
|
@ -0,0 +1,125 @@
|
|||
package com.lishid.openinv;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
|
||||
public class ConfigUpdater {
|
||||
|
||||
private static final int CONFIG_VERSION = 2;
|
||||
|
||||
private final OpenInv plugin;
|
||||
|
||||
public ConfigUpdater(OpenInv plugin) {
|
||||
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!");
|
||||
}
|
||||
}
|
||||
|
||||
private void performUpdate() {
|
||||
// Update according to the right version
|
||||
switch (getConfigVersion()) {
|
||||
case 1:
|
||||
updateConfig1To2();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void updateConfig1To2() {
|
||||
// Backup the old config file
|
||||
try {
|
||||
plugin.getConfig().save(new File(plugin.getDataFolder(), "config_old.yml"));
|
||||
plugin.getLogger().info("Backed up config.yml to config_old.yml before updating.");
|
||||
} catch (IOException e) {
|
||||
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);
|
||||
|
||||
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) {
|
||||
// Ensure section exists
|
||||
if (!plugin.getConfig().isConfigurationSection(sectionName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
ConfigurationSection section = plugin.getConfig().getConfigurationSection(sectionName);
|
||||
Set<String> keys = section.getKeys(false);
|
||||
|
||||
// Ensure section has content
|
||||
if (keys == null || keys.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Map<String, Boolean> toggles = new HashMap<String, Boolean>();
|
||||
|
||||
for (String playerName : keys) {
|
||||
OfflinePlayer player = plugin.matchPlayer(playerName);
|
||||
String dataID = plugin.getPlayerID(player);
|
||||
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);
|
||||
}
|
||||
|
||||
// Set new values
|
||||
for (Map.Entry<String, Boolean> entry : toggles.entrySet()) {
|
||||
section.set(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private Material getMaterialById(int id) {
|
||||
Material material = Material.getMaterial(id);
|
||||
|
||||
if (material == null) {
|
||||
material = Material.STICK;
|
||||
}
|
||||
|
||||
return material;
|
||||
}
|
||||
}
|
|
@ -41,7 +41,6 @@ import com.lishid.openinv.util.Function;
|
|||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.command.PluginCommand;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
|
@ -118,27 +117,7 @@ public class OpenInv extends JavaPlugin {
|
|||
inventoryAccess = accessor.newInventoryAccess();
|
||||
anySilentContainer = accessor.newAnySilentContainer();
|
||||
|
||||
FileConfiguration config = getConfig();
|
||||
boolean dirtyConfig = false;
|
||||
if (!config.isBoolean("NotifySilentChest")) {
|
||||
config.set("NotifySilentChest", true);
|
||||
dirtyConfig = true;
|
||||
}
|
||||
if (!config.isBoolean("NotifyAnyChest")) {
|
||||
config.set("NotifyAnyChest", true);
|
||||
dirtyConfig = true;
|
||||
}
|
||||
if (!config.isBoolean("DisableSaving")) {
|
||||
config.set("DisableSaving", false);
|
||||
dirtyConfig = true;
|
||||
}
|
||||
config.addDefault("NotifySilentChest", true);
|
||||
config.addDefault("NotifyAnyChest", true);
|
||||
config.addDefault("DisableSaving", false);
|
||||
config.options().copyDefaults(true);
|
||||
if (dirtyConfig) {
|
||||
saveConfig();
|
||||
}
|
||||
new ConfigUpdater(this).checkForUpdates();
|
||||
|
||||
pm.registerEvents(new OpenInvPlayerListener(this), this);
|
||||
pm.registerEvents(new OpenInvInventoryListener(this), this);
|
||||
|
@ -256,7 +235,7 @@ public class OpenInv extends JavaPlugin {
|
|||
* @return true unless configured otherwise
|
||||
*/
|
||||
public boolean notifySilentChest() {
|
||||
return getConfig().getBoolean("NotifySilentChest", true);
|
||||
return getConfig().getBoolean("notify.silent-chest", true);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -266,7 +245,7 @@ public class OpenInv extends JavaPlugin {
|
|||
* @return true unless configured otherwise
|
||||
*/
|
||||
public boolean notifyAnyChest() {
|
||||
return getConfig().getBoolean("NotifyAnyChest", true);
|
||||
return getConfig().getBoolean("notify.any-chest", true);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -276,7 +255,7 @@ public class OpenInv extends JavaPlugin {
|
|||
* @return true if SilentChest is enabled
|
||||
*/
|
||||
public boolean getPlayerSilentChestStatus(OfflinePlayer player) {
|
||||
return getConfig().getBoolean("SilentChest." + playerLoader.getPlayerDataID(player) + ".toggle", false);
|
||||
return getConfig().getBoolean("toggles.silent-chest." + playerLoader.getPlayerDataID(player), false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -286,7 +265,7 @@ public class OpenInv extends JavaPlugin {
|
|||
* @param status the status
|
||||
*/
|
||||
public void setPlayerSilentChestStatus(OfflinePlayer player, boolean status) {
|
||||
getConfig().set("SilentChest." + playerLoader.getPlayerDataID(player) + ".toggle", status);
|
||||
getConfig().set("toggles.silent-chest." + playerLoader.getPlayerDataID(player), status);
|
||||
saveConfig();
|
||||
}
|
||||
|
||||
|
@ -297,7 +276,7 @@ public class OpenInv extends JavaPlugin {
|
|||
* @return true if AnyChest is enabled
|
||||
*/
|
||||
public boolean getPlayerAnyChestStatus(OfflinePlayer player) {
|
||||
return getConfig().getBoolean("AnyChest." + playerLoader.getPlayerDataID(player) + ".toggle", true);
|
||||
return getConfig().getBoolean("toggles.any-chest." + playerLoader.getPlayerDataID(player), false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -307,7 +286,7 @@ public class OpenInv extends JavaPlugin {
|
|||
* @param status the status
|
||||
*/
|
||||
public void setPlayerAnyChestStatus(OfflinePlayer player, boolean status) {
|
||||
getConfig().set("AnyChest." + playerLoader.getPlayerDataID(player) + ".toggle", status);
|
||||
getConfig().set("toggles.silent-chest." + playerLoader.getPlayerDataID(player), status);
|
||||
saveConfig();
|
||||
}
|
||||
|
||||
|
|
2
pom.xml
2
pom.xml
|
@ -10,7 +10,7 @@
|
|||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<openinv.version>3.0.0-SNAPSHOT</openinv.version>
|
||||
<openinv.version>3.0.1</openinv.version>
|
||||
</properties>
|
||||
|
||||
<profiles>
|
||||
|
|
Loading…
Reference in a new issue