Changes to the creation of config files

- Comments in multiple languages
- Language files with the new ConfigWriter
This commit is contained in:
2024-07-03 23:48:51 +02:00
parent 5a49cc625a
commit 44c35bd185
24 changed files with 468 additions and 427 deletions

View File

@@ -4,7 +4,7 @@ import com.bencodez.votingplugin.VotingPluginMain;
import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend;
import net.t2code.t2codelib.SPIGOT.api.plugins.T2CpluginCheck;
import net.t2code.t2codelib.SPIGOT.system.T2CodeLibMain;
import net.t2code.t2codelib.SPIGOT.system.config.languages.SelectLibMsg;
import net.t2code.t2codelib.SPIGOT.system.config.languages.old.SelectLibMsg;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;

View File

@@ -2,17 +2,13 @@ package net.t2code.t2codelib.SPIGOT.api.messages;
import net.t2code.t2codelib.SPIGOT.api.update.T2CupdateAPI;
import net.t2code.t2codelib.SPIGOT.system.T2CodeLibMain;
import net.t2code.t2codelib.T2CconfigItem;
import net.t2code.t2codelib.T2CupdateWebData;
import net.t2code.t2codelib.Util;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginLogger;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.logging.Level;
public class T2Ctemplate {
public static Long onLoadHeader(String prefix, List<String> autor, String version, String spigot, String discord) {

View File

@@ -3,7 +3,7 @@ package net.t2code.t2codelib.SPIGOT.api.yaml;
import net.t2code.t2codelib.SPIGOT.api.messages.T2Creplace;
import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend;
import net.t2code.t2codelib.SPIGOT.api.minecraftVersion.T2CmcVersion;
import net.t2code.t2codelib.SPIGOT.system.config.languages.SelectLibMsg;
import net.t2code.t2codelib.SPIGOT.system.config.languages.old.SelectLibMsg;
import org.bukkit.Sound;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;

View File

@@ -2,14 +2,13 @@
package net.t2code.t2codelib.SPIGOT.api.yaml;
import net.t2code.t2codelib.T2CLanguageEnum;
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;
@@ -18,7 +17,6 @@ public class T2CconfigWriter {
private static FileConfiguration config;
public static void createConfig(File configFile, T2CconfigItem[] values, String... header) {
boolean exist = configFile.exists();
if (!exist) {
@@ -34,17 +32,27 @@ public class T2CconfigWriter {
config = YamlConfiguration.loadConfiguration(configFile);
Map<String, List<String>> comments = new LinkedHashMap<>();
T2CLanguageEnum lang = T2CLanguageEnum.english;
try {
lang = T2CLanguageEnum.valueOf(config.getString(values[0].getLanguagePath().replace(".yml", "")));
} catch (Exception ignore) {
}
for (T2CconfigItem item : values) {
if (item.getForceSet() || !exist) {
config.addDefault(item.getPath(), item.getValue());
comments.put(item.getPath(), item.getComments());
List<String> commandList = item.getComments().get(lang);
if (commandList != null) {
comments.put(item.getPath(), commandList);
}
}
}
// Copy default values if they are missing
config.options().copyDefaults(true);
saveConfigWithComments(configFile, comments, header);
T2CymlWriter.saveConfigWithComments(configFile, config, comments, header);
readConfig(config, values);
}
@@ -53,61 +61,4 @@ public class T2CconfigWriter {
value.setValue(config.get(value.getPath()));
}
}
private static void saveConfigWithComments(File file, Map<String, List<String>> comments, String... headers) {
try {
StringBuilder configContent = new StringBuilder();
for (String h : headers) {
configContent.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");
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

@@ -0,0 +1,92 @@
// This class was created by JaTiTV.
package net.t2code.t2codelib.SPIGOT.api.yaml;
import net.t2code.t2codelib.SPIGOT.api.messages.T2Ctemplate;
import net.t2code.t2codelib.SPIGOT.system.T2CodeLibMain;
import net.t2code.t2codelib.T2CLanguageEnum;
import net.t2code.t2codelib.T2CconfigItemLanguages;
import net.t2code.t2codelib.Util;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.io.IOException;
import java.util.*;
public class T2CconfigWriterLanguage {
private static FileConfiguration config;
public static void createConfig(File path, T2CconfigItemLanguages[] values, String loadConfig, String... header) {
File f = new File(path + "/languages/");
f.mkdirs();
File[] fileArray = f.listFiles();
if(fileArray.length == 0){
List<File> zw = new ArrayList<>();
for(T2CLanguageEnum lan : T2CLanguageEnum.values()){
if(values[0].getLanguage().containsKey(lan)){
zw.add(new File(path + "/languages/" + lan.name() + ".yml"));
}
}
fileArray = zw.toArray(new File[0]);
}
for (File langFile : fileArray) {
if (langFile.getName().endsWith(".yml")) {
T2CLanguageEnum lang = T2CLanguageEnum.english;
try {
lang = T2CLanguageEnum.valueOf(langFile.getName().replace(".yml", ""));
} catch (Exception ignore) {
}
if (!langFile.exists()) {
langFile.getParentFile().mkdirs();
try {
langFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
return;
}
}
Map<String, List<String>> comments = new LinkedHashMap<>();
// Copy default values if they are missing
config = YamlConfiguration.loadConfiguration(langFile);
config.options().copyDefaults(true);
for (T2CconfigItemLanguages item : values) {
config.addDefault(item.getPath(), item.getLanguage().get(lang));
List<String> commandList = item.getComments().get(lang);
if (commandList != null) {
comments.put(item.getPath(), commandList);
}
}
T2CymlWriter.saveConfigWithComments(langFile, config, comments, header);
}
}
readConfig(values, loadConfig);
}
private static void readConfig(T2CconfigItemLanguages[] values, String loadConfig) {
String selectMSG;
File msg;
msg = new File(T2CodeLibMain.getPath(), "languages/" + loadConfig + ".yml");
if (!msg.isFile()) {
T2Ctemplate.onStartMsg(Util.getPrefix(), "");
T2Ctemplate.onStartMsg(Util.getPrefix(), "§4!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
T2Ctemplate.onStartMsg(Util.getPrefix(), "§4The selected §c" + loadConfig + " §4language file was not found.");
T2Ctemplate.onStartMsg(Util.getPrefix(), "§6The default language §e" + T2CLanguageEnum.english.name() + " §6is used!");
T2Ctemplate.onStartMsg(Util.getPrefix(), "§4!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
T2Ctemplate.onStartMsg(Util.getPrefix(), "");
msg = new File(T2CodeLibMain.getPath(), "languages/" + T2CLanguageEnum.english.name() + ".yml");
selectMSG = T2CLanguageEnum.english.name();
} else selectMSG = loadConfig;
YamlConfiguration yml = YamlConfiguration.loadConfiguration(msg);
for (T2CconfigItemLanguages value : values) {
value.setValue(yml.get(value.getPath()));
}
T2Ctemplate.onStartMsg(Util.getPrefix(), "§2Language successfully selected to: §6" + selectMSG);
}
}

View File

@@ -0,0 +1,70 @@
// This class was created by JaTiTV.
package net.t2code.t2codelib.SPIGOT.api.yaml;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;
public class T2CymlWriter {
protected static void saveConfigWithComments(File file, FileConfiguration config, Map<String, List<String>> comments, String... headers) {
try {
StringBuilder configContent = new StringBuilder();
for (String h : headers) {
configContent.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();
}
}
protected 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");
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");
}
}
}
}
}