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;
import org.jetbrains.annotations.NotNull;
public enum Unit {
CM,
BLOCK,
MILE,
KM,
HP,
HEART,
TICK,
SECOND,
MINUTE,
HOUR,
DAY,
WEEK
NUMBER (Type.UNTYPED),
CM (Type.DISTANCE),
BLOCK (Type.DISTANCE),
MILE (Type.DISTANCE),
KM (Type.DISTANCE),
HP (Type.DAMAGE),
HEART (Type.DAMAGE),
TICK (Type.TIME),
SECOND (Type.TIME),
MINUTE (Type.TIME),
HOUR (Type.TIME),
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.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;
@ -28,13 +29,11 @@ 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(config);
}
/** Returns [PlayerStats]. */
@ -176,40 +175,48 @@ public class ComponentFactory {
.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) {
if (!statName.toLowerCase().contains("one_cm") && !statName.toLowerCase().contains("damage")) {
return Component.empty();
}
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.Builder statNumberComponent(String number, Target selection) {
return getComponentBuilder(number,
getColorFromString(config.getStatNumberFormatting(selection, false)),
getStyleFromString(config.getStatNumberFormatting(selection, true)));
}
//TODO Add hoverComponent with full number
public TextComponent statNumberComponent(long number, String statName, Target selection) {
public TextComponent statNumberHoverComponent(String mainNumber, String hoverNumber, Unit hoverUnit, Target selection, boolean isTranslatable) {
TextColor baseColor = getColorFromString(config.getStatNumberFormatting(selection, false));
TextDecoration style = 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))));
TextComponent.Builder hoverText = getComponentBuilder(hoverNumber, getLighterColor(baseColor), style);
if (isTranslatable) {
String unitKey = languageKeyHandler.getUnitKey(hoverUnit);
if (unitKey == null) {
unitKey = hoverUnit.getName();
}
hoverText.append(space())
.append(translatable().key(unitKey));
}
return statNumber.build();
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) {

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.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.statistic.StatRequest;
@ -24,9 +25,11 @@ public class MessageWriter {
private static ConfigHandler config;
private static ComponentFactory componentFactory;
private final NumberFormatter formatter;
public MessageWriter(ConfigHandler c) {
config = c;
formatter = new NumberFormatter(config);
getComponentFactory();
}
@ -125,6 +128,7 @@ public class MessageWriter {
boolean useDots = config.useDots();
boolean boldNames = config.playerNameIsBold();
boolean useHover = config.useHoverText();
Set<String> playerNames = topStats.keySet();
MinecraftFont font = new MinecraftFont();
@ -159,8 +163,12 @@ public class MessageWriter {
.append(text(":"))
.build());
}
topList.append(space())
.append(componentFactory.statNumberComponent(topStats.get(playerName), request.getStatistic().toString(), Target.TOP));
topList.append(space());
if (useHover) {
topList.append(
//componentFactory.statNumberHoverComponent(
// formatter.formatMainNumber(request.getStatistic().toString(), topStats.get(playerName)), Target.TOP));
}
}
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.
@param input String to prettify, case-insensitive*/
private String getPrettyName(String input) {
@ -208,18 +237,6 @@ public class MessageWriter {
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) {
return new ExampleMessage(componentFactory, isBukkitConsole);
}

View File

@ -1,5 +1,6 @@
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 org.bukkit.Material;
import org.bukkit.Statistic;
@ -63,8 +64,12 @@ public class LanguageKeyHandler {
}
}
public String getDistanceKey() {
return "soundCategory.block";
public @Nullable String getUnitKey(Unit unit) {
if (unit == Unit.BLOCK) {
return "soundCategory.block";
} else {
return null;
}
}
private void generateDefaultKeys() {