Config reload support

This commit is contained in:
Ash B 2021-08-13 15:12:06 +01:00
parent 915fd82f15
commit e38c28ec60
No known key found for this signature in database
GPG key ID: 1AE71DC3E127235F
5 changed files with 39 additions and 3 deletions

View file

@ -13,6 +13,11 @@ repositories {
name = "NucleoidMC" name = "NucleoidMC"
url = uri("https://maven.nucleoid.xyz/") url = uri("https://maven.nucleoid.xyz/")
} }
// permissions api
maven {
name = "Sonatype OSS"
url = uri("https://oss.sonatype.org/content/repositories/snapshots")
}
} }
dependencies { dependencies {
@ -31,6 +36,10 @@ dependencies {
// more-codecs // more-codecs
modImplementation(libs.more.codecs) modImplementation(libs.more.codecs)
include(libs.more.codecs) include(libs.more.codecs)
// fabric-api-permissions
modImplementation(libs.fabric.permissions)
include(libs.fabric.permissions)
} }
tasks.processResources { tasks.processResources {

View file

@ -8,6 +8,8 @@ fabric-api = "0.37.1+1.17"
placeholder-api = "1.0.1+1.17" placeholder-api = "1.0.1+1.17"
more-codecs = "0.2.0" more-codecs = "0.2.0"
fabric-permissions = "0.1-SNAPSHOT"
[libraries] [libraries]
minecraft = { module = "com.mojang:minecraft", version.ref = "minecraft" } minecraft = { module = "com.mojang:minecraft", version.ref = "minecraft" }
yarn = { module = "net.fabricmc:yarn", version.ref = "yarn" } 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" } placeholder-api = { module = "eu.pb4:placeholder-api", version.ref = "placeholder-api" }
more-codecs = { module = "xyz.nucleoid:more-codecs", version.ref = "more-codecs" } more-codecs = { module = "xyz.nucleoid:more-codecs", version.ref = "more-codecs" }
fabric-permissions = { module = "me.lucko:fabric-permissions-api", version.ref = "fabric-permissions" }

View file

@ -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 { private static void savePronounDatabase(MinecraftServer server) throws IOException {
Path playerData = server.getSavePath(WorldSavePath.PLAYERDATA); Path playerData = server.getSavePath(WorldSavePath.PLAYERDATA);
if (!Files.exists(playerData)) { if (!Files.exists(playerData)) {

View file

@ -5,6 +5,7 @@ import com.mojang.brigadier.CommandDispatcher;
import io.github.ashisbored.playerpronouns.PlayerPronouns; import io.github.ashisbored.playerpronouns.PlayerPronouns;
import io.github.ashisbored.playerpronouns.data.PronounList; import io.github.ashisbored.playerpronouns.data.PronounList;
import io.github.ashisbored.playerpronouns.data.Pronouns; 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.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText; import net.minecraft.text.LiteralText;
@ -46,6 +47,13 @@ public class PronounsCommand {
.formatted(Formatting.GREEN), false); .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; return Command.SINGLE_SUCCESS;
}) })
) )

View file

@ -28,8 +28,8 @@ public class PronounList {
public PronounList(List<Pronoun> defaultSingle, List<Pronoun> defaultPairs, List<Pronoun> customSingle, List<Pronoun> customPairs) { public PronounList(List<Pronoun> defaultSingle, List<Pronoun> defaultPairs, List<Pronoun> customSingle, List<Pronoun> customPairs) {
this.defaultSingle = defaultSingle; this.defaultSingle = defaultSingle;
this.defaultPairs = defaultPairs; this.defaultPairs = defaultPairs;
this.customSingle = customSingle; this.customSingle = new ArrayList<>(customSingle);
this.customPairs = customPairs; this.customPairs = new ArrayList<>(customPairs);
this.calculatedPronounStrings = this.computePossibleCombinations(); this.calculatedPronounStrings = this.computePossibleCombinations();
} }
@ -65,7 +65,8 @@ public class PronounList {
public static void load(Config config) { public static void load(Config config) {
if (INSTANCE != null) { if (INSTANCE != null) {
throw new IllegalStateException("PronounList has already been loaded!"); INSTANCE.reload(config);
return;
} }
Pair<List<Pronoun>, List<Pronoun>> defaults = loadDefaults(); Pair<List<Pronoun>, List<Pronoun>> 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() { public static PronounList get() {
if (INSTANCE == null) { if (INSTANCE == null) {
throw new IllegalStateException("PronounList has not been loaded!"); throw new IllegalStateException("PronounList has not been loaded!");