Make the default placeholder configurable

This commit is contained in:
Ash B 2021-08-13 15:38:39 +01:00
parent 6f1b3e95ca
commit 8b15a67aec
No known key found for this signature in database
GPG key ID: 1AE71DC3E127235F
3 changed files with 25 additions and 7 deletions

View file

@ -12,10 +12,19 @@ It will suggest pronouns that are configured by the server admins, along with th
### Configuration ### Configuration
The mod should work out of the box without any configuration, however if you want player's pronouns to be visible, you probably want to use the placeholder somewhere. The mod should work out of the box without any configuration, however if you want player's pronouns to be visible, you probably want to use the placeholder somewhere.
#### Reloading the config
You can reload the config file using the command `/pronouns reload-config`. This requires either OP level 4 or the permission `playerpronouns.reload_config`.
#### Adding custom pronouns (eg. neo-pronouns) #### Adding custom pronouns (eg. neo-pronouns)
To add custom pronoun sets, you can use the `single` and `pairs` options in the config file. `single` is for singular options such as `any` or `ask` while `pairs` is for pronouns that come in pairs and are used in the form `a/b`, for example `they` and `them`. To add custom pronoun sets, you can use the `single` and `pairs` options in the config file. `single` is for singular options such as `any` or `ask` while `pairs` is for pronouns that come in pairs and are used in the form `a/b`, for example `they` and `them`.
#### Setting the default placeholder
You can configure the default text returned by the placeholder when a player does not have pronouns set by changing the `default_placeholder` config value. You can also override the default in particular cases by passing an argument to the placeholder like this: `%playerpronouns:pronouns/ask%` (or `%playerpronouns:raw_pronouns/ask%`) where `ask` is the default text.
#### Displaying pronouns #### Displaying pronouns
You can display the pronouns in any [TextPlaceholderAPI](https://github.com/Patbox/TextPlaceholderAPI) compatible mods using the following placeholders:
* `playerpronouns:pronouns`: Returns a player's pronouns with any styling that is configured.
* `playerpronouns:raw_pronouns`: Returns a player's pronouns without any styling even if configured.
##### In chat with Styled Chat ##### In chat with Styled Chat
[Styled Chat](https://modrinth.com/mod/styled-chat) allows you to customise the formatting of chat messages. [Styled Chat](https://modrinth.com/mod/styled-chat) allows you to customise the formatting of chat messages.

View file

@ -22,21 +22,24 @@ public class Config {
private static final Codec<Config> CODEC = RecordCodecBuilder.create(instance -> instance.group( private static final Codec<Config> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.BOOL.fieldOf("allow_custom").forGetter(config -> config.allowCustom), Codec.BOOL.fieldOf("allow_custom").forGetter(config -> config.allowCustom),
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)
).apply(instance, Config::new)); ).apply(instance, Config::new));
private final boolean allowCustom; private final boolean allowCustom;
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 Config(boolean allowCustom, List<Pronoun> single, List<Pronoun> pairs) { private Config(boolean allowCustom, List<Pronoun> single, List<Pronoun> pairs, String defaultPlaceholder) {
this.allowCustom = allowCustom; this.allowCustom = allowCustom;
this.single = single; this.single = single;
this.pairs = pairs; this.pairs = pairs;
this.defaultPlaceholder = defaultPlaceholder;
} }
private Config() { private Config() {
this(true, Collections.emptyList(), Collections.emptyList()); this(true, Collections.emptyList(), Collections.emptyList(), "Unknown");
} }
public boolean allowCustom() { public boolean allowCustom() {
@ -51,6 +54,10 @@ public class Config {
return pairs; return pairs;
} }
public String getDefaultPlaceholder() {
return defaultPlaceholder;
}
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

@ -66,13 +66,14 @@ public class PlayerPronouns implements ModInitializer {
if (!ctx.hasPlayer()) { if (!ctx.hasPlayer()) {
return PlaceholderResult.invalid("missing player"); return PlaceholderResult.invalid("missing player");
} }
String defaultMessage = ctx.hasArgument() ? ctx.getArgument() : config.getDefaultPlaceholder();
ServerPlayerEntity player = ctx.getPlayer(); ServerPlayerEntity player = ctx.getPlayer();
if (pronounDatabase == null) { if (pronounDatabase == null) {
return PlaceholderResult.value("Unknown"); return PlaceholderResult.value(defaultMessage);
} }
Pronouns pronouns = pronounDatabase.get(player.getUuid()); Pronouns pronouns = pronounDatabase.get(player.getUuid());
if (pronouns == null) { if (pronouns == null) {
return PlaceholderResult.value("Unknown"); return PlaceholderResult.value(defaultMessage);
} }
return PlaceholderResult.value(pronouns.formatted()); return PlaceholderResult.value(pronouns.formatted());
}); });
@ -81,13 +82,14 @@ public class PlayerPronouns implements ModInitializer {
if (!ctx.hasPlayer()) { if (!ctx.hasPlayer()) {
return PlaceholderResult.invalid("missing player"); return PlaceholderResult.invalid("missing player");
} }
String defaultMessage = ctx.hasArgument() ? ctx.getArgument() : config.getDefaultPlaceholder();
ServerPlayerEntity player = ctx.getPlayer(); ServerPlayerEntity player = ctx.getPlayer();
if (pronounDatabase == null) { if (pronounDatabase == null) {
return PlaceholderResult.value("Unknown"); return PlaceholderResult.value(defaultMessage);
} }
Pronouns pronouns = pronounDatabase.get(player.getUuid()); Pronouns pronouns = pronounDatabase.get(player.getUuid());
if (pronouns == null) { if (pronouns == null) {
return PlaceholderResult.value("Unknown"); return PlaceholderResult.value(defaultMessage);
} }
return PlaceholderResult.value(pronouns.raw()); return PlaceholderResult.value(pronouns.raw());
}); });