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),
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<Pronoun> single;
private final List<Pronoun> pairs;
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.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)) {

View file

@ -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 {