16.7_dev-3 repo release

ConfigWriter
- add List & "" to String
- fix forceSet
This commit is contained in:
JaTiTV 2024-07-03 00:01:02 +02:00
parent 2f168197fe
commit e9a43889cb
6 changed files with 91 additions and 78 deletions

View File

@ -6,7 +6,7 @@
<groupId>net.t2code</groupId> <groupId>net.t2code</groupId>
<artifactId>T2CodeLib</artifactId> <artifactId>T2CodeLib</artifactId>
<version>16.7_dev-2</version> <version>16.7_dev-3</version>
<!--version>VERSION_snapshot-0</version--> <!--version>VERSION_snapshot-0</version-->
<!--version>VERSION_beta-0</version--> <!--version>VERSION_beta-0</version-->
<!--version>VERSION_dev-0</version--> <!--version>VERSION_dev-0</version-->

View File

@ -20,7 +20,8 @@ public class T2CBconfigWriter {
public static void createConfig(File configFile, T2CconfigItem[] values, String... header) { public static void createConfig(File configFile, T2CconfigItem[] values, String... header) {
if (!configFile.exists()) { boolean exist = configFile.exists();
if (!exist) {
configFile.getParentFile().mkdirs(); configFile.getParentFile().mkdirs();
try { try {
configFile.createNewFile(); configFile.createNewFile();
@ -38,10 +39,13 @@ public class T2CBconfigWriter {
Map<String, List<String>> comments = new LinkedHashMap<>(); Map<String, List<String>> comments = new LinkedHashMap<>();
for(T2CconfigItem item : values){ for(T2CconfigItem item : values){
if(!config.contains(item.getPath())){ if (item.getForceSet() || !exist) {
config.set(item.getPath(), item.getValue()); if(!config.contains(item.getPath())){
config.set(item.getPath(), item.getValue());
}
comments.put(item.getPath(), item.getComments());
} }
comments.put(item.getPath(), item.getComments());
} }
saveConfigWithComments(configFile, comments, header); saveConfigWithComments(configFile, comments, header);
readConfig(config,values); readConfig(config,values);
@ -93,7 +97,21 @@ public class T2CBconfigWriter {
addSection((Configuration) value, comments, builder, fullKey, indentLevel + 1); addSection((Configuration) value, comments, builder, fullKey, indentLevel + 1);
} else { } else {
// Add value with proper indentation // Add value with proper indentation
builder.append(indent).append(key).append(": ").append(value).append("\n"); // builder.append(indent).append(key).append(": ").append(value).append("\n");
if (value instanceof List) {
builder.append(indent).append(key).append(": ").append("\n");
List<Object> zw = (List<Object>) value;
for (Object s : zw) {
if (s instanceof String) {
builder.append(indent).append("- \"").append(s).append("\"\n");
} else builder.append(indent).append("- ").append(s).append("\n");
}
} else {
if (value instanceof String) {
builder.append(indent).append(key).append(": \"").append(value).append("\"\n");
} else builder.append(indent).append(key).append(": ").append(value).append("\n");
}
} }
} }
} }

View File

@ -34,10 +34,10 @@ public class T2CconfigWriter {
config = YamlConfiguration.loadConfiguration(configFile); config = YamlConfiguration.loadConfiguration(configFile);
Map<String, List<String>> comments = new LinkedHashMap<>(); Map<String, List<String>> comments = new LinkedHashMap<>();
for (T2CconfigItem value : values) { for (T2CconfigItem item : values) {
if ((value.getForceSet() && exist) || (!exist && !value.getForceSet())) { if (item.getForceSet() || !exist) {
config.addDefault(value.getPath(), value.getValue()); config.addDefault(item.getPath(), item.getValue());
comments.put(value.getPath(), value.getComments()); comments.put(item.getPath(), item.getComments());
} }
} }
@ -93,7 +93,19 @@ public class T2CconfigWriter {
addSection((ConfigurationSection) value, comments, builder, fullKey, indentLevel + 1); addSection((ConfigurationSection) value, comments, builder, fullKey, indentLevel + 1);
} else { } else {
// Add value with proper indentation // Add value with proper indentation
builder.append(indent).append(key).append(": ").append(value).append("\n"); if (value instanceof List) {
builder.append(indent).append(key).append(": ").append("\n");
List<Object> zw = (List<Object>) value;
for (Object s : zw) {
if (s instanceof String) {
builder.append(indent).append("- \"").append(s).append("\"\n");
} else builder.append(indent).append("- ").append(s).append("\n");
}
} else {
if (value instanceof String) {
builder.append(indent).append(key).append(": \"").append(value).append("\"\n");
} else builder.append(indent).append(key).append(": ").append(value).append("\n");
}
} }
} }
} }

View File

@ -5,6 +5,8 @@ import net.t2code.t2codelib.SPIGOT.api.yaml.T2CconfigWriter;
import net.t2code.t2codelib.SPIGOT.system.T2CodeLibMain; import net.t2code.t2codelib.SPIGOT.system.T2CodeLibMain;
import net.t2code.t2codelib.T2CconfigItem; import net.t2code.t2codelib.T2CconfigItem;
import net.t2code.t2codelib.Util; import net.t2code.t2codelib.Util;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
@ -44,14 +46,6 @@ public class T2CLibConfig {
space_command("command", null, true,""), space_command("command", null, true,""),
test("opWhitelist.whitelist", null, true,"Test"),
testp1n("opWhitelist.whitelist.player1.name", "PlayerName", false,"Test"),
testp1u("opWhitelist.whitelist.player1.uuid", "00000000000000000000000000000000", false,"Test"),
commandPermToggleCommand("command.permToggle.permissionSetCommand", "lp user [player] permission set [perm] [value]", commandPermToggleCommand("command.permToggle.permissionSetCommand", "lp user [player] permission set [perm] [value]",
true,"This option specifies which command is to be used for the T2CodeLib command '/t2code permtoggle <player> <permission>'."), true,"This option specifies which command is to be used for the T2CodeLib command '/t2code permtoggle <player> <permission>'."),
; ;

View File

@ -1,24 +0,0 @@
// This class was created by JaTiTV.
package net.t2code.t2codelib.SPIGOT.system.config.config;
import net.t2code.t2codelib.T2CconfigItem;
import org.bukkit.configuration.ConfigurationSection;
import java.util.List;
public class test {
/**
public void test(){
ConfigurationSection test = (ConfigurationSection) T2CLibConfig.VALUES.test.getValue();
for (String key : test.getConfigurationSection("").getKeys(false)) {
String name = test.getString(key + ".Playername");//value.pathPlayerName.replace("KEY", key));
PlayerObject playerObject = new PlayerObject(
name,
test.getString(key + ".UUID")//value.pathPlayerUuid.replace("KEY", key)).replace("-", ""));
PlayerCache.getOpHashMap().put(name, playerObject);
}
}
*/
}

View File

@ -14,11 +14,9 @@ import java.util.Map;
public class T2CVconfigWriter { public class T2CVconfigWriter {
public static void createConfig(Logger logger, File configFile, T2CconfigItem[] manager, String... header) throws IOException { public static void createConfig(Logger logger, File configFile, T2CconfigItem[] manager, String... header) throws IOException {
boolean exist = configFile.exists();
if (!configFile.exists()) { if (!exist) {
configFile.getParentFile().mkdirs(); configFile.getParentFile().mkdirs();
try { try {
configFile.createNewFile(); configFile.createNewFile();
@ -29,78 +27,81 @@ public class T2CVconfigWriter {
} }
Yaml yaml = new Yaml(); Yaml yaml = new Yaml();
Map<String,Object> config = yaml.load(Files.newInputStream(configFile.toPath())); Map<String, Object> config = yaml.load(Files.newInputStream(configFile.toPath()));
Map<String, List<String>> comments = new LinkedHashMap<>(); Map<String, List<String>> comments = new LinkedHashMap<>();
if(config == null){ if (config == null) {
config = new LinkedHashMap<>(); config = new LinkedHashMap<>();
} }
for(T2CconfigItem value : manager){ for (T2CconfigItem item : manager) {
readValue(config,value.getPath(), value); if (item.getForceSet() || !exist) {
addValue(config, value.getPath(), value.getValue()); readValue(config, item.getPath(), item);
comments.put(value.getPath(), value.getComments()); addValue(config, item.getPath(), item.getValue());
comments.put(item.getPath(), item.getComments());
}
} }
saveConfigWithComments(configFile, comments,config, header); saveConfigWithComments(configFile, comments, config, header);
} }
private static void readValue(Map<String, Object> config, String path, T2CconfigItem value) { private static void readValue(Map<String, Object> config, String path, T2CconfigItem value) {
if(path.contains(".")){ if (path.contains(".")) {
String[] pathsplit = path.split("\\."); String[] pathsplit = path.split("\\.");
String key = pathsplit[0]; String key = pathsplit[0];
if(config.containsKey(key)){ if (config.containsKey(key)) {
StringBuilder zw = new StringBuilder(); StringBuilder zw = new StringBuilder();
for(int i=1; i<pathsplit.length;i++){ for (int i = 1; i < pathsplit.length; i++) {
zw.append(pathsplit[i]); zw.append(pathsplit[i]);
if(i!= (pathsplit.length-1)){ if (i != (pathsplit.length - 1)) {
zw.append("."); zw.append(".");
} }
} }
readValue((Map<String, Object>) config.get(key),zw.toString(), value); readValue((Map<String, Object>) config.get(key), zw.toString(), value);
} }
}else{ } else {
Object vl = config.get(path); Object vl = config.get(path);
if(vl != null){ if (vl != null) {
value.setValue(vl); value.setValue(vl);
} }
} }
} }
private static void addValue(Map<String, Object> config, String path, Object value) { private static void addValue(Map<String, Object> config, String path, Object value) {
if(path.contains(".")){ if (path.contains(".")) {
String[] pathsplit = path.split("\\."); String[] pathsplit = path.split("\\.");
String key = pathsplit[0]; String key = pathsplit[0];
if(config.containsKey(key)){ if (config.containsKey(key)) {
StringBuilder zw = new StringBuilder(); StringBuilder zw = new StringBuilder();
for(int i=1; i<pathsplit.length;i++){ for (int i = 1; i < pathsplit.length; i++) {
zw.append(pathsplit[i]); zw.append(pathsplit[i]);
if(i!= (pathsplit.length-1)){ if (i != (pathsplit.length - 1)) {
zw.append("."); zw.append(".");
} }
} }
addValue((Map<String, Object>) config.get(key),zw.toString(),value); addValue((Map<String, Object>) config.get(key), zw.toString(), value);
}else{ } else {
Map<String, Object> newMap = new LinkedHashMap<>(); Map<String, Object> newMap = new LinkedHashMap<>();
StringBuilder zw = new StringBuilder(); StringBuilder zw = new StringBuilder();
for(int i=1; i<pathsplit.length;i++){ for (int i = 1; i < pathsplit.length; i++) {
zw.append(pathsplit[i]); zw.append(pathsplit[i]);
if(i!= (pathsplit.length-1)){ if (i != (pathsplit.length - 1)) {
zw.append("."); zw.append(".");
} }
} }
addValue(newMap,zw.toString(),value); addValue(newMap, zw.toString(), value);
config.put(key, newMap); config.put(key, newMap);
} }
}else{ } else {
if(!config.containsKey(path)){ if (!config.containsKey(path)) {
config.put(path, value); config.put(path, value);
} }
} }
} }
private static void saveConfigWithComments(File file, Map<String, List<String>> comments, Map<String,Object> config, String... headers) { private static void saveConfigWithComments(File file, Map<String, List<String>> comments, Map<String, Object> config, String... headers) {
try { try {
StringBuilder configContent = new StringBuilder(); StringBuilder configContent = new StringBuilder();
for(String h : headers){ for (String h : headers) {
configContent.append(h).append("\n"); configContent.append(h).append("\n");
} }
configContent.append("\n"); configContent.append("\n");
@ -113,7 +114,7 @@ public class T2CVconfigWriter {
} }
} }
private static void addSection(Map<String,Object> config, Map<String, List<String>> comments, StringBuilder builder, String prefix, int indentLevel) { private static void addSection(Map<String, Object> config, Map<String, List<String>> comments, StringBuilder builder, String prefix, int indentLevel) {
String indent = " ".repeat(indentLevel); String indent = " ".repeat(indentLevel);
@ -124,11 +125,10 @@ public class T2CVconfigWriter {
// Add comment if it exists for this key // Add comment if it exists for this key
List<String> commentList = comments.get(fullKey); List<String> commentList = comments.get(fullKey);
if (commentList != null) { if (commentList != null) {
for(String c : commentList){ for (String c : commentList) {
builder.append(indent).append("# ").append(c).append("\n"); builder.append(indent).append("# ").append(c).append("\n");
} }
} }
// Check if the value is a section (nested map) // Check if the value is a section (nested map)
if (value instanceof Map) { if (value instanceof Map) {
// Correctly add the section // Correctly add the section
@ -136,7 +136,20 @@ public class T2CVconfigWriter {
addSection((Map<String, Object>) value, comments, builder, fullKey, indentLevel + 1); addSection((Map<String, Object>) value, comments, builder, fullKey, indentLevel + 1);
} else { } else {
// Add value with proper indentation // Add value with proper indentation
builder.append(indent).append(key.getKey()).append(": ").append(value).append("\n"); // builder.append(indent).append(key.getKey()).append(": ").append(value).append("\n");
if (value instanceof List) {
builder.append(indent).append(key).append(": ").append("\n");
List<Object> zw = (List<Object>) value;
for (Object s : zw) {
if (s instanceof String) {
builder.append(indent).append("- \"").append(s).append("\"\n");
} else builder.append(indent).append("- ").append(s).append("\n");
}
} else {
if (value instanceof String) {
builder.append(indent).append(key).append(": \"").append(value).append("\"\n");
} else builder.append(indent).append(key).append(": ").append(value).append("\n");
}
} }
} }
} }