Reworked all languagekey-business to MessageWriter, fixed all statUnit-getting
This commit is contained in:
parent
f22fc6a7ed
commit
5b96f0b292
|
@ -20,10 +20,6 @@ public enum Unit {
|
|||
|
||||
private final Type type;
|
||||
|
||||
Unit() {
|
||||
this(Type.UNTYPED);
|
||||
}
|
||||
|
||||
Unit(Type type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
@ -80,7 +76,7 @@ public enum Unit {
|
|||
}
|
||||
}
|
||||
|
||||
public Unit fromString(String unitName) {
|
||||
public static @NotNull Unit fromString(String unitName) {
|
||||
switch (unitName.toLowerCase()) {
|
||||
case "cm" -> {
|
||||
return Unit.CM;
|
||||
|
|
|
@ -3,9 +3,6 @@ package com.gmail.artemis.the.gr8.playerstats.msg;
|
|||
import com.gmail.artemis.the.gr8.playerstats.config.ConfigHandler;
|
||||
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.enums.Unit;
|
||||
import com.gmail.artemis.the.gr8.playerstats.statistic.StatRequest;
|
||||
import com.gmail.artemis.the.gr8.playerstats.msg.msgutils.LanguageKeyHandler;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.TranslatableComponent;
|
||||
|
@ -28,12 +25,9 @@ import static net.kyori.adventure.text.Component.text;
|
|||
public class ComponentFactory {
|
||||
|
||||
private static ConfigHandler config;
|
||||
private final LanguageKeyHandler languageKeyHandler;
|
||||
|
||||
public ComponentFactory(ConfigHandler c) {
|
||||
config = c;
|
||||
|
||||
languageKeyHandler = new LanguageKeyHandler();
|
||||
}
|
||||
|
||||
/** Returns [PlayerStats]. */
|
||||
|
@ -105,21 +99,20 @@ public class ComponentFactory {
|
|||
}
|
||||
|
||||
/** Returns a TextComponent with TranslatableComponent as a child.*/
|
||||
public TextComponent statNameTransComponent(@NotNull StatRequest request) {
|
||||
public TextComponent statNameTransComponent(String statKey, String subStatKey, Target selection) {
|
||||
TextComponent.Builder totalStatNameBuilder = getComponentBuilder(null,
|
||||
getColorFromString(config.getStatNameDecoration(request.getSelection(), false)),
|
||||
getStyleFromString(config.getStatNameDecoration(request.getSelection(), true)));
|
||||
TextComponent subStat = subStatNameTransComponent(request);
|
||||
getColorFromString(config.getStatNameDecoration(selection, false)),
|
||||
getStyleFromString(config.getStatNameDecoration(selection, true)));
|
||||
|
||||
String statName = languageKeyHandler.getStatKey(request.getStatistic());
|
||||
if (statName.equalsIgnoreCase("stat_type.minecraft.killed")) {
|
||||
TextComponent subStat = subStatNameTransComponent(subStatKey, selection);
|
||||
if (statKey.equalsIgnoreCase("stat_type.minecraft.killed")) {
|
||||
return totalStatNameBuilder.append(killEntityBuilder(subStat)).build();
|
||||
}
|
||||
else if (statName.equalsIgnoreCase("stat_type.minecraft.killed_by")) {
|
||||
else if (statKey.equalsIgnoreCase("stat_type.minecraft.killed_by")) {
|
||||
return totalStatNameBuilder.append(entityKilledByBuilder(subStat)).build();
|
||||
}
|
||||
else {
|
||||
totalStatNameBuilder.append(translatable().key(statName));
|
||||
totalStatNameBuilder.append(translatable().key(statKey));
|
||||
if (!subStat.equals(Component.empty())) {
|
||||
totalStatNameBuilder.append(
|
||||
space().decorations(TextDecoration.NAMES.values(), false)
|
||||
|
@ -130,26 +123,16 @@ public class ComponentFactory {
|
|||
}
|
||||
|
||||
/** Returns a TranslatableComponent for the subStatName, or an empty component.*/
|
||||
private TextComponent subStatNameTransComponent(@NotNull StatRequest request) {
|
||||
if (request.getSubStatEntry() != null) {
|
||||
String subStatName = request.getSubStatEntry();
|
||||
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());
|
||||
default -> {
|
||||
}
|
||||
}
|
||||
if (subStatName != null) {
|
||||
return getComponentBuilder(null,
|
||||
getColorFromString(config.getSubStatNameDecoration(request.getSelection(), false)),
|
||||
getStyleFromString(config.getSubStatNameDecoration(request.getSelection(), true)))
|
||||
.append(text("("))
|
||||
.append(translatable()
|
||||
.key(subStatName))
|
||||
.append(text(")"))
|
||||
.build();
|
||||
}
|
||||
private TextComponent subStatNameTransComponent(String subStatKey, Target selection) {
|
||||
if (subStatKey != null) {
|
||||
return getComponentBuilder(null,
|
||||
getColorFromString(config.getSubStatNameDecoration(selection, false)),
|
||||
getStyleFromString(config.getSubStatNameDecoration(selection, true)))
|
||||
.append(text("("))
|
||||
.append(translatable()
|
||||
.key(subStatKey))
|
||||
.append(text(")"))
|
||||
.build();
|
||||
}
|
||||
return Component.empty();
|
||||
}
|
||||
|
@ -176,46 +159,40 @@ public class ComponentFactory {
|
|||
}
|
||||
|
||||
//TODO Add hoverComponent with full number
|
||||
public TextComponent.Builder statNumberComponent(String number, Target selection) {
|
||||
return getComponentBuilder(number,
|
||||
public TextComponent.Builder statNumberComponent(String prettyNumber, Target selection) {
|
||||
return getComponentBuilder(prettyNumber,
|
||||
getColorFromString(config.getStatNumberDecoration(selection, false)),
|
||||
getStyleFromString(config.getStatNumberDecoration(selection, true)));
|
||||
}
|
||||
|
||||
public TextComponent statNumberHoverComponent(String mainNumber, String hoverNumber, Unit hoverUnit, Target selection, boolean isTranslatable) {
|
||||
public TextComponent statNumberHoverComponent(String mainNumber, String hoverNumber, @Nullable String hoverUnitName, @Nullable String hoverUnitKey, Target selection) {
|
||||
TextColor baseColor = getColorFromString(config.getStatNumberDecoration(selection, false));
|
||||
TextDecoration style = getStyleFromString(config.getStatNumberDecoration(selection, true));
|
||||
|
||||
TextComponent.Builder hoverText = getComponentBuilder(hoverNumber, getLighterColor(baseColor), style);
|
||||
if (isTranslatable) {
|
||||
String unitKey = languageKeyHandler.getUnitKey(hoverUnit);
|
||||
if (unitKey == null) {
|
||||
unitKey = hoverUnit.getName();
|
||||
}
|
||||
if (hoverUnitKey != null) {
|
||||
hoverText.append(space())
|
||||
.append(translatable().key(unitKey));
|
||||
.append(translatable().key(hoverUnitKey));
|
||||
}
|
||||
else {
|
||||
else if (hoverUnitName != null) {
|
||||
hoverText.append(space())
|
||||
.append(text(hoverUnit.getName()));
|
||||
.append(text(hoverUnitName));
|
||||
}
|
||||
return getComponent(mainNumber, baseColor, style).hoverEvent(HoverEvent.showText(hoverText));
|
||||
}
|
||||
|
||||
//TODO Make this dark gray (or at least darker than statNumber, and at least for time statistics)
|
||||
public TextComponent statUnitComponent(Unit statUnit, Target selection, boolean isTranslatable) {
|
||||
if (statUnit.getType() != Unit.Type.UNTYPED) {
|
||||
public TextComponent statUnitComponent(String unitName, String unitKey, Target selection) {
|
||||
if (!(unitName == null && unitKey == null)) {
|
||||
TextComponent.Builder statUnitBuilder = getComponentBuilder(null,
|
||||
getColorFromString(config.getSubStatNameDecoration(selection, false)),
|
||||
getStyleFromString(config.getSubStatNameDecoration(selection, true)))
|
||||
.append(text("["));
|
||||
if (isTranslatable) {
|
||||
String unitKey = languageKeyHandler.getUnitKey(statUnit);
|
||||
if (unitKey == null) {
|
||||
unitKey = statUnit.getName();
|
||||
}
|
||||
statUnitBuilder.append(translatable().key(unitKey));
|
||||
if (unitKey != null) {
|
||||
statUnitBuilder.append(translatable()
|
||||
.key(unitKey));
|
||||
} else {
|
||||
statUnitBuilder.append(text(statUnit.getName()));
|
||||
statUnitBuilder.append(text(unitName));
|
||||
}
|
||||
return statUnitBuilder.append(text("]")).build();
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import com.gmail.artemis.the.gr8.playerstats.config.ConfigHandler;
|
|||
import com.gmail.artemis.the.gr8.playerstats.enums.Unit;
|
||||
import com.gmail.artemis.the.gr8.playerstats.msg.msgutils.ExampleMessage;
|
||||
import com.gmail.artemis.the.gr8.playerstats.msg.msgutils.HelpMessage;
|
||||
import com.gmail.artemis.the.gr8.playerstats.msg.msgutils.LanguageKeyHandler;
|
||||
import com.gmail.artemis.the.gr8.playerstats.statistic.StatRequest;
|
||||
import com.gmail.artemis.the.gr8.playerstats.utils.MyLogger;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
@ -25,11 +26,13 @@ public class MessageWriter {
|
|||
|
||||
private static ConfigHandler config;
|
||||
private static ComponentFactory componentFactory;
|
||||
private final LanguageKeyHandler languageKeyHandler;
|
||||
private final NumberFormatter formatter;
|
||||
|
||||
public MessageWriter(ConfigHandler c) {
|
||||
config = c;
|
||||
formatter = new NumberFormatter();
|
||||
languageKeyHandler = new LanguageKeyHandler();
|
||||
getComponentFactory();
|
||||
}
|
||||
|
||||
|
@ -111,11 +114,7 @@ public class MessageWriter {
|
|||
.append(getStatNumberComponent(request.getStatistic(), stat, Target.PLAYER))
|
||||
.append(space())
|
||||
.append(getStatNameComponent(request))
|
||||
.append(space())
|
||||
.append(componentFactory.statUnitComponent(
|
||||
config.getStatUnit(Unit.fromStatistic(request.getStatistic()), false),
|
||||
Target.PLAYER,
|
||||
config.useTranslatableComponents()))
|
||||
.append(getStatUnitComponent(request.getStatistic(), request.getSelection()))
|
||||
.build();
|
||||
}
|
||||
|
||||
|
@ -126,11 +125,7 @@ public class MessageWriter {
|
|||
.append(componentFactory.titleComponent(config.getTopStatsTitle(), Target.TOP)).append(space())
|
||||
.append(componentFactory.titleNumberComponent(topStats.size())).append(space())
|
||||
.append(getStatNameComponent(request))
|
||||
.append(space())
|
||||
.append(componentFactory.statUnitComponent(
|
||||
config.getStatUnit(Unit.fromStatistic(request.getStatistic()), false),
|
||||
Target.TOP,
|
||||
config.useTranslatableComponents()));
|
||||
.append(getStatUnitComponent(request.getStatistic(), request.getSelection()));
|
||||
|
||||
boolean useDots = config.useDots();
|
||||
boolean boldNames = config.playerNameIsBold();
|
||||
|
@ -182,11 +177,7 @@ public class MessageWriter {
|
|||
.append(getStatNumberComponent(request.getStatistic(), stat, Target.SERVER))
|
||||
.append(space())
|
||||
.append(getStatNameComponent(request))
|
||||
.append(space())
|
||||
.append(componentFactory.statUnitComponent(
|
||||
config.getStatUnit(Unit.fromStatistic(request.getStatistic()), false),
|
||||
Target.SERVER,
|
||||
config.useTranslatableComponents()))
|
||||
.append(getStatUnitComponent(request.getStatistic(), request.getSelection())) //space is provided by statUnit
|
||||
.build();
|
||||
}
|
||||
|
||||
|
@ -194,8 +185,20 @@ public class MessageWriter {
|
|||
the statName (and potential subStatName), or a TextComponent with capitalized English names.*/
|
||||
private TextComponent getStatNameComponent(StatRequest request) {
|
||||
if (config.useTranslatableComponents()) {
|
||||
return componentFactory.statNameTransComponent(request);
|
||||
} else {
|
||||
String statKey = languageKeyHandler.getStatKey(request.getStatistic());
|
||||
String subStatKey = request.getSubStatEntry();
|
||||
if (subStatKey != null) {
|
||||
switch (request.getStatistic().getType()) {
|
||||
case BLOCK -> subStatKey = languageKeyHandler.getBlockKey(request.getBlock());
|
||||
case ENTITY -> subStatKey = languageKeyHandler.getEntityKey(request.getEntity());
|
||||
case ITEM -> subStatKey = languageKeyHandler.getItemKey(request.getItem());
|
||||
default -> {
|
||||
}
|
||||
}
|
||||
}
|
||||
return componentFactory.statNameTransComponent(statKey, subStatKey, request.getSelection());
|
||||
}
|
||||
else {
|
||||
return componentFactory.statNameTextComponent(
|
||||
getPrettyName(request.getStatistic().toString()),
|
||||
getPrettyName(request.getSubStatEntry()),
|
||||
|
@ -205,17 +208,67 @@ public class MessageWriter {
|
|||
|
||||
private TextComponent getStatNumberComponent(Statistic statistic, long statNumber, Target selection) {
|
||||
Unit.Type type = Unit.fromStatistic(statistic);
|
||||
Unit baseUnit = config.getStatUnit(type, false);
|
||||
String prettyNumber = formatter.format(statNumber, baseUnit);
|
||||
if (config.useHoverText() && type != Unit.Type.UNTYPED) {
|
||||
Unit hoverUnit = config.getStatUnit(type, true);
|
||||
return componentFactory.statNumberHoverComponent(
|
||||
prettyNumber,
|
||||
formatter.format(statNumber, hoverUnit),
|
||||
hoverUnit, selection, config.useTranslatableComponents());
|
||||
} else {
|
||||
Unit statUnit;
|
||||
switch (type) {
|
||||
case DISTANCE -> statUnit = Unit.fromString(config.getDistanceUnit(false));
|
||||
case DAMAGE -> statUnit = Unit.fromString(config.getDamageUnit(false));
|
||||
case TIME -> {
|
||||
return getTimeNumberComponent(statNumber, selection);
|
||||
}
|
||||
default -> statUnit = Unit.NUMBER;
|
||||
}
|
||||
String prettyNumber = formatter.format(statNumber, statUnit);
|
||||
if (!config.useHoverText() || statUnit == Unit.NUMBER) {
|
||||
return componentFactory.statNumberComponent(prettyNumber, selection).build();
|
||||
}
|
||||
Unit hoverUnit = type == Unit.Type.DISTANCE ? Unit.fromString(config.getDistanceUnit(true)) :
|
||||
Unit.fromString(config.getDamageUnit(true));
|
||||
String prettyHoverNumber = formatter.format(statNumber, hoverUnit);
|
||||
if (config.useTranslatableComponents()) {
|
||||
String unitKey = languageKeyHandler.getUnitKey(hoverUnit);
|
||||
if (unitKey == null) {
|
||||
unitKey = hoverUnit.getName();
|
||||
}
|
||||
return componentFactory.statNumberHoverComponent(prettyNumber, prettyHoverNumber, null, unitKey, selection);
|
||||
}
|
||||
else {
|
||||
return componentFactory.statNumberHoverComponent(prettyNumber, prettyHoverNumber, hoverUnit.getName(), null, selection);
|
||||
}
|
||||
}
|
||||
|
||||
private TextComponent getTimeNumberComponent(long statNumber, Target selection) {
|
||||
Unit max = Unit.fromString(config.getTimeUnit(false));
|
||||
Unit min = Unit.fromString(config.getTimeUnit(false, true));
|
||||
String mainNumber = formatter.format(statNumber, max, min);
|
||||
if (!config.useHoverText()) {
|
||||
return componentFactory.statNumberComponent(mainNumber, selection).build();
|
||||
} else {
|
||||
Unit hoverMax = Unit.fromString(config.getTimeUnit(true));
|
||||
Unit hoverMin = Unit.fromString(config.getTimeUnit(true, true));
|
||||
return componentFactory.statNumberHoverComponent(mainNumber,
|
||||
formatter.format(statNumber, hoverMax, hoverMin),
|
||||
null, null, selection); //Time does not support translatable text,
|
||||
} //because the unit and number are so tightly interwoven.
|
||||
}
|
||||
|
||||
private TextComponent getStatUnitComponent(Statistic statistic, Target selection) {
|
||||
Unit statUnit;
|
||||
switch (Unit.fromStatistic(statistic)) {
|
||||
case DAMAGE -> statUnit = Unit.fromString(config.getDamageUnit(false));
|
||||
case DISTANCE -> statUnit = Unit.fromString(config.getDistanceUnit(false));
|
||||
default -> {
|
||||
return Component.empty();
|
||||
}
|
||||
}
|
||||
if (config.useTranslatableComponents()) {
|
||||
String unitKey = languageKeyHandler.getUnitKey(statUnit);
|
||||
if (unitKey != null) {
|
||||
return Component.space()
|
||||
.append(componentFactory.statUnitComponent(null, unitKey, selection));
|
||||
}
|
||||
}
|
||||
return Component.space()
|
||||
.append(componentFactory.statUnitComponent(statUnit.getName(), null, selection));
|
||||
}
|
||||
|
||||
/** Returns "block", "entity", "item", or "sub-statistic" if the provided Type is null. */
|
||||
|
|
|
@ -18,19 +18,24 @@ public class NumberFormatter {
|
|||
(number-of-times, time-, damage- or distance-based) according to the
|
||||
corresponding config settings, and adds commas in groups of 3.*/
|
||||
public String format(long number, Unit statUnit) {
|
||||
switch (statUnit.getType()) {
|
||||
case DISTANCE -> {
|
||||
return formatDistance(number, statUnit);
|
||||
}
|
||||
case DAMAGE -> {
|
||||
return formatDamage(number, statUnit);
|
||||
}
|
||||
case TIME -> {
|
||||
return formatTime(number, statUnit);
|
||||
}
|
||||
default -> {
|
||||
return format.format(number);
|
||||
return format(number, statUnit, null);
|
||||
}
|
||||
|
||||
public String format(long number, Unit statUnit, Unit timeMinimumUnit) {
|
||||
if (timeMinimumUnit == null) {
|
||||
switch (statUnit.getType()) {
|
||||
case DISTANCE -> {
|
||||
return formatDistance(number, statUnit);
|
||||
}
|
||||
case DAMAGE -> {
|
||||
return formatDamage(number, statUnit);
|
||||
}
|
||||
default -> {
|
||||
return format.format(number);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return formatTime(number, statUnit, timeMinimumUnit);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -63,8 +68,9 @@ public class NumberFormatter {
|
|||
}
|
||||
}
|
||||
|
||||
//TODO work in min-max
|
||||
/** The unit of time-based statistics is ticks by default.*/
|
||||
private String formatTime(long number, Unit statUnit) { //5 statistics
|
||||
private String formatTime(long number, Unit statUnit, Unit timeMinimumUnit) { //5 statistics
|
||||
if (number == 0) {
|
||||
return "-";
|
||||
}
|
||||
|
@ -74,25 +80,25 @@ public class NumberFormatter {
|
|||
if (leftover >= 86400) {
|
||||
double days = leftover / 60 / 60 / 24;
|
||||
output.append(format.format(Math.round(days)))
|
||||
.append("D ");
|
||||
.append("d ");
|
||||
leftover = leftover % (60 * 60 * 24);
|
||||
}
|
||||
if (leftover >= 3600) {
|
||||
double hours = leftover / 60 / 60;
|
||||
output.append(format.format(Math.round(hours)))
|
||||
.append("H ");
|
||||
.append("h ");
|
||||
leftover = leftover % (60 * 60);
|
||||
}
|
||||
if (leftover >= 60) {
|
||||
double minutes = leftover / 60;
|
||||
output.append(format.format(Math.round(minutes)))
|
||||
.append("M ");
|
||||
.append("m ");
|
||||
leftover = leftover % 60;
|
||||
}
|
||||
if (leftover > 0) {
|
||||
output.append(format.format(Math.round(leftover)))
|
||||
.append("S");
|
||||
.append("s");
|
||||
}
|
||||
return output.toString();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue