Skip to content

Commit

Permalink
Add a max length to the pronouns a player can set like said in ashhhl…
Browse files Browse the repository at this point in the history
…eyyy#18

This is disabled by default; To disabled you need to set the max length to 0 or -1
  • Loading branch information
RX0kas committed Oct 2, 2024
1 parent 46e949b commit e892de0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/main/java/dev/ashhhleyyy/playerpronouns/impl/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,28 @@ 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;
private final boolean enablePronounDBSync;
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() {
Expand All @@ -66,6 +69,8 @@ public String getDefaultPlaceholder() {
return defaultPlaceholder;
}

public int getMaxLength() {return maxLength;}

public static Config load() {
Path path = FabricLoader.getInstance().getConfigDir().resolve("player-pronouns.json");
if (!Files.exists(path)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,29 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
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);
} else {
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 {
Expand All @@ -64,6 +80,7 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
).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 {
Expand Down

0 comments on commit e892de0

Please sign in to comment.