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
This commit is contained in:
RX0kas 2024-10-02 15:45:09 +02:00
parent 46e949bffe
commit e892de0f88
2 changed files with 25 additions and 3 deletions

View file

@ -25,7 +25,8 @@ public class Config {
Codec.BOOL.fieldOf("enable_pronoundb_sync").forGetter(config -> config.enablePronounDBSync), Codec.BOOL.fieldOf("enable_pronoundb_sync").forGetter(config -> config.enablePronounDBSync),
Pronoun.CODEC.listOf().fieldOf("single").forGetter(config -> config.single), Pronoun.CODEC.listOf().fieldOf("single").forGetter(config -> config.single),
Pronoun.CODEC.listOf().fieldOf("pairs").forGetter(config -> config.pairs), 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)); ).apply(instance, Config::new));
private final boolean allowCustom; private final boolean allowCustom;
@ -33,17 +34,19 @@ public class Config {
private final List<Pronoun> single; private final List<Pronoun> single;
private final List<Pronoun> pairs; private final List<Pronoun> pairs;
private final String defaultPlaceholder; private final String defaultPlaceholder;
private final int maxLength;
private Config(boolean allowCustom, boolean enablePronounDBSync, List<Pronoun> single, List<Pronoun> pairs, String defaultPlaceholder) { private Config(boolean allowCustom, boolean enablePronounDBSync, List<Pronoun> single, List<Pronoun> pairs, String defaultPlaceholder,int maxLength) {
this.allowCustom = allowCustom; this.allowCustom = allowCustom;
this.enablePronounDBSync = enablePronounDBSync; this.enablePronounDBSync = enablePronounDBSync;
this.single = single; this.single = single;
this.pairs = pairs; this.pairs = pairs;
this.defaultPlaceholder = defaultPlaceholder; this.defaultPlaceholder = defaultPlaceholder;
this.maxLength = maxLength;
} }
private Config() { private Config() {
this(true, true, Collections.emptyList(), Collections.emptyList(), "Unknown"); this(true, true, Collections.emptyList(), Collections.emptyList(), "Unknown",-1);
} }
public boolean allowCustom() { public boolean allowCustom() {
@ -66,6 +69,8 @@ public class Config {
return defaultPlaceholder; return defaultPlaceholder;
} }
public int getMaxLength() {return maxLength;}
public static Config load() { public static Config load() {
Path path = FabricLoader.getInstance().getConfigDir().resolve("player-pronouns.json"); Path path = FabricLoader.getInstance().getConfigDir().resolve("player-pronouns.json");
if (!Files.exists(path)) { if (!Files.exists(path)) {

View file

@ -36,6 +36,21 @@ public class PronounsCommand {
return 0; 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; Pronouns pronouns;
if (pronounTexts.containsKey(pronounsString)) { if (pronounTexts.containsKey(pronounsString)) {
pronouns = new Pronouns(pronounsString, pronounTexts.get(pronounsString), false); pronouns = new Pronouns(pronounsString, pronounTexts.get(pronounsString), false);
@ -43,6 +58,7 @@ public class PronounsCommand {
pronouns = new Pronouns(pronounsString, Text.literal(pronounsString), false); pronouns = new Pronouns(pronounsString, Text.literal(pronounsString), false);
} }
assert player != null;
if (!PronounsApi.getSetter().setPronouns(player, pronouns)) { if (!PronounsApi.getSetter().setPronouns(player, pronouns)) {
ctx.getSource().sendError(Text.literal("Failed to update pronouns, sorry")); ctx.getSource().sendError(Text.literal("Failed to update pronouns, sorry"));
} else { } else {
@ -64,6 +80,7 @@ public class PronounsCommand {
).then(literal("unset") ).then(literal("unset")
.executes(ctx -> { .executes(ctx -> {
ServerPlayerEntity player = ctx.getSource().getPlayer(); ServerPlayerEntity player = ctx.getSource().getPlayer();
assert player != null;
if (!PronounsApi.getSetter().setPronouns(player, null)) { if (!PronounsApi.getSetter().setPronouns(player, null)) {
ctx.getSource().sendError(Text.literal("Failed to update pronouns, sorry")); ctx.getSource().sendError(Text.literal("Failed to update pronouns, sorry"));
} else { } else {