From 62a270957b55eb90a7d8baabe42b16b48ce81ab5 Mon Sep 17 00:00:00 2001 From: mfnalex <1122571+mfnalex@users.noreply.github.com> Date: Fri, 3 May 2019 14:58:53 +0200 Subject: [PATCH 1/2] improved config files and auto config updater --- pom.xml | 2 +- .../JeffChestSortConfigUpdater.java | 99 ++++++++++++++++++- .../JeffChestSort/JeffChestSortPlugin.java | 23 +++-- src/main/java/de/jeffclan/utils/Utils.java | 9 ++ .../categories/900-weapons.default.txt | 3 +- .../categories/905-common-tools.default.txt | 19 ++++ ...efault.txt => 907-other-tools.default.txt} | 11 ++- ...-food.default.txt => 909-food.default.txt} | 7 ++ .../categories/910-valuables.default.txt | 7 ++ ...t.txt => 920-armor-and-arrows.default.txt} | 8 +- .../categories/930-brewing.default.txt | 7 ++ .../categories/950-redstone.default.txt | 7 ++ .../resources/categories/960-wood.default.txt | 7 ++ .../categories/970-stone.default.txt | 7 ++ .../categories/980-plants.default.txt | 7 ++ .../categories/981-corals.default.txt | 9 +- 16 files changed, 213 insertions(+), 19 deletions(-) create mode 100644 src/main/resources/categories/905-common-tools.default.txt rename src/main/resources/categories/{905-tools.default.txt => 907-other-tools.default.txt} (51%) rename src/main/resources/categories/{940-food.default.txt => 909-food.default.txt} (66%) rename src/main/resources/categories/{920-armor.default.txt => 920-armor-and-arrows.default.txt} (57%) diff --git a/pom.xml b/pom.xml index fe126c8..b050587 100644 --- a/pom.xml +++ b/pom.xml @@ -47,7 +47,7 @@ org.bstats - de.jeffclan.utils + de.jeffclan.JeffChestSort diff --git a/src/main/java/de/jeffclan/JeffChestSort/JeffChestSortConfigUpdater.java b/src/main/java/de/jeffclan/JeffChestSort/JeffChestSortConfigUpdater.java index f012684..13f0a64 100644 --- a/src/main/java/de/jeffclan/JeffChestSort/JeffChestSortConfigUpdater.java +++ b/src/main/java/de/jeffclan/JeffChestSort/JeffChestSortConfigUpdater.java @@ -1,10 +1,103 @@ package de.jeffclan.JeffChestSort; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Map; +import java.util.Scanner; + +import org.bukkit.configuration.InvalidConfigurationException; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; + +import de.jeffclan.utils.Utils; + public class JeffChestSortConfigUpdater { - // Admins hate config updates. Just relax and let ChestSort update to the newest config version + JeffChestSortPlugin plugin; + + public JeffChestSortConfigUpdater(JeffChestSortPlugin jeffChestSortPlugin) { + this.plugin = jeffChestSortPlugin; + } + + // Admins hate config updates. Just relax and let ChestSort update to the newest + // config version // Don't worry! Your changes will be kept - - + + void updateConfig() { + + if(plugin.debug) plugin.getLogger().info("rename config.yml -> config.old.yml"); + Utils.renameFileInPluginDir(plugin, "config.yml", "config.old.yml"); + if(plugin.debug) plugin.getLogger().info("saving new config.yml"); + plugin.saveDefaultConfig(); + + File oldConfigFile = new File(plugin.getDataFolder().getAbsolutePath() + File.separator + "config.old.yml"); + FileConfiguration oldConfig = new YamlConfiguration(); + + try { + oldConfig.load(oldConfigFile); + } catch (IOException | InvalidConfigurationException e) { + e.printStackTrace(); + } + + Map oldValues = oldConfig.getValues(false); + + // Read default config to keep comments + ArrayList linesInDefaultConfig = new ArrayList(); + try { + + Scanner scanner = new Scanner( + new File(plugin.getDataFolder().getAbsolutePath() + File.separator + "config.yml")); + while (scanner.hasNextLine()) { + linesInDefaultConfig.add(scanner.nextLine() + ""); + } + scanner.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + + ArrayList newLines = new ArrayList(); + for (String line : linesInDefaultConfig) { + String newline = line; + if (!line.startsWith("config-version:")) { // dont replace config-version + for (String node : oldValues.keySet()) { + if (line.startsWith(node + ":")) { + + String quotes = ""; + + if (node.equalsIgnoreCase("sorting-method")) // needs single quotes + quotes = "'"; + if (node.startsWith("message-")) // needs double quotes + quotes = "\""; + + newline = node + ": " + quotes + oldValues.get(node).toString() + quotes; + if(plugin.debug) plugin.getLogger().info("Updating config node " + newline); + break; + } + } + } + newLines.add(newline); + } + + FileWriter fw; + String[] linesArray = newLines.toArray(new String[linesInDefaultConfig.size()]); + try { + fw = new FileWriter(plugin.getDataFolder().getAbsolutePath() + File.separator + "config.yml"); + for (int i = 0; i < linesArray.length; i++) { + fw.write(linesArray[i] + "\n"); + } + fw.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + // Utils.renameFileInPluginDir(plugin, "config.yml.default", "config.yml"); + + } } diff --git a/src/main/java/de/jeffclan/JeffChestSort/JeffChestSortPlugin.java b/src/main/java/de/jeffclan/JeffChestSort/JeffChestSortPlugin.java index e46959b..6014196 100644 --- a/src/main/java/de/jeffclan/JeffChestSort/JeffChestSortPlugin.java +++ b/src/main/java/de/jeffclan/JeffChestSort/JeffChestSortPlugin.java @@ -104,7 +104,11 @@ public class JeffChestSortPlugin extends JavaPlugin { // use the default values later on } else if (getConfig().getInt("config-version", 0) != currentConfigVersion) { showOldConfigWarning(); - usingMatchingConfig = false; + JeffChestSortConfigUpdater configUpdater = new JeffChestSortConfigUpdater(this); + configUpdater.updateConfig(); + configUpdater = null; + usingMatchingConfig = true; + //createConfig(); } createDirectories(); @@ -151,13 +155,16 @@ public class JeffChestSortPlugin extends JavaPlugin { private void showOldConfigWarning() { getLogger().warning("========================================================"); - getLogger().warning("YOU ARE USING AN OLD CONFIG FILE!"); - getLogger().warning("This is not a problem, as ChestSort will just use the"); - getLogger().warning("default settings for unset values. However, if you want"); - getLogger().warning("to configure the new options, please go to"); - getLogger().warning("https://www.chestsort.de and replace your config.yml"); - getLogger().warning("with the new one. You can then insert your old changes"); - getLogger().warning("into the new file."); +// getLogger().warning("YOU ARE USING AN OLD CONFIG FILE!"); +// getLogger().warning("This is not a problem, as ChestSort will just use the"); +// getLogger().warning("default settings for unset values. However, if you want"); +// getLogger().warning("to configure the new options, please go to"); +// getLogger().warning("https://www.chestsort.de and replace your config.yml"); +// getLogger().warning("with the new one. You can then insert your old changes"); +// getLogger().warning("into the new file."); + getLogger().warning("You were using an old config file. ChestSort has"); + getLogger().warning("updated the file to the newest version. Your changes"); + getLogger().warning("have been kept."); getLogger().warning("========================================================"); } diff --git a/src/main/java/de/jeffclan/utils/Utils.java b/src/main/java/de/jeffclan/utils/Utils.java index ec9b17e..23e338c 100644 --- a/src/main/java/de/jeffclan/utils/Utils.java +++ b/src/main/java/de/jeffclan/utils/Utils.java @@ -2,9 +2,12 @@ package de.jeffclan.utils; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.File; import java.io.IOException; import java.io.InputStream; +import de.jeffclan.JeffChestSort.JeffChestSortPlugin; + public class Utils { // We need this to write the category files inside the .jar to the disk @@ -32,4 +35,10 @@ public class Utils { public static String shortToStringWithLeadingZeroes(short number) { return String.format("%05d", number); } + + public static void renameFileInPluginDir(JeffChestSortPlugin plugin,String oldName, String newName) { + File oldFile = new File(plugin.getDataFolder().getAbsolutePath() + File.separator + oldName); + File newFile = new File(plugin.getDataFolder().getAbsolutePath() + File.separator + newName); + oldFile.getAbsoluteFile().renameTo(newFile.getAbsoluteFile()); + } } diff --git a/src/main/resources/categories/900-weapons.default.txt b/src/main/resources/categories/900-weapons.default.txt index ad07c7e..b2831fd 100644 --- a/src/main/resources/categories/900-weapons.default.txt +++ b/src/main/resources/categories/900-weapons.default.txt @@ -15,4 +15,5 @@ sticky=true bow *_sword -trident \ No newline at end of file +trident +shield \ No newline at end of file diff --git a/src/main/resources/categories/905-common-tools.default.txt b/src/main/resources/categories/905-common-tools.default.txt new file mode 100644 index 0000000..c7f0dcd --- /dev/null +++ b/src/main/resources/categories/905-common-tools.default.txt @@ -0,0 +1,19 @@ +# +# ChestSort Default Category File +# +# If you want to change this file, rename it. +# Please do NOT use file prefixed between 900 and 999 for +# your custom files because ChestSort will overwrite them +# + +# When sticky is set to true, order the items in this +# category exactly as defined in this file. +# When set to false, the items are only grouped together +# and then sorted according to the other variables +# in your sorting-method +sticky=true + +*_pickaxe +*_axe +*_shovel +*_hoe \ No newline at end of file diff --git a/src/main/resources/categories/905-tools.default.txt b/src/main/resources/categories/907-other-tools.default.txt similarity index 51% rename from src/main/resources/categories/905-tools.default.txt rename to src/main/resources/categories/907-other-tools.default.txt index b04511f..c79f16e 100644 --- a/src/main/resources/categories/905-tools.default.txt +++ b/src/main/resources/categories/907-other-tools.default.txt @@ -6,10 +6,13 @@ # your custom files because ChestSort will overwrite them # -*_pickaxe -*_shovel -*_hoe -*_axe +# When sticky is set to true, order the items in this +# category exactly as defined in this file. +# When set to false, the items are only grouped together +# and then sorted according to the other variables +# in your sorting-method +sticky=false + flint_and_steel fishing_rod compass diff --git a/src/main/resources/categories/940-food.default.txt b/src/main/resources/categories/909-food.default.txt similarity index 66% rename from src/main/resources/categories/940-food.default.txt rename to src/main/resources/categories/909-food.default.txt index f4632fb..85cdbea 100644 --- a/src/main/resources/categories/940-food.default.txt +++ b/src/main/resources/categories/909-food.default.txt @@ -6,6 +6,13 @@ # your custom files because ChestSort will overwrite them # +# When sticky is set to true, order the items in this +# category exactly as defined in this file. +# When set to false, the items are only grouped together +# and then sorted according to the other variables +# in your sorting-method +sticky=false + apple baked_potato beef diff --git a/src/main/resources/categories/910-valuables.default.txt b/src/main/resources/categories/910-valuables.default.txt index bc69ebc..862cfce 100644 --- a/src/main/resources/categories/910-valuables.default.txt +++ b/src/main/resources/categories/910-valuables.default.txt @@ -6,6 +6,13 @@ # your custom files because ChestSort will overwrite them # +# When sticky is set to true, order the items in this +# category exactly as defined in this file. +# When set to false, the items are only grouped together +# and then sorted according to the other variables +# in your sorting-method +sticky=false + diamond emerald diamond_ore diff --git a/src/main/resources/categories/920-armor.default.txt b/src/main/resources/categories/920-armor-and-arrows.default.txt similarity index 57% rename from src/main/resources/categories/920-armor.default.txt rename to src/main/resources/categories/920-armor-and-arrows.default.txt index 59d2894..f908d9d 100644 --- a/src/main/resources/categories/920-armor.default.txt +++ b/src/main/resources/categories/920-armor-and-arrows.default.txt @@ -6,6 +6,13 @@ # your custom files because ChestSort will overwrite them # +# When sticky is set to true, order the items in this +# category exactly as defined in this file. +# When set to false, the items are only grouped together +# and then sorted according to the other variables +# in your sorting-method +sticky=false + turtle_helmet bow arrow @@ -16,6 +23,5 @@ arrow *_boots spectral_arrow tipped_arrow -shield totem_of_undying trident \ No newline at end of file diff --git a/src/main/resources/categories/930-brewing.default.txt b/src/main/resources/categories/930-brewing.default.txt index 5285ad6..6154799 100644 --- a/src/main/resources/categories/930-brewing.default.txt +++ b/src/main/resources/categories/930-brewing.default.txt @@ -6,6 +6,13 @@ # your custom files because ChestSort will overwrite them # +# When sticky is set to true, order the items in this +# category exactly as defined in this file. +# When set to false, the items are only grouped together +# and then sorted according to the other variables +# in your sorting-method +sticky=false + ghast_tear potion glass_bottle diff --git a/src/main/resources/categories/950-redstone.default.txt b/src/main/resources/categories/950-redstone.default.txt index 3820f5a..ab511bb 100644 --- a/src/main/resources/categories/950-redstone.default.txt +++ b/src/main/resources/categories/950-redstone.default.txt @@ -6,6 +6,13 @@ # your custom files because ChestSort will overwrite them # +# When sticky is set to true, order the items in this +# category exactly as defined in this file. +# When set to false, the items are only grouped together +# and then sorted according to the other variables +# in your sorting-method +sticky=false + dispenser note_block sticky_piston diff --git a/src/main/resources/categories/960-wood.default.txt b/src/main/resources/categories/960-wood.default.txt index 5d8f0f8..2dc5858 100644 --- a/src/main/resources/categories/960-wood.default.txt +++ b/src/main/resources/categories/960-wood.default.txt @@ -6,6 +6,13 @@ # your custom files because ChestSort will overwrite them # +# When sticky is set to true, order the items in this +# category exactly as defined in this file. +# When set to false, the items are only grouped together +# and then sorted according to the other variables +# in your sorting-method +sticky=false + *_planks *_sapling *_log diff --git a/src/main/resources/categories/970-stone.default.txt b/src/main/resources/categories/970-stone.default.txt index 9d170a2..bd8a078 100644 --- a/src/main/resources/categories/970-stone.default.txt +++ b/src/main/resources/categories/970-stone.default.txt @@ -6,6 +6,13 @@ # your custom files because ChestSort will overwrite them # +# When sticky is set to true, order the items in this +# category exactly as defined in this file. +# When set to false, the items are only grouped together +# and then sorted according to the other variables +# in your sorting-method +sticky=false + stone cobblestone granite diff --git a/src/main/resources/categories/980-plants.default.txt b/src/main/resources/categories/980-plants.default.txt index cd4db7f..5a2f16f 100644 --- a/src/main/resources/categories/980-plants.default.txt +++ b/src/main/resources/categories/980-plants.default.txt @@ -6,6 +6,13 @@ # your custom files because ChestSort will overwrite them # +# When sticky is set to true, order the items in this +# category exactly as defined in this file. +# When set to false, the items are only grouped together +# and then sorted according to the other variables +# in your sorting-method +sticky=false + grass fern dead_bush diff --git a/src/main/resources/categories/981-corals.default.txt b/src/main/resources/categories/981-corals.default.txt index 34b1ef4..d68ed2b 100644 --- a/src/main/resources/categories/981-corals.default.txt +++ b/src/main/resources/categories/981-corals.default.txt @@ -6,4 +6,11 @@ # your custom files because ChestSort will overwrite them # -*_coral* \ No newline at end of file +# When sticky is set to true, order the items in this +# category exactly as defined in this file. +# When set to false, the items are only grouped together +# and then sorted according to the other variables +# in your sorting-method +sticky=false + +*_coral* # This should get everything related to corals! \ No newline at end of file From 0d51f9a8b0df32a1274b37bb53eb9d92f4db6e18 Mon Sep 17 00:00:00 2001 From: mfnalex <1122571+mfnalex@users.noreply.github.com> Date: Fri, 3 May 2019 15:01:35 +0200 Subject: [PATCH 2/2] fixed category names --- .../java/de/jeffclan/JeffChestSort/JeffChestSortPlugin.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/de/jeffclan/JeffChestSort/JeffChestSortPlugin.java b/src/main/java/de/jeffclan/JeffChestSort/JeffChestSortPlugin.java index 6014196..5bf7a01 100644 --- a/src/main/java/de/jeffclan/JeffChestSort/JeffChestSortPlugin.java +++ b/src/main/java/de/jeffclan/JeffChestSort/JeffChestSortPlugin.java @@ -321,7 +321,7 @@ public class JeffChestSortPlugin extends JavaPlugin { } // Isn't there a smarter way to find all the 9** files in the .jar? - String[] defaultCategories = { "900-weapons", "905-tools", "910-valuables", "920-armor", "930-brewing", "940-food", + String[] defaultCategories = { "900-weapons", "905-common-tools", "907-other-tools", "909-food", "910-valuables", "920-armor-and-arrows", "930-brewing", "950-redstone", "960-wood", "970-stone", "980-plants", "981-corals","_ReadMe - Category files" }; // Delete all files starting with 9..