From effe7e552300402ea569abfdc3d46777f41be198 Mon Sep 17 00:00:00 2001 From: Artemis-the-gr8 Date: Tue, 21 Jun 2022 10:49:07 +0200 Subject: [PATCH] Experimented with NameSpacedKeys for entity/block/item/stat-names, got TranslatableComponents to work for everything except statNames --- dependency-reduced-pom.xml | 1 + pom.xml | 1 + .../gr8/playerstats/commands/StatCommand.java | 85 +++++++++++++++++++ .../gr8/playerstats/msg/MessageFactory.java | 75 ++++++++++++---- .../gr8/playerstats/statistic/StatThread.java | 1 + .../gr8/playerstats/utils/EnumHandler.java | 36 ++++++++ 6 files changed, 182 insertions(+), 17 deletions(-) diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml index b5e4073..75dafb4 100644 --- a/dependency-reduced-pom.xml +++ b/dependency-reduced-pom.xml @@ -32,6 +32,7 @@ org.jetbrains:annotations + META-INF/versions/** diff --git a/pom.xml b/pom.xml index b381ce9..217a328 100644 --- a/pom.xml +++ b/pom.xml @@ -95,6 +95,7 @@ org.jetbrains:annotations + META-INF/versions/** 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 f39feef..1ef48a8 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 @@ -7,7 +7,11 @@ import com.gmail.artemis.the.gr8.playerstats.statistic.StatRequest; import com.gmail.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler; import com.gmail.artemis.the.gr8.playerstats.msg.MessageFactory; import net.kyori.adventure.platform.bukkit.BukkitAudiences; +import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextComponent; +import net.kyori.adventure.text.TranslatableComponent; +import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.format.TextColor; import org.bukkit.Statistic; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -16,6 +20,9 @@ import org.bukkit.command.ConsoleCommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; +import static net.kyori.adventure.text.Component.space; +import static net.kyori.adventure.text.Component.text; + public class StatCommand implements CommandExecutor { @@ -45,6 +52,12 @@ public class StatCommand implements CommandExecutor { adventure.sender(sender).sendMessage(messageFactory.usageExamples(sender instanceof ConsoleCommandSender)); return true; } + else if (args[0].equalsIgnoreCase("test")) { + String selection = (args.length > 1) ? args[1] : null; + boolean extra = (args.length > 2); + printTranslatableNames(sender, selection, extra); + return true; + } else { //part 1: collecting all relevant information from the args StatRequest request = generateRequest(sender, args); @@ -60,6 +73,78 @@ public class StatCommand implements CommandExecutor { } } + //test method + private void printTranslatableNames(CommandSender sender, String selection, boolean extra) { + 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))) + .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))) + .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("stat")) { + try { + for (String name : EnumHandler.getStatNames()) { + TranslatableComponent msg = Component.translatable((EnumHandler.getStatKey(name))) + .color(TextColor.fromHexString("#FFB80E")) + .append(space()) + .append(text("for statName: ").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 { + TextComponent msg = Component.text("hi :)").color(TextColor.fromHexString("#FFB80E")); + adventure.sender(sender).sendMessage(msg); + } + } + //create a StatRequest Object with all the relevant information from the args private StatRequest generateRequest(CommandSender sender, String[] args) { StatRequest request = new StatRequest(sender); 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 2ef1735..ffa2589 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 @@ -2,8 +2,10 @@ package com.gmail.artemis.the.gr8.playerstats.msg; import com.gmail.artemis.the.gr8.playerstats.enums.Query; import com.gmail.artemis.the.gr8.playerstats.config.ConfigHandler; +import com.gmail.artemis.the.gr8.playerstats.utils.EnumHandler; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextComponent; +import net.kyori.adventure.text.TranslatableComponent; import net.kyori.adventure.text.event.HoverEvent; import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.TextColor; @@ -159,9 +161,11 @@ public class MessageFactory { .append(statNumberComponent(Query.PLAYER, stat)) .append(space()) .append(statNameComponent(Query.PLAYER, statName)) - .append(space()) - .append(subStatNameComponent(Query.PLAYER, subStatEntryName)); + .append(space()); + if (subStatNameComponent(Query.PLAYER, subStatEntryName) != null) { + singleStat.append(subStatNameComponent(Query.PLAYER, subStatEntryName)); + } return singleStat.build(); } @@ -212,9 +216,11 @@ public class MessageFactory { .append(statNumberComponent(Query.SERVER, stat)) .append(space()) .append(statNameComponent(Query.SERVER, statName)) - .append(space()) - .append(subStatNameComponent(Query.SERVER, subStatEntry)); + .append(space()); + if (subStatNameComponent(Query.SERVER, subStatEntry) != null) { + serverStat.append(subStatNameComponent(Query.SERVER, subStatEntry)); + } return serverStat.build(); } @@ -234,12 +240,17 @@ public class MessageFactory { } protected TextComponent getTopStatTitle(int topLength, String statName, String subStatEntryName, boolean isConsoleSender) { - return Component.newline() + 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(subStatNameComponent(Query.TOP, subStatEntryName)); + .append(statNameComponent(Query.TOP, statName)).append(space()); + + if (subStatNameComponent(Query.TOP, subStatEntryName) != null) { + topStat.append(subStatNameComponent(Query.TOP, subStatEntryName)); + } + return topStat.build(); } protected TextComponent playerNameComponent(Query selection, String playerName) { @@ -248,21 +259,51 @@ public class MessageFactory { getStyleFromString(config.getPlayerNameFormatting(selection, true))); } - protected TextComponent statNameComponent(Query selection, @NotNull String statName) { - return getComponent(statName.toLowerCase().replace("_", " "), - getColorFromString(config.getStatNameFormatting(selection, false)), - getStyleFromString(config.getStatNameFormatting(selection, true))); + protected TranslatableComponent statNameComponent(Query selection, @NotNull String statName) { + TextDecoration style = getStyleFromString(config.getStatNameFormatting(selection, true)); + String name = EnumHandler.getStatKey(statName); + if (style != null) { + return Component.translatable( + name, + getColorFromString(config.getStatNameFormatting(selection, false)), + style); + } else { + return Component.translatable(name, + getColorFromString(config.getStatNameFormatting(selection, false))); + } } - protected TextComponent subStatNameComponent(Query selection, String subStatName) { + protected TranslatableComponent subStatNameComponent(Query selection, @Nullable String subStatName) { if (subStatName == null) { - return empty(); + return null; + } + String name = null; + if (EnumHandler.isEntity(subStatName)){ + name = EnumHandler.getEntityKey(subStatName); + } + else if (EnumHandler.isBlock(subStatName)) { + name = EnumHandler.getBlockKey(subStatName); + } + else if (EnumHandler.isItem(subStatName)) { + name = EnumHandler.getItemKey(subStatName, false); + } + if (name != null) { + TextDecoration style = getStyleFromString(config.getSubStatNameFormatting(selection, true)); + if (style != null) { + return Component.translatable( + name, + getColorFromString(config.getSubStatNameFormatting(selection, false)), + style) + .append(space()); + } else { + return Component.translatable( + name, + getColorFromString(config.getSubStatNameFormatting(selection, false))) + .append(space()); + } } else { - return getComponent("(" + subStatName.toLowerCase().replace("_", " ") + ")", - getColorFromString(config.getSubStatNameFormatting(selection, false)), - getStyleFromString(config.getSubStatNameFormatting(selection, true))) - .append(space()); + return null; } } diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatThread.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatThread.java index 57ade52..c135eb5 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatThread.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatThread.java @@ -94,6 +94,7 @@ public class StatThread extends Thread { } } catch (Exception e) { adventure.sender(sender).sendMessage(messageFactory.formatExceptions(e.toString(), isConsoleSencer)); + e.printStackTrace(); } } 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 d1fd98f..80216a0 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,5 +1,6 @@ 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; @@ -63,6 +64,22 @@ 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()); @@ -85,6 +102,10 @@ 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) { @@ -109,6 +130,10 @@ 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) { @@ -131,6 +156,17 @@ 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 */