From 4e945ad0b5e2136854aa2c7ba498af25649d05e6 Mon Sep 17 00:00:00 2001 From: Artemis-the-gr8 Date: Wed, 1 Feb 2023 15:12:33 +0100 Subject: [PATCH] Changed all #toLowerCase and #toUpperCase methods to use English Locale instead of default --- .../gr8/playerstats/commands/TabCompleter.java | 3 ++- .../commands/cmdutils/TabCompleteHelper.java | 5 +++-- .../artemis/the/gr8/playerstats/enums/Unit.java | 15 ++++----------- .../msg/msgutils/LanguageKeyHandler.java | 7 ++++--- .../playerstats/msg/msgutils/StringUtils.java | 4 +++- .../the/gr8/playerstats/utils/EnumHandler.java | 17 +++++++++-------- 6 files changed, 25 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/artemis/the/gr8/playerstats/commands/TabCompleter.java b/src/main/java/com/artemis/the/gr8/playerstats/commands/TabCompleter.java index 6a8238b..deab6a8 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/commands/TabCompleter.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/commands/TabCompleter.java @@ -10,6 +10,7 @@ import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.List; +import java.util.Locale; import java.util.stream.Collectors; public final class TabCompleter implements org.bukkit.command.TabCompleter { @@ -88,7 +89,7 @@ public final class TabCompleter implements org.bukkit.command.TabCompleter { private List getTabSuggestions(List completeList, String currentArg) { return completeList.stream() - .filter(item -> item.toLowerCase().contains(currentArg.toLowerCase())) + .filter(item -> item.toLowerCase(Locale.ENGLISH).contains(currentArg.toLowerCase(Locale.ENGLISH))) .collect(Collectors.toList()); } diff --git a/src/main/java/com/artemis/the/gr8/playerstats/commands/cmdutils/TabCompleteHelper.java b/src/main/java/com/artemis/the/gr8/playerstats/commands/cmdutils/TabCompleteHelper.java index d9839cc..ab4f00a 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/commands/cmdutils/TabCompleteHelper.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/commands/cmdutils/TabCompleteHelper.java @@ -6,6 +6,7 @@ import org.bukkit.entity.EntityType; import java.util.Arrays; import java.util.List; +import java.util.Locale; import java.util.stream.Collectors; public final class TabCompleteHelper { @@ -43,7 +44,7 @@ public final class TabCompleteHelper { .filter(Material::isItem) .filter(item -> item.getMaxDurability() != 0) .map(Material::toString) - .map(String::toLowerCase) + .map(string -> string.toLowerCase(Locale.ENGLISH)) .collect(Collectors.toList()); //the only statistics dealing with entities are killed_entity and entity_killed_by @@ -51,7 +52,7 @@ public final class TabCompleteHelper { .parallel() .filter(EntityType::isAlive) .map(EntityType::toString) - .map(String::toLowerCase) + .map(string -> string.toLowerCase(Locale.ENGLISH)) .collect(Collectors.toList()); } } \ No newline at end of file diff --git a/src/main/java/com/artemis/the/gr8/playerstats/enums/Unit.java b/src/main/java/com/artemis/the/gr8/playerstats/enums/Unit.java index cdd08e3..dc6e997 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/enums/Unit.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/enums/Unit.java @@ -3,6 +3,8 @@ package com.artemis.the.gr8.playerstats.enums; import org.bukkit.Statistic; import org.jetbrains.annotations.NotNull; +import java.util.Locale; + /** * All the units PlayerStats can display statistics in, separated * by {@link Unit.Type}. @@ -131,15 +133,6 @@ public enum Unit { }; } - /** - * Gets the Unit corresponding to the given String. This String - * does not need to match exactly (it can be "day" or "days", - * for example), and is case-insensitive. - * - * @param unitName the name belonging to the desired Unit, - * case-insensitive - * @return the Unit - */ /** Converts the current Unit into a short label (and returns a '?' if the current Unit is not of Type TIME)*/ public char getShortLabel(){ return switch (this) { @@ -155,7 +148,7 @@ public enum Unit { match exactly (it can be "day" or "days", for example), and is case-insensitive. @param unitName an approximation of the name belonging to the desired Unit, case-insensitive */ public static @NotNull Unit fromString(@NotNull String unitName) { - return switch (unitName.toLowerCase()) { + return switch (unitName.toLowerCase(Locale.ENGLISH)) { case "cm" -> Unit.CM; case "m", "block", "blocks" -> Unit.BLOCK; case "mile", "miles" -> Unit.MILE; @@ -179,7 +172,7 @@ public enum Unit { * @return the Type of this Unit */ public static @NotNull Type getTypeFromStatistic(Statistic statistic) { - String name = statistic.toString().toLowerCase(); + String name = statistic.toString().toLowerCase(Locale.ENGLISH); if (name.contains("one_cm")) { return Type.DISTANCE; } else if (name.contains("damage")) { diff --git a/src/main/java/com/artemis/the/gr8/playerstats/msg/msgutils/LanguageKeyHandler.java b/src/main/java/com/artemis/the/gr8/playerstats/msg/msgutils/LanguageKeyHandler.java index 46afe4c..59bc63b 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/msg/msgutils/LanguageKeyHandler.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/msg/msgutils/LanguageKeyHandler.java @@ -16,6 +16,7 @@ import org.jetbrains.annotations.ApiStatus.Internal; import java.io.File; import java.util.Arrays; import java.util.HashMap; +import java.util.Locale; /** * @@ -239,8 +240,8 @@ public final class LanguageKeyHandler { */ public @Nullable String getBlockKey(Material block) { if (block == null) return null; - else if (block.toString().toLowerCase().contains("wall_banner")) { //replace wall_banner with regular banner, since there is no key for wall banners - String blockName = block.toString().toLowerCase().replace("wall_", ""); + else if (block.toString().toLowerCase(Locale.ENGLISH).contains("wall_banner")) { //replace wall_banner with regular banner, since there is no key for wall banners + String blockName = block.toString().toLowerCase(Locale.ENGLISH).replace("wall_", ""); Material newBlock = EnumHandler.getBlockEnum(blockName); return (newBlock != null) ? "block.minecraft." + newBlock.getKey().getKey() : null; } @@ -264,7 +265,7 @@ public final class LanguageKeyHandler { private @NotNull HashMap generateStatNameKeys() { //get the enum names for all statistics first HashMap statNames = new HashMap<>(Statistic.values().length); - Arrays.stream(Statistic.values()).forEach(statistic -> statNames.put(statistic, statistic.toString().toLowerCase())); + Arrays.stream(Statistic.values()).forEach(statistic -> statNames.put(statistic, statistic.toString().toLowerCase(Locale.ENGLISH))); //replace the ones for which the language key is different from the enum name statNames.put(Statistic.ARMOR_CLEANED, "clean_armor"); diff --git a/src/main/java/com/artemis/the/gr8/playerstats/msg/msgutils/StringUtils.java b/src/main/java/com/artemis/the/gr8/playerstats/msg/msgutils/StringUtils.java index 307dabc..79cc7cf 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/msg/msgutils/StringUtils.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/msg/msgutils/StringUtils.java @@ -2,6 +2,8 @@ package com.artemis.the.gr8.playerstats.msg.msgutils; import com.artemis.the.gr8.playerstats.utils.MyLogger; +import java.util.Locale; + /** * A small utility class that helps make enum constant * names prettier for output in stat-messages. @@ -18,7 +20,7 @@ public final class StringUtils { */ public static String prettify(String input) { if (input == null) return null; - StringBuilder capitals = new StringBuilder(input.toLowerCase()); + StringBuilder capitals = new StringBuilder(input.toLowerCase(Locale.ENGLISH)); capitals.setCharAt(0, Character.toUpperCase(capitals.charAt(0))); while (capitals.indexOf("_") != -1) { MyLogger.logHighLevelMsg("Replacing underscores and capitalizing names..."); diff --git a/src/main/java/com/artemis/the/gr8/playerstats/utils/EnumHandler.java b/src/main/java/com/artemis/the/gr8/playerstats/utils/EnumHandler.java index c311f25..617f5a7 100644 --- a/src/main/java/com/artemis/the/gr8/playerstats/utils/EnumHandler.java +++ b/src/main/java/com/artemis/the/gr8/playerstats/utils/EnumHandler.java @@ -9,6 +9,7 @@ import org.jetbrains.annotations.Nullable; import java.util.Arrays; import java.util.Collection; import java.util.List; +import java.util.Locale; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -80,7 +81,7 @@ public final class EnumHandler { */ public static @Nullable EntityType getEntityEnum(String entityName) { try { - return EntityType.valueOf(entityName.toUpperCase()); + return EntityType.valueOf(entityName.toUpperCase(Locale.ENGLISH)); } catch (IllegalArgumentException | NullPointerException e) { return null; @@ -109,7 +110,7 @@ public final class EnumHandler { */ public static @Nullable Statistic getStatEnum(@NotNull String statName) { try { - return Statistic.valueOf(statName.toUpperCase()); + return Statistic.valueOf(statName.toUpperCase(Locale.ENGLISH)); } catch (IllegalArgumentException e) { return null; @@ -123,7 +124,7 @@ public final class EnumHandler { * @return true if this String is a valid Statistic */ public boolean isStatistic(@NotNull String statName) { - return statNames.contains(statName.toLowerCase()); + return statNames.contains(statName.toLowerCase(Locale.ENGLISH)); } /** @@ -147,7 +148,7 @@ public final class EnumHandler { * of Type.Untyped */ public boolean isSubStatEntry(@NotNull String statName) { - return subStatNames.contains(statName.toLowerCase()); + return subStatNames.contains(statName.toLowerCase(Locale.ENGLISH)); } /** @@ -171,20 +172,20 @@ public final class EnumHandler { private void prepareLists() { List entityNames = Arrays.stream(EntityType.values()) .map(EntityType::toString) - .map(String::toLowerCase) + .map(string -> string.toLowerCase(Locale.ENGLISH)) .filter(entityName -> !entityName.equalsIgnoreCase("unknown")) .collect(Collectors.toList()); blockNames = Arrays.stream(Material.values()) .filter(Material::isBlock) .map(Material::toString) - .map(String::toLowerCase) + .map(string -> string.toLowerCase(Locale.ENGLISH)) .collect(Collectors.toList()); itemNames = Arrays.stream(Material.values()) .filter(Material::isItem) .map(Material::toString) - .map(String::toLowerCase) + .map(string -> string.toLowerCase(Locale.ENGLISH)) .collect(Collectors.toList()); subStatNames = Stream.of(blockNames, entityNames, itemNames) @@ -194,7 +195,7 @@ public final class EnumHandler { statNames = Arrays.stream(Statistic.values()) .map(Statistic::toString) - .map(String::toLowerCase) + .map(string -> string.toLowerCase(Locale.ENGLISH)) .collect(Collectors.toList()); } } \ No newline at end of file