Reworked MessageFactory to work with a StatRequest, and reworked LanguageKeyHandler to only work with Enums. Now the only place a String is converted into an Enum is inside the StatCommand. Also added method isValid to StatRequest and changed selection to SERVER for "me" in console

This commit is contained in:
Artemis-the-gr8 2022-06-27 13:54:14 +02:00
parent fcb5283982
commit 6253627846
6 changed files with 121 additions and 101 deletions

View File

@ -73,22 +73,25 @@ public class StatCommand implements CommandExecutor {
} }
} }
//check for selection //check for selection
else if (request.getSelection() == null) { else if (arg.equalsIgnoreCase("top")) {
if (arg.equalsIgnoreCase("top")) { request.setSelection(Target.TOP);
request.setSelection(Target.TOP); }
} else if (arg.equalsIgnoreCase("server")) {
else if (arg.equalsIgnoreCase("server")) { request.setSelection(Target.SERVER);
request.setSelection(Target.SERVER); }
} else if (arg.equalsIgnoreCase("me")) {
else if (arg.equalsIgnoreCase("me") && sender instanceof Player) { if (sender instanceof Player) {
request.setPlayerName(sender.getName()); request.setPlayerName(sender.getName());
request.setSelection(Target.PLAYER); request.setSelection(Target.PLAYER);
} }
else if (OfflinePlayerHandler.isRelevantPlayer(arg) && request.getPlayerName() == null) { else if (sender instanceof ConsoleCommandSender) {
request.setPlayerName(arg); request.setSelection(Target.SERVER);
request.setSelection(Target.PLAYER);
} }
} }
else if (OfflinePlayerHandler.isRelevantPlayer(arg) && request.getPlayerName() == null) {
request.setPlayerName(arg);
request.setSelection(Target.PLAYER);
}
} }
patchRequest(request); patchRequest(request);
return request; return request;
@ -105,6 +108,9 @@ public class StatCommand implements CommandExecutor {
if (type == Statistic.Type.ENTITY && request.getSubStatEntry() == null) { if (type == Statistic.Type.ENTITY && request.getSubStatEntry() == null) {
request.setSubStatEntry("player"); request.setSubStatEntry("player");
} }
else {
request.setSelection(Target.PLAYER);
}
} }
String subStatEntry = request.getSubStatEntry(); String subStatEntry = request.getSubStatEntry();
@ -125,10 +131,6 @@ public class StatCommand implements CommandExecutor {
if (subStatEntry != null) request.setSubStatEntry(null); if (subStatEntry != null) request.setSubStatEntry(null);
} }
} }
if (request.getSelection() == null) { //assume top as default
request.setSelection(Target.TOP);
}
} }
} }

View File

@ -28,39 +28,39 @@ public class LanguageKeyHandler {
} }
} }
/** Get the official Key from the NameSpacedKey for the entityType corresponding to this entityName, /** Get the official Key from the NameSpacedKey for this entityType,
or return null if no enum constant can be retrieved.*/ or return null if no enum constant can be retrieved or entityType is UNKNOWN.*/
public @Nullable String getEntityKey(@NotNull String entityName) { public @Nullable String getEntityKey(EntityType entity) {
if (entityName.equalsIgnoreCase("UNKNOWN")) { if (entity == null || entity == EntityType.UNKNOWN) return null;
return null; else {
return "entity.minecraft." + entity.getKey().getKey();
} }
EntityType entity = EnumHandler.getEntityEnum(entityName);
return (entity != null) ? "entity.minecraft." + entity.getKey().getKey() : null;
} }
/** Get the official Key from the NameSpacedKey for the Material corresponding to this itemName, /** Get the official Key from the NameSpacedKey for this item Material,
or return null if no enum constant can be retrieved.*/ or return null if no enum constant can be retrieved.*/
public @Nullable String getItemKey(@NotNull String itemName) { public @Nullable String getItemKey(Material item) {
Material item = EnumHandler.getItemEnum(itemName); if (item == null) return null;
if (item == null) { else if (item.isBlock()) {
return null; return getBlockKey(item);
}
if (item.isBlock()) {
return "block.minecraft." + item.getKey().getKey();
} }
else { else {
return "item.minecraft." + item.getKey().getKey(); return "item.minecraft." + item.getKey().getKey();
} }
} }
/** Get the official Key from the NameSpacedKey for the Material corresponding to this blockName, /** Returns the official Key from the NameSpacedKey for the block Material provided,
or return null if no enum constant can be retrieved.*/ or return null if no enum constant can be retrieved.*/
public @Nullable String getBlockKey(@NotNull String blockName) { public @Nullable String getBlockKey(Material block) {
if (blockName.toLowerCase().contains("wall_banner")) { if (block == null) return null;
blockName = blockName.replace("wall_", ""); 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_", "");
Material newBlock = EnumHandler.getBlockEnum(blockName);
return (newBlock != null) ? "block.minecraft." + newBlock.getKey().getKey() : null;
}
else {
return "block.minecraft." + block.getKey().getKey();
} }
Material block = EnumHandler.getBlockEnum(blockName);
return (block != null) ? "block.minecraft." + block.getKey().getKey() : null;
} }
private void generateDefaultKeys() { private void generateDefaultKeys() {

View File

@ -2,6 +2,7 @@ package com.gmail.artemis.the.gr8.playerstats.msg;
import com.gmail.artemis.the.gr8.playerstats.enums.Target; import com.gmail.artemis.the.gr8.playerstats.enums.Target;
import com.gmail.artemis.the.gr8.playerstats.config.ConfigHandler; import com.gmail.artemis.the.gr8.playerstats.config.ConfigHandler;
import com.gmail.artemis.the.gr8.playerstats.statistic.StatRequest;
import com.gmail.artemis.the.gr8.playerstats.utils.MyLogger; import com.gmail.artemis.the.gr8.playerstats.utils.MyLogger;
import com.gmail.artemis.the.gr8.playerstats.utils.NumberFormatter; import com.gmail.artemis.the.gr8.playerstats.utils.NumberFormatter;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
@ -157,19 +158,22 @@ public class MessageFactory {
.append(newline()); .append(newline());
} }
public TextComponent formatPlayerStat(String playerName, Statistic statistic, String subStatEntry, int stat) { public TextComponent formatPlayerStat(int stat, @NotNull StatRequest request) {
if (!request.isValid()) return unknownError(request.isConsoleSender());
return Component.text() return Component.text()
.append(playerNameComponent(Target.PLAYER, playerName + ": ")) .append(playerNameComponent(Target.PLAYER, request.getPlayerName() + ": "))
.append(statNumberComponent(Target.PLAYER, stat)) .append(statNumberComponent(Target.PLAYER, stat))
.append(space()) .append(space())
.append(statNameComponent(Target.PLAYER, statistic, subStatEntry)) .append(statNameComponent(request))
.append(space()) .append(space())
.build(); .build();
} }
public TextComponent formatTopStats(@NotNull LinkedHashMap<String, Integer> topStats, Statistic statistic, String subStatEntry, boolean isConsoleSender) { public TextComponent formatTopStats(@NotNull LinkedHashMap<String, Integer> topStats, @NotNull StatRequest request) {
if (!request.isValid()) return unknownError(request.isConsoleSender());
TextComponent.Builder topList = Component.text(); TextComponent.Builder topList = Component.text();
topList.append(getTopStatTitle(topStats.size(), statistic, subStatEntry, isConsoleSender)); topList.append(getTopStatTitle(topStats.size(), request));
boolean useDots = config.useDots(); boolean useDots = config.useDots();
Set<String> playerNames = topStats.keySet(); Set<String> playerNames = topStats.keySet();
@ -187,7 +191,7 @@ public class MessageFactory {
topList.append(space()); topList.append(space());
int dots = (int) Math.round((130.0 - font.getWidth(count + ". " + playerName))/2); int dots = (int) Math.round((130.0 - font.getWidth(count + ". " + playerName))/2);
if (isConsoleSender) { if (request.isConsoleSender()) {
dots = (int) Math.round((130.0 - font.getWidth(count + ". " + playerName))/6) + 7; dots = (int) Math.round((130.0 - font.getWidth(count + ". " + playerName))/6) + 7;
} }
else if (config.playerNameIsBold()) { else if (config.playerNameIsBold()) {
@ -205,7 +209,8 @@ public class MessageFactory {
return topList.build(); return topList.build();
} }
public TextComponent formatServerStat(Statistic statistic, String subStatEntry, long stat) { public TextComponent formatServerStat(long stat, @NotNull StatRequest request) {
if (!request.isValid()) return unknownError(request.isConsoleSender());
return Component.text() return Component.text()
.append(titleComponent(Target.SERVER, config.getServerTitle())) .append(titleComponent(Target.SERVER, config.getServerTitle()))
.append(space()) .append(space())
@ -213,7 +218,7 @@ public class MessageFactory {
.append(space()) .append(space())
.append(statNumberComponent(Target.SERVER, stat)) .append(statNumberComponent(Target.SERVER, stat))
.append(space()) .append(space())
.append(statNameComponent(Target.SERVER, statistic, subStatEntry)) .append(statNameComponent(request))
.append(space()) .append(space())
.build(); .build();
} }
@ -233,15 +238,15 @@ public class MessageFactory {
.append(text(underscores)); .append(text(underscores));
} }
protected TextComponent getTopStatTitle(int topLength, @NotNull Statistic statistic, String subStatEntry, boolean isConsoleSender) { protected TextComponent getTopStatTitle(int topLength, @NotNull StatRequest request) {
return Component.text() return Component.text()
.append(newline()) .append(newline())
.append(pluginPrefix(isConsoleSender)) .append(pluginPrefix(request.isConsoleSender()))
.append(titleComponent(Target.TOP, config.getTopStatsTitle())) .append(titleComponent(Target.TOP, config.getTopStatsTitle()))
.append(space()) .append(space())
.append(titleNumberComponent(topLength)) .append(titleNumberComponent(topLength))
.append(space()) .append(space())
.append(statNameComponent(Target.TOP, statistic, subStatEntry)) .append(statNameComponent(request))
.append(space()) .append(space())
.build(); .build();
} }
@ -252,26 +257,28 @@ public class MessageFactory {
getStyleFromString(config.getPlayerNameFormatting(selection, true))); getStyleFromString(config.getPlayerNameFormatting(selection, true)));
} }
protected TranslatableComponent statNameComponent(Target selection, @NotNull Statistic statistic, String subStatName) { protected TranslatableComponent statNameComponent(@NotNull StatRequest request) {
TextColor statNameColor = getColorFromString(config.getStatNameFormatting(selection, false)); if (request.getStatistic() == null) return null;
TextDecoration statNameStyle = getStyleFromString(config.getStatNameFormatting(selection, true)); TextColor statNameColor = getColorFromString(config.getStatNameFormatting(request.getSelection(), false));
TextDecoration statNameStyle = getStyleFromString(config.getStatNameFormatting(request.getSelection(), true));
String statName; String statName = request.getStatistic().name();
String subStatName = request.getSubStatEntry();
if (!config.useTranslatableComponents()) { if (!config.useTranslatableComponents()) {
statName = getPrettyName(statistic.name().toLowerCase()); statName = getPrettyName(statName);
subStatName = getPrettyName(subStatName); subStatName = getPrettyName(subStatName);
} }
else { else {
statName = language.getStatKey(statistic); statName = language.getStatKey(request.getStatistic());
switch (statistic.getType()) { switch (request.getStatistic().getType()) {
case BLOCK -> subStatName = language.getBlockKey(subStatName); case BLOCK -> subStatName = language.getBlockKey(request.getBlock());
case ENTITY -> subStatName = language.getEntityKey(subStatName); case ENTITY -> subStatName = language.getEntityKey(request.getEntity());
case ITEM -> subStatName = language.getItemKey(subStatName); case ITEM -> subStatName = language.getItemKey(request.getItem());
case UNTYPED -> { case UNTYPED -> {
} }
} }
} }
TextComponent subStat = subStatNameComponent(selection, subStatName); TextComponent subStat = subStatNameComponent(request.getSelection(), subStatName);
TranslatableComponent.Builder totalName; TranslatableComponent.Builder totalName;
if (statName.equalsIgnoreCase("stat_type.minecraft.killed") && subStat != null) { if (statName.equalsIgnoreCase("stat_type.minecraft.killed") && subStat != null) {
@ -291,22 +298,24 @@ public class MessageFactory {
.build(); .build();
} }
/** Construct a custom translation for kill_entity */ /** Construct a custom translation for kill_entity with the language key for commands.kill.success.single ("Killed %s").
private TranslatableComponent.@NotNull Builder killEntityComponent(TextComponent subStat) { @return a TranslatableComponent Builder with the subStat Component as args.*/
TranslatableComponent.Builder totalName = translatable() private TranslatableComponent.Builder killEntityComponent(@NotNull TextComponent subStat) {
.key("commands.kill.success.single"); //"Killed %s" return translatable()
.key("commands.kill.success.single") //"Killed %s"
if (subStat != null) totalName.args(subStat); .args(subStat);
return totalName;
} }
/** Construct a custom translation for entity_killed_by */ /** Construct a custom translation for entity_killed_by with the language keys for stat.minecraft.deaths
private TranslatableComponent.@NotNull Builder entityKilledByComponent(@NotNull TextComponent subStat) { ("Number of Deaths") and book.byAuthor ("by %s").
@return a TranslatableComponent Builder with stat.minecraft.deaths as key, with a ChildComponent
with book.byAuthor as key and the subStat Component as args.*/
private TranslatableComponent.Builder entityKilledByComponent(@NotNull TextComponent subStat) {
return translatable() return translatable()
.key("stat.minecraft.deaths") //"Number of Deaths" .key("stat.minecraft.deaths") //"Number of Deaths"
.append(space()) .append(space())
.append(translatable() .append(translatable()
.key("book.byAuthor") .key("book.byAuthor") //"by %s"
.args(subStat)); .args(subStat));
} }
@ -371,10 +380,11 @@ public class MessageFactory {
return style == null ? text(content).color(color) : text(content).color(color).decoration(style, TextDecoration.State.TRUE); return style == null ? text(content).color(color) : text(content).color(color).decoration(style, TextDecoration.State.TRUE);
} }
/** Replace "_" with " " and capitalize each first letter of the input.*/ /** Replace "_" with " " and capitalize each first letter of the input.
@param input String to prettify, case-insensitive*/
private String getPrettyName(String input) { private String getPrettyName(String input) {
if (input == null) return null; if (input == null) return null;
StringBuilder capitals = new StringBuilder(input); StringBuilder capitals = new StringBuilder(input.toLowerCase());
capitals.setCharAt(0, Character.toUpperCase(capitals.charAt(0))); capitals.setCharAt(0, Character.toUpperCase(capitals.charAt(0)));
while (capitals.indexOf("_") != -1) { while (capitals.indexOf("_") != -1) {
MyLogger.replacingUnderscores(); MyLogger.replacingUnderscores();

View File

@ -4,6 +4,7 @@ import com.gmail.artemis.the.gr8.playerstats.enums.Target;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Statistic; import org.bukkit.Statistic;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -21,16 +22,41 @@ public class StatRequest {
private Material item; private Material item;
private boolean playerFlag; private boolean playerFlag;
//playerFlag is set to false by default, will be set to true if "player" is in the args //make a StatRequest for a given CommandSender with some default values
public StatRequest(@NotNull CommandSender s) { public StatRequest(@NotNull CommandSender s) {
sender = s; sender = s;
selection = Target.TOP;
playerFlag = false; playerFlag = false;
} }
public CommandSender getCommandSender() { /** Returns true if this StatRequest has all the information needed for a Statistic lookup to succeed.*/
public boolean isValid() {
if (statistic == null) return false;
switch (statistic.getType()) {
case BLOCK -> {
if (block == null) return false;
}
case ENTITY -> {
if (entity == null) return false;
}
case ITEM -> {
if (item == null) return false;
}
case UNTYPED -> {
if (subStatEntry != null) return false;
}
} //if target = PLAYER and playerName = null, return false, otherwise return true
return selection != Target.PLAYER || playerName != null;
}
public @NotNull CommandSender getCommandSender() {
return sender; return sender;
} }
public boolean isConsoleSender() {
return sender instanceof ConsoleCommandSender;
}
public void setStatistic(Statistic statistic) { public void setStatistic(Statistic statistic) {
this.statistic = statistic; this.statistic = statistic;
} }
@ -73,7 +99,7 @@ public class StatRequest {
this.selection = selection; this.selection = selection;
} }
public Target getSelection() { public @NotNull Target getSelection() {
return selection; return selection;
} }

View File

@ -76,9 +76,6 @@ public class StatThread extends Thread {
CommandSender sender = request.getCommandSender(); CommandSender sender = request.getCommandSender();
boolean isConsoleSencer = sender instanceof ConsoleCommandSender; boolean isConsoleSencer = sender instanceof ConsoleCommandSender;
Statistic statistic = request.getStatistic();
String playerName = request.getPlayerName();
String subStatEntry = request.getSubStatEntry();
Target selection = request.getSelection(); Target selection = request.getSelection();
if (selection == Target.TOP || selection == Target.SERVER) { if (selection == Target.TOP || selection == Target.SERVER) {
@ -91,12 +88,10 @@ public class StatThread extends Thread {
try { try {
if (selection == Target.TOP) { if (selection == Target.TOP) {
adventure.sender(sender).sendMessage(messageFactory.formatTopStats( adventure.sender(sender).sendMessage(messageFactory.formatTopStats(getTopStats(), request));
getTopStats(), statistic, subStatEntry, sender instanceof ConsoleCommandSender));
} }
else { else {
adventure.sender(sender).sendMessage(messageFactory.formatServerStat( adventure.sender(sender).sendMessage(messageFactory.formatServerStat(getServerTotal(), request));
statistic, subStatEntry, getServerTotal()));
} }
} catch (ConcurrentModificationException e) { } catch (ConcurrentModificationException e) {
@ -112,8 +107,7 @@ public class StatThread extends Thread {
else if (selection == Target.PLAYER) { else if (selection == Target.PLAYER) {
try { try {
adventure.sender(sender).sendMessage( adventure.sender(sender).sendMessage(
messageFactory.formatPlayerStat( messageFactory.formatPlayerStat(getIndividualStat(), request));
playerName, statistic, subStatEntry, getIndividualStat()));
} catch (UnsupportedOperationException | NullPointerException e) { } catch (UnsupportedOperationException | NullPointerException e) {
adventure.sender(sender).sendMessage(messageFactory.formatExceptions(e.toString(), isConsoleSencer)); adventure.sender(sender).sendMessage(messageFactory.formatExceptions(e.toString(), isConsoleSencer));

View File

@ -59,12 +59,6 @@ public class EnumHandler {
private EnumHandler() { private EnumHandler() {
} }
/** Checks whether the provided string is a valid item
@param itemName String, case-insensitive */
public static boolean isItem(@NotNull String itemName) {
return itemNames.contains(itemName.toLowerCase());
}
/** Returns all item names in lowercase */ /** Returns all item names in lowercase */
public static List<String> getItemNames() { public static List<String> getItemNames() {
return itemNames; return itemNames;
@ -74,12 +68,10 @@ public class EnumHandler {
@param itemName String, case-insensitive @param itemName String, case-insensitive
@return Material enum constant, uppercase */ @return Material enum constant, uppercase */
public static @Nullable Material getItemEnum(String itemName) { public static @Nullable Material getItemEnum(String itemName) {
return Material.matchMaterial(itemName); if (itemName == null) return null;
}
/** Checks whether the provided string is a valid entity */ Material item = Material.matchMaterial(itemName);
public static boolean isEntity(@NotNull String entityName) { return (item != null && item.isItem()) ? item : null;
return entityNames.contains(entityName.toLowerCase());
} }
/** Returns all entitytype names in lowercase */ /** Returns all entitytype names in lowercase */
@ -99,12 +91,6 @@ public class EnumHandler {
} }
} }
/** Checks whether the provided string is a valid block
@param materialName String, case-insensitive */
public static boolean isBlock(@NotNull String materialName) {
return blockNames.contains(materialName.toLowerCase());
}
/** Returns all block names in lowercase */ /** Returns all block names in lowercase */
public static List<String> getBlockNames() { public static List<String> getBlockNames() {
return blockNames; return blockNames;
@ -114,8 +100,10 @@ public class EnumHandler {
@param materialName String, case-insensitive @param materialName String, case-insensitive
@return Material enum constant, uppercase */ @return Material enum constant, uppercase */
public static @Nullable Material getBlockEnum(String materialName) { public static @Nullable Material getBlockEnum(String materialName) {
return Material.matchMaterial(materialName); if (materialName == null) return null;
Material block = Material.matchMaterial(materialName);
return (block != null && block.isBlock()) ? block : null;
} }
/** Checks if string is a valid statistic /** Checks if string is a valid statistic
@ -150,4 +138,4 @@ public class EnumHandler {
public static List<String> getEntitySubStatNames() { public static List<String> getEntitySubStatNames() {
return entitySubStatNames; return entitySubStatNames;
} }
} }