Merge pull request #19 from JEFF-Media-GbR/config-updater

Config updater
This commit is contained in:
JEFF 2019-05-03 15:02:20 +02:00 committed by GitHub
commit b8510b36fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 214 additions and 20 deletions

View File

@ -47,7 +47,7 @@
<relocations> <relocations>
<relocation> <relocation>
<pattern>org.bstats</pattern> <pattern>org.bstats</pattern>
<shadedPattern>de.jeffclan.utils</shadedPattern> <shadedPattern>de.jeffclan.JeffChestSort</shadedPattern>
</relocation> </relocation>
</relocations> </relocations>
</configuration> </configuration>

View File

@ -1,10 +1,103 @@
package de.jeffclan.JeffChestSort; 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 { 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 // 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<String, Object> oldValues = oldConfig.getValues(false);
// Read default config to keep comments
ArrayList<String> linesInDefaultConfig = new ArrayList<String>();
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<String> newLines = new ArrayList<String>();
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");
}
} }

View File

@ -104,7 +104,11 @@ public class JeffChestSortPlugin extends JavaPlugin {
// use the default values later on // use the default values later on
} else if (getConfig().getInt("config-version", 0) != currentConfigVersion) { } else if (getConfig().getInt("config-version", 0) != currentConfigVersion) {
showOldConfigWarning(); showOldConfigWarning();
usingMatchingConfig = false; JeffChestSortConfigUpdater configUpdater = new JeffChestSortConfigUpdater(this);
configUpdater.updateConfig();
configUpdater = null;
usingMatchingConfig = true;
//createConfig();
} }
createDirectories(); createDirectories();
@ -151,13 +155,16 @@ public class JeffChestSortPlugin extends JavaPlugin {
private void showOldConfigWarning() { private void showOldConfigWarning() {
getLogger().warning("========================================================"); getLogger().warning("========================================================");
getLogger().warning("YOU ARE USING AN OLD CONFIG 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("This is not a problem, as ChestSort will just use the");
getLogger().warning("default settings for unset values. However, if you want"); // getLogger().warning("default settings for unset values. However, if you want");
getLogger().warning("to configure the new options, please go to"); // getLogger().warning("to configure the new options, please go to");
getLogger().warning("https://www.chestsort.de and replace your config.yml"); // 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("with the new one. You can then insert your old changes");
getLogger().warning("into the new file."); // 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("========================================================"); getLogger().warning("========================================================");
} }
@ -314,7 +321,7 @@ public class JeffChestSortPlugin extends JavaPlugin {
} }
// Isn't there a smarter way to find all the 9** files in the .jar? // 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" }; "950-redstone", "960-wood", "970-stone", "980-plants", "981-corals","_ReadMe - Category files" };
// Delete all files starting with 9.. // Delete all files starting with 9..

View File

@ -2,9 +2,12 @@ package de.jeffclan.utils;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import de.jeffclan.JeffChestSort.JeffChestSortPlugin;
public class Utils { public class Utils {
// We need this to write the category files inside the .jar to the disk // 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) { public static String shortToStringWithLeadingZeroes(short number) {
return String.format("%05d", 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());
}
} }

View File

@ -16,3 +16,4 @@ sticky=true
bow bow
*_sword *_sword
trident trident
shield

View File

@ -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

View File

@ -6,10 +6,13 @@
# your custom files because ChestSort will overwrite them # your custom files because ChestSort will overwrite them
# #
*_pickaxe # When sticky is set to true, order the items in this
*_shovel # category exactly as defined in this file.
*_hoe # When set to false, the items are only grouped together
*_axe # and then sorted according to the other variables
# in your sorting-method
sticky=false
flint_and_steel flint_and_steel
fishing_rod fishing_rod
compass compass

View File

@ -6,6 +6,13 @@
# your custom files because ChestSort will overwrite them # 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 apple
baked_potato baked_potato
beef beef

View File

@ -6,6 +6,13 @@
# your custom files because ChestSort will overwrite them # 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 diamond
emerald emerald
diamond_ore diamond_ore

View File

@ -6,6 +6,13 @@
# your custom files because ChestSort will overwrite them # 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 turtle_helmet
bow bow
arrow arrow
@ -16,6 +23,5 @@ arrow
*_boots *_boots
spectral_arrow spectral_arrow
tipped_arrow tipped_arrow
shield
totem_of_undying totem_of_undying
trident trident

View File

@ -6,6 +6,13 @@
# your custom files because ChestSort will overwrite them # 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 ghast_tear
potion potion
glass_bottle glass_bottle

View File

@ -6,6 +6,13 @@
# your custom files because ChestSort will overwrite them # 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 dispenser
note_block note_block
sticky_piston sticky_piston

View File

@ -6,6 +6,13 @@
# your custom files because ChestSort will overwrite them # 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 *_planks
*_sapling *_sapling
*_log *_log

View File

@ -6,6 +6,13 @@
# your custom files because ChestSort will overwrite them # 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 stone
cobblestone cobblestone
granite granite

View File

@ -6,6 +6,13 @@
# your custom files because ChestSort will overwrite them # 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 grass
fern fern
dead_bush dead_bush

View File

@ -6,4 +6,11 @@
# your custom files because ChestSort will overwrite them # your custom files because ChestSort will overwrite them
# #
*_coral* # 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!