mirror of
https://github.com/Sollace/Unicopia.git
synced 2024-11-27 15:17:59 +01:00
Improve the /unicopia racelist command and give better feedback when it is enabled or disabled
This commit is contained in:
parent
91b8b827a6
commit
9b3cb7944a
4 changed files with 82 additions and 41 deletions
57
src/main/java/com/minelittlepony/unicopia/AllowList.java
Normal file
57
src/main/java/com/minelittlepony/unicopia/AllowList.java
Normal 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());
|
||||
}
|
||||
}
|
|
@ -134,12 +134,7 @@ public record Race (Supplier<Composite> compositeSupplier, Availability availabi
|
|||
}
|
||||
|
||||
public boolean isPermitted(@Nullable PlayerEntity sender) {
|
||||
Set<String> 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) {
|
||||
|
|
|
@ -21,14 +21,13 @@ class RacelistCommand {
|
|||
.then(CommandManager.literal("show")
|
||||
.executes(context -> {
|
||||
context.getSource().sendFeedback(() -> {
|
||||
Set<String> whitelist = Unicopia.getConfig().speciesWhiteList.get();
|
||||
if (whitelist.isEmpty()) {
|
||||
if (!AllowList.INSTANCE.isEnabled()) {
|
||||
return Text.translatable("commands.racelist.inactive");
|
||||
}
|
||||
Set<MutableText> allowed = new HashSet<>();
|
||||
Set<MutableText> 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<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(() -> {
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -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 <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.inactive": "The allowlist is not active. Add races with /unicopia racelist allow <race> 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",
|
||||
|
|
Loading…
Reference in a new issue