Improve the /unicopia racelist command and give better feedback when it is enabled or disabled

This commit is contained in:
Sollace 2024-02-13 21:58:19 +00:00
parent 91b8b827a6
commit 9b3cb7944a
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB
4 changed files with 82 additions and 41 deletions

View file

@ -0,0 +1,57 @@
package com.minelittlepony.unicopia;
import java.util.Set;
public class AllowList {
public static final AllowList INSTANCE = new AllowList();
public AllowList() {
}
public boolean disable() {
if (!isEnabled()) {
return false;
}
Unicopia.getConfig().speciesWhiteList.get().clear();
Unicopia.getConfig().save();
return true;
}
public boolean isEnabled() {
return !Unicopia.getConfig().speciesWhiteList.get().isEmpty();
}
public boolean add(Race race) {
if (race.isUnset() || race.isHuman()) {
return false;
}
Set<String> values = Unicopia.getConfig().speciesWhiteList.get();
boolean added = values.add(race.getId().toString());
Unicopia.getConfig().save();
return added;
}
public boolean remove(Race race) {
Set<String> values = Unicopia.getConfig().speciesWhiteList.get();
if (values.isEmpty()) {
for (Race r : Race.REGISTRY) {
if (!r.isUnset() && r != race) {
values.add(r.getId().toString());
}
}
Unicopia.getConfig().save();
return true;
}
boolean removed = values.remove(race.getId().toString());
Unicopia.getConfig().save();
return removed;
}
public boolean permits(Race race) {
return race.isUnset()
|| race.isHuman()
|| !isEnabled()
|| Unicopia.getConfig().speciesWhiteList.get().contains(race.getId().toString());
}
}

View file

@ -134,12 +134,7 @@ public record Race (Supplier<Composite> compositeSupplier, Availability availabi
} }
public boolean isPermitted(@Nullable PlayerEntity sender) { public boolean isPermitted(@Nullable PlayerEntity sender) {
Set<String> whitelist = Unicopia.getConfig().speciesWhiteList.get(); return AllowList.INSTANCE.permits(this);
return this == HUMAN
|| isUnset()
|| whitelist.isEmpty()
|| whitelist.contains(getId().toString());
} }
public Race validate(PlayerEntity sender) { public Race validate(PlayerEntity sender) {

View file

@ -21,14 +21,13 @@ class RacelistCommand {
.then(CommandManager.literal("show") .then(CommandManager.literal("show")
.executes(context -> { .executes(context -> {
context.getSource().sendFeedback(() -> { context.getSource().sendFeedback(() -> {
Set<String> whitelist = Unicopia.getConfig().speciesWhiteList.get(); if (!AllowList.INSTANCE.isEnabled()) {
if (whitelist.isEmpty()) {
return Text.translatable("commands.racelist.inactive"); return Text.translatable("commands.racelist.inactive");
} }
Set<MutableText> allowed = new HashSet<>(); Set<MutableText> allowed = new HashSet<>();
Set<MutableText> unallowed = new HashSet<>(); Set<MutableText> unallowed = new HashSet<>();
Race.REGISTRY.forEach(race -> { Race.REGISTRY.forEach(race -> {
(race.isPermitted(null) ? allowed : unallowed).add(Text.translatable("commands.racelist.get.list_item", (AllowList.INSTANCE.permits(race) ? allowed : unallowed).add(Text.translatable("commands.racelist.get.list_item",
race.getDisplayName(), race.getDisplayName(),
Text.literal(race.getId().toString()).formatted(Formatting.GRAY) Text.literal(race.getId().toString()).formatted(Formatting.GRAY)
)); ));
@ -45,44 +44,32 @@ class RacelistCommand {
) )
.then(CommandManager.literal("reset") .then(CommandManager.literal("reset")
.executes(context -> { .executes(context -> {
Unicopia.getConfig().speciesWhiteList.get().clear(); boolean success = AllowList.INSTANCE.disable();
Unicopia.getConfig().save(); context.getSource().sendFeedback(() -> Text.translatable("commands.racelist.reset." + (success ? "success" : "fail")).formatted(Formatting.YELLOW), false);
context.getSource().sendFeedback(() -> Text.translatable("commands.racelist.clear.success").formatted(Formatting.GREEN), false);
return 0; return 0;
}) })
) )
.then(CommandManager.literal("allow") .then(CommandManager.literal("allow")
.then(CommandManager.argument("race", Race.argument()).suggests(UCommandSuggestion.ALL_RACE_SUGGESTIONS) .then(CommandManager.argument("race", Race.argument()).suggests(UCommandSuggestion.ALL_RACE_SUGGESTIONS)
.executes(context -> toggle(context.getSource(), context.getSource().getPlayer(), Race.fromArgument(context, "race"), "allowed", race -> { .executes(context -> toggle(context.getSource(), context.getSource().getPlayer(), Race.fromArgument(context, "race"), "allowed", AllowList.INSTANCE::add))
if (race.isUnset()) {
return false;
}
boolean result = Unicopia.getConfig().speciesWhiteList.get().add(race.getId().toString());
Unicopia.getConfig().save();
return result;
}))
)) ))
.then(CommandManager.literal("disallow") .then(CommandManager.literal("disallow")
.then(CommandManager.argument("race", Race.argument()).suggests(UCommandSuggestion.ALL_RACE_SUGGESTIONS) .then(CommandManager.argument("race", Race.argument()).suggests(UCommandSuggestion.ALL_RACE_SUGGESTIONS)
.executes(context -> toggle(context.getSource(), context.getSource().getPlayer(), Race.fromArgument(context, "race"), "disallowed", race -> { .executes(context -> toggle(context.getSource(), context.getSource().getPlayer(), Race.fromArgument(context, "race"), "disallowed", AllowList.INSTANCE::remove))
boolean result = Unicopia.getConfig().speciesWhiteList.get().remove(race.getId().toString());
Unicopia.getConfig().save();
return result;
}))
)); ));
} }
static int toggle(ServerCommandSource source, ServerPlayerEntity player, Race race, String action, Function<Race, Boolean> func) { static int toggle(ServerCommandSource source, ServerPlayerEntity player, Race race, String action, Function<Race, Boolean> func) {
boolean enabled = AllowList.INSTANCE.isEnabled();
boolean success = func.apply(race);
if (enabled != AllowList.INSTANCE.isEnabled()) {
source.sendFeedback(() -> Text.translatable("commands.racelist." + (enabled ? "disabled" : "enabled")).formatted(enabled ? Formatting.RED : Formatting.GREEN), false);
}
source.sendFeedback(() -> { source.sendFeedback(() -> {
String translationKey = "commands.racelist." + action; String translationKey = "commands.racelist." + action;
if (!success) {
if (!func.apply(race)) {
if (race.isUnset()) { if (race.isUnset()) {
translationKey = "commands.racelist.illegal"; translationKey = "commands.racelist.illegal";
} else { } else {
@ -90,8 +77,7 @@ class RacelistCommand {
} }
} }
Text formattedName = race.getDisplayName().copy().formatted(Formatting.GOLD); return Text.translatable(translationKey, race.getDisplayName().copy().formatted(Formatting.GOLD)).formatted(success ? Formatting.GREEN : Formatting.RED);
return Text.translatable(translationKey, formattedName).formatted(Formatting.GREEN);
}, false); }, false);
return 0; return 0;
} }

View file

@ -1327,16 +1327,19 @@
"commands.race.tell.other.alt": "%s is an ", "commands.race.tell.other.alt": "%s is an ",
"commands.racelist.illegal": "The default race %s cannot be used with this command.", "commands.racelist.illegal": "The default race %s cannot be used with this command.",
"commands.racelist.allowed": "Added %1$s to the whitelist.",
"commands.racelist.get.allowed": "Allowed (%s):", "commands.racelist.get.allowed": "Allowed (%s):",
"commands.racelist.get.not_allowed": "Not Allowed (%s):", "commands.racelist.get.not_allowed": "Not Allowed (%s):",
"commands.racelist.get.list_item": "- %s (%s)", "commands.racelist.get.list_item": "- %s (%s)",
"commands.racelist.clear.success": "Disabled Whitelist", "commands.racelist.reset.success": "Cleared and disabled allow list.",
"commands.racelist.reset.fail": "The allow list is not active. Doing nothing.",
"commands.racelist.inactive": "The allow list is not active. Add races with /unicopia racelist <allow|disallow> <race> to configure it.",
"commands.racelist.enabled": "Enabled allow list",
"commands.racelist.disabled": "Disabled allow list",
"commands.racelist.allowed": "Added %1$s to the allow list.",
"commands.racelist.allowed.failed": "%1$s is already allowed.", "commands.racelist.allowed.failed": "%1$s is already allowed.",
"commands.racelist.inactive": "The allowlist is not active. Add races with /unicopia racelist allow <race> to configure it.", "commands.racelist.disallowed": "Removed %1$s from the allow list.",
"commands.racelist.disallowed.failed": "%1$s is already not allowed.",
"commands.racelist.disallowed": "Removed %1$s from the whitelist.",
"commands.racelist.disallowed.failed": "%1$s is not on the whitelist.",
"commands.worldtribe.success.get": "Default race for all new players is currently set to: %s", "commands.worldtribe.success.get": "Default race for all new players is currently set to: %s",
"commands.worldtribe.success.set": "Set default race for new players is now set to: %s", "commands.worldtribe.success.set": "Set default race for new players is now set to: %s",