add Component serializer
This commit is contained in:
parent
72134f8d74
commit
63c72fe04b
4 changed files with 41 additions and 8 deletions
|
@ -36,6 +36,7 @@ dependencies {
|
|||
implementation "org.bstats:bstats-bukkit:3.0.2"
|
||||
implementation "com.github.robertlit:SpigotResourcesAPI:2.0"
|
||||
implementation "net.kyori:adventure-platform-bukkit:4.3.0"
|
||||
implementation "net.kyori:adventure-text-minimessage:4.13.1"
|
||||
implementation "com.github.retrooper.packetevents:spigot:2.0.0-SNAPSHOT"
|
||||
implementation "space.arim.dazzleconf:dazzleconf-ext-snakeyaml:1.2.1"
|
||||
}
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
package lol.pyr.znpcsplus.config;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import space.arim.dazzleconf.error.BadValueException;
|
||||
import space.arim.dazzleconf.serialiser.Decomposer;
|
||||
import space.arim.dazzleconf.serialiser.FlexibleType;
|
||||
import space.arim.dazzleconf.serialiser.ValueSerialiser;
|
||||
|
||||
public class ComponentSerializer implements ValueSerialiser<Component> {
|
||||
@Override
|
||||
public Class<Component> getTargetClass() {
|
||||
return Component.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Component deserialise(FlexibleType flexibleType) throws BadValueException {
|
||||
return MiniMessage.miniMessage().deserialize(flexibleType.getString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object serialise(Component value, Decomposer decomposer) {
|
||||
return MiniMessage.miniMessage().serialize(value);
|
||||
}
|
||||
}
|
|
@ -9,6 +9,7 @@ import space.arim.dazzleconf.ext.snakeyaml.CommentMode;
|
|||
import space.arim.dazzleconf.ext.snakeyaml.SnakeYamlConfigurationFactory;
|
||||
import space.arim.dazzleconf.ext.snakeyaml.SnakeYamlOptions;
|
||||
import space.arim.dazzleconf.helper.ConfigurationHelper;
|
||||
import space.arim.dazzleconf.serialiser.ValueSerialiser;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
@ -20,15 +21,17 @@ public class Configs {
|
|||
private volatile static MessageConfig messages;
|
||||
private static ConfigurationHelper<MessageConfig> messagesHelper;
|
||||
|
||||
private static <T> ConfigurationHelper<T> createHelper(Class<T> configClass, File file) {
|
||||
private static <T> ConfigurationHelper<T> createHelper(Class<T> configClass, File file, ValueSerialiser<?>... serialisers) {
|
||||
SnakeYamlOptions yamlOptions = new SnakeYamlOptions.Builder().commentMode(CommentMode.fullComments()).build();
|
||||
ConfigurationFactory<T> configFactory = SnakeYamlConfigurationFactory.create(configClass, ConfigurationOptions.defaults(), yamlOptions);
|
||||
ConfigurationOptions.Builder optionBuilder = new ConfigurationOptions.Builder();
|
||||
if (serialisers != null && serialisers.length > 0) optionBuilder.addSerialisers(serialisers);
|
||||
ConfigurationFactory<T> configFactory = SnakeYamlConfigurationFactory.create(configClass, optionBuilder.build(), yamlOptions);
|
||||
return new ConfigurationHelper<>(file.getParentFile().toPath(), file.getName(), configFactory);
|
||||
}
|
||||
|
||||
public static void init(File pluginFolder) {
|
||||
configHelper = createHelper(MainConfig.class, new File(pluginFolder, "config.yaml"));
|
||||
messagesHelper = createHelper(MessageConfig.class, new File(pluginFolder, "messages.yaml"));
|
||||
messagesHelper = createHelper(MessageConfig.class, new File(pluginFolder, "messages.yaml"), new ComponentSerializer());
|
||||
load();
|
||||
}
|
||||
|
||||
|
|
|
@ -1,24 +1,28 @@
|
|||
package lol.pyr.znpcsplus.config;
|
||||
|
||||
import space.arim.dazzleconf.annote.ConfComments;
|
||||
import space.arim.dazzleconf.annote.ConfKey;
|
||||
|
||||
import static space.arim.dazzleconf.annote.ConfDefault.*;
|
||||
|
||||
// TODO: Add comments to the values using @ConfComments()
|
||||
public interface MainConfig {
|
||||
@ConfKey("view-distance")
|
||||
@ConfComments("How far away do you need to be from any NPC for it to disappear, measured in blocks")
|
||||
@DefaultInteger(32)
|
||||
int viewDistance();
|
||||
|
||||
@ConfKey("line-spacing")
|
||||
@ConfComments("The height between hologram lines, measured in blocks")
|
||||
@DefaultDouble(0.3D)
|
||||
double lineSpacing();
|
||||
|
||||
@ConfKey("debug-enabled")
|
||||
@DefaultBoolean(false)
|
||||
boolean debugEnabled();
|
||||
|
||||
@ConfKey("check-for-updates")
|
||||
@ConfComments("Should the plugin check for available updates and notify admins about them?")
|
||||
@DefaultBoolean(true)
|
||||
boolean checkForUpdates();
|
||||
|
||||
@ConfKey("debug-enabled")
|
||||
@ConfComments({"Should debug mode be enabled?", "This is used in development to test various things, you probably don't want to enable this"})
|
||||
@DefaultBoolean(false)
|
||||
boolean debugEnabled();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue