From e63b7309cfb003cd29dd4c3b1a400ac094cd2127 Mon Sep 17 00:00:00 2001 From: lijinhong11 Date: Sat, 8 Jun 2024 18:29:00 +0800 Subject: [PATCH] some fixes --- .../bbox/moreflags/MoreFlagsAddon.java | 4 +-- .../bbox/moreflags/config/FlagSet.java | 12 +++---- .../moreflags/config/FlagSetSerializer.java | 33 +++++++++++++++++++ .../bbox/moreflags/config/Settings.java | 12 ++++--- src/main/resources/config.yml | 5 +-- 5 files changed, 47 insertions(+), 19 deletions(-) create mode 100644 src/main/java/me/mmmjjkx/bbox/moreflags/config/FlagSetSerializer.java diff --git a/src/main/java/me/mmmjjkx/bbox/moreflags/MoreFlagsAddon.java b/src/main/java/me/mmmjjkx/bbox/moreflags/MoreFlagsAddon.java index 7ebb6b1..c7bb75b 100644 --- a/src/main/java/me/mmmjjkx/bbox/moreflags/MoreFlagsAddon.java +++ b/src/main/java/me/mmmjjkx/bbox/moreflags/MoreFlagsAddon.java @@ -37,6 +37,7 @@ private void registerFlags(EntityListener entityListener) { @Override public void onReload() { super.onReload(); + saveConfig(); settings = new Config<>(this, Settings.class).loadConfigObject(); @@ -55,12 +56,9 @@ private void registerFlagSet(String id, Material icon, FlagSet flagSet, Listener .mode(Flag.Mode.EXPERT) .listener(listener) .type(Flag.Type.SETTING) - .defaultSetting(flagSet.getDefaultValue()) .cooldown(flagSet.getChangeCooldown()) .build(); registerFlag(flag); - - flag.setDefaultSetting(flagSet.getDefaultValue()); } } } diff --git a/src/main/java/me/mmmjjkx/bbox/moreflags/config/FlagSet.java b/src/main/java/me/mmmjjkx/bbox/moreflags/config/FlagSet.java index 54b0d4b..aeec4e1 100644 --- a/src/main/java/me/mmmjjkx/bbox/moreflags/config/FlagSet.java +++ b/src/main/java/me/mmmjjkx/bbox/moreflags/config/FlagSet.java @@ -4,18 +4,14 @@ import lombok.Setter; import world.bentobox.bentobox.api.configuration.ConfigEntry; +@Getter @Setter public class FlagSet { - @Getter + public FlagSet() { + } + @ConfigEntry(path = "enabled") private boolean enabled = true; - @Getter @ConfigEntry(path = "change-cooldown") private int changeCooldown = 0; - @ConfigEntry(path = "default-value") - private boolean defaultValue = false; - - public boolean getDefaultValue() { - return defaultValue; - } } diff --git a/src/main/java/me/mmmjjkx/bbox/moreflags/config/FlagSetSerializer.java b/src/main/java/me/mmmjjkx/bbox/moreflags/config/FlagSetSerializer.java new file mode 100644 index 0000000..8f875c0 --- /dev/null +++ b/src/main/java/me/mmmjjkx/bbox/moreflags/config/FlagSetSerializer.java @@ -0,0 +1,33 @@ +package me.mmmjjkx.bbox.moreflags.config; + +import org.bukkit.configuration.MemorySection; +import world.bentobox.bentobox.database.objects.adapters.AdapterInterface; + +import java.util.Map; + +public class FlagSetSerializer implements AdapterInterface> { + + public FlagSetSerializer() { + } + + @Override + public FlagSet deserialize(Object o) { + FlagSet flagSet = new FlagSet(); + if (o instanceof MemorySection ms) { + flagSet.setEnabled(ms.getBoolean("enabled")); + flagSet.setChangeCooldown(ms.getInt("change-cooldown")); + } + return flagSet; + } + + @Override + public Map serialize(Object o) { + if (o instanceof FlagSet fs) { + boolean enabled = fs.isEnabled(); + int changeCooldown = fs.getChangeCooldown(); + return Map.of("enabled", enabled, "change-cooldown", changeCooldown); + } else { + return null; + } + } +} diff --git a/src/main/java/me/mmmjjkx/bbox/moreflags/config/Settings.java b/src/main/java/me/mmmjjkx/bbox/moreflags/config/Settings.java index fc44512..06cb5fb 100644 --- a/src/main/java/me/mmmjjkx/bbox/moreflags/config/Settings.java +++ b/src/main/java/me/mmmjjkx/bbox/moreflags/config/Settings.java @@ -3,20 +3,24 @@ import lombok.Getter; import lombok.Setter; import world.bentobox.bentobox.api.configuration.*; +import world.bentobox.bentobox.database.objects.adapters.Adapter; -@StoreAt(filename = "config.yml") +@StoreAt(filename = "config.yml", path = "flags") @Getter @Setter public class Settings implements ConfigObject { - @ConfigEntry(path = "flags.creeper_explosion") + @ConfigEntry(path = "creeper_explosion") @ConfigComment("It can control creeper explosions.") + @Adapter(FlagSetSerializer.class) private FlagSet creeperExplosions = new FlagSet(); - @ConfigEntry(path = "flags.wither_explosion") + @ConfigEntry(path = ".wither_explosion") @ConfigComment("It can control wither explosions.") + @Adapter(FlagSetSerializer.class) private FlagSet witherExplosions = new FlagSet(); - @ConfigEntry(path = "flags.phantom_spawning") + @ConfigEntry(path = "phantom_spawning") @ConfigComment("It can control phantom spawning.") + @Adapter(FlagSetSerializer.class) private FlagSet phantomSpawning = new FlagSet(); } \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 89f66f1..6b1bd6e 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -2,12 +2,9 @@ flags: creeper_explosion: enabled: true change-cooldown: 0 - default-value: true wither_explosion: enabled: true change-cooldown: 0 - default-value: true phantom_spawning: enabled: true - change-cooldown: 0 - default-value: true \ No newline at end of file + change-cooldown: 0 \ No newline at end of file