Set up translatableComponents for all items, blocks, entities and custom statNames, and started working on killed/killed_by solution (#54, #43)

This commit is contained in:
Artemis-the-gr8 2022-06-21 17:35:46 +02:00
parent effe7e5523
commit f1cda8a7dd
6 changed files with 213 additions and 91 deletions

View File

@ -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

View File

@ -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)))
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"));

View File

@ -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<Statistic, String> 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");
}
}

View File

@ -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<String, Integer> topStats, String statName, String subStatEntryName, boolean isConsoleSender) {
public TextComponent formatTopStats(@NotNull LinkedHashMap<String, Integer> 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<String> 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));

View File

@ -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

View File

@ -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 */