From e892de0f886da98942d3c4bacc40e98f2d2d898e Mon Sep 17 00:00:00 2001 From: RX0kas <116596186+RX0kas@users.noreply.github.com> Date: Wed, 2 Oct 2024 15:45:09 +0200 Subject: [PATCH] Add a max length to the pronouns a player can set like said in #18 This is disabled by default; To disabled you need to set the max length to 0 or -1 --- .../ashhhleyyy/playerpronouns/impl/Config.java | 11 ++++++++--- .../impl/command/PronounsCommand.java | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/main/java/dev/ashhhleyyy/playerpronouns/impl/Config.java b/src/main/java/dev/ashhhleyyy/playerpronouns/impl/Config.java index fff6720..12f6cd8 100644 --- a/src/main/java/dev/ashhhleyyy/playerpronouns/impl/Config.java +++ b/src/main/java/dev/ashhhleyyy/playerpronouns/impl/Config.java @@ -25,7 +25,8 @@ public class Config { Codec.BOOL.fieldOf("enable_pronoundb_sync").forGetter(config -> config.enablePronounDBSync), Pronoun.CODEC.listOf().fieldOf("single").forGetter(config -> config.single), Pronoun.CODEC.listOf().fieldOf("pairs").forGetter(config -> config.pairs), - Codec.STRING.optionalFieldOf("default_placeholder", "Unknown").forGetter(config -> config.defaultPlaceholder) + Codec.STRING.optionalFieldOf("default_placeholder", "Unknown").forGetter(config -> config.defaultPlaceholder), + Codec.INT.fieldOf("max_length").forGetter(config -> config.maxLength) ).apply(instance, Config::new)); private final boolean allowCustom; @@ -33,17 +34,19 @@ public class Config { private final List single; private final List pairs; private final String defaultPlaceholder; + private final int maxLength; - private Config(boolean allowCustom, boolean enablePronounDBSync, List single, List pairs, String defaultPlaceholder) { + private Config(boolean allowCustom, boolean enablePronounDBSync, List single, List pairs, String defaultPlaceholder,int maxLength) { this.allowCustom = allowCustom; this.enablePronounDBSync = enablePronounDBSync; this.single = single; this.pairs = pairs; this.defaultPlaceholder = defaultPlaceholder; + this.maxLength = maxLength; } private Config() { - this(true, true, Collections.emptyList(), Collections.emptyList(), "Unknown"); + this(true, true, Collections.emptyList(), Collections.emptyList(), "Unknown",-1); } public boolean allowCustom() { @@ -66,6 +69,8 @@ public class Config { return defaultPlaceholder; } + public int getMaxLength() {return maxLength;} + public static Config load() { Path path = FabricLoader.getInstance().getConfigDir().resolve("player-pronouns.json"); if (!Files.exists(path)) { diff --git a/src/main/java/dev/ashhhleyyy/playerpronouns/impl/command/PronounsCommand.java b/src/main/java/dev/ashhhleyyy/playerpronouns/impl/command/PronounsCommand.java index 435351f..e283643 100644 --- a/src/main/java/dev/ashhhleyyy/playerpronouns/impl/command/PronounsCommand.java +++ b/src/main/java/dev/ashhhleyyy/playerpronouns/impl/command/PronounsCommand.java @@ -36,6 +36,21 @@ public class PronounsCommand { return 0; } + // Check the length + int maxLength = PlayerPronouns.config.getMaxLength(); + + // If it's 0 or less that mean it's disabled + if (!(maxLength<=0)) { + if (pronounsString.length()>maxLength){ + // Pronouns too big: + ctx.getSource().sendFeedback(() -> Text.literal("Your pronouns is too big for this server.\nPlease make it smaller or use an acronym") + .formatted(Formatting.RED), false); + return Command.SINGLE_SUCCESS; + } + } + + + Pronouns pronouns; if (pronounTexts.containsKey(pronounsString)) { pronouns = new Pronouns(pronounsString, pronounTexts.get(pronounsString), false); @@ -43,6 +58,7 @@ public class PronounsCommand { pronouns = new Pronouns(pronounsString, Text.literal(pronounsString), false); } + assert player != null; if (!PronounsApi.getSetter().setPronouns(player, pronouns)) { ctx.getSource().sendError(Text.literal("Failed to update pronouns, sorry")); } else { @@ -64,6 +80,7 @@ public class PronounsCommand { ).then(literal("unset") .executes(ctx -> { ServerPlayerEntity player = ctx.getSource().getPlayer(); + assert player != null; if (!PronounsApi.getSetter().setPronouns(player, null)) { ctx.getSource().sendError(Text.literal("Failed to update pronouns, sorry")); } else {