From e38c28ec604dddf89d0821b164c89a62a1f0ed7d Mon Sep 17 00:00:00 2001 From: Ash B Date: Fri, 13 Aug 2021 15:12:06 +0100 Subject: [PATCH] Config reload support --- build.gradle.kts | 9 +++++++++ libs.versions.toml | 4 ++++ .../playerpronouns/PlayerPronouns.java | 5 +++++ .../playerpronouns/command/PronounsCommand.java | 8 ++++++++ .../playerpronouns/data/PronounList.java | 16 +++++++++++++--- 5 files changed, 39 insertions(+), 3 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index b3fd50a..beac1be 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -13,6 +13,11 @@ repositories { name = "NucleoidMC" url = uri("https://maven.nucleoid.xyz/") } + // permissions api + maven { + name = "Sonatype OSS" + url = uri("https://oss.sonatype.org/content/repositories/snapshots") + } } dependencies { @@ -31,6 +36,10 @@ dependencies { // more-codecs modImplementation(libs.more.codecs) include(libs.more.codecs) + + // fabric-api-permissions + modImplementation(libs.fabric.permissions) + include(libs.fabric.permissions) } tasks.processResources { diff --git a/libs.versions.toml b/libs.versions.toml index 4fa0253..7d01922 100644 --- a/libs.versions.toml +++ b/libs.versions.toml @@ -8,6 +8,8 @@ fabric-api = "0.37.1+1.17" placeholder-api = "1.0.1+1.17" more-codecs = "0.2.0" +fabric-permissions = "0.1-SNAPSHOT" + [libraries] minecraft = { module = "com.mojang:minecraft", version.ref = "minecraft" } yarn = { module = "net.fabricmc:yarn", version.ref = "yarn" } @@ -17,3 +19,5 @@ fabric-api = { module = "net.fabricmc.fabric-api:fabric-api", version.ref = "fab placeholder-api = { module = "eu.pb4:placeholder-api", version.ref = "placeholder-api" } more-codecs = { module = "xyz.nucleoid:more-codecs", version.ref = "more-codecs" } + +fabric-permissions = { module = "me.lucko:fabric-permissions-api", version.ref = "fabric-permissions" } diff --git a/src/main/java/io/github/ashisbored/playerpronouns/PlayerPronouns.java b/src/main/java/io/github/ashisbored/playerpronouns/PlayerPronouns.java index 6bbbed3..983e838 100644 --- a/src/main/java/io/github/ashisbored/playerpronouns/PlayerPronouns.java +++ b/src/main/java/io/github/ashisbored/playerpronouns/PlayerPronouns.java @@ -93,6 +93,11 @@ public class PlayerPronouns implements ModInitializer { }); } + public static void reloadConfig() { + config = Config.load(); + PronounList.load(config); + } + private static void savePronounDatabase(MinecraftServer server) throws IOException { Path playerData = server.getSavePath(WorldSavePath.PLAYERDATA); if (!Files.exists(playerData)) { diff --git a/src/main/java/io/github/ashisbored/playerpronouns/command/PronounsCommand.java b/src/main/java/io/github/ashisbored/playerpronouns/command/PronounsCommand.java index ecb8828..adca34a 100644 --- a/src/main/java/io/github/ashisbored/playerpronouns/command/PronounsCommand.java +++ b/src/main/java/io/github/ashisbored/playerpronouns/command/PronounsCommand.java @@ -5,6 +5,7 @@ import com.mojang.brigadier.CommandDispatcher; import io.github.ashisbored.playerpronouns.PlayerPronouns; import io.github.ashisbored.playerpronouns.data.PronounList; import io.github.ashisbored.playerpronouns.data.Pronouns; +import me.lucko.fabric.api.permissions.v0.Permissions; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.LiteralText; @@ -46,6 +47,13 @@ public class PronounsCommand { .formatted(Formatting.GREEN), false); } + return Command.SINGLE_SUCCESS; + }) + ).then(literal("reload-config") + .requires(ctx -> Permissions.check(ctx, "playerpronouns.reload_config", 4)) + .executes(ctx -> { + PlayerPronouns.reloadConfig(); + ctx.getSource().sendFeedback(new LiteralText("Reloaded the config!").formatted(Formatting.GREEN), true); return Command.SINGLE_SUCCESS; }) ) diff --git a/src/main/java/io/github/ashisbored/playerpronouns/data/PronounList.java b/src/main/java/io/github/ashisbored/playerpronouns/data/PronounList.java index 82d0679..ae3b781 100644 --- a/src/main/java/io/github/ashisbored/playerpronouns/data/PronounList.java +++ b/src/main/java/io/github/ashisbored/playerpronouns/data/PronounList.java @@ -28,8 +28,8 @@ public class PronounList { public PronounList(List defaultSingle, List defaultPairs, List customSingle, List customPairs) { this.defaultSingle = defaultSingle; this.defaultPairs = defaultPairs; - this.customSingle = customSingle; - this.customPairs = customPairs; + this.customSingle = new ArrayList<>(customSingle); + this.customPairs = new ArrayList<>(customPairs); this.calculatedPronounStrings = this.computePossibleCombinations(); } @@ -65,7 +65,8 @@ public class PronounList { public static void load(Config config) { if (INSTANCE != null) { - throw new IllegalStateException("PronounList has already been loaded!"); + INSTANCE.reload(config); + return; } Pair, List> defaults = loadDefaults(); @@ -77,6 +78,15 @@ public class PronounList { ); } + private void reload(Config config) { + this.customSingle.clear(); + this.customPairs.clear(); + this.customSingle.addAll(config.getSingle()); + this.customPairs.addAll(config.getPairs()); + this.calculatedPronounStrings.clear(); + this.calculatedPronounStrings.putAll(this.computePossibleCombinations()); + } + public static PronounList get() { if (INSTANCE == null) { throw new IllegalStateException("PronounList has not been loaded!");