development status
add EnumManager
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
// This class was created by JaTiTV.
|
||||
|
||||
package net.t2code.t2codelib.SPIGOT.api.yaml;
|
||||
|
||||
import net.t2code.t2codelib.T2CconfigItem;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class T2CconfigEnumManager {
|
||||
|
||||
private final List<AdditionalEnum> additionalEnums = new ArrayList<>();
|
||||
|
||||
public static class AdditionalEnum implements T2CconfigItem {
|
||||
private final String key;
|
||||
private String path;
|
||||
private Object value;
|
||||
private List<String> comments;
|
||||
|
||||
public AdditionalEnum(String key, String path, Object value, String... comments) {
|
||||
this.key = key;
|
||||
this.path = path;
|
||||
this.value = value;
|
||||
this.comments = new ArrayList<>(Arrays.asList(comments));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getComments() {
|
||||
return comments;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValue(Object newValue) {
|
||||
value = newValue;
|
||||
}
|
||||
}
|
||||
|
||||
// Method to add or change an "enum" value
|
||||
public void addOrChangeEnum(Object key, String path, Object value, String... comments) {
|
||||
// Check if the key already exists
|
||||
for (AdditionalEnum enumValue : additionalEnums) {
|
||||
if (enumValue.key.equals(key)) {
|
||||
// Update existing enum
|
||||
enumValue.path = path;
|
||||
enumValue.value = value;
|
||||
enumValue.comments = Arrays.asList(comments);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Add new enum
|
||||
AdditionalEnum newEnum = new AdditionalEnum(key.toString(), path, value, comments);
|
||||
additionalEnums.add(newEnum);
|
||||
}
|
||||
|
||||
// Method to retrieve all enums (original and additional)
|
||||
public List<AdditionalEnum> getAllEnums() {
|
||||
return new ArrayList<>(additionalEnums);
|
||||
}
|
||||
|
||||
// Method to retrieve specific enum details
|
||||
public AdditionalEnum getEnumDetails(Object key) {
|
||||
for (AdditionalEnum e : additionalEnums) {
|
||||
if (e.key.equals(key.toString())) {
|
||||
return e;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@@ -0,0 +1,96 @@
|
||||
// This class was created by JaTiTV.
|
||||
|
||||
package net.t2code.t2codelib.SPIGOT.api.yaml;
|
||||
|
||||
import net.t2code.t2codelib.T2CconfigItem;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
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 T2CconfigWriter {
|
||||
|
||||
private static FileConfiguration config;
|
||||
|
||||
|
||||
public static void createConfig(File configFile, T2CconfigEnumManager manager, String... header) {
|
||||
if (!configFile.exists()) {
|
||||
configFile.getParentFile().mkdirs();
|
||||
try {
|
||||
configFile.createNewFile();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
config = YamlConfiguration.loadConfiguration(configFile);
|
||||
Map<String, List<String>> comments = new LinkedHashMap<>();
|
||||
|
||||
for(T2CconfigEnumManager.AdditionalEnum item : manager.getAllEnums()){
|
||||
config.addDefault(item.getKey(), item.getValue());
|
||||
comments.put(item.getKey(), item.getComments());
|
||||
}
|
||||
|
||||
// Copy default values if they are missing
|
||||
config.options().copyDefaults(true);
|
||||
saveConfigWithComments(configFile, comments, header);
|
||||
readConfig(config,values);
|
||||
}
|
||||
|
||||
private static void readConfig(FileConfiguration config, T2CconfigItem[] values) {
|
||||
for(T2CconfigItem item : values){
|
||||
item.setValue(config.get(item.getKey()));
|
||||
}
|
||||
}
|
||||
|
||||
private static void saveConfigWithComments(File file, Map<String, List<String>> comments, 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(ConfigurationSection section, Map<String, List<String>> comments, StringBuilder builder, String prefix, int indentLevel) {
|
||||
String indent = " ".repeat(indentLevel);
|
||||
|
||||
|
||||
for (String key : section.getKeys(false)) {
|
||||
String fullKey = prefix.isEmpty() ? key : prefix + "." + key;
|
||||
Object value = section.get(key);
|
||||
|
||||
// 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 ConfigurationSection) {
|
||||
// Correctly add the section
|
||||
builder.append(indent).append(key).append(":\n");
|
||||
addSection((ConfigurationSection) value, comments, builder, fullKey, indentLevel + 1);
|
||||
} else {
|
||||
// Add value with proper indentation
|
||||
builder.append(indent).append(key).append(": ").append(value).append("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user