Finished toggle for TranslatableComponents (#57), rewrote non-translated stat/block/item/entity names and added more debug logging

This commit is contained in:
Artemis-the-gr8 2022-06-23 15:45:54 +02:00
parent 3e968232c6
commit d225b1e235
7 changed files with 126 additions and 78 deletions

View File

@ -30,7 +30,7 @@ public class ConfigHandler {
/** Returns the desired debugging level.
<p>1 = low (only show unexpected errors)</p>
<p>2 = medium (show all encountered exceptions, log main tasks and show time taken)</p>
<p>2 = medium (detail all encountered exceptions, log main tasks and show time taken)</p>
<p>3 = high (log all tasks and time taken)</p>
<p>Default: 1</p>*/
public int debugLevel() {

View File

@ -1,7 +1,7 @@
package com.gmail.artemis.the.gr8.playerstats.msg;
import com.gmail.artemis.the.gr8.playerstats.utils.EnumHandler;
import org.bukkit.Bukkit;
import com.gmail.artemis.the.gr8.playerstats.utils.MyLogger;
import org.bukkit.Material;
import org.bukkit.Statistic;
import org.jetbrains.annotations.NotNull;
@ -30,7 +30,7 @@ public class LanguageKeyHandler {
}
}
catch (IllegalArgumentException e) {
Bukkit.getLogger().info("PlayerStats, LanguageKeyHandler 33: " + e);
MyLogger.logException(e, "LanguageKeyHandler, getStatKey", 34);
return null;
}
}
@ -44,7 +44,7 @@ public class LanguageKeyHandler {
return "entity.minecraft." + EnumHandler.getEntityEnum(entityName).getKey().getKey();
}
catch (IllegalArgumentException e) {
Bukkit.getLogger().info("PlayerStats, LanguageKeyHandler 47: " + e);
MyLogger.logException(e, "LanguageKeyHandler, getEntityKey", 48);
return null;
}
}
@ -61,7 +61,7 @@ public class LanguageKeyHandler {
}
}
catch (IllegalArgumentException e) {
Bukkit.getLogger().info("PlayerStats, LanguageKeyHandler 64: " + e);
MyLogger.logException(e, "LanguageKeyHandler, getItemKey", 65);
return null;
}
}
@ -75,7 +75,7 @@ public class LanguageKeyHandler {
return "block.minecraft." + EnumHandler.getBlockEnum(name).getKey().getKey();
}
catch (IllegalArgumentException e) {
Bukkit.getLogger().info("PlayerStats, LanguageKeyHandler 78: " + e);
MyLogger.logException(e, "LanguageKeyHandler, getBlockKey", 79);
return null;
}
}

View File

@ -3,6 +3,7 @@ package com.gmail.artemis.the.gr8.playerstats.msg;
import com.gmail.artemis.the.gr8.playerstats.enums.Query;
import com.gmail.artemis.the.gr8.playerstats.config.ConfigHandler;
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 net.kyori.adventure.text.TranslatableComponent;
@ -254,70 +255,59 @@ public class MessageFactory {
protected TranslatableComponent statNameComponent(Query selection, @NotNull String statName, String subStatName) {
TextColor statNameColor = getColorFromString(config.getStatNameFormatting(selection, false));
TextDecoration statNameStyle = getStyleFromString(config.getStatNameFormatting(selection, true));
TranslatableComponent subStat = subStatNameComponent(selection, subStatName);
String key = language.getStatKey(statName);
if (key == null) {
key = statName;
Statistic.Type statType = EnumHandler.getStatType(statName);
TranslatableComponent subStat = subStatNameComponent(selection, subStatName, statType);
TranslatableComponent.Builder totalName;
String key = getLanguageKey(statName, null, statType);
if (key == null) key = statName;
if (key.equalsIgnoreCase("stat_type.minecraft.killed") && subStat != null) {
totalName = killEntityComponent(subStat);
}
//case for kill_entity
else if (key.equalsIgnoreCase("stat_type.minecraft.killed") && subStat != null) {
TranslatableComponent.Builder totalName = translatable()
.key("commands.kill.success.single") //"Killed %s"
.args(subStat)
.color(statNameColor);
if (statNameStyle != null) {
totalName.decoration(statNameStyle, TextDecoration.State.TRUE);
}
return totalName.build();
}
//case for entity_killed_by
else if (key.equalsIgnoreCase("stat_type.minecraft.killed_by") && subStat != null) {
String newKey = "stat.minecraft.player_kills"; //"Player Kills"
if (selection == Query.PLAYER) {
newKey = "stat.minecraft.deaths"; //"Number of Deaths"
}
TranslatableComponent.Builder totalName = translatable()
.key(newKey)
.append(space())
.append(translatable()
.key("book.byAuthor") //"by %1$s"
.args(subStat))
.color(statNameColor);
if (statNameStyle != null) {
totalName.decoration(statNameStyle, TextDecoration.State.TRUE);
}
return totalName.build();
totalName = entityKilledByComponent(selection, subStat);
}
else {
totalName = translatable().key(key);
}
//all other cases
TranslatableComponent.Builder totalName = translatable()
.key(key)
.color(statNameColor);
if (statNameStyle != null) totalName.decoration(statNameStyle, TextDecoration.State.TRUE);
if (subStat != null) {
totalName.append(space()).append(subStat);
}
return totalName.build();
if (subStat != null) totalName.append(space()).append(subStat);
return totalName
.color(statNameColor)
.build();
}
protected @Nullable TranslatableComponent subStatNameComponent(Query selection, @Nullable String subStatName) {
if (subStatName == null) {
return null;
}
String key = null;
if (EnumHandler.isEntity(subStatName)){
key = language.getEntityKey(subStatName);
}
else if (EnumHandler.isBlock(subStatName)) {
key = language.getBlockKey(subStatName);
}
else if (EnumHandler.isItem(subStatName)) {
key = language.getItemKey(subStatName);
/** Construct a custom translation for kill_entity */
private TranslatableComponent.@NotNull Builder killEntityComponent(TranslatableComponent subStat) {
TranslatableComponent.Builder totalName = translatable()
.key("commands.kill.success.single"); //"Killed %s"
if (subStat != null) totalName.args(subStat);
return totalName;
}
/** Construct a custom translation for entity_killed_by */
private TranslatableComponent.@NotNull Builder entityKilledByComponent(Query selection, TranslatableComponent subStat) {
String key = "stat.minecraft.player_kills"; //"Player Kills"
if (selection == Query.PLAYER) {
key = "stat.minecraft.deaths"; //"Number of Deaths"
}
TranslatableComponent.Builder totalName = translatable()
.key(key)
.append(space())
.append(translatable()
.key("book.byAuthor")); //"by %1$s"
if (subStat != null) totalName.args(subStat);
return totalName;
}
protected @Nullable TranslatableComponent subStatNameComponent(Query selection, @Nullable String subStatName, Statistic.Type statType) {
String key = getLanguageKey(null, subStatName, statType);
if (key != null) {
TextDecoration style = getStyleFromString(config.getSubStatNameFormatting(selection, true));
TranslatableComponent.Builder subStat = translatable()
@ -376,6 +366,47 @@ public class MessageFactory {
return style == null ? text(content).color(color) : text(content).color(color).decoration(style, TextDecoration.State.TRUE);
}
/** If TranslatableComponents are enabled, this will attempt to get the appropriate language key.
Otherwise, it will attempt to replace "_" with " " and capitalize each first letter of the input.
If that fails too, it will simply return the String it received as input.
If even THAT somehow fails, it will return null.*/
private @Nullable String getLanguageKey(@Nullable String statName, @Nullable String subStatName, Statistic.Type statType) {
String key = null;
if (config.useTranslatableComponents()) {
if (statName != null) {
key = language.getStatKey(statName);
} else if (subStatName != null && statType != null) {
switch (statType) {
case BLOCK -> key = language.getBlockKey(subStatName);
case ENTITY -> key = language.getEntityKey(subStatName);
case ITEM -> key = language.getItemKey(subStatName);
case UNTYPED -> {
}
}
}
if (key != null) {
return key;
}
}
if (statName != null) key = statName;
else if (subStatName != null) key = subStatName;
if (key != null) {
StringBuilder capitals = new StringBuilder(key);
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, ' ');
}
key = capitals.toString();
}
return key;
}
private TextColor getColorFromString(String configString) {
if (configString != null) {
try {
@ -411,7 +442,7 @@ public class MessageFactory {
}
}
//returns the type of the substatistic in String-format, or null if this statistic is not of type block, item or entity
/** Returns the type of the substatistic in String-format, or null if this statistic is not of type block, item or entity */
private String getSubStatTypeName(Statistic.Type statType) {
String subStat;
if (statType == Statistic.Type.BLOCK) {
@ -429,7 +460,7 @@ public class MessageFactory {
return subStat;
}
//returns the usage-explanation with hovering text
/** Returns the usage-explanation with hovering text */
private @NotNull TextComponent helpMsgHover() {
TextComponent spaces = text(" "); //4 spaces
TextComponent arrow = text("").color(NamedTextColor.GOLD); //alt + 26
@ -487,8 +518,8 @@ public class MessageFactory {
.append(text(", add the player's name").color(hoverBaseColor))))));
}
//returns the usage-explanation without any hovering text
//if BukkitVersion is CraftBukkit, this doesn't use unicode symbols or hex colors
/** Returns the usage-explanation without any hovering text.
If BukkitVersion is CraftBukkit, this doesn't use unicode symbols or hex colors */
private @NotNull TextComponent helpMsgPlain(boolean isConsoleSender) {
TextComponent spaces = text(" "); //4 spaces
TextComponent arrow = text("").color(NamedTextColor.GOLD); //alt + 26;

View File

@ -21,6 +21,7 @@ public class PrideMessageFactory extends MessageFactory {
config = c;
}
@Override
protected TextComponent getPrefixAsTitle(boolean isConsoleSender) {
if (cancelRainbow(isConsoleSender)) {
@ -63,11 +64,5 @@ public class PrideMessageFactory extends MessageFactory {
private boolean cancelRainbow(boolean isConsoleSender) {
return !(config.useRainbowPrefix() || (config.useFestiveFormatting() && LocalDate.now().getMonth().equals(Month.JUNE))) ||
(isConsoleSender && Bukkit.getName().equalsIgnoreCase("CraftBukkit"));
// If a player uses the command after pride month, with rainbow enabled
// not (true OR (true && false)) OR (false && false)
// not (true OR (false)) OR (false)
// not (true) OR (false) not (false) OR (false)
// false OR (false) not (true) true OR false not (false)
// false true
}
}

View File

@ -98,18 +98,18 @@ public class ReloadThread extends Thread {
OfflinePlayer[] offlinePlayers;
if (config.whitelistOnly()) {
offlinePlayers = Bukkit.getWhitelistedPlayers().toArray(OfflinePlayer[]::new);
MyLogger.logTimeTaken("ReloadThread", "getting white-list-only list", time);
MyLogger.logTimeTaken("ReloadThread", "retrieved whitelist", time);
}
else if (config.excludeBanned()) {
Set<OfflinePlayer> bannedPlayers = Bukkit.getBannedPlayers();
offlinePlayers = Arrays.stream(Bukkit.getOfflinePlayers())
.parallel()
.filter(offlinePlayer -> !bannedPlayers.contains(offlinePlayer)).toArray(OfflinePlayer[]::new);
MyLogger.logTimeTaken("ReloadThread", "getting excluding-banned-players list", time);
MyLogger.logTimeTaken("ReloadThread", "retrieved banlist", time);
}
else {
offlinePlayers = Bukkit.getOfflinePlayers();
MyLogger.logTimeTaken("ReloadThread", "getting regular player list", time);
MyLogger.logTimeTaken("ReloadThread", "retrieved list of Offline Players", time);
}
int size = offlinePlayers != null ? offlinePlayers.length : 16;

View File

@ -3,6 +3,7 @@ package com.gmail.artemis.the.gr8.playerstats.utils;
import com.gmail.artemis.the.gr8.playerstats.enums.DebugLevel;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
@ -22,8 +23,7 @@ public class MyLogger {
static{
Plugin plugin = Bukkit.getPluginManager().getPlugin("PlayerStats");
logger = plugin != null ? plugin.getLogger() : Bukkit.getLogger();
logger = (plugin != null) ? plugin.getLogger() : Bukkit.getLogger();
debugLevel = DebugLevel.LOW;
processedPlayers = new String[10];
@ -34,6 +34,7 @@ public class MyLogger {
private MyLogger() {
}
/** Accesses the playersIndex to up it by 1 and return its previous value. */
private static int nextPlayersIndex() {
return playersIndex.getAndIncrement();
@ -46,7 +47,7 @@ public class MyLogger {
/** Sets the desired debugging level.
<p>1 = low (only show unexpected errors)</p>
<p>2 = medium (show all encountered exceptions, log main tasks and show time taken)</p>
<p>2 = medium (detail all encountered exceptions, log main tasks and show time taken)</p>
<p>3 = high (log all tasks and time taken)</p>
<p>Default: 1</p>*/
public static void setDebugLevel(int level) {
@ -61,6 +62,27 @@ public class MyLogger {
}
}
/** Log the encountered exception to console, including a printStackTrace if DebugLevel is MEDIUM or HIGH.
@param exception The encountered exception
@param caughtBy The name of the class/method that caught the exception
@param lineNumber The line where the exception is caught */
public static void logException(@NotNull Exception exception, String caughtBy, int lineNumber) {
String line = (lineNumber == 0) ? "" : " [" + lineNumber + "]";
String caught = (debugLevel != DebugLevel.LOW) ? " (" + caughtBy + line + ")" : "";
logger.info(exception + caught);
if (debugLevel == DebugLevel.HIGH) {
exception.printStackTrace();
}
}
/** If DebugLevel is MEDIUM or HIGH, logs when the while loop in MessageFactory, getLanguageKey is being run. */
public static void replacingUnderscores() {
if (debugLevel != DebugLevel.LOW) {
logger.info("Replacing underscores and capitalizing names...");
}
}
/** Output to console that the given thread has been created (but not started yet).*/
public static void threadCreated(String threadName) {
if (debugLevel != DebugLevel.LOW) {

View File

@ -10,7 +10,7 @@ config-version: 3.1
# How much output in console you'll get while PlayerStats is processing
# 1 = low (only show unexpected errors)
# 2 = medium (show all encountered exceptions, log main tasks and show time taken)
# 2 = medium (detail all encountered exceptions, log main tasks and show time taken)
# 3 = high (log all tasks and time taken)
debug-level: 1