diff --git a/src/main/java/com/gamingmesh/jobs/CMILib/ConfigReader.java b/src/main/java/com/gamingmesh/jobs/CMILib/ConfigReader.java new file mode 100644 index 00000000..a15f2cd8 --- /dev/null +++ b/src/main/java/com/gamingmesh/jobs/CMILib/ConfigReader.java @@ -0,0 +1,347 @@ +package com.gamingmesh.jobs.CMILib; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.nio.charset.Charset; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashMap; +import java.util.List; + +import org.bukkit.Location; +import org.bukkit.configuration.InvalidConfigurationException; +import org.bukkit.configuration.file.YamlConfiguration; + +import com.gamingmesh.jobs.Jobs; +import com.google.common.io.Files; + +/* + * Made by Zrips + */ + +public class ConfigReader extends YamlConfiguration { + private HashMap comments; + YamlConfiguration config; + private String p = null; + private File file = null; + + public ConfigReader(String fileName) throws Exception { + this(new File(Jobs.getInstance().getDataFolder(), fileName)); + } + + public ConfigReader(File file) throws Exception { + super(); + comments = new HashMap(); + this.file = file; + this.config = getyml(file); + } + + @Override + public void save(String file) throws IOException { + if (file == null) { + throw new IllegalArgumentException("File cannot be null"); + } + + save(new File(file)); + } + + @Override + public void save(File file) throws IOException { + if (file == null) { + throw new IllegalArgumentException("File cannot be null"); + } + Files.createParentDirs(file); + String data = insertComments(saveToString()); + PrintWriter writer = new PrintWriter(file, "UTF-8"); + try { + writer.write(data); + } finally { + writer.close(); + } + } + + private String insertComments(String yaml) { + if (!comments.isEmpty()) { + String[] yamlContents = yaml.split("[" + System.getProperty("line.separator") + "]"); + StringBuilder newContents = new StringBuilder(); + StringBuilder currentPath = new StringBuilder(); + boolean commentedPath = false; + boolean node = false; + int depth = 0; + + boolean firstLine = true; + for (final String line : yamlContents) { + if (firstLine) { + firstLine = false; + if (line.startsWith("#")) { + continue; + } + } + + boolean keyOk = true; + if (line.contains(": ")) { + int index = 0; + index = line.indexOf(": "); + if (index < 0) { + index = line.length() - 1; + } + int whiteSpace = 0; + for (int n = 0; n < line.length(); n++) { + if (line.charAt(n) == ' ') { + whiteSpace++; + } else { + break; + } + } + String key = line.substring(whiteSpace, index); + if (key.contains(" ")) + keyOk = false; + else if (key.contains("&")) + keyOk = false; + else if (key.contains(".")) + keyOk = false; + else if (key.contains("'")) + keyOk = false; + else if (key.contains("\"")) + keyOk = false; + } + + if (line.contains(": ") && keyOk || (line.length() > 1 && line.charAt(line.length() - 1) == ':')) { + commentedPath = false; + node = true; + + int index = 0; + index = line.indexOf(": "); + if (index < 0) { + index = line.length() - 1; + } + if (currentPath.toString().isEmpty()) { + currentPath = new StringBuilder(line.substring(0, index)); + } else { + int whiteSpace = 0; + for (int n = 0; n < line.length(); n++) { + if (line.charAt(n) == ' ') { + whiteSpace++; + } else { + break; + } + } + if (whiteSpace / 2 > depth) { + currentPath.append(".").append(line.substring(whiteSpace, index)); + depth++; + } else if (whiteSpace / 2 < depth) { + int newDepth = whiteSpace / 2; + for (int i = 0; i < depth - newDepth; i++) { + currentPath.replace(currentPath.lastIndexOf("."), currentPath.length(), ""); + } + int lastIndex = currentPath.lastIndexOf("."); + if (lastIndex < 0) { + currentPath = new StringBuilder(); + } else { + currentPath.replace(currentPath.lastIndexOf("."), currentPath.length(), "").append("."); + } + currentPath.append(line.substring(whiteSpace, index)); + depth = newDepth; + } else { + int lastIndex = currentPath.lastIndexOf("."); + if (lastIndex < 0) { + currentPath = new StringBuilder(); + } else { + currentPath.replace(currentPath.lastIndexOf("."), currentPath.length(), "").append("."); + } + currentPath.append(line.substring(whiteSpace, index)); + } + } + } else { + node = false; + } + StringBuilder newLine = new StringBuilder(line); + if (node) { + String comment = null; + if (!commentedPath) { + comment = comments.get(currentPath.toString()); + } + if (comment != null && !comment.isEmpty()) { + newLine.insert(0, System.getProperty("line.separator")).insert(0, comment); + comment = null; + commentedPath = true; + } + } + newLine.append(System.getProperty("line.separator")); + newContents.append(newLine.toString()); + } + + return newContents.toString(); + } + return yaml; + } + + public void addComment(String path, String... commentLines) { + StringBuilder commentstring = new StringBuilder(); + String leadingSpaces = ""; + for (int n = 0; n < path.length(); n++) { + if (path.charAt(n) == '.') { + leadingSpaces += " "; + } + } + for (String line : commentLines) { + if (!line.isEmpty()) { + line = leadingSpaces + "# " + line; + } + if (commentstring.length() > 0) { + commentstring.append(System.getProperty("line.separator")); + } + commentstring.append(line); + } + comments.put(path, commentstring.toString()); + } + + public YamlConfiguration getyml(File file) throws Exception { + YamlConfiguration config = new YamlConfiguration(); + FileInputStream fileinputstream = null; + + try { + fileinputstream = new FileInputStream(file); + InputStreamReader str = new InputStreamReader(fileinputstream, Charset.forName("UTF-8")); + config.load(str); + str.close(); + } catch (FileNotFoundException e) { + } catch (InvalidConfigurationException | IOException e) { + e.printStackTrace(); + saveToBackup(); + throw e; + } finally { + if (fileinputstream != null) + try { + fileinputstream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + return config; + } + + public void saveToBackup() { + File cc = new File(Jobs.getInstance().getDataFolder(), "FileBackups"); + if (!cc.isDirectory()) + cc.mkdir(); + + Date date = new Date(); + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss "); + String newFileName = dateFormat.format(date) + file.getName(); + + Jobs.consoleMsg("&cFailed to load " + file.getName() + "! Backup have been saved into " + Jobs.getInstance().getDataFolder().getPath() + File.separator + "FileBackups" + + File.separator + newFileName); + + File f = new File(Jobs.getInstance().getDataFolder(), "FileBackups" + File.separator + newFileName); +// file.renameTo(f); + try { + Files.copy(file, f); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void save() { + try { + save(file); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static void newLn(StringBuilder header) { + header.append(System.lineSeparator()); + } + + private static StringBuilder formStringBuilder(List list) { + StringBuilder header = new StringBuilder(); + for (String one : list) { + header.append(one); + newLn(header); + } + return header; + } + + public void header(List list) { + options().header(formStringBuilder(list).toString()); + } + + String[] waitingComment = null; + + private void checkWaitingComment(String path) { + if (waitingComment == null) + return; + addComment(path, waitingComment); + waitingComment = null; + } + + public YamlConfiguration getC() { + return config; + } + + public void copyDefaults(boolean value) { + getC().options().copyDefaults(value); + } + + private String process(String path, Object value) { + if (this.p != null) + path = this.p + path; + checkWaitingComment(path); + config.addDefault(path, value); + copySetting(path); + return path; + } + + public Boolean get(String path, Boolean boo) { + path = process(path, boo); + return config.getBoolean(path); + } + + public Object get(String path, Location boo) { + path = process(path, boo); + return config.get(path); + } + + public int get(String path, int boo) { + path = process(path, boo); + return config.getInt(path); + } + + public List getIntList(String path, List list) { + path = process(path, list); + return config.getIntegerList(path); + } + + public List get(String path, List list) { + path = process(path, list); + return config.getStringList(path); + } + + public String get(String path, String boo) { + path = process(path, boo); + return config.getString(path); + } + + public Double get(String path, Double boo) { + path = process(path, boo); + return config.getDouble(path); + } + + private synchronized void copySetting(String path) { + set(path, config.get(path)); + } + + public void resetP() { + p = null; + } + + public void setP(String cmd) { + this.p = "command." + cmd + ".info."; + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 1702c60c..467c1318 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,7 +1,7 @@ name: Jobs description: Jobs Plugin for the BukkitAPI main: com.gamingmesh.jobs.Jobs -version: 4.9.0 +version: 4.9.1 api-version: 1.13 website: https://www.spigotmc.org/resources/jobs-reborn.4216/ author: phrstbrn diff --git a/src/main/resources/restrictedBlocks.yml b/src/main/resources/restrictedBlocks.yml new file mode 100644 index 00000000..4ec35b24 --- /dev/null +++ b/src/main/resources/restrictedBlocks.yml @@ -0,0 +1,81 @@ +# Block protected by timer in sec +# Category name can be any you like to be easily recognized +# id can be actual block id (use /jobs blockinfo to get correct id) or use block name +# By setting time to -1 will keep block protected until global cleanup, mainly used for structure blocks like diamond +# If you want to have default value for all blocks, enale GlobalBlockTimer in generalConfig file +blocksTimer: + OAK_SAPLING: 60 + ACACIA_SAPLING: 60 + BIRCH_SAPLING: 60 + DARK_OAK_SAPLING: 60 + JUNGLE_SAPLING: 60 + OAK_LEAVES: 60 + SPRUCE_LEAVES: 60 + ACACIA_LEAVES: 60 + BIRCH_LEAVES: 60 + DARK_OAK_LEAVES: 60 + JUNGLE_LEAVES: 60 + GRASS: 60 + TALL_GRASS: 60 + GRASS_BLOCK: 60 + GRASS_PATH: 60 + SEAGRASS: 60 + DEAD_BUSH: 60 + RAIL: 60 + POWERED_RAIL: 60 + DETECTOR_RAIL: 60 + ACTIVATOR_RAIL: 60 + COBWEB: 60 + DANDELION: 60 + POPPY: 60 + SUNFLOWER: 60 + BROWN_MUSHROOM: 60 + BROWN_MUSHROOM_BLOCK: 60 + MUSHROOM_STEM: 60 + RED_MUSHROOM: 60 + RED_MUSHROOM_BLOCK: 60 + TORCH: 60 + REDSTONE_TORCH: 60 + LADDER: 5 + LIGHT_BLUE_CARPET: 60 + LIGHT_GRAY_CARPET: 60 + LIME_CARPET: 60 + MAGENTA_CARPET: 60 + ORANGE_CARPET: 60 + PINK_CARPET: 60 + BLACK_CARPET: 60 + BLUE_CARPET: 60 + BROWN_CARPET: 60 + CYAN_CARPET: 60 + GRAY_CARPET: 60 + GREEN_CARPET: 60 + OAK_BUTTON: 5 + SPRUCE_BUTTON: 5 + STONE_BUTTON: 60 + ACACIA_BUTTON: 60 + BIRCH_BUTTON: 60 + DARK_OAK_BUTTON: 60 + JUNGLE_BUTTON: 60 + LEVER: 60 + SNOW: 60 + SNOW_BLOCK: 60 + TRIPWIRE_HOOK: 60 + REPEATER: 60 + COMPARATOR: 60 + LILY_PAD: 30 + VINE: 30 + WHEAT: 5 + SUGAR_CANE: 30 + CACTUS: 30 + NETHER_WART: 60 + PUMPKIN: 30 + CARVED_PUMPKIN: 30 + MELON: 30 + GOLD_ORE: -1 + IRON_ORE: -1 + COAL_ORE: -1 + LAPIS_ORE: -1 + DIAMOND_ORE: -1 + REDSTONE_ORE: -1 + EMERALD_ORE: -1 + NETHER_QUARTZ_ORE: -1 \ No newline at end of file