Gave distance-, time- and damage-based statistics their own formatting (#75, #74, #73)

This commit is contained in:
Artemis-the-gr8 2022-06-30 15:10:52 +02:00
parent 054c8b6d7c
commit 5cf477614f
5 changed files with 103 additions and 41 deletions

View File

@ -5,7 +5,6 @@ import com.gmail.artemis.the.gr8.playerstats.enums.PluginColor;
import com.gmail.artemis.the.gr8.playerstats.enums.Target;
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.NumberFormatter;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.TranslatableComponent;
@ -15,6 +14,7 @@ import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.util.Index;
import org.bukkit.Bukkit;
import org.bukkit.Statistic;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -27,9 +27,14 @@ import static net.kyori.adventure.text.Component.text;
public class ComponentFactory {
private static ConfigHandler config;
private final LanguageKeyHandler languageKeyHandler;
private final NumberFormatter format;
public ComponentFactory(ConfigHandler c) {
config = c;
languageKeyHandler = new LanguageKeyHandler();
format = new NumberFormatter();
}
/** Returns [PlayerStats] followed by a single space. */
@ -188,11 +193,11 @@ public class ComponentFactory {
subStatName = getPrettyName(subStatName);
}
else {
statName = LanguageKeyHandler.getStatKey(request.getStatistic());
statName = languageKeyHandler.getStatKey(request.getStatistic());
switch (request.getStatistic().getType()) {
case BLOCK -> subStatName = LanguageKeyHandler.getBlockKey(request.getBlock());
case ENTITY -> subStatName = LanguageKeyHandler.getEntityKey(request.getEntity());
case ITEM -> subStatName = LanguageKeyHandler.getItemKey(request.getItem());
case BLOCK -> subStatName = languageKeyHandler.getBlockKey(request.getBlock());
case ENTITY -> subStatName = languageKeyHandler.getEntityKey(request.getEntity());
case ITEM -> subStatName = languageKeyHandler.getItemKey(request.getItem());
case UNTYPED -> {
}
}
@ -263,8 +268,9 @@ public class ComponentFactory {
.args(subStat));
}
public TextComponent statNumber(long number, Target selection) {
return createComponent(NumberFormatter.format(number),
//TODO Add hoverComponent with full number
public TextComponent statNumber(long number, Statistic statistic, Target selection) {
return createComponent(format.format(statistic, number),
getColorFromString(config.getStatNumberFormatting(selection, false)),
getStyleFromString(config.getStatNumberFormatting(selection, true)));
}

View File

@ -12,17 +12,14 @@ import java.util.HashMap;
public class LanguageKeyHandler {
private final static HashMap<Statistic, String> statNameKeys;
private final HashMap<Statistic, String> statNameKeys;
static {
public LanguageKeyHandler() {
statNameKeys = new HashMap<>();
generateStatNameKeys();
}
private LanguageKeyHandler() {
}
public static String getStatKey(@NotNull Statistic statistic) {
public String getStatKey(@NotNull Statistic statistic) {
if (statistic.getType() == Statistic.Type.UNTYPED) {
return "stat.minecraft." + statNameKeys.get(statistic);
}
@ -33,7 +30,7 @@ public class LanguageKeyHandler {
/** Get the official Key from the NameSpacedKey for this entityType,
or return null if no enum constant can be retrieved or entityType is UNKNOWN.*/
public static @Nullable String getEntityKey(EntityType entity) {
public @Nullable String getEntityKey(EntityType entity) {
if (entity == null || entity == EntityType.UNKNOWN) return null;
else {
return "entity.minecraft." + entity.getKey().getKey();
@ -42,7 +39,7 @@ public class LanguageKeyHandler {
/** Get the official Key from the NameSpacedKey for this item Material,
or return null if no enum constant can be retrieved.*/
public static @Nullable String getItemKey(Material item) {
public @Nullable String getItemKey(Material item) {
if (item == null) return null;
else if (item.isBlock()) {
return getBlockKey(item);
@ -54,7 +51,7 @@ public class LanguageKeyHandler {
/** Returns the official Key from the NameSpacedKey for the block Material provided,
or return null if no enum constant can be retrieved.*/
public static @Nullable String getBlockKey(Material block) {
public @Nullable String getBlockKey(Material block) {
if (block == null) return null;
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_", "");
@ -66,11 +63,11 @@ public class LanguageKeyHandler {
}
}
private static void generateDefaultKeys() {
private void generateDefaultKeys() {
Arrays.stream(Statistic.values()).forEach(statistic -> statNameKeys.put(statistic, statistic.toString().toLowerCase()));
}
private static void generateStatNameKeys() {
private void generateStatNameKeys() {
//get the enum names for all statistics first
generateDefaultKeys();

View File

@ -93,7 +93,7 @@ public class MessageWriter {
if (!request.isValid()) return unknownError(request.isBukkitConsoleSender());
return Component.text()
.append(componentFactory.playerName( request.getPlayerName() + ": ", Target.PLAYER))
.append(componentFactory.statNumber(stat, Target.PLAYER))
.append(componentFactory.statNumber(stat, request.getStatistic(), Target.PLAYER))
.append(space())
.append(componentFactory.statName(request))
.append(space())
@ -143,7 +143,7 @@ public class MessageWriter {
else {
topList.append(componentFactory.playerName(":", Target.TOP));
}
topList.append(space()).append(componentFactory.statNumber(topStats.get(playerName), Target.TOP));
topList.append(space()).append(componentFactory.statNumber(topStats.get(playerName), request.getStatistic(), Target.TOP));
}
return topList.build();
}
@ -155,7 +155,7 @@ public class MessageWriter {
.append(space())
.append(componentFactory.serverName(config.getServerName()))
.append(space())
.append(componentFactory.statNumber(stat, Target.SERVER))
.append(componentFactory.statNumber(stat, request.getStatistic(), Target.SERVER))
.append(space())
.append(componentFactory.statName(request))
.append(space())

View File

@ -0,0 +1,79 @@
package com.gmail.artemis.the.gr8.playerstats.msg;
import org.bukkit.Statistic;
import java.text.DecimalFormat;
public class NumberFormatter {
private final DecimalFormat format;
public NumberFormatter() {
format = new DecimalFormat();
format.setGroupingUsed(true);
format.setGroupingSize(3);
}
//TODO deal with unit name after number (add "blocks", etc in appropriate places)
public String format(Statistic statistic, long number) {
String statName = statistic.toString().toLowerCase();
if (statName.contains("one_cm")) {
return formatDistance(number); //language-key: "soundCategory.block": "Blocks",
}
else if (statName.contains("damage")) {
return formatDamage(number);
}
else if (statName.contains("time") || statName.contains("one_minute")) {
return formatTime(number);
}
else {
return format.format(number);
}
}
/** The unit of damage-based statistics is 1/10th of a health point (half a heart) by default.
That would be 1/20th of a heart. This method turns the number into hearts. */
private String formatDamage(long number) { //7 statistics
return format.format(number / 20);
}
/** The unit of distance-based statistics is cm by default. This method turns it into blocks. */
private String formatDistance(long number) { //15 statistics
return format.format(number / 100);
}
/** The unit of time-based statistics is ticks by default.*/
private String formatTime(long number) { //5 statistics
if (number == 0) {
return "-";
}
StringBuilder output = new StringBuilder();
long leftover = number / 20;
if (leftover >= 86400) {
long days = leftover / 60 / 60 / 24;
if (days > 999) {
output.append(format.format(days));
}
else {
output.append(days);
}
output.append("D ");
leftover = leftover % (60 * 60 * 24);
}
if (leftover >= 3600) {
long hours = leftover / 60 / 60;
output.append(hours).append("H ");
leftover = leftover % (60 * 60);
}
if (leftover >= 60) {
long minutes = leftover / 60;
output.append(minutes).append("M ");
leftover = leftover % 60;
}
if (leftover > 0) {
output.append(leftover).append("S");
}
return output.toString();
}
}

View File

@ -1,20 +0,0 @@
package com.gmail.artemis.the.gr8.playerstats.utils;
import java.text.DecimalFormat;
public class NumberFormatter {
private static final DecimalFormat format;
static{
format = new DecimalFormat();
format.setGroupingUsed(true);
format.setGroupingSize(3);
}
private NumberFormatter(){
}
public static String format(long number) {
return format.format(number);
}
}