This commit is contained in:
JaTiTV 2024-07-01 15:12:53 +02:00
parent bbe8fbce49
commit 1397656bf2
5 changed files with 222 additions and 0 deletions

View File

@ -227,6 +227,12 @@
<version>20230227</version> <version>20230227</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>2.0</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -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<String,Object> config = yaml.load(Files.newInputStream(configFile.toPath()));
Map<String, List<String>> 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<String, Object> 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<pathsplit.length;i++){
zw.append(pathsplit[i]);
if(i!= (pathsplit.length-1)){
zw.append(".");
}
}
readValue((Map<String, Object>) config.get(key),zw.toString(), value);
}
}else{
Object vl = config.get(path);
if(vl != null){
value.setValue(vl);
}
}
}
private static void addValue(Map<String, Object> 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<pathsplit.length;i++){
zw.append(pathsplit[i]);
if(i!= (pathsplit.length-1)){
zw.append(".");
}
}
addValue((Map<String, Object>) config.get(key),zw.toString(),value);
}else{
Map<String, Object> newMap = new LinkedHashMap<>();
StringBuilder zw = new StringBuilder();
for(int i=1; i<pathsplit.length;i++){
zw.append(pathsplit[i]);
if(i!= (pathsplit.length-1)){
zw.append(".");
}
}
addValue(newMap,zw.toString(),value);
config.put(key, newMap);
}
}else{
if(!config.containsKey(path)){
config.put(path, value);
}
}
}
private static void saveConfigWithComments(File file, Map<String, List<String>> comments, Map<String,Object> 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<String,Object> config, Map<String, List<String>> comments, StringBuilder builder, String prefix, int indentLevel) {
String indent = " ".repeat(indentLevel);
for (Map.Entry<String, Object> 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<String> 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<String, Object>) value, comments, builder, fullKey, indentLevel + 1);
} else {
// Add value with proper indentation
builder.append(indent).append(key.getKey()).append(": ").append(value).append("\n");
}
}
}
}

View File

@ -0,0 +1,6 @@
// This class was created by JaTiTV.
package net.t2code.t2codelib.VELOCITY.system;
public class T2CodeVMain {
}

View File

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

View File

@ -0,0 +1 @@
{"id":"t2codelib","name":"T2CodeLib","version":"${project.version}","authors":["JaTiTV"],"dependencies":[],"main":"net.t2code.t2codelib.VELOCITY.system.T2CodeVMain"}