improve various config things

This commit is contained in:
hyperdefined
2023-10-21 18:11:28 -04:00
parent 68aed25834
commit 59bfdf69ca
4 changed files with 55 additions and 29 deletions

View File

@@ -118,7 +118,7 @@ public final class ToolStats extends JavaPlugin {
public InventoryOpen inventoryOpen; public InventoryOpen inventoryOpen;
public PlayerJoin playerJoin; public PlayerJoin playerJoin;
public NumberFormat numberFormat; public NumberFormat numberFormat;
public FileConfiguration config; public YamlConfiguration config;
private BukkitAudiences adventure; private BukkitAudiences adventure;
public MorePaperLib morePaperLib; public MorePaperLib morePaperLib;
public HashMaker hashMaker; public HashMaker hashMaker;
@@ -177,11 +177,15 @@ public final class ToolStats extends JavaPlugin {
public void loadConfig() { public void loadConfig() {
config = YamlConfiguration.loadConfiguration(configFile); config = YamlConfiguration.loadConfiguration(configFile);
logger.info(String.valueOf(config.getInt("config-version")));
if (config.getInt("config-version") != CONFIG_VERSION) { if (config.getInt("config-version") != CONFIG_VERSION) {
logger.warning("Your config file is outdated! We will try to update it, but you should regenerate it!"); logger.warning("Your config file is outdated! We will try to update it, but you should regenerate it!");
ConfigUpdater configUpdater = new ConfigUpdater(this); ConfigUpdater configUpdater = new ConfigUpdater(this);
try {
configUpdater.updateConfig(); configUpdater.updateConfig();
} catch (IOException exception) {
logger.severe("Unable to update config.yml! Please regenerate it!");
throw new RuntimeException(exception);
}
} }
numberFormat = new NumberFormat(this); numberFormat = new NumberFormat(this);

View File

@@ -18,10 +18,12 @@
package lol.hyper.toolstats.tools; package lol.hyper.toolstats.tools;
import lol.hyper.toolstats.ToolStats; import lol.hyper.toolstats.ToolStats;
import org.bukkit.configuration.file.FileConfiguration;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ConfigUpdater { public class ConfigUpdater {
@@ -31,33 +33,53 @@ public class ConfigUpdater {
this.toolStats = toolStats; this.toolStats = toolStats;
} }
public void updateConfig() { public void updateConfig() throws IOException {
// get a copy of the current config // get a copy of the current config
FileConfiguration newConfig = toolStats.config; int version = toolStats.config.getInt("config-version");
int version = newConfig.getInt("config-version");
if (version == 5) { if (version == 5) {
newConfig.set("config-version", 6); // save the old config first
newConfig.set("enabled.spawned-in.pickaxe", true);
newConfig.set("enabled.spawned-in.sword", true);
newConfig.set("enabled.spawned-in.shovel", true);
newConfig.set("enabled.spawned-in.axe", true);
newConfig.set("enabled.spawned-in.hoe", true);
newConfig.set("enabled.spawned-in.shears", true);
newConfig.set("enabled.spawned-in.bow", true);
newConfig.set("enabled.spawned-in.armor", true);
newConfig.set("messages.spawned.spawned-by", "&7Spawned in by: &8{player}");
newConfig.set("messages.spawned.spawned-on", "&7Spawned on: &8{date}");
newConfig.set("generate-hash-for-items", "true");
try {
toolStats.config.save("plugins" + File.separator + "ToolStats" + File.separator + "config-5.yml"); toolStats.config.save("plugins" + File.separator + "ToolStats" + File.separator + "config-5.yml");
newConfig.save("plugins" + File.separator + "ToolStats" + File.separator + "config.yml");
} catch (IOException exception) { // we make this super verbose so that admins can see what's being added
toolStats.logger.severe("Unable to save config.yml"); toolStats.logger.info("Updating config.yml to version 6.");
throw new RuntimeException(exception); toolStats.config.set("config-version", 6);
} toolStats.logger.info("Adding enabled.spawned-in.pickaxe to config.yml.");
toolStats.config.set("enabled.spawned-in.pickaxe", true);
toolStats.logger.info("Adding enabled.spawned-in.sword to config.yml.");
toolStats.config.set("enabled.spawned-in.sword", true);
toolStats.logger.info("Adding enabled.spawned-in.shovel to config.yml.");
toolStats.config.set("enabled.spawned-in.shovel", true);
toolStats.logger.info("Adding enabled.spawned-in.axe to config.yml.");
toolStats.config.set("enabled.spawned-in.axe", true);
toolStats.logger.info("Adding enabled.spawned-in.hoe to config.yml.");
toolStats.config.set("enabled.spawned-in.hoe", true);
toolStats.logger.info("Adding enabled.spawned-in.fishing-rod to config.yml.");
toolStats.config.set("enabled.spawned-in.shears", true);
toolStats.logger.info("Adding enabled.spawned-in.shears to config.yml.");
toolStats.config.set("enabled.spawned-in.bow", true);
toolStats.logger.info("Adding enabled.spawned-in.bow to config.yml.");
toolStats.config.set("enabled.spawned-in.armor", true);
toolStats.logger.info("Adding enabled.spawned-in.armor to config.yml.");
toolStats.config.set("messages.spawned.spawned-by", "&7Spawned in by: &8{player}");
toolStats.logger.info("Adding messages.spawned-in.spawned-by to config.yml.");
toolStats.config.set("messages.spawned.spawned-on", "&7Spawned on: &8{date}");
toolStats.logger.info("Adding messages.spawned-in.spawned-on to config.yml.");
toolStats.config.set("generate-hash-for-items", true);
toolStats.logger.info("Adding generate-hash-for-items to config.yml.");
List<String> hashComments = new ArrayList<>();
hashComments.add("When any tool is created, it will generate a hash for the item.");
hashComments.add("This hash is not on the item lore, only stored in the NBT data.");
hashComments.add("This has no use currently, but can be used for future features for dupe detection.");
toolStats.config.setComments("generate-hash-for-items", hashComments);
toolStats.config.setComments("enabled.spawned-in", Collections.singletonList("Will show \"Spawned in by <player>\""));
// save the config and reload it
toolStats.config.save("plugins" + File.separator + "ToolStats" + File.separator + "config.yml");
toolStats.loadConfig(); toolStats.loadConfig();
toolStats.logger.info("Config has been updated to version 6. A copy of version 5 has been saved as config-5.yml");
} }
} }
} }

View File

@@ -122,8 +122,8 @@ public class ItemLore {
break; break;
} }
case 6: { case 6: {
dateCreated = toolStats.getLoreFromConfig("spawned.spawned-on", true); dateCreated = toolStats.getLoreFromConfig("spawned-in.spawned-on", true);
itemOwner = toolStats.getLoreFromConfig("spawned.spawned-by", true); itemOwner = toolStats.getLoreFromConfig("spawned-in.spawned-by", true);
break; break;
} }
} }

View File

@@ -100,7 +100,7 @@ messages:
kills: kills:
mob: "&7Mob kills: &8{kills}" mob: "&7Mob kills: &8{kills}"
player: "&7Player kills: &8{kills}" player: "&7Player kills: &8{kills}"
spawned: spawned-in:
spawned-by: "&7Spawned in by: &8{player}" spawned-by: "&7Spawned in by: &8{player}"
spawned-on: "&7Spawned on: &8{date}" spawned-on: "&7Spawned on: &8{date}"
blocks-mined: "&7Blocks mined: &8{blocks}" blocks-mined: "&7Blocks mined: &8{blocks}"