From f1cda8a7dd0352116df084e5a423fe4e59bdbf0f Mon Sep 17 00:00:00 2001 From: Artemis-the-gr8 Date: Tue, 21 Jun 2022 17:35:46 +0200 Subject: [PATCH] Set up translatableComponents for all items, blocks, entities and custom statNames, and started working on killed/killed_by solution (#54, #43) --- .../artemis/the/gr8/playerstats/Main.java | 10 +- .../gr8/playerstats/commands/StatCommand.java | 55 ++++---- .../playerstats/msg/LanguageKeyHandler.java | 132 ++++++++++++++++++ .../gr8/playerstats/msg/MessageFactory.java | 67 ++++++--- .../playerstats/msg/PrideMessageFactory.java | 4 +- .../gr8/playerstats/utils/EnumHandler.java | 36 ----- 6 files changed, 213 insertions(+), 91 deletions(-) create mode 100644 src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/LanguageKeyHandler.java diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/Main.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/Main.java index 0046052..2b81d68 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/Main.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/Main.java @@ -5,6 +5,7 @@ import com.gmail.artemis.the.gr8.playerstats.commands.StatCommand; import com.gmail.artemis.the.gr8.playerstats.commands.TabCompleter; import com.gmail.artemis.the.gr8.playerstats.config.ConfigHandler; import com.gmail.artemis.the.gr8.playerstats.listeners.JoinListener; +import com.gmail.artemis.the.gr8.playerstats.msg.LanguageKeyHandler; import com.gmail.artemis.the.gr8.playerstats.msg.MessageFactory; import com.gmail.artemis.the.gr8.playerstats.msg.PrideMessageFactory; import net.kyori.adventure.platform.bukkit.BukkitAudiences; @@ -33,21 +34,22 @@ public class Main extends JavaPlugin { //initialize the Adventure library adventure = BukkitAudiences.create(this); - //first get an instance of the ConfigHandler + //first get an instance of the ConfigHandler and LanguageKeyHandler ConfigHandler config = new ConfigHandler(this); + LanguageKeyHandler language = new LanguageKeyHandler(); //then determine if we need a regular MessageFactory or a festive one MessageFactory messageFactory; if (config.useFestiveFormatting()) { if (LocalDate.now().getMonth().equals(Month.JUNE)) { - messageFactory = new PrideMessageFactory(config); + messageFactory = new PrideMessageFactory(config, language); } else { - messageFactory = new MessageFactory(config); + messageFactory = new MessageFactory(config, language); } } else { - messageFactory = new MessageFactory(config); + messageFactory = new MessageFactory(config, language); } //initialize the threadManager diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/StatCommand.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/StatCommand.java index 1ef48a8..60bcffd 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/StatCommand.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/StatCommand.java @@ -2,6 +2,7 @@ package com.gmail.artemis.the.gr8.playerstats.commands; import com.gmail.artemis.the.gr8.playerstats.ThreadManager; import com.gmail.artemis.the.gr8.playerstats.enums.Query; +import com.gmail.artemis.the.gr8.playerstats.msg.LanguageKeyHandler; import com.gmail.artemis.the.gr8.playerstats.utils.EnumHandler; import com.gmail.artemis.the.gr8.playerstats.statistic.StatRequest; import com.gmail.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler; @@ -75,59 +76,56 @@ public class StatCommand implements CommandExecutor { //test method private void printTranslatableNames(CommandSender sender, String selection, boolean extra) { + LanguageKeyHandler lang = new LanguageKeyHandler(); + if (selection == null) { TextComponent msg = Component.text("Include 'block', 'item', 'entity' or 'stat'").color(TextColor.fromHexString("#FFB80E")); adventure.sender(sender).sendMessage(msg); } else if (selection.equalsIgnoreCase("block")) { for (String name : EnumHandler.getBlockNames()) { - try { - TranslatableComponent msg = Component.translatable((EnumHandler.getBlockKey(name))) + String key = lang.getBlockKey(name); + if (key != null) { + TranslatableComponent msg = Component.translatable(key) .color(TextColor.fromHexString("#FFB80E")) .append(space()) .append(text("for blockName: ").color(NamedTextColor.WHITE)) .append(text(name).color(TextColor.fromHexString("#55AAFF"))); adventure.sender(sender).sendMessage(msg); } - catch (IllegalArgumentException e) { - adventure.sender(sender).sendMessage(Component.text(e.toString())); - } - } - } - else if (selection.equalsIgnoreCase("item")) { - for (String name : EnumHandler.getItemNames()) { - try { - TranslatableComponent msg = Component.translatable((EnumHandler.getItemKey(name, extra))) - .color(TextColor.fromHexString("#FFB80E")) - .append(space()) - .append(text("for itemName: ").color(NamedTextColor.WHITE)) - .append(text(name).color(TextColor.fromHexString("#55AAFF"))); - adventure.sender(sender).sendMessage(msg); - } - catch (IllegalArgumentException e) { - adventure.sender(sender).sendMessage(Component.text(e.toString())); - } } } else if (selection.equalsIgnoreCase("entity")) { for (String name : EnumHandler.getEntityNames()) { - try { - TranslatableComponent msg = Component.translatable((EnumHandler.getEntityKey(name))) + String key = lang.getEntityKey(name); + if (key != null) { + TranslatableComponent msg = Component.translatable(key) .color(TextColor.fromHexString("#FFB80E")) .append(space()) .append(text("for entityName: ").color(NamedTextColor.WHITE)) .append(text(name).color(TextColor.fromHexString("#55AAFF"))); adventure.sender(sender).sendMessage(msg); } - catch (IllegalArgumentException e) { - adventure.sender(sender).sendMessage(Component.text(e.toString())); + } + } + else if (selection.equalsIgnoreCase("item")) { + for (String name : EnumHandler.getItemNames()) { + String key = lang.getItemKey(name, extra); + if (key != null) { + TranslatableComponent msg = Component.translatable(key) + .color(TextColor.fromHexString("#FFB80E")) + .append(space()) + .append(text("for itemName: ").color(NamedTextColor.WHITE)) + .append(text(name).color(TextColor.fromHexString("#55AAFF"))); + adventure.sender(sender).sendMessage(msg); } } } else if (selection.equalsIgnoreCase("stat")) { - try { - for (String name : EnumHandler.getStatNames()) { - TranslatableComponent msg = Component.translatable((EnumHandler.getStatKey(name))) + for (String name : EnumHandler.getStatNames()) { + String key = lang.getStatKey(name); + if (key != null) { + TranslatableComponent msg = Component.translatable(key) .color(TextColor.fromHexString("#FFB80E")) .append(space()) .append(text("for statName: ").color(NamedTextColor.WHITE)) @@ -135,9 +133,6 @@ public class StatCommand implements CommandExecutor { adventure.sender(sender).sendMessage(msg); } } - catch (IllegalArgumentException e) { - adventure.sender(sender).sendMessage(Component.text(e.toString())); - } } else { TextComponent msg = Component.text("hi :)").color(TextColor.fromHexString("#FFB80E")); diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/LanguageKeyHandler.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/LanguageKeyHandler.java new file mode 100644 index 0000000..2ad3a88 --- /dev/null +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/LanguageKeyHandler.java @@ -0,0 +1,132 @@ +package com.gmail.artemis.the.gr8.playerstats.msg; + +import com.gmail.artemis.the.gr8.playerstats.utils.EnumHandler; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.Statistic; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Arrays; +import java.util.HashMap; + +public class LanguageKeyHandler { + + private final HashMap statNameKeys; + + public LanguageKeyHandler() { + statNameKeys = new HashMap<>(); + generateStatNameKeys(); + } + + public @Nullable String getStatKey(@NotNull String statName) { + try { + Statistic stat = EnumHandler.getStatEnum(statName); + if (stat.getType() == Statistic.Type.UNTYPED) { + return "stat.minecraft." + statNameKeys.get(stat); + } + else { + return "stat_type.minecraft." + statNameKeys.get(stat); + } + } + catch (IllegalArgumentException e) { + Bukkit.getLogger().info("PlayerStats, LanguageKeyHandler 33: " + e); + return null; + } + } + + public @Nullable String getEntityKey(@NotNull String entityName) { + if (entityName.equalsIgnoreCase("UNKNOWN")) { + return null; + } + else { + try { + return "entity.minecraft." + EnumHandler.getEntityEnum(entityName).getKey().getKey(); + } + catch (IllegalArgumentException e) { + Bukkit.getLogger().info("PlayerStats, LanguageKeyHandler 47: " + e); + return null; + } + } + } + + public @Nullable String getItemKey(@NotNull String itemName, boolean logCC) { + try { + Material item = EnumHandler.getItemEnum(itemName); + if (item.isBlock()) { + if (logCC) { + Bukkit.getLogger().info("Creative Category for Block " + item + " : " + item.getCreativeCategory()); + } + return "block.minecraft." + item.getKey().getKey(); + } + else { + if (logCC) { + Bukkit.getLogger().info("Creative Category for Item " + item + " : " + item.getCreativeCategory()); + } + return "item.minecraft." + EnumHandler.getItemEnum(itemName).getKey().getKey(); + } + } + catch (IllegalArgumentException e) { + Bukkit.getLogger().info("PlayerStats, LanguageKeyHandler 70: " + e); + return null; + } + } + + public @Nullable String getBlockKey(@NotNull String materialName) { + String name = materialName; + if (materialName.toLowerCase().contains("wall_banner")) { + name = materialName.replace("wall_", ""); + } + try { + return "block.minecraft." + EnumHandler.getBlockEnum(name).getKey().getKey(); + } + catch (IllegalArgumentException e) { + Bukkit.getLogger().info("PlayerStats, LanguageKeyHandler 84: " + e); + return null; + } + } + + private void generateDefaultKeys() { + Arrays.stream(Statistic.values()).forEach(statistic -> statNameKeys.put(statistic, statistic.toString().toLowerCase())); + } + + private void generateStatNameKeys() { + //get the enum names for all statistics first + generateDefaultKeys(); + + //replace the ones for which the language key is different from the enum name + statNameKeys.put(Statistic.ARMOR_CLEANED, "clean_armor"); + statNameKeys.put(Statistic.BANNER_CLEANED, "clean_banner"); + statNameKeys.put(Statistic.DROP_COUNT, "drop"); + statNameKeys.put(Statistic.CAKE_SLICES_EATEN, "eat_cake_slice"); + statNameKeys.put(Statistic.ITEM_ENCHANTED, "enchant_item"); + statNameKeys.put(Statistic.CAULDRON_FILLED, "fill_cauldron"); + statNameKeys.put(Statistic.DISPENSER_INSPECTED, "inspect_dispenser"); + statNameKeys.put(Statistic.DROPPER_INSPECTED, "inspect_dropper"); + statNameKeys.put(Statistic.HOPPER_INSPECTED, "inspect_hopper"); + statNameKeys.put(Statistic.BEACON_INTERACTION, "interact_with_beacon"); + statNameKeys.put(Statistic.BREWINGSTAND_INTERACTION, "interact_with_brewingstand"); + statNameKeys.put(Statistic.CRAFTING_TABLE_INTERACTION, "interact_with_crafting_table"); + statNameKeys.put(Statistic.FURNACE_INTERACTION, "interact_with_furnace"); + statNameKeys.put(Statistic.CHEST_OPENED, "open_chest"); + statNameKeys.put(Statistic.ENDERCHEST_OPENED, "open_enderchest"); + statNameKeys.put(Statistic.SHULKER_BOX_OPENED, "open_shulker_box"); + statNameKeys.put(Statistic.NOTEBLOCK_PLAYED, "play_noteblock"); + statNameKeys.put(Statistic.PLAY_ONE_MINUTE, "play_time"); + statNameKeys.put(Statistic.RECORD_PLAYED, "play_record"); + statNameKeys.put(Statistic.FLOWER_POTTED, "pot_flower"); + statNameKeys.put(Statistic.TRAPPED_CHEST_TRIGGERED, "trigger_trapped_chest"); + statNameKeys.put(Statistic.NOTEBLOCK_TUNED, "tune_noteblock"); + statNameKeys.put(Statistic.CAULDRON_USED, "use_cauldron"); + + //do the same for the statistics that have a subtype + statNameKeys.put(Statistic.DROP, "dropped"); + statNameKeys.put(Statistic.PICKUP, "picked_up"); + statNameKeys.put(Statistic.MINE_BLOCK, "mined"); + statNameKeys.put(Statistic.USE_ITEM, "used"); + statNameKeys.put(Statistic.BREAK_ITEM, "broken"); + statNameKeys.put(Statistic.CRAFT_ITEM, "crafted"); + statNameKeys.put(Statistic.KILL_ENTITY, "killed"); + statNameKeys.put(Statistic.ENTITY_KILLED_BY, "killed_by"); + } +} diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/MessageFactory.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/MessageFactory.java index ffa2589..70ad585 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/MessageFactory.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/MessageFactory.java @@ -24,14 +24,16 @@ import static net.kyori.adventure.text.Component.*; public class MessageFactory { private static ConfigHandler config; + private final LanguageKeyHandler language; private final TextColor msgColor; //my favorite shade of light blue, somewhere between blue and aqua private final TextColor hoverBaseColor; //light blue - one shade lighter than msgColor private final TextColor accentColor1; //gold - one shade lighter than standard gold private final TextColor accentColor2; //yellow - a few shades darker than standard yellow - public MessageFactory(ConfigHandler c) { + public MessageFactory(ConfigHandler c, LanguageKeyHandler l) { config = c; + language = l; msgColor = TextColor.fromHexString("#55AAFF"); hoverBaseColor = TextColor.fromHexString("#55C6FF"); @@ -154,24 +156,24 @@ public class MessageFactory { .append(newline()); } - public TextComponent formatPlayerStat(String playerName, String statName, String subStatEntryName, int stat) { + public TextComponent formatPlayerStat(String playerName, String statName, String subStatEntry, int stat) { TextComponent.Builder singleStat = Component.text(); singleStat.append(playerNameComponent(Query.PLAYER, playerName + ": ")) .append(statNumberComponent(Query.PLAYER, stat)) .append(space()) - .append(statNameComponent(Query.PLAYER, statName)) + .append(statNameComponent(Query.PLAYER, statName, subStatEntry)) .append(space()); - if (subStatNameComponent(Query.PLAYER, subStatEntryName) != null) { - singleStat.append(subStatNameComponent(Query.PLAYER, subStatEntryName)); + if (subStatNameComponent(Query.PLAYER, subStatEntry) != null) { + singleStat.append(subStatNameComponent(Query.PLAYER, subStatEntry)); } return singleStat.build(); } - public TextComponent formatTopStats(@NotNull LinkedHashMap topStats, String statName, String subStatEntryName, boolean isConsoleSender) { + public TextComponent formatTopStats(@NotNull LinkedHashMap topStats, String statName, String subStatEntry, boolean isConsoleSender) { TextComponent.Builder topList = Component.text(); - topList.append(getTopStatTitle(topStats.size(), statName, subStatEntryName, isConsoleSender)); + topList.append(getTopStatTitle(topStats.size(), statName, subStatEntry, isConsoleSender)); boolean useDots = config.useDots(); Set playerNames = topStats.keySet(); @@ -215,7 +217,7 @@ public class MessageFactory { .append(space()) .append(statNumberComponent(Query.SERVER, stat)) .append(space()) - .append(statNameComponent(Query.SERVER, statName)) + .append(statNameComponent(Query.SERVER, statName, subStatEntry)) .append(space()); if (subStatNameComponent(Query.SERVER, subStatEntry) != null) { @@ -239,16 +241,16 @@ public class MessageFactory { .append(text(underscores)); } - protected TextComponent getTopStatTitle(int topLength, String statName, String subStatEntryName, boolean isConsoleSender) { + protected TextComponent getTopStatTitle(int topLength, String statName, String subStatEntry, boolean isConsoleSender) { TextComponent.Builder topStat = Component.text(); topStat.append(newline()) .append(pluginPrefix(isConsoleSender)) .append(titleComponent(Query.TOP, config.getTopStatsTitle())).append(space()) .append(titleNumberComponent(topLength)).append(space()) - .append(statNameComponent(Query.TOP, statName)).append(space()); + .append(statNameComponent(Query.TOP, statName, subStatEntry)).append(space()); - if (subStatNameComponent(Query.TOP, subStatEntryName) != null) { - topStat.append(subStatNameComponent(Query.TOP, subStatEntryName)); + if (subStatNameComponent(Query.TOP, subStatEntry) != null) { + topStat.append(subStatNameComponent(Query.TOP, subStatEntry)); } return topStat.build(); } @@ -259,16 +261,43 @@ public class MessageFactory { getStyleFromString(config.getPlayerNameFormatting(selection, true))); } - protected TranslatableComponent statNameComponent(Query selection, @NotNull String statName) { + protected TranslatableComponent statNameComponent(Query selection, @NotNull String statName, String subStatName) { TextDecoration style = getStyleFromString(config.getStatNameFormatting(selection, true)); - String name = EnumHandler.getStatKey(statName); + String key = language.getStatKey(statName); + if (key == null) { + key = statName; + } + else if (key.equalsIgnoreCase("stat_type.minecraft.killed")) { + TranslatableComponent.Builder statAndSubStat = translatable() + .key("commands.kill.success.single") //"Killed %s" + .args(subStatNameComponent(selection, subStatName)) + .color(getColorFromString(config.getStatNameFormatting(selection, false))); + if (style != null) { + statAndSubStat.decoration(style, TextDecoration.State.TRUE); + } + return statAndSubStat.build(); + } + else if (key.equalsIgnoreCase("stat_type.minecraft.killed_by")) { //"commands.kill.success.single" + "book.byAuthor"; //"Killed %s" + "by %1$s" + TranslatableComponent.Builder totalName = translatable() + .key("commands.kill.success.single") //"Killed %s" + .color(getColorFromString(config.getStatNameFormatting(selection, false))) + .append(translatable() + .key("book.byAuthor") //"by %1$s" + .args(subStatNameComponent(selection, subStatName))); + if (style != null) { + totalName.decoration(style, TextDecoration.State.TRUE); + } + return totalName.build(); + } + if (style != null) { return Component.translatable( - name, + key, getColorFromString(config.getStatNameFormatting(selection, false)), style); } else { - return Component.translatable(name, + return Component.translatable( + key, getColorFromString(config.getStatNameFormatting(selection, false))); } } @@ -279,13 +308,13 @@ public class MessageFactory { } String name = null; if (EnumHandler.isEntity(subStatName)){ - name = EnumHandler.getEntityKey(subStatName); + name = language.getEntityKey(subStatName); } else if (EnumHandler.isBlock(subStatName)) { - name = EnumHandler.getBlockKey(subStatName); + name = language.getBlockKey(subStatName); } else if (EnumHandler.isItem(subStatName)) { - name = EnumHandler.getItemKey(subStatName, false); + name = language.getItemKey(subStatName, false); } if (name != null) { TextDecoration style = getStyleFromString(config.getSubStatNameFormatting(selection, true)); diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/PrideMessageFactory.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/PrideMessageFactory.java index e8d22bb..df6efa9 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/PrideMessageFactory.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/PrideMessageFactory.java @@ -12,8 +12,8 @@ import static net.kyori.adventure.text.Component.*; public class PrideMessageFactory extends MessageFactory { - public PrideMessageFactory(ConfigHandler c) { - super(c); + public PrideMessageFactory(ConfigHandler c, LanguageKeyHandler l) { + super(c, l); } @Override diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/EnumHandler.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/EnumHandler.java index 80216a0..d1fd98f 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/EnumHandler.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/utils/EnumHandler.java @@ -1,6 +1,5 @@ package com.gmail.artemis.the.gr8.playerstats.utils; -import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.Statistic; import org.bukkit.entity.EntityType; @@ -64,22 +63,6 @@ public class EnumHandler { } } - public static String getItemKey(@NotNull String itemName, boolean logCC) throws IllegalArgumentException { - Material item = getItemEnum(itemName); - if (item.isBlock()) { - if (logCC) { - Bukkit.getLogger().info("Creative Category for Block " + item + " : " + item.getCreativeCategory()); - } - return "block.minecraft." + item.getKey().getKey(); - } - else { - if (logCC) { - Bukkit.getLogger().info("Creative Category for Item " + item + " : " + item.getCreativeCategory()); - } - return "item.minecraft." + getItemEnum(itemName).getKey().getKey(); - } - } - /** Checks whether the provided string is a valid entity */ public static boolean isEntity(@NotNull String entityName) { return entityNames.contains(entityName.toLowerCase()); @@ -102,10 +85,6 @@ public class EnumHandler { } } - public static String getEntityKey(@NotNull String entityName) throws IllegalArgumentException { - return "entity.minecraft." + getEntityEnum(entityName).getKey().getKey(); - } - /** Checks whether the provided string is a valid block @param materialName String, case-insensitive */ public static boolean isBlock(@NotNull String materialName) { @@ -130,10 +109,6 @@ public class EnumHandler { } } - public static String getBlockKey(String materialName) throws IllegalArgumentException { - return "block.minecraft." + getBlockEnum(materialName).getKey().getKey(); - } - /** Checks if string is a valid statistic @param statName String, case-insensitive */ public static boolean isStatistic(@NotNull String statName) { @@ -156,17 +131,6 @@ public class EnumHandler { } } - public static String getStatKey(@NotNull String statName) throws IllegalArgumentException { - Statistic stat = getStatEnum(statName); - if (stat.getType() == Statistic.Type.UNTYPED) { - return "stat.minecraft." + getStatEnum(statName).getKey().getKey(); - } - else { - return "stat_type.minecraft." + getStatEnum(statName).getKey().getKey(); - } - } - - /** Gets the type of the statistic from the string @param statName String, case-insensitive @return Statistic.Type */