From ac265d02603863ebe9a98bdec0eacdde406d0d9b Mon Sep 17 00:00:00 2001 From: Auxilor Date: Sun, 29 Aug 2021 15:30:25 +0100 Subject: [PATCH] Added more formatting options --- .../eco/core/config/interfaces/Config.java | 137 +++++++++++++++++- .../java/com/willfp/eco/util/StringUtils.java | 120 ++++++++++++--- .../config/json/EcoJSONConfigWrapper.kt | 70 ++++----- .../config/yaml/EcoYamlConfigWrapper.kt | 92 +++++------- .../eco/internal/gui/menu/EcoMenuBuilder.kt | 4 +- .../eco/internal/gui/slot/EcoSlotBuilder.kt | 2 +- .../eco/internal/proxy/EcoProxyFactory.kt | 2 +- 7 files changed, 301 insertions(+), 126 deletions(-) diff --git a/eco-api/src/main/java/com/willfp/eco/core/config/interfaces/Config.java b/eco-api/src/main/java/com/willfp/eco/core/config/interfaces/Config.java index f14e989b..d1b9d35f 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/config/interfaces/Config.java +++ b/eco-api/src/main/java/com/willfp/eco/core/config/interfaces/Config.java @@ -1,5 +1,6 @@ package com.willfp.eco.core.config.interfaces; +import com.willfp.eco.util.StringUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -165,7 +166,9 @@ public interface Config extends Cloneable { * @return The found value, or an empty string if not found. */ @NotNull - String getString(@NotNull String path); + default String getString(@NotNull String path) { + return getString(path, true); + } /** * Get a string from config. @@ -175,8 +178,36 @@ public interface Config extends Cloneable { * @return The found value, or an empty string if not found. */ @NotNull + default String getString(@NotNull String path, + boolean format) { + return this.getString(path, format, StringUtils.FormatOption.WITH_PLACEHOLDERS); + } + + /** + * Get a string from config. + * + * @param path The key to fetch the value from. + * @param option The format option. + * @return The found value, or an empty string if not found. + */ + @NotNull + default String getString(@NotNull String path, + @NotNull final StringUtils.FormatOption option) { + return this.getString(path, true, option); + } + + /** + * Get a string from config. + * + * @param path The key to fetch the value from. + * @param format If the string should be formatted. + * @param option The format option. + * @return The found value, or an empty string if not found. + */ + @NotNull String getString(@NotNull String path, - boolean format); + boolean format, + @NotNull StringUtils.FormatOption option); /** * Get a string from config. @@ -185,7 +216,9 @@ public interface Config extends Cloneable { * @return The found value, or null if not found. */ @Nullable - String getStringOrNull(@NotNull String path); + default String getStringOrNull(@NotNull String path) { + return getStringOrNull(path, true); + } /** * Get a string from config. @@ -195,8 +228,36 @@ public interface Config extends Cloneable { * @return The found value, or null if not found. */ @Nullable + default String getStringOrNull(@NotNull String path, + boolean format) { + return this.getStringOrNull(path, format, StringUtils.FormatOption.WITH_PLACEHOLDERS); + } + + /** + * Get a string from config. + * + * @param path The key to fetch the value from. + * @param option The format option. + * @return The found value, or null if not found. + */ + @Nullable + default String getStringOrNull(@NotNull String path, + @NotNull StringUtils.FormatOption option) { + return this.getStringOrNull(path, true, option); + } + + /** + * Get a string from config. + * + * @param path The key to fetch the value from. + * @param format If the string should be formatted. + * @param option The format option. + * @return The found value, or null if not found. + */ + @Nullable String getStringOrNull(@NotNull String path, - boolean format); + boolean format, + @NotNull StringUtils.FormatOption option); /** * Get a list of strings from config. @@ -207,7 +268,9 @@ public interface Config extends Cloneable { * @return The found value, or a blank {@link java.util.ArrayList} if not found. */ @NotNull - List getStrings(@NotNull String path); + default List getStrings(@NotNull String path) { + return getStrings(path, true); + } /** * Get a list of strings from config. @@ -217,8 +280,36 @@ public interface Config extends Cloneable { * @return The found value, or a blank {@link java.util.ArrayList} if not found. */ @NotNull + default List getStrings(@NotNull String path, + boolean format) { + return this.getStrings(path, format, StringUtils.FormatOption.WITH_PLACEHOLDERS); + } + + /** + * Get a list of strings from config. + * + * @param path The key to fetch the value from. + * @param option The format option. + * @return The found value, or a blank {@link java.util.ArrayList} if not found. + */ + @Nullable + default List getStrings(@NotNull String path, + @NotNull StringUtils.FormatOption option) { + return getStrings(path, true, option); + } + + /** + * Get a list of strings from config. + * + * @param path The key to fetch the value from. + * @param format If the strings should be formatted. + * @param option The option. + * @return The found value, or a blank {@link java.util.ArrayList} if not found. + */ + @NotNull List getStrings(@NotNull String path, - boolean format); + boolean format, + @NotNull StringUtils.FormatOption option); /** * Get a list of strings from config. @@ -227,7 +318,9 @@ public interface Config extends Cloneable { * @return The found value, or null if not found. */ @Nullable - List getStringsOrNull(@NotNull String path); + default List getStringsOrNull(@NotNull String path) { + return getStringsOrNull(path, true); + } /** * Get a list of strings from config. @@ -237,8 +330,36 @@ public interface Config extends Cloneable { * @return The found value, or null if not found. */ @Nullable + default List getStringsOrNull(@NotNull String path, + boolean format) { + return getStringsOrNull(path, format, StringUtils.FormatOption.WITH_PLACEHOLDERS); + } + + /** + * Get a list of strings from config. + * + * @param path The key to fetch the value from. + * @param option The format option. + * @return The found value, or null if not found. + */ + @Nullable + default List getStringsOrNull(@NotNull String path, + @NotNull StringUtils.FormatOption option) { + return getStringsOrNull(path, true, option); + } + + /** + * Get a list of strings from config. + * + * @param path The key to fetch the value from. + * @param format If the strings should be formatted. + * @param option The format option. + * @return The found value, or null if not found. + */ + @Nullable List getStringsOrNull(@NotNull String path, - boolean format); + boolean format, + @NotNull StringUtils.FormatOption option); /** * Get a decimal from config. diff --git a/eco-api/src/main/java/com/willfp/eco/util/StringUtils.java b/eco-api/src/main/java/com/willfp/eco/util/StringUtils.java index fc08ff0a..6ab0d0a4 100644 --- a/eco-api/src/main/java/com/willfp/eco/util/StringUtils.java +++ b/eco-api/src/main/java/com/willfp/eco/util/StringUtils.java @@ -12,7 +12,7 @@ import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.awt.*; +import java.awt.Color; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -57,7 +57,21 @@ public class StringUtils { .build(); /** - * Format a list of strings - converts Placeholders and Color codes. + * Format a list of strings. + *

+ * Converts color codes and placeholders. + * + * @param list The messages to format. + * @return The message, formatted. + */ + public List formatList(@NotNull final List list) { + return formatList(list, (Player) null); + } + + /** + * Format a list of strings. + *

+ * Coverts color codes and placeholders for a player. * * @param list The messages to format. * @param player The player to translate placeholders with respect to. @@ -65,26 +79,61 @@ public class StringUtils { */ public List formatList(@NotNull final List list, @Nullable final Player player) { + return formatList(list, player, FormatOption.WITH_PLACEHOLDERS); + } + + /** + * Format a list of strings. + *

+ * Converts color codes and placeholders if specified. + * + * @param list The messages to format. + * @param option The format option. + * @return The message, formatted. + */ + public List formatList(@NotNull final List list, + @NotNull final FormatOption option) { + return formatList(list, null, option); + } + + /** + * Format a list of strings. + *

+ * Coverts color codes and placeholders for a player if specified. + * + * @param list The messages to format. + * @param player The player to translate placeholders with respect to. + * @param option The options. + * @return The message, format. + */ + public List formatList(@NotNull final List list, + @Nullable final Player player, + @NotNull final FormatOption option) { List translated = new ArrayList<>(); for (String string : list) { - translated.add(format(string, player)); + translated.add(format(string, player, option)); } return translated; } /** - * Format a list of strings - converts Placeholders and Color codes. + * Format a string. + *

+ * Converts color codes and placeholders. * - * @param list The messages to format. + * @param message The message to translate. * @return The message, formatted. + * @see StringUtils#format(String, Player) */ - public List formatList(@NotNull final List list) { - return formatList(list, null); + public String format(@NotNull final String message) { + return format(message, (Player) null); } /** - * Format a string - converts Placeholders and Color codes. + * Format a string. + *

+ * Converts color codes and placeholders for a player. * * @param message The message to format. * @param player The player to translate placeholders with respect to. @@ -92,23 +141,45 @@ public class StringUtils { */ public String format(@NotNull final String message, @Nullable final Player player) { - String processedMessage = message; - processedMessage = translateGradients(processedMessage); - processedMessage = PlaceholderManager.translatePlaceholders(processedMessage, player); - processedMessage = translateHexColorCodes(processedMessage); - processedMessage = ChatColor.translateAlternateColorCodes('&', processedMessage); - return processedMessage; + return format(message, player, FormatOption.WITH_PLACEHOLDERS); } /** - * Format a string without respect to a player. + * Format a string. + *

+ * Converts color codes and placeholders if specified. * * @param message The message to translate. + * @param option The format option. * @return The message, formatted. * @see StringUtils#format(String, Player) */ - public String format(@NotNull final String message) { - return format(message, null); + public String format(@NotNull final String message, + @NotNull final FormatOption option) { + return format(message, null, option); + } + + /** + * Format a string. + *

+ * Coverts color codes and placeholders for a player if specified. + * + * @param message The message to format. + * @param player The player to translate placeholders with respect to. + * @param option The format options. + * @return The message, formatted. + */ + public String format(@NotNull final String message, + @Nullable final Player player, + @NotNull final FormatOption option) { + String processedMessage = message; + processedMessage = translateGradients(processedMessage); + if (option == FormatOption.WITH_PLACEHOLDERS) { + processedMessage = PlaceholderManager.translatePlaceholders(processedMessage, player); + } + processedMessage = translateHexColorCodes(processedMessage); + processedMessage = ChatColor.translateAlternateColorCodes('&', processedMessage); + return processedMessage; } private static String translateHexColorCodes(@NotNull final String message) { @@ -271,4 +342,19 @@ public class StringUtils { GsonComponentSerializer.gson().deserialize(json) ); } + + /** + * Options for formatting. + */ + public enum FormatOption { + /** + * Completely formatted. + */ + WITH_PLACEHOLDERS, + + /** + * Completely formatted without placeholders. + */ + WITHOUT_PLACEHOLDERS + } } diff --git a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/config/json/EcoJSONConfigWrapper.kt b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/config/json/EcoJSONConfigWrapper.kt index 1bd75669..0d439db0 100644 --- a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/config/json/EcoJSONConfigWrapper.kt +++ b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/config/json/EcoJSONConfigWrapper.kt @@ -148,7 +148,7 @@ open class EcoJSONConfigWrapper : JSONConfig { for (map in maps) { configs.add(EcoJSONConfigSection(map)) } - return configs + return configs.toMutableList() } override fun getInt(path: String): Int { @@ -170,11 +170,11 @@ open class EcoJSONConfigWrapper : JSONConfig { return Objects.requireNonNullElse(getOfKnownType(path, Int::class.java), def) } - override fun getInts(path: String): List { - return Objects.requireNonNullElse(getOfKnownType(path, Any::class.java), ArrayList()) as List + override fun getInts(path: String): MutableList { + return (Objects.requireNonNullElse(getOfKnownType(path, Any::class.java), ArrayList()) as List).toMutableList() } - override fun getIntsOrNull(path: String): List? { + override fun getIntsOrNull(path: String): MutableList? { return if (has(path)) { getInts(path) } else { @@ -194,11 +194,11 @@ open class EcoJSONConfigWrapper : JSONConfig { } } - override fun getBools(path: String): List { - return Objects.requireNonNullElse(getOfKnownType(path, Any::class.java), ArrayList()) as List + override fun getBools(path: String): MutableList { + return (Objects.requireNonNullElse(getOfKnownType(path, Any::class.java), ArrayList()) as List).toMutableList() } - override fun getBoolsOrNull(path: String): List? { + override fun getBoolsOrNull(path: String): MutableList? { return if (has(path)) { getBools(path) } else { @@ -206,64 +206,44 @@ open class EcoJSONConfigWrapper : JSONConfig { } } - override fun getString(path: String): String { - return getString(path, true) - } - override fun getString( path: String, - format: Boolean + format: Boolean, + option: StringUtils.FormatOption ): String { val string = getOfKnownType(path, String::class.java) ?: "" - return if (format) StringUtils.format(string) else string - } - - override fun getStringOrNull(path: String): String? { - return if (has(path)) { - getString(path) - } else { - null - } + return if (format) StringUtils.format(string, option) else string } override fun getStringOrNull( path: String, - format: Boolean + format: Boolean, + option: StringUtils.FormatOption ): String? { return if (has(path)) { - getString(path, format) + getString(path, format, option) } else { null } } - override fun getStrings(path: String): List { - return getStrings(path, true) - } - override fun getStrings( path: String, - format: Boolean - ): List { + format: Boolean, + option: StringUtils.FormatOption + ): MutableList { val strings = - Objects.requireNonNullElse(getOfKnownType(path, Any::class.java), ArrayList()) as List - return if (format) StringUtils.formatList(strings) else strings - } - - override fun getStringsOrNull(path: String): List? { - return if (has(path)) { - getStrings(path) - } else { - null - } + (Objects.requireNonNullElse(getOfKnownType(path, Any::class.java), ArrayList()) as List).toMutableList() + return if (format) StringUtils.formatList(strings, option) else strings } override fun getStringsOrNull( path: String, - format: Boolean - ): List? { + format: Boolean, + option: StringUtils.FormatOption + ): MutableList? { return if (has(path)) { - getStrings(path, format) + getStrings(path, format, option) } else { null } @@ -281,11 +261,11 @@ open class EcoJSONConfigWrapper : JSONConfig { } } - override fun getDoubles(path: String): List { - return Objects.requireNonNullElse(getOfKnownType(path, Any::class.java), ArrayList()) as List + override fun getDoubles(path: String): MutableList { + return (Objects.requireNonNullElse(getOfKnownType(path, Any::class.java), ArrayList()) as List).toMutableList() } - override fun getDoublesOrNull(path: String): List? { + override fun getDoublesOrNull(path: String): MutableList? { return if (has(path)) { getDoubles(path) } else { diff --git a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/config/yaml/EcoYamlConfigWrapper.kt b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/config/yaml/EcoYamlConfigWrapper.kt index d1fea3e7..fc2a1c9b 100644 --- a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/config/yaml/EcoYamlConfigWrapper.kt +++ b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/config/yaml/EcoYamlConfigWrapper.kt @@ -10,7 +10,7 @@ import java.io.StringReader @Suppress("UNCHECKED_CAST") open class EcoYamlConfigWrapper : Config { lateinit var handle: T - private val cache: MutableMap = HashMap() + private val cache = mutableMapOf() protected fun init(config: T): Config { handle = config @@ -98,16 +98,16 @@ open class EcoYamlConfigWrapper : Config { } } - override fun getInts(path: String): List { + override fun getInts(path: String): MutableList { return if (cache.containsKey(path)) { - cache[path] as List + (cache[path] as MutableList).toMutableList() } else { cache[path] = if (has(path)) ArrayList(handle.getIntegerList(path)) else ArrayList() getInts(path) } } - override fun getIntsOrNull(path: String): List? { + override fun getIntsOrNull(path: String): MutableList? { return if (has(path)) { getInts(path) } else { @@ -132,9 +132,9 @@ open class EcoYamlConfigWrapper : Config { } } - override fun getBools(path: String): List { + override fun getBools(path: String): MutableList { return if (cache.containsKey(path)) { - cache[path] as List + (cache[path] as MutableList).toMutableList() } else { cache[path] = if (has(path)) ArrayList(handle.getBooleanList(path)) else ArrayList() @@ -142,7 +142,7 @@ open class EcoYamlConfigWrapper : Config { } } - override fun getBoolsOrNull(path: String): List? { + override fun getBoolsOrNull(path: String): MutableList? { return if (has(path)) { getBools(path) } else { @@ -150,92 +150,80 @@ open class EcoYamlConfigWrapper : Config { } } - override fun getString(path: String): String { - return getString(path, true) - } - override fun getString( path: String, - format: Boolean + format: Boolean, + option: StringUtils.FormatOption ): String { - if (format) { + if (format && option == StringUtils.FormatOption.WITHOUT_PLACEHOLDERS) { return if (cache.containsKey("$path\$FMT")) { cache["$path\$FMT"] as String } else { val string: String = handle.getString(path, "")!! - cache["$path\$FMT"] = StringUtils.format(string) - getString(path, format) + cache["$path\$FMT"] = StringUtils.format(string, option) + getString(path, format, option) } } else { - return if (cache.containsKey(path)) { + val string = if (cache.containsKey(path)) { cache[path] as String } else { cache[path] = handle.getString(path, "")!! - getString(path) + getString(path, format, option) } - } - } - override fun getStringOrNull(path: String): String? { - return if (has(path)) { - getString(path) - } else { - null + return if (format) StringUtils.format(string) else string } } override fun getStringOrNull( path: String, - format: Boolean + format: Boolean, + option: StringUtils.FormatOption ): String? { return if (has(path)) { - getString(path, format) + getString(path, format, option) } else { null } } - override fun getStrings(path: String): List { - return getStrings(path, true) - } - override fun getStrings( path: String, - format: Boolean - ): List { - if (format) { + format: Boolean, + option: StringUtils.FormatOption + ): MutableList { + if (format && option == StringUtils.FormatOption.WITHOUT_PLACEHOLDERS) { return if (cache.containsKey("$path\$FMT")) { - cache["$path\$FMT"] as List + (cache["$path\$FMT"] as MutableList).toMutableList() } else { val list = if (has(path)) handle.getStringList(path) else ArrayList() - cache["$path\$FMT"] = StringUtils.formatList(list); - getStrings(path, true) + cache["$path\$FMT"] = StringUtils.formatList(list, option) + getStrings(path, true, option) } } else { - return if (cache.containsKey(path)) { - cache[path] as List + val strings = if (cache.containsKey(path)) { + (cache[path] as MutableList).toMutableList() } else { cache[path] = if (has(path)) ArrayList(handle.getStringList(path)) else ArrayList() - getStrings(path, false) + getStrings(path, false, option) } - } - } - override fun getStringsOrNull(path: String): List? { - return if (has(path)) { - getStrings(path) - } else { - null + return if (format) { + StringUtils.formatList(strings, StringUtils.FormatOption.WITH_PLACEHOLDERS) + } else { + strings + } } } override fun getStringsOrNull( path: String, - format: Boolean - ): List? { + format: Boolean, + option: StringUtils.FormatOption + ): MutableList? { return if (has(path)) { - getStrings(path, format) + getStrings(path, format, option) } else { null } @@ -258,16 +246,16 @@ open class EcoYamlConfigWrapper : Config { } } - override fun getDoubles(path: String): List { + override fun getDoubles(path: String): MutableList { return if (cache.containsKey(path)) { - cache[path] as List + (cache[path] as MutableList).toMutableList() } else { cache[path] = if (has(path)) ArrayList(handle.getDoubleList(path)) else ArrayList() getDoubles(path) } } - override fun getDoublesOrNull(path: String): List? { + override fun getDoublesOrNull(path: String): MutableList? { return if (has(path)) { getDoubles(path) } else { diff --git a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/gui/menu/EcoMenuBuilder.kt b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/gui/menu/EcoMenuBuilder.kt index de863424..a8056079 100644 --- a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/gui/menu/EcoMenuBuilder.kt +++ b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/gui/menu/EcoMenuBuilder.kt @@ -61,10 +61,10 @@ class EcoMenuBuilder(private val rows: Int) : MenuBuilder { } } - val finalSlots: MutableList> = ArrayList() + val finalSlots = mutableListOf>() for (row in tempSlots) { - val tempRow = ArrayList() + val tempRow = mutableListOf() for (slot in row) { var tempSlot = slot if (tempSlot is FillerSlot) { diff --git a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/gui/slot/EcoSlotBuilder.kt b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/gui/slot/EcoSlotBuilder.kt index 15792759..a27c53f5 100644 --- a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/gui/slot/EcoSlotBuilder.kt +++ b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/gui/slot/EcoSlotBuilder.kt @@ -8,7 +8,7 @@ import com.willfp.eco.core.gui.slot.functional.SlotProvider class EcoSlotBuilder(private val provider: SlotProvider) : SlotBuilder { private var captive = false - var modifier: SlotModifier = SlotModifier{ player, menu, _ -> provider.provide(player, menu)} + private var modifier: SlotModifier = SlotModifier{ player, menu, _ -> provider.provide(player, menu)} private var onLeftClick = SlotHandler { _, _, _ -> run { } } diff --git a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/proxy/EcoProxyFactory.kt b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/proxy/EcoProxyFactory.kt index 0bbef769..b163ee5b 100644 --- a/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/proxy/EcoProxyFactory.kt +++ b/eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/proxy/EcoProxyFactory.kt @@ -1,4 +1,4 @@ -package com.willfp.eco.internal.proxy; +package com.willfp.eco.internal.proxy import com.willfp.eco.core.EcoPlugin import com.willfp.eco.core.PluginDependent