Made hoverText with the full statNumber, with dynamic color, added config support for distance- and damage-units, started on adding unit to hoverText as well

This commit is contained in:
Artemis-the-gr8 2022-07-02 15:48:29 +02:00
parent 77808da3a3
commit a05a97617b
5 changed files with 104 additions and 24 deletions

View File

@ -93,14 +93,19 @@ public class ConfigHandler {
} }
public Unit getDistanceUnit() { public Unit getDistanceUnit() {
String unit = config.getString("distance-unit", "blocks"); return getUnitFromString(config.getString("distance-unit", "blocks"), Unit.BLOCK);
if (unit.equalsIgnoreCase("cm")) { }
return Unit.CM;
} else if (unit.equalsIgnoreCase("km")) { public Unit getDistanceUnitHoverText() {
return Unit.KM; return getUnitFromString(config.getString("distance-unit-hover-text", "km"), Unit.KM);
} else { }
return Unit.BLOCK;
} public Unit getDamageUnit() {
return getUnitFromString(config.getString("damage-unit", "hearts"), Unit.HEART);
}
public Unit getDamageUnitHoverText() {
return getUnitFromString(config.getString("damage-unit-hover-text", "hp"), Unit.HP);
} }
/** Whether to use TranslatableComponents for statistic, block, item and entity names. /** Whether to use TranslatableComponents for statistic, block, item and entity names.
@ -265,6 +270,32 @@ public class ConfigHandler {
return section != null ? section.getString(path, defaultValue) : null; return section != null ? section.getString(path, defaultValue) : null;
} }
private Unit getUnitFromString(String unitString, Unit defaultUnit) {
switch (unitString.toLowerCase()) {
case "cm" -> {
return Unit.CM;
}
case "m", "block", "blocks" -> {
return Unit.BLOCK;
}
case "mile", "miles" -> {
return Unit.MILE;
}
case "km" -> {
return Unit.KM;
}
case "hp" -> {
return Unit.HP;
}
case "heart", "hearts" -> {
return Unit.HEART;
}
default -> {
return defaultUnit;
}
}
}
/** Returns the config section that contains the relevant color or style option. */ /** Returns the config section that contains the relevant color or style option. */
private @Nullable ConfigurationSection getRelevantSection(Target selection) { private @Nullable ConfigurationSection getRelevantSection(Target selection) {
switch (selection) { switch (selection) {

View File

@ -3,6 +3,7 @@ package com.gmail.artemis.the.gr8.playerstats.enums;
public enum Unit { public enum Unit {
CM, CM,
BLOCK, BLOCK,
MILE,
KM, KM,
HP, HP,
HEART, HEART,

View File

@ -8,9 +8,11 @@ import com.gmail.artemis.the.gr8.playerstats.msg.msgutils.LanguageKeyHandler;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.TranslatableComponent; 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.NamedTextColor;
import net.kyori.adventure.text.format.TextColor; import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration; import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.util.HSVLike;
import net.kyori.adventure.util.Index; import net.kyori.adventure.util.Index;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -174,14 +176,16 @@ public class ComponentFactory {
.args(subStat)); .args(subStat));
} }
//TODO Make this dark gray (or at least darker than statNumber, and at least for time statistics)
public TextComponent statUnitComponent(String statName, Target selection) { public TextComponent statUnitComponent(String statName, Target selection) {
if (!statName.toLowerCase().contains("one_cm")) { if (!statName.toLowerCase().contains("one_cm") && !statName.toLowerCase().contains("damage")) {
return Component.empty(); return Component.empty();
} }
String key; String key;
switch (config.getDistanceUnit()) { switch (config.getDistanceUnit()) {
case CM -> key = "cm"; case CM -> key = "cm";
case KM -> key = "km"; case KM -> key = "km";
case MILE -> key = "Miles";
default -> key = config.useTranslatableComponents() ? languageKeyHandler.getDistanceKey() : "Blocks"; default -> key = config.useTranslatableComponents() ? languageKeyHandler.getDistanceKey() : "Blocks";
} }
return getComponentBuilder(null, return getComponentBuilder(null,
@ -195,9 +199,17 @@ public class ComponentFactory {
//TODO Add hoverComponent with full number //TODO Add hoverComponent with full number
public TextComponent statNumberComponent(long number, String statName, Target selection) { public TextComponent statNumberComponent(long number, String statName, Target selection) {
return getComponent(format.format(statName, number), TextColor baseColor = getColorFromString(config.getStatNumberFormatting(selection, false));
getColorFromString(config.getStatNumberFormatting(selection, false)), TextDecoration style = getStyleFromString(config.getStatNumberFormatting(selection, true));
getStyleFromString(config.getStatNumberFormatting(selection, true))); TextComponent.Builder statNumber = getComponentBuilder(format.formatMainNumber(statName, number), baseColor, style);
if (config.useHoverText()) {
statNumber.hoverEvent(HoverEvent.showText(getComponent(format.formatHoverNumber(statName, number),
getLighterColor(baseColor), style)
.append(space())
.append(statUnitComponent(statName, selection))));
}
return statNumber.build();
} }
public TextComponent titleComponent(String content, Target selection) { public TextComponent titleComponent(String content, Target selection) {
@ -271,6 +283,12 @@ public class ComponentFactory {
return names.value(textColor); return names.value(textColor);
} }
private TextColor getLighterColor(TextColor color) {
HSVLike oldColor = HSVLike.fromRGB(color.red(), color.green(), color.blue());
HSVLike newColor = HSVLike.hsvLike(oldColor.h(), 0.45F, oldColor.v());
return TextColor.color(newColor);
}
private @Nullable TextDecoration getStyleFromString(@NotNull String configString) { private @Nullable TextDecoration getStyleFromString(@NotNull String configString) {
if (configString.equalsIgnoreCase("none")) { if (configString.equalsIgnoreCase("none")) {
return null; return null;

View File

@ -19,15 +19,33 @@ public class NumberFormatter {
format.setGroupingSize(3); format.setGroupingSize(3);
} }
public String format(String statName, long number) { /** Turns the input number into a more readable format depending on its type
(number-or-times, time-, damage- or distance-based) according to the
corresponding config settings, and adds commas in groups of 3.*/
public String formatMainNumber(String statName, long number) {
return format(statName, number, false);
}
/** Turns the input number into a more readable format depending on its type
(number-or-times, time-, damage- or distance-based) according to the
corresponding config settings, and adds commas in groups of 3.*/
public String formatHoverNumber(String statName, long number) {
return format(statName, number, true);
}
/** Formats the input based on the desired config settings from *-unit or *-unit-hover-text.
@param statName the Statistic enum name in String format
@param number the statistic number
@param isHoverText boolean that indicates whether this number will be displayed in a HoverComponent or not*/
private String format(String statName, long number, boolean isHoverText) {
if (EnumHandler.isDistanceStatistic(statName)) { if (EnumHandler.isDistanceStatistic(statName)) {
return formatDistance(number); return formatDistance(number, isHoverText);
} }
else if (EnumHandler.isDamageStatistic(statName)) { else if (EnumHandler.isDamageStatistic(statName)) {
return formatDamage(number); return formatDamage(number, isHoverText);
} }
else if (EnumHandler.isTimeStatistic(statName)) { else if (EnumHandler.isTimeStatistic(statName)) {
return formatTime(number); return formatTime(number, isHoverText);
} }
else { else {
return format.format(number); return format.format(number);
@ -36,19 +54,26 @@ public class NumberFormatter {
/** The unit of damage-based statistics is half a heart by default. /** The unit of damage-based statistics is half a heart by default.
This method turns the number into hearts. */ This method turns the number into hearts. */
private String formatDamage(long number) { //7 statistics private String formatDamage(long number, boolean isHoverText) { //7 statistics
return format.format(Math.round(number / 2.0)); Unit unit = isHoverText ? config.getDamageUnitHoverText() : config.getDamageUnit();
if (unit == Unit.HEART) {
return format.format(Math.round(number / 2.0));
} else {
return format.format(number);
}
} }
//TODO add cm and km
/** The unit of distance-based statistics is cm by default. This method turns it into blocks by default, /** The unit of distance-based statistics is cm by default. This method turns it into blocks by default,
and turns it into km or leaves it as cm otherwise, depending on the config settings. */ and turns it into km or leaves it as cm otherwise, depending on the config settings. */
private String formatDistance(long number) { //15 statistics private String formatDistance(long number, boolean isHoverText) { //15 statistics
Unit unit = config.getDistanceUnit(); Unit unit = isHoverText ? config.getDistanceUnitHoverText() : config.getDistanceUnit();
switch (unit) { switch (unit) {
case CM -> { case CM -> {
return format.format(number); return format.format(number);
} }
case MILE -> {
return format.format(Math.round(number / 160900.0)); //to get from CM to Miles
}
case KM -> { case KM -> {
return format.format(Math.round(number / 100000.0)); //divide by 100 to get M, divide by 1000 to get KM return format.format(Math.round(number / 100000.0)); //divide by 100 to get M, divide by 1000 to get KM
} }
@ -59,7 +84,7 @@ public class NumberFormatter {
} }
/** The unit of time-based statistics is ticks by default.*/ /** The unit of time-based statistics is ticks by default.*/
private String formatTime(long number) { //5 statistics private String formatTime(long number, boolean isHoverText) { //5 statistics
if (number == 0) { if (number == 0) {
return "-"; return "-";
} }
@ -69,7 +94,7 @@ public class NumberFormatter {
if (leftover >= 86400) { if (leftover >= 86400) {
double days = leftover / 60 / 60 / 24; double days = leftover / 60 / 60 / 24;
if (days > 999) { if (days > 999) {
output.append(format.format(days)); output.append(format.format(Math.round(days)));
} }
else { else {
output.append(days); output.append(days);

View File

@ -28,8 +28,13 @@ number-of-days-since-last-joined: 0
# # ------------------------------- # # # # ------------------------------- # #
# The unit to display certain statistics in. # The unit to display certain statistics in.
# Minecraft measures distance in cm. PlayerStats supports the following units: blocks, cm, m (= blocks), km # Minecraft measures distance in cm. PlayerStats supports the following units: blocks, cm, m (= blocks), miles & km
distance-unit: blocks distance-unit: blocks
distance-unit-hover-text: km
# Minecraft measures damage in 0.5 hearts (1HP). PlayerStats supports the following units: hp, hearts
damage-unit: hearts
damage-unit-hover-text: hp
# Display all statistic, block, item and entity names in the client language of the receiving player # Display all statistic, block, item and entity names in the client language of the receiving player
# The actual translation is handled by the Minecraft language files and happens automatically # The actual translation is handled by the Minecraft language files and happens automatically