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 PlayerJoin playerJoin;
public NumberFormat numberFormat;
public FileConfiguration config;
public YamlConfiguration config;
private BukkitAudiences adventure;
public MorePaperLib morePaperLib;
public HashMaker hashMaker;
@@ -177,11 +177,15 @@ public final class ToolStats extends JavaPlugin {
public void loadConfig() {
config = YamlConfiguration.loadConfiguration(configFile);
logger.info(String.valueOf(config.getInt("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!");
ConfigUpdater configUpdater = new ConfigUpdater(this);
configUpdater.updateConfig();
try {
configUpdater.updateConfig();
} catch (IOException exception) {
logger.severe("Unable to update config.yml! Please regenerate it!");
throw new RuntimeException(exception);
}
}
numberFormat = new NumberFormat(this);

View File

@@ -18,10 +18,12 @@
package lol.hyper.toolstats.tools;
import lol.hyper.toolstats.ToolStats;
import org.bukkit.configuration.file.FileConfiguration;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ConfigUpdater {
@@ -31,33 +33,53 @@ public class ConfigUpdater {
this.toolStats = toolStats;
}
public void updateConfig() {
public void updateConfig() throws IOException {
// get a copy of the current config
FileConfiguration newConfig = toolStats.config;
int version = newConfig.getInt("config-version");
int version = toolStats.config.getInt("config-version");
if (version == 5) {
newConfig.set("config-version", 6);
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");
// save the old config first
toolStats.config.save("plugins" + File.separator + "ToolStats" + File.separator + "config-5.yml");
try {
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) {
toolStats.logger.severe("Unable to save config.yml");
throw new RuntimeException(exception);
}
// we make this super verbose so that admins can see what's being added
toolStats.logger.info("Updating config.yml to version 6.");
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.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;
}
case 6: {
dateCreated = toolStats.getLoreFromConfig("spawned.spawned-on", true);
itemOwner = toolStats.getLoreFromConfig("spawned.spawned-by", true);
dateCreated = toolStats.getLoreFromConfig("spawned-in.spawned-on", true);
itemOwner = toolStats.getLoreFromConfig("spawned-in.spawned-by", true);
break;
}
}

View File

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