started working with Unit.Type as a sub-enum

This commit is contained in:
Artemis-the-gr8 2022-07-02 22:01:47 +02:00
parent a05a97617b
commit f723a657e7
4 changed files with 161 additions and 59 deletions

View File

@ -1,16 +1,89 @@
package com.gmail.artemis.the.gr8.playerstats.enums; package com.gmail.artemis.the.gr8.playerstats.enums;
import org.jetbrains.annotations.NotNull;
public enum Unit { public enum Unit {
CM, NUMBER (Type.UNTYPED),
BLOCK, CM (Type.DISTANCE),
MILE, BLOCK (Type.DISTANCE),
KM, MILE (Type.DISTANCE),
HP, KM (Type.DISTANCE),
HEART, HP (Type.DAMAGE),
TICK, HEART (Type.DAMAGE),
SECOND, TICK (Type.TIME),
MINUTE, SECOND (Type.TIME),
HOUR, MINUTE (Type.TIME),
DAY, HOUR (Type.TIME),
WEEK DAY (Type.TIME),
WEEK (Type.TIME);
Unit(Type type) {
}
public static @NotNull Type getType(String statName) {
String name = statName.toLowerCase();
if (name.contains("one_cm")) {
return Type.DISTANCE;
} else if (name.contains("damage")) {
return Type.DAMAGE;
} else if (name.contains("time") || name.contains("one_minute")) {
return Type.TIME;
} else {
return Type.UNTYPED;
}
}
public @NotNull String getName() {
switch (this) {
case CM -> {
return "cm";
}
case BLOCK -> {
return "Blocks";
}
case MILE -> {
return "Miles";
}
case KM -> {
return "km";
}
case HP -> {
return "HP";
}
case HEART -> {
return "Hearts";
}
case TICK -> {
return "ticks";
}
case SECOND -> {
return "seconds";
}
case MINUTE -> {
return "minutes";
}
case DAY -> {
return "days";
}
case HOUR -> {
return "hours";
}
case WEEK -> {
return "weeks";
}
default ->
throw new NullPointerException("Trying to get the name of an enum constant that does not exist!");
}
}
public static enum Type{
DAMAGE, //7 statistics
DISTANCE, //15 statistics
TIME, //5 statistics
UNTYPED;
private Type() {
}
}
} }

View File

@ -3,6 +3,7 @@ package com.gmail.artemis.the.gr8.playerstats.msg;
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.enums.PluginColor; 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.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.statistic.StatRequest;
import com.gmail.artemis.the.gr8.playerstats.msg.msgutils.LanguageKeyHandler; import com.gmail.artemis.the.gr8.playerstats.msg.msgutils.LanguageKeyHandler;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
@ -28,13 +29,11 @@ public class ComponentFactory {
private static ConfigHandler config; private static ConfigHandler config;
private final LanguageKeyHandler languageKeyHandler; private final LanguageKeyHandler languageKeyHandler;
private final NumberFormatter format;
public ComponentFactory(ConfigHandler c) { public ComponentFactory(ConfigHandler c) {
config = c; config = c;
languageKeyHandler = new LanguageKeyHandler(); languageKeyHandler = new LanguageKeyHandler();
format = new NumberFormatter(config);
} }
/** Returns [PlayerStats]. */ /** Returns [PlayerStats]. */
@ -176,40 +175,48 @@ public class ComponentFactory {
.args(subStat)); .args(subStat));
} }
//TODO Make this dark gray (or at least darker than statNumber, and at least for time statistics) //TODO Add hoverComponent with full number
public TextComponent statUnitComponent(String statName, Target selection) { public TextComponent.Builder statNumberComponent(String number, Target selection) {
if (!statName.toLowerCase().contains("one_cm") && !statName.toLowerCase().contains("damage")) { return getComponentBuilder(number,
return Component.empty(); getColorFromString(config.getStatNumberFormatting(selection, false)),
} getStyleFromString(config.getStatNumberFormatting(selection, true)));
String key;
switch (config.getDistanceUnit()) {
case CM -> key = "cm";
case KM -> key = "km";
case MILE -> key = "Miles";
default -> key = config.useTranslatableComponents() ? languageKeyHandler.getDistanceKey() : "Blocks";
}
return getComponentBuilder(null,
getColorFromString(config.getSubStatNameFormatting(selection, false)),
getStyleFromString(config.getSubStatNameFormatting(selection, true)))
.append(text("["))
.append(translatable(key))
.append(text("]"))
.build();
} }
//TODO Add hoverComponent with full number public TextComponent statNumberHoverComponent(String mainNumber, String hoverNumber, Unit hoverUnit, Target selection, boolean isTranslatable) {
public TextComponent statNumberComponent(long number, String statName, Target selection) {
TextColor baseColor = getColorFromString(config.getStatNumberFormatting(selection, false)); TextColor baseColor = getColorFromString(config.getStatNumberFormatting(selection, false));
TextDecoration style = getStyleFromString(config.getStatNumberFormatting(selection, true)); TextDecoration style = getStyleFromString(config.getStatNumberFormatting(selection, true));
TextComponent.Builder statNumber = getComponentBuilder(format.formatMainNumber(statName, number), baseColor, style); TextComponent.Builder hoverText = getComponentBuilder(hoverNumber, getLighterColor(baseColor), style);
if (isTranslatable) {
if (config.useHoverText()) { String unitKey = languageKeyHandler.getUnitKey(hoverUnit);
statNumber.hoverEvent(HoverEvent.showText(getComponent(format.formatHoverNumber(statName, number), if (unitKey == null) {
getLighterColor(baseColor), style) unitKey = hoverUnit.getName();
.append(space())
.append(statUnitComponent(statName, selection))));
} }
return statNumber.build(); hoverText.append(space())
.append(translatable().key(unitKey));
}
else {
hoverText.append(space())
.append(text(hoverUnit.getName()));
}
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) {
TextComponent.Builder statUnitBuilder = getComponentBuilder(null,
getColorFromString(config.getSubStatNameFormatting(selection, false)),
getStyleFromString(config.getSubStatNameFormatting(selection, true)))
.append(text("["));
if (isTranslatable) {
String unitKey = languageKeyHandler.getUnitKey(statUnit);
if (unitKey == null) {
unitKey = statUnit.getName();
}
statUnitBuilder.append(translatable().key(unitKey));
} else {
statUnitBuilder.append(text(statUnit.getName()));
}
return statUnitBuilder.append(text("]")).build();
} }
public TextComponent titleComponent(String content, Target selection) { public TextComponent titleComponent(String content, Target selection) {

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.enums.Unit;
import com.gmail.artemis.the.gr8.playerstats.msg.msgutils.ExampleMessage; 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.HelpMessage;
import com.gmail.artemis.the.gr8.playerstats.statistic.StatRequest; import com.gmail.artemis.the.gr8.playerstats.statistic.StatRequest;
@ -24,9 +25,11 @@ public class MessageWriter {
private static ConfigHandler config; private static ConfigHandler config;
private static ComponentFactory componentFactory; private static ComponentFactory componentFactory;
private final NumberFormatter formatter;
public MessageWriter(ConfigHandler c) { public MessageWriter(ConfigHandler c) {
config = c; config = c;
formatter = new NumberFormatter(config);
getComponentFactory(); getComponentFactory();
} }
@ -125,6 +128,7 @@ public class MessageWriter {
boolean useDots = config.useDots(); boolean useDots = config.useDots();
boolean boldNames = config.playerNameIsBold(); boolean boldNames = config.playerNameIsBold();
boolean useHover = config.useHoverText();
Set<String> playerNames = topStats.keySet(); Set<String> playerNames = topStats.keySet();
MinecraftFont font = new MinecraftFont(); MinecraftFont font = new MinecraftFont();
@ -159,8 +163,12 @@ public class MessageWriter {
.append(text(":")) .append(text(":"))
.build()); .build());
} }
topList.append(space()) topList.append(space());
.append(componentFactory.statNumberComponent(topStats.get(playerName), request.getStatistic().toString(), Target.TOP)); if (useHover) {
topList.append(
//componentFactory.statNumberHoverComponent(
// formatter.formatMainNumber(request.getStatistic().toString(), topStats.get(playerName)), Target.TOP));
}
} }
return topList.build(); return topList.build();
} }
@ -192,6 +200,27 @@ public class MessageWriter {
} }
} }
private Component getStatNumberComponent(String statName, long statNumber) {
Unit.getType(statName);
if (config.useHoverText()) {
return componentFactory.statNumberHoverComponent(formatter.formatMainNumber(statName, statNumber),
formatter.formatHoverNumber(statName, statNumber),
//something like config.getUnit that calls a bunch of smaller specific getUnit methods)
}
}
/** Returns "block", "entity", "item", or "sub-statistic" if the provided Type is null. */
private String getSubStatTypeName(Statistic.Type statType) {
String subStat = "sub-statistic";
if (statType == null) return subStat;
switch (statType) {
case BLOCK -> subStat = "block";
case ENTITY -> subStat = "entity";
case ITEM -> subStat = "item";
}
return subStat;
}
/** 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*/ @param input String to prettify, case-insensitive*/
private String getPrettyName(String input) { private String getPrettyName(String input) {
@ -208,18 +237,6 @@ public class MessageWriter {
return capitals.toString(); return capitals.toString();
} }
/** Returns "block", "entity", "item", or "sub-statistic" if the provided Type is null. */
private String getSubStatTypeName(Statistic.Type statType) {
String subStat = "sub-statistic";
if (statType == null) return subStat;
switch (statType) {
case BLOCK -> subStat = "block";
case ENTITY -> subStat = "entity";
case ITEM -> subStat = "item";
}
return subStat;
}
public TextComponent usageExamples(boolean isBukkitConsole) { public TextComponent usageExamples(boolean isBukkitConsole) {
return new ExampleMessage(componentFactory, isBukkitConsole); return new ExampleMessage(componentFactory, isBukkitConsole);
} }

View File

@ -1,5 +1,6 @@
package com.gmail.artemis.the.gr8.playerstats.msg.msgutils; package com.gmail.artemis.the.gr8.playerstats.msg.msgutils;
import com.gmail.artemis.the.gr8.playerstats.enums.Unit;
import com.gmail.artemis.the.gr8.playerstats.utils.EnumHandler; import com.gmail.artemis.the.gr8.playerstats.utils.EnumHandler;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Statistic; import org.bukkit.Statistic;
@ -63,8 +64,12 @@ public class LanguageKeyHandler {
} }
} }
public String getDistanceKey() { public @Nullable String getUnitKey(Unit unit) {
if (unit == Unit.BLOCK) {
return "soundCategory.block"; return "soundCategory.block";
} else {
return null;
}
} }
private void generateDefaultKeys() { private void generateDefaultKeys() {