diff --git a/pom.xml b/pom.xml index fe3045c..644e298 100644 --- a/pom.xml +++ b/pom.xml @@ -227,6 +227,12 @@ 20230227 compile + + org.yaml + snakeyaml + 2.0 + compile + diff --git a/src/main/java/net/t2code/t2codelib/VELOCITY/api/yml/T2CVconfigWriter.java b/src/main/java/net/t2code/t2codelib/VELOCITY/api/yml/T2CVconfigWriter.java new file mode 100644 index 0000000..6a8bb10 --- /dev/null +++ b/src/main/java/net/t2code/t2codelib/VELOCITY/api/yml/T2CVconfigWriter.java @@ -0,0 +1,141 @@ +package net.t2code.t2codelib.VELOCITY.api.yml;// This class was created by JaTiTV. + + +import net.t2code.t2codelib.T2CconfigItem; +import org.yaml.snakeyaml.Yaml; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +public class T2CVconfigWriter { + + + + public static void createConfig(File configFile, T2CconfigItem[] manager, String... header) throws IOException { + if (!configFile.exists()) { + configFile.getParentFile().mkdirs(); + try { + configFile.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + return; + } + } + + Yaml yaml = new Yaml(); + Map config = yaml.load(Files.newInputStream(configFile.toPath())); + Map> comments = new LinkedHashMap<>(); + if(config == null){ + config = new LinkedHashMap<>(); + } + for(T2CconfigItem value : manager){ + readValue(config,value.getPath(), value); + addValue(config, value.getPath(), value.getValue()); + comments.put(value.getPath(), value.getComments()); + } + saveConfigWithComments(configFile, comments,config, header); + } + + private static void readValue(Map config, String path, T2CconfigItem value) { + if(path.contains(".")){ + String[] pathsplit = path.split("\\."); + String key = pathsplit[0]; + if(config.containsKey(key)){ + StringBuilder zw = new StringBuilder(); + for(int i=1; i) config.get(key),zw.toString(), value); + } + }else{ + Object vl = config.get(path); + if(vl != null){ + value.setValue(vl); + } + } + } + + private static void addValue(Map config, String path, Object value) { + if(path.contains(".")){ + String[] pathsplit = path.split("\\."); + String key = pathsplit[0]; + if(config.containsKey(key)){ + StringBuilder zw = new StringBuilder(); + for(int i=1; i) config.get(key),zw.toString(),value); + }else{ + Map newMap = new LinkedHashMap<>(); + StringBuilder zw = new StringBuilder(); + for(int i=1; i> comments, Map config, String... headers) { + try { + StringBuilder configContent = new StringBuilder(); + for(String h : headers){ + configContent.append("# ").append(h).append("\n"); + } + configContent.append("\n"); + addSection(config, comments, configContent, "", 0); + + // Write the content to the file + Files.write(file.toPath(), configContent.toString().getBytes()); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static void addSection(Map config, Map> comments, StringBuilder builder, String prefix, int indentLevel) { + String indent = " ".repeat(indentLevel); + + + for (Map.Entry key : config.entrySet()) { + String fullKey = prefix.isEmpty() ? key.getKey() : prefix + "." + key.getKey(); + Object value = key.getValue(); + + // Add comment if it exists for this key + List commentList = comments.get(fullKey); + if (commentList != null) { + for(String c : commentList){ + builder.append(indent).append("# ").append(c).append("\n"); + } + } + + // Check if the value is a section (nested map) + if (value instanceof Map) { + // Correctly add the section + builder.append(indent).append(key.getKey()).append(":\n"); + addSection((Map) value, comments, builder, fullKey, indentLevel + 1); + } else { + // Add value with proper indentation + builder.append(indent).append(key.getKey()).append(": ").append(value).append("\n"); + } + } + } +} diff --git a/src/main/java/net/t2code/t2codelib/VELOCITY/system/T2CodeVMain.java b/src/main/java/net/t2code/t2codelib/VELOCITY/system/T2CodeVMain.java new file mode 100644 index 0000000..e416d90 --- /dev/null +++ b/src/main/java/net/t2code/t2codelib/VELOCITY/system/T2CodeVMain.java @@ -0,0 +1,6 @@ +// This class was created by JaTiTV. + +package net.t2code.t2codelib.VELOCITY.system; + +public class T2CodeVMain { +} diff --git a/src/main/java/net/t2code/t2codelib/VELOCITY/system/config/T2CVlibConfig.java b/src/main/java/net/t2code/t2codelib/VELOCITY/system/config/T2CVlibConfig.java new file mode 100644 index 0000000..ea88c46 --- /dev/null +++ b/src/main/java/net/t2code/t2codelib/VELOCITY/system/config/T2CVlibConfig.java @@ -0,0 +1,68 @@ +package net.t2code.t2codelib.VELOCITY.system.config; + +import net.t2code.t2codelib.BUNGEE.api.yaml.T2CBconfigWriter; +import net.t2code.t2codelib.BUNGEE.system.T2CodeBMain; +import net.t2code.t2codelib.T2CconfigItem; +import net.t2code.t2codelib.Util; + +import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class T2CVlibConfig { + + public enum VALUES implements T2CconfigItem{ + + + /** + * TODO Converter !!!!! + */ + + updateTimer("UpdateCheck.TimerInMin", 60), + seePreReleaseUpdates("UpdateCheck.SeePreReleaseUpdates", true), + updateCheckFullDisable("Plugin.UpdateCheck.AllPlugins.FullDisable", false), + + apiCommandGUIEnable("API.CommandGUI.Enable", false, "Aktiviere die API für CommandGUI"), + apiAutoResponse("API.AutoResponse.Enable", false), + apiOpSecurity("API.OPSecurity.Enable", false), + + ; + + + private final String path; + private Object value; + private final List comments; + + VALUES(String path, Object value, String... comments) { + this.path = path; + this.value = value; + this.comments = new ArrayList<>(Arrays.asList(comments)); + } + + @Override + public String getPath() { + return path; + } + + @Override + public Object getValue() { + return value; + } + + @Override + public List getComments() { + return comments; + } + + @Override + public void setValue(Object newValue) { + value = newValue; + } + } + + public static void set(){ + T2CBconfigWriter.createConfig(new File(T2CodeBMain.getPlugin().getDataFolder(), "config.yml"), VALUES.values(), Util.getConfigLogo()); + } + +} diff --git a/src/main/resources/velocity-plugin.json b/src/main/resources/velocity-plugin.json new file mode 100644 index 0000000..04b68d0 --- /dev/null +++ b/src/main/resources/velocity-plugin.json @@ -0,0 +1 @@ +{"id":"t2codelib","name":"T2CodeLib","version":"${project.version}","authors":["JaTiTV"],"dependencies":[],"main":"net.t2code.t2codelib.VELOCITY.system.T2CodeVMain"} \ No newline at end of file