From d2e9d2fb9b433d44f7a7b945ef3ef0463ec2d3fc Mon Sep 17 00:00:00 2001 From: Andrew Katz Date: Tue, 1 Jun 2021 13:08:44 -0600 Subject: [PATCH 1/2] add getdouble --- build.gradle | 2 +- .../bukkit/command/BukkitCommandBuilder.java | 1 + bukkit/src/test/resources/rulebook.json | 91 ++++++++++++++++++- .../common/config/Configuration.java | 32 +++++++ .../common/config/providers/YamlProvider.java | 50 ++++++++++ 5 files changed, 173 insertions(+), 3 deletions(-) diff --git a/build.gradle b/build.gradle index cccc2194..0c5853fb 100644 --- a/build.gradle +++ b/build.gradle @@ -11,7 +11,7 @@ plugins { allprojects { group = "com.dumbdogdiner" - version = "3.0.3" + version = "3.0.4a" // java plugin is applied in subprojects apply plugin: "jacoco" diff --git a/bukkit/src/main/java/com/dumbdogdiner/stickyapi/bukkit/command/BukkitCommandBuilder.java b/bukkit/src/main/java/com/dumbdogdiner/stickyapi/bukkit/command/BukkitCommandBuilder.java index 3265f080..320da7b0 100644 --- a/bukkit/src/main/java/com/dumbdogdiner/stickyapi/bukkit/command/BukkitCommandBuilder.java +++ b/bukkit/src/main/java/com/dumbdogdiner/stickyapi/bukkit/command/BukkitCommandBuilder.java @@ -37,6 +37,7 @@ * * @since 2.0 */ +@Deprecated public class BukkitCommandBuilder extends CommandBuilder { // Hmm... diff --git a/bukkit/src/test/resources/rulebook.json b/bukkit/src/test/resources/rulebook.json index 2dbc04f8..1cea4c07 100644 --- a/bukkit/src/test/resources/rulebook.json +++ b/bukkit/src/test/resources/rulebook.json @@ -1,6 +1,93 @@ { "pages": [ [ + [ + { + "text": "Dumb Dog Diner MC\\nSeason 4 Handbook", + "bold": true, + "italic": true, + "color": "dark_purple" + }, + { + "text": "\\n\\n", + "color": "reset" + }, + { + "text": "Table of Contents:", + "bold": true, + "italic": true, + "underlined": true, + "color": "dark_red" + }, + { + "text": "\\n", + "color": "reset" + }, + { + "text": "Page 2: ", + "color": "red" + }, + { + "text": "Rules\\n", + "color": "reset" + }, + { + "text": "Pg. 3: ", + "color": "red" + }, + { + "text": "Gameplay\\n", + "color": "reset" + }, + { + "text": "Pg. 5: ", + "color": "red" + }, + { + "text": "Trading\\n", + "color": "reset" + }, + { + "text": "Pg. 7: ", + "color": "red" + }, + { + "text": "Commands\\n", + "color": "reset" + }, + { + "text": "Pg. 9: ", + "color": "red" + }, + { + "text": "Trading\\nCommands\\n", + "color": "reset" + }, + { + "text": "Pg. 10: ", + "color": "red" + }, + { + "text": "Extras\\n", + "color": "reset" + }, + { + "text": "Pg. 11: ", + "color": "red" + }, + { + "text": "Getting Help\\n", + "color": "reset" + }, + { + "text": "Pg. 13: ", + "color": "red" + }, + { + "text": "Credits", + "color": "reset" + } + ], { "text": "Dumb Dog Diner MC\\nSurvival Handbook", "bold": true, @@ -444,14 +531,14 @@ ], "title": "\u0026ddddMC Survival Handbook v1.0", "author": "Stixil", - "lore" : [ + "lore": [ { "text": "dddMC 2020", "bold": true, "color": "dark_purple" }, { - "text" : "", + "text": "", "color": "reset" } ] diff --git a/config/src/main/java/com/dumbdogdiner/stickyapi/common/config/Configuration.java b/config/src/main/java/com/dumbdogdiner/stickyapi/common/config/Configuration.java index 2665f057..d85495c4 100644 --- a/config/src/main/java/com/dumbdogdiner/stickyapi/common/config/Configuration.java +++ b/config/src/main/java/com/dumbdogdiner/stickyapi/common/config/Configuration.java @@ -22,4 +22,36 @@ public interface Configuration { * @return The requested string, or null. */ public String getString(String path); + + /** + * Gets the given double from the path. + * @param path The path of the double to get + * @return The requested double, or null. + */ + public Double getDouble(String path); + + /** + * Gets the given double from the path, returning the given default value if not found. + * @param path The path of the double to get + * @param def The default value if the path is not found or is not a double + * @return The requested double. + */ + public double getDouble(String path, double def); + + /** + * Gets the given integer from the path. + * @param path The path of the integer to get + * @return The requested integer, or null. + */ + public Integer getInt(String path); + + /** + * Gets the given integer from the path, returning the given default value if not found. + * @param path The path of the integer to get + * @param def The default value if the path is not found or is not an integer + * @return The requested integer. + */ + public int getInt(String path, int def); + + } diff --git a/config/src/main/java/com/dumbdogdiner/stickyapi/common/config/providers/YamlProvider.java b/config/src/main/java/com/dumbdogdiner/stickyapi/common/config/providers/YamlProvider.java index 3ea6d5a6..468e2c62 100644 --- a/config/src/main/java/com/dumbdogdiner/stickyapi/common/config/providers/YamlProvider.java +++ b/config/src/main/java/com/dumbdogdiner/stickyapi/common/config/providers/YamlProvider.java @@ -10,7 +10,11 @@ import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; +import java.util.Collections; +import java.util.HashSet; import java.util.Map; +import java.util.Objects; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import com.dumbdogdiner.stickyapi.common.config.FileConfiguration; @@ -60,6 +64,52 @@ public String getString(String path) { return (value != null) ? value.toString() : null; } + @Override + public Double getDouble(String path) { + Object value = this.data.get(path); + return (value instanceof Double) ? (double) value : null; + } + + @Override + public double getDouble(String path, double def) { + return Objects.requireNonNullElse(getDouble(path), def); + } + + @Override + public Integer getInt(String path) { + Object value = this.data.get(path); + return (value instanceof Integer) ? (int) value : null; + } + + @Override + public int getInt(String path, int def) { + return Objects.requireNonNullElse(getInt(path), def); + } + + /** + * Gets a generic type from the configuration + * @param path The path of the object to get in the configuration + * @param def The default value + * @param The type of the object to get + * @return The requested object, or the default value + */ + public T getAs(String path, T def){ + Object value = this.data.get(path); + try { + return value != null ? (T) value : def; + } catch (ClassCastException e) { + return def; + } + } + + /** + * Gets a {@link Set} of all of the paths that were loaded + * @return A set of all loaded paths + */ + public Set getPaths(){ + return new HashSet<>(Collections.list(data.keys())); + } + @Override public boolean save(String path) { FileWriter fileWriter; From bfc11d68bdadd3014d7369f74515f2fdb2a88730 Mon Sep 17 00:00:00 2001 From: Andrew Katz Date: Sat, 5 Jun 2021 11:33:11 -0600 Subject: [PATCH 2/2] Minor changes to LocaleProvider; Fix nested params in YamlProvider --- common/build.gradle | 3 +- .../common/translation/LocaleProvider.java | 5 ++- .../translation/LocaleProviderTest.java | 5 +++ .../localeprovider/group1/messages.en_us.yml | 10 +++-- .../common/config/Configuration.java | 34 +++------------ .../common/config/providers/YamlProvider.java | 41 +++++++++++++++---- 6 files changed, 55 insertions(+), 43 deletions(-) diff --git a/common/build.gradle b/common/build.gradle index 6a5d3468..3ad84694 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -18,7 +18,8 @@ dependencies { // Font width data (see above) dddResource "dumbdogdiner:mc-font-extractor:main:mojangles_width_data@json" - implementation "com.google.code.gson:gson:2.8.7" + // Expose newer gson + api "com.google.code.gson:gson:2.8.7" // Dependencies available in both bukkit and bungee compileOnly "net.md-5:bungeecord-chat:1.16-R0.5-SNAPSHOT" diff --git a/common/src/main/java/com/dumbdogdiner/stickyapi/common/translation/LocaleProvider.java b/common/src/main/java/com/dumbdogdiner/stickyapi/common/translation/LocaleProvider.java index cb1db7fd..f9540ff0 100644 --- a/common/src/main/java/com/dumbdogdiner/stickyapi/common/translation/LocaleProvider.java +++ b/common/src/main/java/com/dumbdogdiner/stickyapi/common/translation/LocaleProvider.java @@ -5,6 +5,7 @@ package com.dumbdogdiner.stickyapi.common.translation; import java.io.File; +import java.util.HashMap; import java.util.concurrent.ConcurrentHashMap; import java.util.Map; import java.util.TreeMap; @@ -269,7 +270,7 @@ private void checkForLoadedLocale(@NotNull String name) throws IllegalArgumentEx * * @return {@link java.util.TreeMap} */ - public TreeMap newVariables() { - return new TreeMap(String.CASE_INSENSITIVE_ORDER); + public static Map newVariables() { + return new HashMap<>(); } } diff --git a/common/src/test/java/com/dumbdogdiner/stickyapi/common/translation/LocaleProviderTest.java b/common/src/test/java/com/dumbdogdiner/stickyapi/common/translation/LocaleProviderTest.java index cb8948c9..7f2a7826 100644 --- a/common/src/test/java/com/dumbdogdiner/stickyapi/common/translation/LocaleProviderTest.java +++ b/common/src/test/java/com/dumbdogdiner/stickyapi/common/translation/LocaleProviderTest.java @@ -12,6 +12,7 @@ import java.io.File; import java.util.HashMap; +import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import com.dumbdogdiner.stickyapi.common.util.Debugger; @@ -130,6 +131,10 @@ public void testTranslate() { // Translate changes & to § for ChatColor codes assertEquals("Msg>§cError! The player Notch has not joined before!", output); + Map vars2 = localeProviderGroup1.newVariables(); + vars2.put("player", "Rodwuff"); + System.out.println( + localeProviderGroup1.translate("afk.afk-m", vars2)); } @Test diff --git a/common/src/test/resources/localeprovider/group1/messages.en_us.yml b/common/src/test/resources/localeprovider/group1/messages.en_us.yml index 9f62345e..19ecce25 100644 --- a/common/src/test/resources/localeprovider/group1/messages.en_us.yml +++ b/common/src/test/resources/localeprovider/group1/messages.en_us.yml @@ -21,6 +21,10 @@ player-has-not-joined: "{prefix}&cError! The player {target} has not joined befo must-be-player: "{prefix}&cYou must be a player to execute this command!" not-online-player: "{prefix}&c Error! {player} is not online!" invalid-group: "{prefix}&cError! Group {GROUP} is invalid!" +afk: + not-afk: "&7&o* {player} is no longer AFK!" + afk-m: "&7&o* {player} is now AFK!" + afk-kick: "&cYou have been AFK for {time|duration} and have been kicked!" # The config supports new lines (\n), it can be used on any node whois-message: "{prefix}&bWhois data for {target}\n{newline}&b{target_online|yesno:'&aOnline,&cOffline'} &bsince: &f{target_last_seen|expiry}\n{newline}&bIP: &f{target_ipaddress}\n{newline}&bFirst Login: &f{target_first_seen|expiry}\n{newline}&bLast Login: &f{target_last_seen|expiry}\n{newline}&bLast Server: &f{target_last_server}\n{newline}&bFly/Walk: &f{target_fly_speed}&b / &f{target_walk_speed}" @@ -51,10 +55,8 @@ sell: no-sales: "{prefix}&bThere have been no sales!" paginator: "{prefix}&bpage &f{current}&b/&f{total}" -afk: - not-afk: "&7&o* {player} is no longer AFK!" - afk: "&7&o* {player} is now AFK!" - afk-kick: "&cYou have been AFK for {time|duration} and have been kicked!" + + hat: no-hat: "{prefix}&bYou do not have a hat!" diff --git a/config/src/main/java/com/dumbdogdiner/stickyapi/common/config/Configuration.java b/config/src/main/java/com/dumbdogdiner/stickyapi/common/config/Configuration.java index d85495c4..6b75fd10 100644 --- a/config/src/main/java/com/dumbdogdiner/stickyapi/common/config/Configuration.java +++ b/config/src/main/java/com/dumbdogdiner/stickyapi/common/config/Configuration.java @@ -15,7 +15,7 @@ public interface Configuration { * @return The requested string. */ public String getString(String path, String def); - + /** * Gets the given string from the path. * @param path The path of the string to get @@ -23,35 +23,11 @@ public interface Configuration { */ public String getString(String path); - /** - * Gets the given double from the path. - * @param path The path of the double to get - * @return The requested double, or null. - */ - public Double getDouble(String path); - - /** - * Gets the given double from the path, returning the given default value if not found. - * @param path The path of the double to get - * @param def The default value if the path is not found or is not a double - * @return The requested double. - */ - public double getDouble(String path, double def); + Double getDouble(String path); - /** - * Gets the given integer from the path. - * @param path The path of the integer to get - * @return The requested integer, or null. - */ - public Integer getInt(String path); - - /** - * Gets the given integer from the path, returning the given default value if not found. - * @param path The path of the integer to get - * @param def The default value if the path is not found or is not an integer - * @return The requested integer. - */ - public int getInt(String path, int def); + double getDouble(String path, double def); + Integer getInt(String path); + int getInt(String path, int def); } diff --git a/config/src/main/java/com/dumbdogdiner/stickyapi/common/config/providers/YamlProvider.java b/config/src/main/java/com/dumbdogdiner/stickyapi/common/config/providers/YamlProvider.java index 468e2c62..e4ba7d0d 100644 --- a/config/src/main/java/com/dumbdogdiner/stickyapi/common/config/providers/YamlProvider.java +++ b/config/src/main/java/com/dumbdogdiner/stickyapi/common/config/providers/YamlProvider.java @@ -10,6 +10,7 @@ import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; +import java.lang.reflect.Type; import java.util.Collections; import java.util.HashSet; import java.util.Map; @@ -20,11 +21,11 @@ import com.dumbdogdiner.stickyapi.common.config.FileConfiguration; import org.apache.commons.lang.Validate; +import org.jetbrains.annotations.Nullable; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; public class YamlProvider implements FileConfiguration { - private ConcurrentHashMap data; private InputStream inputStream; @@ -54,19 +55,18 @@ public void reload() { @Override public String getString(String path, String def) { - Object value = this.data.get(path); - return (value != null) ? value.toString() : def; + return getAs(path, def); } @Override public String getString(String path) { - Object value = this.data.get(path); + Object value = get(path); return (value != null) ? value.toString() : null; } @Override public Double getDouble(String path) { - Object value = this.data.get(path); + Object value = get(path); return (value instanceof Double) ? (double) value : null; } @@ -77,7 +77,7 @@ public double getDouble(String path, double def) { @Override public Integer getInt(String path) { - Object value = this.data.get(path); + Object value = get(path); return (value instanceof Integer) ? (int) value : null; } @@ -94,7 +94,7 @@ public int getInt(String path, int def) { * @return The requested object, or the default value */ public T getAs(String path, T def){ - Object value = this.data.get(path); + Object value = get(path); try { return value != null ? (T) value : def; } catch (ClassCastException e) { @@ -102,6 +102,33 @@ public T getAs(String path, T def){ } } + /** + * Generalized convinience version of {@link #get(String, Map)} + * @param path The path of the object + * @return The value at the key of the path + */ + private @Nullable Object get(String path){ + return get(path, data); + } + + /** + * Internal method to deal with recursive, nested map params + * @param path The path of the object + * @param map The map we are working with + * @return The value of that key at the path + */ + private @Nullable Object get(String path, Map map){ + if(path == null) + return null; + // Split off the head of a path, if it exists + String [] splitPath = path.split("\\.", 2); + if(splitPath.length == 2 && map.containsKey(splitPath[0]) && map.get(splitPath[0]) instanceof Map){ + return get(splitPath[1], (Map) map.get(splitPath[0])); + } + + return map.containsKey(splitPath[0]) ? map.get(splitPath[0]) : null; + } + /** * Gets a {@link Set} of all of the paths that were loaded * @return A set of all loaded paths