Experimented with NameSpacedKeys for entity/block/item/stat-names, got TranslatableComponents to work for everything except statNames

This commit is contained in:
Artemis-the-gr8 2022-06-21 10:49:07 +02:00
parent 1bf8acf6ee
commit effe7e5523
6 changed files with 182 additions and 17 deletions

View File

@ -32,6 +32,7 @@
<artifactSet>
<excludes>
<exclude>org.jetbrains:annotations</exclude>
<exclude>META-INF/versions/**</exclude>
</excludes>
</artifactSet>
</configuration>

View File

@ -95,6 +95,7 @@
<artifactSet>
<excludes>
<exclude>org.jetbrains:annotations</exclude>
<exclude>META-INF/versions/**</exclude>
</excludes>
</artifactSet>
</configuration>

View File

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

View File

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

View File

@ -94,6 +94,7 @@ public class StatThread extends Thread {
}
} catch (Exception e) {
adventure.sender(sender).sendMessage(messageFactory.formatExceptions(e.toString(), isConsoleSencer));
e.printStackTrace();
}
}

View File

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