diff --git a/src/main/java/com/minelittlepony/unicopia/AllowList.java b/src/main/java/com/minelittlepony/unicopia/AllowList.java new file mode 100644 index 00000000..f1810cd1 --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/AllowList.java @@ -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 values = Unicopia.getConfig().speciesWhiteList.get(); + boolean added = values.add(race.getId().toString()); + Unicopia.getConfig().save(); + return added; + } + + public boolean remove(Race race) { + Set 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()); + } +} diff --git a/src/main/java/com/minelittlepony/unicopia/Race.java b/src/main/java/com/minelittlepony/unicopia/Race.java index 72ae4d08..a6d73d72 100644 --- a/src/main/java/com/minelittlepony/unicopia/Race.java +++ b/src/main/java/com/minelittlepony/unicopia/Race.java @@ -134,12 +134,7 @@ public record Race (Supplier compositeSupplier, Availability availabi } public boolean isPermitted(@Nullable PlayerEntity sender) { - Set whitelist = Unicopia.getConfig().speciesWhiteList.get(); - - return this == HUMAN - || isUnset() - || whitelist.isEmpty() - || whitelist.contains(getId().toString()); + return AllowList.INSTANCE.permits(this); } public Race validate(PlayerEntity sender) { diff --git a/src/main/java/com/minelittlepony/unicopia/command/RacelistCommand.java b/src/main/java/com/minelittlepony/unicopia/command/RacelistCommand.java index 30a19dd0..00d776a8 100644 --- a/src/main/java/com/minelittlepony/unicopia/command/RacelistCommand.java +++ b/src/main/java/com/minelittlepony/unicopia/command/RacelistCommand.java @@ -21,14 +21,13 @@ class RacelistCommand { .then(CommandManager.literal("show") .executes(context -> { context.getSource().sendFeedback(() -> { - Set whitelist = Unicopia.getConfig().speciesWhiteList.get(); - if (whitelist.isEmpty()) { + if (!AllowList.INSTANCE.isEnabled()) { return Text.translatable("commands.racelist.inactive"); } Set allowed = new HashSet<>(); Set unallowed = new HashSet<>(); 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(), Text.literal(race.getId().toString()).formatted(Formatting.GRAY) )); @@ -45,44 +44,32 @@ class RacelistCommand { ) .then(CommandManager.literal("reset") .executes(context -> { - Unicopia.getConfig().speciesWhiteList.get().clear(); - Unicopia.getConfig().save(); - context.getSource().sendFeedback(() -> Text.translatable("commands.racelist.clear.success").formatted(Formatting.GREEN), false); + boolean success = AllowList.INSTANCE.disable(); + context.getSource().sendFeedback(() -> Text.translatable("commands.racelist.reset." + (success ? "success" : "fail")).formatted(Formatting.YELLOW), false); return 0; }) ) .then(CommandManager.literal("allow") .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 -> { - - if (race.isUnset()) { - return false; - } - - boolean result = Unicopia.getConfig().speciesWhiteList.get().add(race.getId().toString()); - - Unicopia.getConfig().save(); - - return result; - })) + .executes(context -> toggle(context.getSource(), context.getSource().getPlayer(), Race.fromArgument(context, "race"), "allowed", AllowList.INSTANCE::add)) )) .then(CommandManager.literal("disallow") .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 -> { - boolean result = Unicopia.getConfig().speciesWhiteList.get().remove(race.getId().toString()); - - Unicopia.getConfig().save(); - - return result; - })) + .executes(context -> toggle(context.getSource(), context.getSource().getPlayer(), Race.fromArgument(context, "race"), "disallowed", AllowList.INSTANCE::remove)) )); } static int toggle(ServerCommandSource source, ServerPlayerEntity player, Race race, String action, Function 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(() -> { String translationKey = "commands.racelist." + action; - - if (!func.apply(race)) { + if (!success) { if (race.isUnset()) { translationKey = "commands.racelist.illegal"; } else { @@ -90,8 +77,7 @@ class RacelistCommand { } } - Text formattedName = race.getDisplayName().copy().formatted(Formatting.GOLD); - return Text.translatable(translationKey, formattedName).formatted(Formatting.GREEN); + return Text.translatable(translationKey, race.getDisplayName().copy().formatted(Formatting.GOLD)).formatted(success ? Formatting.GREEN : Formatting.RED); }, false); return 0; } diff --git a/src/main/resources/assets/unicopia/lang/en_us.json b/src/main/resources/assets/unicopia/lang/en_us.json index ff4debfb..24d06e29 100644 --- a/src/main/resources/assets/unicopia/lang/en_us.json +++ b/src/main/resources/assets/unicopia/lang/en_us.json @@ -1327,16 +1327,19 @@ "commands.race.tell.other.alt": "%s is an ", "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.not_allowed": "Not Allowed (%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 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.inactive": "The allowlist is not active. Add races with /unicopia racelist allow to configure it.", - - "commands.racelist.disallowed": "Removed %1$s from the whitelist.", - "commands.racelist.disallowed.failed": "%1$s is not on the whitelist.", + "commands.racelist.disallowed": "Removed %1$s from the allow list.", + "commands.racelist.disallowed.failed": "%1$s is already not allowed.", "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",