Implement config save, comment out old config api

This commit is contained in:
PiggyPiglet
2026-01-30 20:53:24 +08:00
parent 6d8f223591
commit 3bf99821ef
4 changed files with 190 additions and 171 deletions

View File

@@ -4,26 +4,36 @@ import at.helpch.placeholderapi.PlaceholderAPIPlugin;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.hypixel.hytale.logger.HytaleLogger;
import com.hypixel.hytale.server.core.plugin.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;
public final class ConfigManager {
private static final Yaml YAML = new Yaml();
private static final Yaml YAML;
private static final Gson GSON = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();
// .registerTypeAdapter()
private static final Pattern LINE_DELIMITER = Pattern.compile("\n");
static {
final DumperOptions options = new DumperOptions();
options.setPrettyFlow(true);
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
YAML = new Yaml(options);
}
private final JavaPlugin main;
private final HytaleLogger logger;
@@ -59,7 +69,14 @@ public final class ConfigManager {
}
public void save() {
try {
final Map<String, Object> map = GSON.fromJson(GSON.toJsonTree(config), new TypeToken<Map<String, Object>>(){}.getType());
final String yaml = YAML.dump(map);
final Path path = Paths.get(main.getDataDirectory().toString() + "/config.yml");
Files.write(path, Arrays.asList(LINE_DELIMITER.split(yaml)), StandardCharsets.UTF_8, StandardOpenOption.TRUNCATE_EXISTING);
} catch (Exception e) {
logger.atSevere().log("Something went wrong when saving config.yml: ", e);
}
}
@NotNull
@@ -84,7 +101,7 @@ public final class ConfigManager {
Files.createFile(file);
} catch (IOException e) {
logger.atSevere().log("Something went wrong when trying to craete ", file);
logger.atSevere().log("Something went wrong when trying to create ", file);
return null;
}

View File

@@ -20,9 +20,7 @@
package at.helpch.placeholderapi.configuration;
import com.google.gson.annotations.JsonAdapter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.Map;

View File

@@ -199,17 +199,17 @@ public abstract class PlaceholderExpansion implements PlaceholderHook {
// === Configuration ===
/**
* Gets the ConfigurationSection of the expansion located in the config.yml of PlaceholderAPI or
* null when not specified.
* <br>You may use the {@link Configurable} interface to define default values set
*
* @return ConfigurationSection that this expansion has.
*/
@NotNull
public final Map<String, Object> getExpansionConfig() {
return (Map<String, Object>) getPlaceholderAPI().configManager().config().expansions().getOrDefault(getIdentifier(), new HashMap<>());
}
// /**
// * Gets the ConfigurationSection of the expansion located in the config.yml of PlaceholderAPI or
// * null when not specified.
// * <br>You may use the {@link Configurable} interface to define default values set
// *
// * @return ConfigurationSection that this expansion has.
// */
// @NotNull
// public final Map<String, Object> getExpansionConfig() {
// return (Map<String, Object>) getPlaceholderAPI().configManager().config().expansions().getOrDefault(getIdentifier(), new HashMap<>());
// }
@Nullable
public final <T> T getExpansionConfig(@NotNull final Class<? extends Configurable<T>> configurableType) {
@@ -229,157 +229,157 @@ public abstract class PlaceholderExpansion implements PlaceholderHook {
// return section == null ? null : section.getConfigurationSection(path);
// }
/**
* Gets the Object relative to the config section set
* by the expansion or the provided Default Object, when the default ConfigurationSection is null
*
* @param path The path to get the Object from. This is relative to the default section
* @param def The default Object to return when the ConfigurationSection returns null
* @return Object from the provided path or the default one provided
*/
@Nullable
@Contract("_, !null -> !null")
public final Object get(@NotNull final String path, final Object def) {
return get(new ArrayDeque<>(Arrays.asList(PATH_DELIMITER.split(path))), def, getExpansionConfig());
}
private Object get(@NotNull final Queue<String> path, final Object def, @NotNull final Map<String, Object> map) {
if (path.size() == 1) {
return map.getOrDefault(path.poll(), def);
}
Object obj = map.get(path.poll());
if (!(obj instanceof Map<?, ?>)) {
return def;
}
return get(path, def, (Map<String, Object>) obj);
}
/**
* Gets the int relative to the config section set
* by the expansion or the provided Default int, when the default ConfigurationSection is null
*
* @param path The path to get the int from. This is relative to the default section
* @param def The default int to return when the ConfigurationSection returns null
* @return int from the provided path or the default one provided
*/
public final int getInt(@NotNull final String path, final int def) {
final Object obj = get(path, def);
if (!(obj instanceof Integer)) {
return def;
}
return (Integer) obj;
}
/**
* Gets the long relative to the config section set
* by the expansion or the provided Default long, when the default ConfigurationSection is null
*
* @param path The path to get the long from. This is relative to the default section
* @param def The default long to return when the ConfigurationSection returns null
* @return long from the provided path or the default one provided
*/
public final long getLong(@NotNull final String path, final long def) {
final Object obj = get(path, def);
if (!(obj instanceof Long) ) {
return def;
}
return (Long) obj;
}
/**
* Gets the double relative to the config section set
* by the expansion or the provided Default double, when the default ConfigurationSection is null
*
* @param path The path to get the double from. This is relative to the default section
* @param def The default double to return when the ConfigurationSection returns null
* @return double from the provided path or the default one provided
*/
public final double getDouble(@NotNull final String path, final double def) {
final Object obj = get(path, def);
if (!(obj instanceof Double) ) {
return def;
}
return (Double) obj;
}
/**
* Gets the String relative to the config section set
* by the expansion or the provided Default String, when the default ConfigurationSection is null
*
* @param path The path to get the String from. This is relative to the default section
* @param def The default String to return when the ConfigurationSection returns null. Can be null
* @return String from the provided path or the default one provided
*/
@Nullable
@Contract("_, !null -> !null")
public final String getString(@NotNull final String path, @Nullable final String def) {
final Object obj = get(path, def);
if (!(obj instanceof String)) {
return def;
}
return (String) obj;
}
/**
* Gets a String List relative to the config section set
* by the expansion or an empty List, when the default ConfigurationSection is null
*
* @param path The path to get the String list from. This is relative to the default section
* @return String list from the provided path or an empty list
*/
@NotNull
public final List<String> getStringList(@NotNull final String path) {
final Object obj = get(path, new ArrayList<>());
if (!(obj instanceof List<?>)) {
return new ArrayList<>();
}
return (List<String>) obj;
}
/**
* Gets the boolean relative to the config section set
* by the expansion or the default boolean, when the default ConfigurationSection is null
*
* @param path The path to get the boolean from. This is relative to the default section
* @param def The default boolean to return when the ConfigurationSection is null
* @return boolean from the provided path or the default one provided
*/
public final boolean getBoolean(@NotNull final String path, final boolean def) {
final Object obj = get(path, def);
if (!(obj instanceof Boolean)) {
return def;
}
return (Boolean) obj;
}
/**
* Whether the config section contains the provided path
* or not. This will return {@code false} when either the default section is null, or doesn't
* contain the provided path
*
* @param path The path to check
* @return true when the default ConfigurationSection is not null and contains the path, false otherwise
*/
public final boolean configurationContains(@NotNull final String path) {
final Object obj = get(path, null);
return obj == null;
}
// /**
// * Gets the Object relative to the config section set
// * by the expansion or the provided Default Object, when the default ConfigurationSection is null
// *
// * @param path The path to get the Object from. This is relative to the default section
// * @param def The default Object to return when the ConfigurationSection returns null
// * @return Object from the provided path or the default one provided
// */
// @Nullable
// @Contract("_, !null -> !null")
// public final Object get(@NotNull final String path, final Object def) {
// return get(new ArrayDeque<>(Arrays.asList(PATH_DELIMITER.split(path))), def, getExpansionConfig());
// }
//
// private Object get(@NotNull final Queue<String> path, final Object def, @NotNull final Map<String, Object> map) {
// if (path.size() == 1) {
// return map.getOrDefault(path.poll(), def);
// }
//
// Object obj = map.get(path.poll());
//
// if (!(obj instanceof Map<?, ?>)) {
// return def;
// }
//
// return get(path, def, (Map<String, Object>) obj);
// }
//
// /**
// * Gets the int relative to the config section set
// * by the expansion or the provided Default int, when the default ConfigurationSection is null
// *
// * @param path The path to get the int from. This is relative to the default section
// * @param def The default int to return when the ConfigurationSection returns null
// * @return int from the provided path or the default one provided
// */
// public final int getInt(@NotNull final String path, final int def) {
// final Object obj = get(path, def);
//
// if (!(obj instanceof Integer)) {
// return def;
// }
//
// return (Integer) obj;
// }
//
// /**
// * Gets the long relative to the config section set
// * by the expansion or the provided Default long, when the default ConfigurationSection is null
// *
// * @param path The path to get the long from. This is relative to the default section
// * @param def The default long to return when the ConfigurationSection returns null
// * @return long from the provided path or the default one provided
// */
// public final long getLong(@NotNull final String path, final long def) {
// final Object obj = get(path, def);
//
// if (!(obj instanceof Long) ) {
// return def;
// }
//
// return (Long) obj;
// }
//
// /**
// * Gets the double relative to the config section set
// * by the expansion or the provided Default double, when the default ConfigurationSection is null
// *
// * @param path The path to get the double from. This is relative to the default section
// * @param def The default double to return when the ConfigurationSection returns null
// * @return double from the provided path or the default one provided
// */
// public final double getDouble(@NotNull final String path, final double def) {
// final Object obj = get(path, def);
//
// if (!(obj instanceof Double) ) {
// return def;
// }
//
// return (Double) obj;
// }
//
// /**
// * Gets the String relative to the config section set
// * by the expansion or the provided Default String, when the default ConfigurationSection is null
// *
// * @param path The path to get the String from. This is relative to the default section
// * @param def The default String to return when the ConfigurationSection returns null. Can be null
// * @return String from the provided path or the default one provided
// */
// @Nullable
// @Contract("_, !null -> !null")
// public final String getString(@NotNull final String path, @Nullable final String def) {
// final Object obj = get(path, def);
//
// if (!(obj instanceof String)) {
// return def;
// }
//
// return (String) obj;
// }
//
// /**
// * Gets a String List relative to the config section set
// * by the expansion or an empty List, when the default ConfigurationSection is null
// *
// * @param path The path to get the String list from. This is relative to the default section
// * @return String list from the provided path or an empty list
// */
// @NotNull
// public final List<String> getStringList(@NotNull final String path) {
// final Object obj = get(path, new ArrayList<>());
//
// if (!(obj instanceof List<?>)) {
// return new ArrayList<>();
// }
//
// return (List<String>) obj;
// }
//
// /**
// * Gets the boolean relative to the config section set
// * by the expansion or the default boolean, when the default ConfigurationSection is null
// *
// * @param path The path to get the boolean from. This is relative to the default section
// * @param def The default boolean to return when the ConfigurationSection is null
// * @return boolean from the provided path or the default one provided
// */
// public final boolean getBoolean(@NotNull final String path, final boolean def) {
// final Object obj = get(path, def);
//
// if (!(obj instanceof Boolean)) {
// return def;
// }
//
// return (Boolean) obj;
// }
//
// /**
// * Whether the config section contains the provided path
// * or not. This will return {@code false} when either the default section is null, or doesn't
// * contain the provided path
// *
// * @param path The path to check
// * @return true when the default ConfigurationSection is not null and contains the path, false otherwise
// */
// public final boolean configurationContains(@NotNull final String path) {
// final Object obj = get(path, null);
//
// return obj == null;
// }
/**
* Logs the provided message with the provided Level in the console.

View File

@@ -231,6 +231,10 @@ public final class LocalExpansionManager /*implements Listener*/ {
if (expansion instanceof Configurable<?> configurable) {
final PlaceholderAPIConfig config = configManager.config();
if (config.expansions() == null) {
config.expansions(new HashMap<>());
}
if (!config.expansions().containsKey(expansion.getIdentifier())) {
config.expansions().put(expansion.getIdentifier(), configurable.provideDefault());
configManager.save();