Use explicit locale for upper/lowercase. EXPERIMENTAL: may be bugs from this commit

This commit is contained in:
fullwall 2024-06-29 14:46:25 +08:00
parent 147df027bb
commit ea74254cdd
7 changed files with 27 additions and 27 deletions

View File

@ -13,6 +13,7 @@ import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -558,7 +559,7 @@ public class NPCCommands {
if (permissions != null) { if (permissions != null) {
perms.addAll(Arrays.asList(permissions.split(","))); perms.addAll(Arrays.asList(permissions.split(",")));
} }
if (command.toLowerCase().startsWith("npc select")) if (command.startsWith("npc select"))
throw new CommandException("npc select not currently supported within commands. Use --id <id> instead"); throw new CommandException("npc select not currently supported within commands. Use --id <id> instead");
try { try {
@ -707,8 +708,8 @@ public class NPCCommands {
flags = "mo") flags = "mo")
public void controllable(CommandContext args, CommandSender sender, NPC npc, public void controllable(CommandContext args, CommandSender sender, NPC npc,
@Flag("controls") BuiltInControls controls, @Flag("enabled") Boolean enabled) throws CommandException { @Flag("controls") BuiltInControls controls, @Flag("enabled") Boolean enabled) throws CommandException {
if ((npc.isSpawned() && !sender.hasPermission( if ((npc.isSpawned()
"citizens.npc.controllable." + npc.getEntity().getType().name().toLowerCase().replace("_", ""))) && !sender.hasPermission("citizens.npc.controllable." + Util.prettyEnum(npc.getEntity().getType())))
|| !sender.hasPermission("citizens.npc.controllable")) || !sender.hasPermission("citizens.npc.controllable"))
throw new NoPermissionsException(); throw new NoPermissionsException();
if (!npc.hasTrait(Controllable.class) && enabled == null) { if (!npc.hasTrait(Controllable.class) && enabled == null) {
@ -801,7 +802,7 @@ public class NPCCommands {
throw new CommandException(); throw new CommandException();
if (!sender.hasPermission("citizens.npc.create.*") && !sender.hasPermission("citizens.npc.createall") if (!sender.hasPermission("citizens.npc.create.*") && !sender.hasPermission("citizens.npc.createall")
&& !sender.hasPermission("citizens.npc.create." + type.name().toLowerCase().replace("_", ""))) && !sender.hasPermission("citizens.npc.create." + Util.prettyEnum(type)))
throw new NoPermissionsException(); throw new NoPermissionsException();
if ((at != null || registryName != null || traits != null || templateName != null) if ((at != null || registryName != null || traits != null || templateName != null)
@ -1180,8 +1181,7 @@ public class NPCCommands {
public void gamemode(CommandContext args, CommandSender sender, NPC npc, @Arg(1) GameMode mode) { public void gamemode(CommandContext args, CommandSender sender, NPC npc, @Arg(1) GameMode mode) {
Player player = (Player) npc.getEntity(); Player player = (Player) npc.getEntity();
if (args.argsLength() == 1) { if (args.argsLength() == 1) {
Messaging.sendTr(sender, Messages.GAMEMODE_DESCRIBE, npc.getName(), Messaging.sendTr(sender, Messages.GAMEMODE_DESCRIBE, npc.getName(), Util.prettyEnum(player.getGameMode()));
player.getGameMode().name().toLowerCase());
return; return;
} }
if (mode == null) { if (mode == null) {
@ -2845,7 +2845,7 @@ public class NPCCommands {
if (args.hasValueFlag("color")) { if (args.hasValueFlag("color")) {
if (color != null) { if (color != null) {
trait.setColor(color); trait.setColor(color);
Messaging.sendTr(sender, Messages.SHEEP_COLOR_SET, color.toString().toLowerCase()); Messaging.sendTr(sender, Messages.SHEEP_COLOR_SET, Util.prettyEnum(color));
} else { } else {
Messaging.sendErrorTr(sender, Messages.INVALID_SHEEP_COLOR, Util.listValuesPretty(DyeColor.values())); Messaging.sendErrorTr(sender, Messages.INVALID_SHEEP_COLOR, Util.listValuesPretty(DyeColor.values()));
} }
@ -2875,7 +2875,7 @@ public class NPCCommands {
} }
NPCShop shop = npc != null ? npc.getOrAddTrait(ShopTrait.class).getDefaultShop() : null; NPCShop shop = npc != null ? npc.getOrAddTrait(ShopTrait.class).getDefaultShop() : null;
if (args.argsLength() == 3) { if (args.argsLength() == 3) {
shop = shops.getShop(args.getString(2).toLowerCase()); shop = shops.getShop(args.getString(2));
} }
if (shop == null) if (shop == null)
throw new CommandUsageException(); throw new CommandUsageException();
@ -3653,7 +3653,7 @@ public class NPCCommands {
trait.setInterested(!trait.isInterested()); trait.setInterested(!trait.isInterested());
} }
if (variant != null) { if (variant != null) {
variant = variant.toUpperCase(); variant = variant.toUpperCase(Locale.US);
try { try {
Wolf.Variant.class.getField(variant); Wolf.Variant.class.getField(variant);
} catch (Throwable t) { } catch (Throwable t) {

View File

@ -12,6 +12,7 @@ import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.trait.SheepTrait; import net.citizensnpcs.trait.SheepTrait;
import net.citizensnpcs.trait.WoolColor; import net.citizensnpcs.trait.WoolColor;
import net.citizensnpcs.util.Messages; import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.Util;
public class SheepEquipper implements Equipper { public class SheepEquipper implements Equipper {
@Override @Override
@ -28,7 +29,7 @@ public class SheepEquipper implements Equipper {
DyeColor color = dye.getColor(); DyeColor color = dye.getColor();
toEquip.getOrAddTrait(WoolColor.class).setColor(color); toEquip.getOrAddTrait(WoolColor.class).setColor(color);
Messaging.sendTr(equipper, Messages.EQUIPMENT_EDITOR_SHEEP_COLOURED, toEquip.getName(), Messaging.sendTr(equipper, Messages.EQUIPMENT_EDITOR_SHEEP_COLOURED, toEquip.getName(),
color.name().toLowerCase().replace("_", " ")); Util.prettyEnum(color));
hand.setAmount(hand.getAmount() - 1); hand.setAmount(hand.getAmount() - 1);
} else { } else {

View File

@ -2,6 +2,7 @@ package net.citizensnpcs.npc;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
@ -176,7 +177,7 @@ public class CitizensTraitFactory implements TraitFactory {
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T extends Trait> T getTrait(String name) { public <T extends Trait> T getTrait(String name) {
TraitInfo info = registered.get(name.toLowerCase()); TraitInfo info = registered.get(name.toLowerCase(Locale.US));
if (info == null) if (info == null)
return null; return null;
return (T) create(info); return (T) create(info);
@ -184,7 +185,7 @@ public class CitizensTraitFactory implements TraitFactory {
@Override @Override
public Class<? extends Trait> getTraitClass(String name) { public Class<? extends Trait> getTraitClass(String name) {
TraitInfo info = registered.get(name.toLowerCase()); TraitInfo info = registered.get(name.toLowerCase(Locale.US));
return info == null ? null : info.getTraitClass(); return info == null ? null : info.getTraitClass();
} }

View File

@ -7,6 +7,7 @@ import java.util.Comparator;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
@ -509,7 +510,7 @@ public class CommandTrait extends Trait {
@Override @Override
public String toString() { public String toString() {
return name().charAt(0) + name().substring(1).toLowerCase(); return name().charAt(0) + name().substring(1).toLowerCase(Locale.US);
} }
} }

View File

@ -52,8 +52,7 @@ public class Controllable extends Trait implements Toggleable {
} }
return; return;
} }
if (!player.hasPermission( if (!player.hasPermission("citizens.npc.controllable." + Util.prettyEnum(npc.getEntity().getType()))
"citizens.npc.controllable." + npc.getEntity().getType().name().toLowerCase().replace("_", ""))
|| !player.hasPermission("citizens.npc.controllable") || !player.hasPermission("citizens.npc.controllable")
|| ownerRequired && !npc.getOrAddTrait(Owner.class).isOwnedBy(player)) || ownerRequired && !npc.getOrAddTrait(Owner.class).isOwnedBy(player))
return; return;

View File

@ -3,6 +3,7 @@ package net.citizensnpcs.trait;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -803,7 +804,7 @@ public class HologramTrait extends Trait {
public static class TabCompletions implements CompletionsProvider { public static class TabCompletions implements CompletionsProvider {
@Override @Override
public Collection<String> getCompletions(CommandContext args, CommandSender sender, NPC npc) { public Collection<String> getCompletions(CommandContext args, CommandSender sender, NPC npc) {
if (args.length() > 1 && npc != null && LINE_ARGS.contains(args.getString(1).toLowerCase())) { if (args.length() > 1 && npc != null && LINE_ARGS.contains(args.getString(1).toLowerCase(Locale.US))) {
HologramTrait ht = npc.getOrAddTrait(HologramTrait.class); HologramTrait ht = npc.getOrAddTrait(HologramTrait.class);
return IntStream.range(0, ht.getLines().size()).mapToObj(Integer::toString) return IntStream.range(0, ht.getLines().size()).mapToObj(Integer::toString)
.collect(Collectors.toList()); .collect(Collectors.toList());
@ -811,7 +812,7 @@ public class HologramTrait extends Trait {
return Collections.emptyList(); return Collections.emptyList();
} }
private static Set<String> LINE_ARGS = ImmutableSet.of("set", "remove", "margintop", "marginbottom"); private static final Set<String> LINE_ARGS = ImmutableSet.of("set", "remove", "margintop", "marginbottom");
} }
public static class TextDisplayRenderer extends SingleEntityHologramRenderer { public static class TextDisplayRenderer extends SingleEntityHologramRenderer {

View File

@ -6,6 +6,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Random; import java.util.Random;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
@ -340,7 +341,7 @@ public class Util {
} }
public static String listValuesPretty(Enum<?>[] values) { public static String listValuesPretty(Enum<?>[] values) {
return "<yellow>" + Joiner.on("<green>, <yellow>").join(values).toLowerCase(); return "<yellow>" + Joiner.on("<green>, <yellow>").join(values).toLowerCase(Locale.US);
} }
public static boolean locationWithinRange(Location current, Location target, double range) { public static boolean locationWithinRange(Location current, Location target, double range) {
@ -350,15 +351,15 @@ public class Util {
} }
public static <T extends Enum<?>> T matchEnum(T[] values, String toMatch) { public static <T extends Enum<?>> T matchEnum(T[] values, String toMatch) {
toMatch = toMatch.toLowerCase().replace('-', '_').replace(' ', '_'); toMatch = toMatch.replace('-', '_').replace(' ', '_');
for (T check : values) { for (T check : values) {
if (toMatch.equals(check.name().toLowerCase()) if (toMatch.equalsIgnoreCase(check.name())
|| toMatch.equals("item") && check.name().equals("DROPPED_ITEM")) || toMatch.equalsIgnoreCase("item") && check.name().equals("DROPPED_ITEM"))
return check; // check for an exact match first return check; // check for an exact match first
} }
for (T check : values) { for (T check : values) {
String name = check.name().toLowerCase(); String name = check.name().toLowerCase(Locale.US);
if (name.replace("_", "").equals(toMatch) || name.startsWith(toMatch)) if (name.replace("_", "").equals(toMatch) || name.startsWith(toMatch))
return check; return check;
@ -442,7 +443,7 @@ public class Util {
} }
public static String prettyEnum(Enum<?> e) { public static String prettyEnum(Enum<?> e) {
return e.name().toLowerCase().replace('_', ' '); return e.name().toLowerCase(Locale.US).replace('_', ' ');
} }
public static String prettyPrintLocation(Location to) { public static String prettyPrintLocation(Location to) {
@ -451,10 +452,6 @@ public class Util {
TWO_DIGIT_DECIMAL.format(to.getYaw()), TWO_DIGIT_DECIMAL.format(to.getPitch())); TWO_DIGIT_DECIMAL.format(to.getYaw()), TWO_DIGIT_DECIMAL.format(to.getPitch()));
} }
public static String rawtype(Enum<?>[] values) {
return "<yellow>" + Joiner.on("<green>, <yellow>").join(values).toLowerCase();
}
public static void runCommand(NPC npc, Player clicker, String command, boolean op, boolean player) { public static void runCommand(NPC npc, Player clicker, String command, boolean op, boolean player) {
List<String> split = Splitter.on(' ').omitEmptyStrings().trimResults().limit(2).splitToList(command); List<String> split = Splitter.on(' ').omitEmptyStrings().trimResults().limit(2).splitToList(command);
String bungeeServer = split.size() == 2 && split.get(0).equalsIgnoreCase("server") ? split.get(1) : null; String bungeeServer = split.size() == 2 && split.get(0).equalsIgnoreCase("server") ? split.get(1) : null;