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>
<relocation>
<pattern>org.bstats</pattern>
<shadedPattern>de.jeffclan.utils</shadedPattern>
<shadedPattern>de.jeffclan.JeffChestSort</shadedPattern>
</relocation>
</relocations>
</configuration>

View File

@ -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<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
} 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("========================================================");
}
@ -314,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..

View File

@ -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());
}
}

View File

@ -15,4 +15,5 @@ sticky=true
bow
*_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
#
*_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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -6,4 +6,11 @@
# 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!