Started reworking ConfigHandler and MessageFactory to account for the new category (server-top)

This commit is contained in:
Artemis-the-gr8 2022-06-06 17:16:34 +02:00
parent 772127426b
commit 8233fde53f
3 changed files with 192 additions and 132 deletions

View File

@ -4,6 +4,7 @@ import com.gmail.artemis.the.gr8.playerstats.Main;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.jetbrains.annotations.Nullable;
import java.io.File;
@ -19,7 +20,7 @@ public class ConfigHandler {
saveDefaultConfig();
}
//reload the config after changes have been made to it
/** Reloads the config from file, or creates a new file with default values if there is none. */
public boolean reloadConfig() {
try {
if (!configFile.exists()) {
@ -34,63 +35,41 @@ public class ConfigHandler {
}
}
//returns the config setting for include-whitelist-only, or the default value "false"
/** Returns the config setting for include-whitelist-only, or the default value "false". */
public boolean whitelistOnly() {
try {
return config.getBoolean("include-whitelist-only");
}
catch (Exception e) {
plugin.getLogger().warning(e.toString());
return false;
}
return config.getBoolean("include-whitelist-only", false);
}
//returns the config setting for exclude-banned-players, or the default value "false"
/** Returns the config setting for exclude-banned-players, or the default value "false". */
public boolean excludeBanned() {
try {
return config.getBoolean("exclude-banned-players");
}
catch (Exception e) {
plugin.getLogger().warning(e.toString());
return false;
}
return config.getBoolean("exclude-banned-players", false);
}
//returns the number of maximum days since a player has last been online, or the default value of 0 to not use this constraint
/** Returns the number of maximum days since a player has last been online, or the default value of 0 to not use this constraint. */
public int lastPlayedLimit() {
try {
return config.getInt("number-of-days-since-last-joined");
}
catch (Exception e) {
plugin.getLogger().warning(e.toString());
return 0;
}
return config.getInt("number-of-days-since-last-joined", 0);
}
//returns the config setting for top-list-max-size, or the default value of 10 if no value can be retrieved
/** Returns the config setting for top-list-max-size, or the default value of 10 if no value can be retrieved. */
public int getTopListMaxSize() {
try {
return config.getInt("top-list-max-size");
}
catch (Exception e) {
plugin.getLogger().warning(e.toString());
return 10;
}
return config.getInt("top-list-max-size", 10);
}
//returns the config setting for use-dots, or the default value "true" if no value can be retrieved
/** Returns the config setting for use-dots, or the default value "true" if no value can be retrieved. */
public boolean useDots() {
try {
return config.getBoolean("use-dots");
}
catch (Exception e) {
plugin.getLogger().warning(e.toString());
return true;
}
return config.getBoolean("use-dots", true);
}
/** Returns the specified server name, or "this server" if no value can be retrieved. */
public String getServerName() {
return config.getString("your-server-name", "this server");
}
/** Returns a String that represents either a Chat Color, hex color code, or Style. Default values are "none" for Style,
and "green" or "gold" for Color (for top or individual color). */
public String getPlayerNameFormatting(boolean topStat, boolean isStyle) {
return getStringFromConfig(topStat, isStyle, "player-names");
String def = topStat ? "green" : "gold";
return getStringFromConfig(topStat, false, isStyle, def, "player-names");
}
public boolean playerNameIsBold() {
@ -103,61 +82,78 @@ public class ConfigHandler {
return false;
}
public String getStatNameFormatting(boolean topStat, boolean isStyle) {
return getStringFromConfig(topStat, isStyle, "stat-names");
/** Returns a String that represents either a Chat Color, hex color code, or Style. Default values are "none" for Style,
and "yellow" for Color. */
public String getStatNameFormatting(boolean topStat, boolean serverStat, boolean isStyle) {
return getStringFromConfig(topStat, serverStat, isStyle, "yellow", "stat-names");
}
public String getSubStatNameFormatting(boolean topStat, boolean isStyle) {
return getStringFromConfig(topStat, isStyle, "sub-stat-names");
/** Returns a String that represents either a Chat Color, hex color code, or Style. Default values are "none" for Style,
and "#FFD52B" for Color. */
public String getSubStatNameFormatting(boolean topStat, boolean serverStat, boolean isStyle) {
return getStringFromConfig(topStat, serverStat, isStyle, "#FFD52B", "sub-stat-names");
}
public String getStatNumberFormatting(boolean topStat, boolean isStyle) {
return getStringFromConfig(topStat, isStyle, "stat-numbers");
/** Returns a String that represents either a Chat Color, hex color code, or Style. Default values are "none" for Style,
and "#55AAFF" or "#ADE7FF" for Color (for the top or individual color). */
public String getStatNumberFormatting(boolean topStat, boolean serverStat, boolean isStyle) {
String def = topStat ? "#55AAFF" : "#ADE7FF";
return getStringFromConfig(topStat, serverStat, isStyle, def,"stat-numbers");
}
public String getListTitleFormatting(boolean isStyle) {
return getStringFromConfig(true, isStyle, "list-title");
/** Returns a String that represents either a Chat Color, hex color code, or Style. Default values are "none" for Style,
and "yellow" for Color. */
public String getTitleFormatting(boolean topStat, boolean isStyle) {
return getStringFromConfig(topStat, (!topStat), isStyle, "yellow", "title");
}
public String getListTitleNumberFormatting(boolean isStyle) {
return getStringFromConfig(true, isStyle, "list-title-number");
/** Returns a String that represents either a Chat Color, hex color code, or Style. Default values are "none" for Style,
and "gold" for Color. */
public String getTitleNumberFormatting(boolean isStyle) {
return getStringFromConfig(true, false, isStyle, "gold", "title-number");
}
public String getRankingNumberFormatting(boolean isStyle) {
return getStringFromConfig(true, isStyle, "ranking-numbers");
/** Returns a String that represents either a Chat Color, hex color code, or Style. Default values are "none" for Style,
and "#FFB80E" for Color. */
public String getServerNameFormatting(boolean isStyle) {
return getStringFromConfig(false, true, isStyle, "#FFB80E", "server-name");
}
public String getDotsColor() {
return getStringFromConfig(true, false, "dots");
/** Returns a String that represents either a Chat Color, hex color code, or Style. Default values are "none" for Style,
and "gold" for Color. */
public String getRankNumberFormatting(boolean isStyle) {
return getStringFromConfig(true, false, isStyle, "gold", "rank-numbers");
}
//returns the config value for a color or style option in string-format, or null if no value was found
private String getStringFromConfig(boolean topStat, boolean isStyle, String pathName){
ConfigurationSection section = getRelevantSection(topStat, isStyle);
return section != null ? section.getString(pathName) : null;
/** Returns a String that represents either a Chat Color, hex color code, or Style. Default values are "none" for Style,
and "dark_gray" for Color. */
public String getDotsFormatting(boolean isStyle) {
return getStringFromConfig(true, false, isStyle, "dark_gray", "dots");
}
//returns the config section that contains the relevant color or style option
private ConfigurationSection getRelevantSection(boolean topStat, boolean isStyle) {
ConfigurationSection section;
try {
if (!topStat) {
if (!isStyle) section = config.getConfigurationSection("individual-statistics-color");
else section = config.getConfigurationSection("individual-statistics-style");
}
else {
if (!isStyle) section = config.getConfigurationSection("top-list-color");
else section = config.getConfigurationSection("top-list-style");
}
return section;
/** Returns the config value for a color or style option in string-format, the supplied default value, or null if no configSection was found. */
private @Nullable String getStringFromConfig(boolean topStat, boolean serverStat, boolean isStyle, String def, String pathName){
String path = isStyle ? pathName + "-style" : pathName;
String defaultValue = isStyle ? "none" : def;
ConfigurationSection section = getRelevantSection(topStat, serverStat);
return section != null ? section.getString(path, defaultValue) : null;
}
/** Returns the config section that contains the relevant color or style option. */
private @Nullable ConfigurationSection getRelevantSection(boolean topStat, boolean serverStat) {
if (topStat) {
return config.getConfigurationSection("top-list");
}
catch (IllegalArgumentException | NullPointerException exception) {
plugin.getLogger().warning(exception.toString());
return null;
else if (serverStat) {
return config.getConfigurationSection("total-server");
}
else {
return config.getConfigurationSection("individual-statistics");
}
}
//create a config file if none exists yet (from the config.yml in the plugin's resources)
/** Create a config file if none exists yet (from the config.yml in the plugin's resources). */
private void saveDefaultConfig() {
config = plugin.getConfig();
plugin.saveDefaultConfig();

View File

@ -11,6 +11,7 @@ import net.kyori.adventure.util.Index;
import org.bukkit.Bukkit;
import org.bukkit.Statistic;
import org.bukkit.map.MinecraftFont;
import org.jetbrains.annotations.Nullable;
import java.util.*;
@ -170,7 +171,7 @@ public class MessageFactory {
TextComponent.Builder topList = Component.text();
topList.append(newline()).append(getPluginPrefix())
.append(titleComponent("Top")).append(space())
.append(titleComponent(true, "Top")).append(space())
.append(titleNumberComponent(topStats.size())).append(space())
.append(statNameComponent(statName, true)).append(space())
.append(subStatNameComponent(subStatEntryName, true));
@ -208,12 +209,12 @@ public class MessageFactory {
public TextComponent formatServerStat(String statName, String subStatEntry, int stat) {
TextComponent.Builder serverStat = Component.text();
serverStat.append(titleComponent("All"))
serverStat.append(titleComponent(false, "All"))
.append(space())
.append(statNameComponent(statName, true))
.append(space())
.append(subStatNameComponent(subStatEntry, true))
.append(titleComponent("on this server:"))
.append(titleComponent(false, "on this server:"))
.append(space())
.append(statNumberComponent(stat, true));
@ -238,9 +239,6 @@ public class MessageFactory {
return subStat;
}
//try to get the hex color or NamedTextColor from config String, substitute a default ChatColor if both fail, and try to apply style where necessary
private TextComponent playerNameComponent(String playerName, boolean topStat) {
NamedTextColor defaultColor = topStat ? NamedTextColor.GREEN : NamedTextColor.GOLD;
TextComponent.Builder player = applyColor(
@ -250,8 +248,8 @@ public class MessageFactory {
private TextComponent statNameComponent(String statName, boolean topStat) {
TextComponent.Builder stat = applyColor(
config.getStatNameFormatting(topStat, false), statName.toLowerCase().replace("_", " "), NamedTextColor.YELLOW);
return applyStyle(config.getStatNameFormatting(topStat, true), stat).build();
config.getStatNameFormatting(topStat, false, false), statName.toLowerCase().replace("_", " "), NamedTextColor.YELLOW);
return applyStyle(config.getStatNameFormatting(topStat, false, true), stat).build();
}
private TextComponent subStatNameComponent(String subStatName, boolean topStat) {
@ -259,33 +257,45 @@ public class MessageFactory {
"(" + subStatName.toLowerCase().replace("_", " ") + ") " : "";
TextComponent.Builder subStat = applyColor(
config.getSubStatNameFormatting(topStat, false), subStatString, NamedTextColor.YELLOW);
return applyStyle(config.getSubStatNameFormatting(topStat, true), subStat).build();
config.getSubStatNameFormatting(topStat, false, false), subStatString, NamedTextColor.YELLOW);
return applyStyle(config.getSubStatNameFormatting(topStat, false,true), subStat).build();
}
private TextComponent statNumberComponent(int statNumber, boolean topStat) {
TextComponent.Builder number = applyColor(
config.getStatNumberFormatting(topStat, false), statNumber + "", NamedTextColor.LIGHT_PURPLE);
return applyStyle(config.getStatNumberFormatting(topStat, true), number).build();
config.getStatNumberFormatting(topStat, false, false), statNumber + "", NamedTextColor.LIGHT_PURPLE);
return applyStyle(config.getStatNumberFormatting(topStat, false, true), number).build();
}
private TextComponent titleComponent(String content) {
TextComponent.Builder server = applyColor(config.getListTitleFormatting(false), content, NamedTextColor.YELLOW);
return applyStyle(config.getListTitleFormatting(true), server).build();
private TextComponent titleComponent(boolean topStat, String content) {
TextComponent.Builder server = applyColor(config.getTitleFormatting(topStat, false), content, NamedTextColor.YELLOW);
return applyStyle(config.getTitleFormatting(topStat, true), server).build();
}
private TextComponent titleNumberComponent(int number) {
TextComponent.Builder titleNumber = applyColor(config.getListTitleNumberFormatting(false), number + "", NamedTextColor.GOLD);
return applyStyle(config.getListTitleNumberFormatting(true), titleNumber).build();
return getComponent(
number + "",
getColorFromString(config.getTitleNumberFormatting(false)),
getStyleFromString(config.getTitleNumberFormatting(true)));
}
private TextComponent rankingNumberComponent(String number) {
TextComponent.Builder list = applyColor(config.getRankingNumberFormatting(false), number + "", NamedTextColor.GOLD);
return applyStyle(config.getRankingNumberFormatting(true), list).build();
return getComponent(
number,
getColorFromString(config.getRankNumberFormatting(false)),
getStyleFromString(config.getRankNumberFormatting(true)));
}
private TextComponent dotsComponent(String dots) {
return text(dots).color(getColorFromString(config.getDotsColor())).colorIfAbsent(NamedTextColor.DARK_GRAY);
return getComponent(
dots,
getColorFromString(config.getDotsFormatting(false)),
getStyleFromString(config.getDotsFormatting(true)));
}
private TextComponent getComponent(String content, TextColor color, @Nullable TextDecoration style) {
Bukkit.getLogger().info("Style = " + style);
return style == null ? text(content).color(color) : text(content).color(color).decoration(style, TextDecoration.State.TRUE);
}
private TextColor getColorFromString(String configString) {
@ -305,6 +315,35 @@ public class MessageFactory {
return null;
}
private TextColor getTextColorByName(String textColor) {
Index<String, NamedTextColor> names = NamedTextColor.NAMES;
return names.value(textColor);
}
private @Nullable TextDecoration getStyleFromString(String configString) {
if (configString != null) {
if (configString.equalsIgnoreCase("none")) {
return null;
}
else if (configString.equalsIgnoreCase("bold")) {
return TextDecoration.BOLD;
}
else if (configString.equalsIgnoreCase("italic")) {
return TextDecoration.ITALIC;
}
else if (configString.equalsIgnoreCase("magic")) {
return TextDecoration.OBFUSCATED;
}
else if (configString.equalsIgnoreCase("strikethrough")) {
return TextDecoration.STRIKETHROUGH;
}
else if (configString.equalsIgnoreCase("underlined")) {
return TextDecoration.UNDERLINED;
}
}
return null;
}
private TextComponent.Builder applyColor(String configString, String content, NamedTextColor defaultColor) {
TextComponent.Builder component = Component.text();
@ -324,11 +363,6 @@ public class MessageFactory {
return component.content(content).colorIfAbsent(defaultColor);
}
private TextColor getTextColorByName(String textColor) {
Index<String, NamedTextColor> names = NamedTextColor.NAMES;
return names.value(textColor);
}
private TextComponent.Builder applyStyle(String configString, TextComponent.Builder component) {
if (configString != null) {
if (configString.equalsIgnoreCase("none")) {

View File

@ -1,8 +1,8 @@
# ------------------------------
# PlayerStats Configuration
# ------------------------------
# --------------------------------
# PlayerStats Configuration
# --------------------------------
# ------ General Options -------
# ----------- General ------------
# Filtering options to control which players should be included in statistic calculations
include-whitelist-only: false
exclude-banned-players: false
@ -11,47 +11,77 @@ exclude-banned-players: false
# Leave this on 0 to include all players
number-of-days-since-last-joined: 0
# ------ Format Options --------
# ----------- Format -------------
# The name you want displayed for a total-on-this-server statistic
your-server-name: 'this server'
# The maximum number of results displayed in the top list
top-list-max-size: 10
# If true, the top list will be aligned with lines of dots so that the stat numbers are all underneath each other
use-dots: true
# ------ Color Options ---------
# -------- Color & Style ---------
# The colors below can be chat color names or hex codes (format: '#xxxxxx' <-- including quotation marks!)
# The style options include: bold, italic, underlined, strikethrough, and magic
individual-statistics-color:
player-names: gold
# The style options include: bold, italic, underlined, strikethrough (and for some reason I decided to also include magic)
top-list:
title: yellow
title-style: none
title-number: gold
title-number-style: none
stat-names: yellow
stat-names-style: none
sub-stat-names: '#FFD52B'
stat-numbers: '#ADE7FF'
sub-stat-names-style: none
individual-statistics-style:
player-names: none
stat-names: none
sub-stat-names: none
stat-numbers: none
rank-numbers: gold
rank-numbers-style: none
top-list-color:
player-names: green
stat-names: yellow
sub-stat-names: '#FFD52B'
player-names-style: none
stat-numbers: '#55AAFF'
list-title: yellow
list-title-number: gold
ranking-numbers: gold
stat-numbers-style: none
dots: dark_gray
dots-style: none
top-list-style:
player-names: none
stat-names: none
sub-stat-names: none
stat-numbers: none
list-title: none
list-title-number: none
ranking-numbers: none
individual-statistics:
player-names: gold
player-names-style: none
stat-names: yellow
stat-names-style: none
sub-stat-names: '#FFD52B'
sub-stat-names-style: none
stat-numbers: '#ADE7FF'
stat-numbers-style: none
total-server:
title: gold
title-style: none
server-name: '#FFB80E'
server-name-style: none
stat-names: yellow
stat-names-style: none
sub-stat-names: '#FFD52B'
sub-stat-names-style: none
stat-numbers: '#ADE7FF'
stat-numbers-style: none