Split some utility methods into their own class (StringUtils and FontUtils), did some minor general code improvement

This commit is contained in:
Artemis-the-gr8 2022-07-11 18:59:13 +02:00
parent 80a5f17079
commit dca4968a91
8 changed files with 93 additions and 90 deletions

View File

@ -59,7 +59,8 @@ public class TabCompleter implements org.bukkit.command.TabCompleter {
//if previous arg = "player", suggest playerNames
else if (previousArg.equalsIgnoreCase("player")) {
if (args.length >= 3 && EnumHandler.getEntityTypeStatNames().contains(args[args.length-3].toLowerCase())) {
//if args.length-3 is kill_entity or entity_killed_by
if (args.length >= 3 && EnumHandler.isEntityStatistic(args[args.length-3])) {
tabSuggestions = commandOptions;
}
else {

View File

@ -10,8 +10,8 @@ import java.util.stream.Collectors;
public class TabCompleteHelper {
private List<String> itemBrokenSuggestions;
private List<String> entityKilledSuggestions;
private static List<String> itemBrokenSuggestions;
private static List<String> entityKilledSuggestions;
public TabCompleteHelper() {
prepareLists();
@ -34,7 +34,7 @@ public class TabCompleteHelper {
}
private void prepareLists() {
private static void prepareLists() {
itemBrokenSuggestions = Arrays.stream(Material.values())
.parallel()
.filter(Material::isItem)

View File

@ -4,16 +4,14 @@ import com.gmail.artemis.the.gr8.playerstats.enums.DebugLevel;
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.msg.msgutils.LanguageKeyHandler;
import com.gmail.artemis.the.gr8.playerstats.msg.msgutils.*;
import com.gmail.artemis.the.gr8.playerstats.statistic.StatRequest;
import com.gmail.artemis.the.gr8.playerstats.utils.EnumHandler;
import com.gmail.artemis.the.gr8.playerstats.utils.MyLogger;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.Statistic;
import org.bukkit.map.MinecraftFont;
import org.jetbrains.annotations.NotNull;
import java.util.*;
@ -82,7 +80,7 @@ public class MessageWriter {
return componentFactory.pluginPrefixComponent(isBukkitConsole)
.append(space())
.append(componentFactory.messageComponent().content(
"Please add a valid " + getSubStatTypeName(statType) + " to look up this statistic!"));
"Please add a valid " + EnumHandler.getSubStatTypeName(statType) + " to look up this statistic!"));
}
public TextComponent missingPlayerName(boolean isBukkitConsole) {
@ -96,7 +94,7 @@ public class MessageWriter {
return componentFactory.pluginPrefixComponent(isBukkitConsole)
.append(space())
.append(componentFactory.messageComponent().content(
"\"" + subStatEntry + "\" is not a valid " + getSubStatTypeName(statType) + "!"));
"\"" + subStatEntry + "\" is not a valid " + EnumHandler.getSubStatTypeName(statType) + "!"));
}
public TextComponent requestAlreadyRunning(boolean isBukkitConsole) {
@ -135,46 +133,28 @@ public class MessageWriter {
.append(getStatNameComponent(request)) //space is provided by statUnitComponent
.append(getStatUnitComponent(request.getStatistic(), request.getSelection(), request.isConsoleSender()));
ArrayList<Unit> timeUnits = null;
if (Unit.getTypeFromStatistic(request.getStatistic()) == Unit.Type.TIME) {
timeUnits = getTimeUnitRange(topStats.values().iterator().next());
}
boolean useDots = config.useDots();
boolean boldNames = config.playerNameIsBold();
MinecraftFont font = new MinecraftFont();
Set<String> playerNames = topStats.keySet();
int count = 0;
for (String playerName : playerNames) {
TextComponent.Builder playerNameBuilder = componentFactory.playerNameBuilder(playerName, Target.TOP);
count++;
topList.append(newline())
.append(componentFactory.rankingNumberComponent(count + "."))
.append(componentFactory.rankingNumberComponent(++count + "."))
.append(space());
if (useDots) {
topList.append(playerNameBuilder)
.append(space());
TextComponent.Builder dotsBuilder = componentFactory.dotsBuilder();
int dots;
if (request.isConsoleSender()) {
dots = (int) Math.round((130.0 - font.getWidth(count + ". " + playerName))/6) + 7;
} else if (!boldNames) {
dots = (int) Math.round((130.0 - font.getWidth(count + ". " + playerName))/2);
} else {
dots = (int) Math.round((130.0 - font.getWidth(count + ". ") - (font.getWidth(playerName) * 1.19))/2);
}
int dots = FontUtils.getNumberOfDotsToAlign(count + ". " + playerName, request.isConsoleSender(), boldNames);
if (dots >= 1) {
topList.append(dotsBuilder.append(text((".".repeat(dots)))));
topList.append(componentFactory.dotsBuilder().append(text((".".repeat(dots)))));
}
} else {
}
else {
topList.append(playerNameBuilder.append(text(":")));
}
if (timeUnits != null) {
topList.append(space()).append(getTimeNumberComponent(topStats.get(playerName), request.getSelection(), timeUnits));
} else {
topList.append(space()).append(getStatNumberComponent(request.getStatistic(), topStats.get(playerName), Target.TOP, request.isConsoleSender()));
}
topList.append(space()).append(getStatNumberComponent(request.getStatistic(), topStats.get(playerName), Target.TOP, request.isConsoleSender()));
}
return topList.build();
}
@ -192,6 +172,17 @@ public class MessageWriter {
.build();
}
public TextComponent usageExamples(boolean isBukkitConsole) {
return new ExampleMessage(componentFactory, isBukkitConsole);
}
public TextComponent helpMsg(boolean isConsoleSender) {
return new HelpMessage(componentFactory,
config.useHoverText() && !isConsoleSender,
isConsoleSender && Bukkit.getName().equalsIgnoreCase("CraftBukkit"),
config.getTopListMaxSize());
}
/** Depending on the config settings, return either a TranslatableComponent representing
the statName (and potential subStatName), or a TextComponent with capitalized English names.*/
private TextComponent getStatNameComponent(StatRequest request) {
@ -211,8 +202,8 @@ public class MessageWriter {
}
else {
return componentFactory.statNameTextComponent(
getPrettyName(request.getStatistic().toString()),
getPrettyName(request.getSubStatEntry()),
StringUtils.prettify(request.getStatistic().toString()),
StringUtils.prettify(request.getSubStatEntry()),
request.getSelection());
}
}
@ -326,43 +317,4 @@ public class MessageWriter {
return Component.space()
.append(componentFactory.statUnitComponent(statName, null, selection));
}
/** 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) {
if (input == null) return null;
StringBuilder capitals = new StringBuilder(input.toLowerCase());
capitals.setCharAt(0, Character.toUpperCase(capitals.charAt(0)));
while (capitals.indexOf("_") != -1) {
MyLogger.replacingUnderscores();
int index = capitals.indexOf("_");
capitals.setCharAt(index + 1, Character.toUpperCase(capitals.charAt(index + 1)));
capitals.setCharAt(index, ' ');
}
return capitals.toString();
}
public TextComponent usageExamples(boolean isBukkitConsole) {
return new ExampleMessage(componentFactory, isBukkitConsole);
}
public TextComponent helpMsg(boolean isConsoleSender) {
return new HelpMessage(componentFactory,
config.useHoverText() && !isConsoleSender,
isConsoleSender && Bukkit.getName().equalsIgnoreCase("CraftBukkit"),
config.getTopListMaxSize());
}
}

View File

@ -0,0 +1,19 @@
package com.gmail.artemis.the.gr8.playerstats.msg.msgutils;
import org.bukkit.map.MinecraftFont;
public final class FontUtils {
private FontUtils() {
}
public static int getNumberOfDotsToAlign(String displayText, boolean isConsoleSender, boolean fontIsBold) {
if (isConsoleSender) {
return (int) Math.round((130.0 - MinecraftFont.Font.getWidth(displayText))/6) + 7;
} else if (!fontIsBold) {
return (int) Math.round((130.0 - MinecraftFont.Font.getWidth(displayText))/2);
} else {
return (int) Math.round((130.0 - (MinecraftFont.Font.getWidth(displayText) * 1.5))/2);
}
}
}

View File

@ -11,7 +11,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.HashMap;
public class LanguageKeyHandler {
public final class LanguageKeyHandler {
private final HashMap<Statistic, String> statNameKeys;

View File

@ -1,10 +1,10 @@
package com.gmail.artemis.the.gr8.playerstats.msg;
package com.gmail.artemis.the.gr8.playerstats.msg.msgutils;
import com.gmail.artemis.the.gr8.playerstats.enums.Unit;
import java.text.DecimalFormat;
public class NumberFormatter {
public final class NumberFormatter {
private final DecimalFormat format;

View File

@ -0,0 +1,25 @@
package com.gmail.artemis.the.gr8.playerstats.msg.msgutils;
import com.gmail.artemis.the.gr8.playerstats.utils.MyLogger;
public final class StringUtils {
private StringUtils() {
}
/** Replace "_" with " " and capitalize each first letter of the input.
@param input String to prettify, case-insensitive*/
public static String prettify(String input) {
if (input == null) return null;
StringBuilder capitals = new StringBuilder(input.toLowerCase());
capitals.setCharAt(0, Character.toUpperCase(capitals.charAt(0)));
while (capitals.indexOf("_") != -1) {
MyLogger.replacingUnderscores();
int index = capitals.indexOf("_");
capitals.setCharAt(index + 1, Character.toUpperCase(capitals.charAt(index + 1)));
capitals.setCharAt(index, ' ');
}
return capitals.toString();
}
}

View File

@ -18,7 +18,6 @@ public class EnumHandler {
private final static List<String> entityNames;
private final static List<String> itemNames;
private final static List<String> statNames;
private final static List<String> entityTypeStatNames;
private final static List<String> subStatNames;
static {
@ -49,12 +48,6 @@ public class EnumHandler {
.map(Statistic::toString)
.map(String::toLowerCase)
.collect(Collectors.toList());
entityTypeStatNames = Arrays.stream(Statistic.values())
.filter(statistic -> statistic.getType().equals(Statistic.Type.ENTITY))
.map(Statistic::toString)
.map(String::toLowerCase)
.collect(Collectors.toList());
}
private EnumHandler() {
@ -112,6 +105,12 @@ public class EnumHandler {
return statNames.contains(statName.toLowerCase());
}
/** Checks whether the given String equals the name of an entity-type statistic. */
public static boolean isEntityStatistic(String statName) {
return statName.equalsIgnoreCase(Statistic.ENTITY_KILLED_BY.toString()) ||
statName.equalsIgnoreCase(Statistic.KILL_ENTITY.toString());
}
/** Returns the names of all general statistics in lowercase */
public static List<String> getStatNames() {
return statNames;
@ -128,14 +127,21 @@ public class EnumHandler {
}
}
/** Returns "block", "entity", "item", or "sub-statistic" if the provided Type is null. */
public static 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;
}
/** Checks if this statistic is a subStatEntry, meaning it is a block, item or entity
@param statName String, case-insensitive*/
public static boolean isSubStatEntry(@NotNull String statName) {
return subStatNames.contains(statName.toLowerCase());
}
/** Returns all statistics that have type entities, in lowercase */
public static List<String> getEntityTypeStatNames() {
return entityTypeStatNames;
}
}