From 6455f15d924fb8e2c6135293affdf3b6ec914e45 Mon Sep 17 00:00:00 2001 From: William Date: Mon, 24 Jul 2023 11:42:09 +0100 Subject: [PATCH] Fixup config map reading inconsistencies, close #471 --- build.gradle | 6 +- bukkit/build.gradle | 2 +- .../huskhomes/BukkitPluginTests.java | 7 ++ common/build.gradle | 2 +- .../william278/huskhomes/config/Settings.java | 88 ++++++++++++------- .../huskhomes/database/Database.java | 11 ++- .../huskhomes/util/TransactionResolver.java | 26 +++++- docs/Config-Files.md | 28 +++--- paper/build.gradle | 2 +- 9 files changed, 114 insertions(+), 58 deletions(-) diff --git a/build.gradle b/build.gradle index 34dc185f..ade403b4 100644 --- a/build.gradle +++ b/build.gradle @@ -59,9 +59,9 @@ allprojects { } dependencies { - testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.3' - testImplementation 'org.junit.jupiter:junit-jupiter-params:5.9.3' - testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.9.3' + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.0' + testImplementation 'org.junit.jupiter:junit-jupiter-params:5.10.0' + testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.10.0' } license { diff --git a/bukkit/build.gradle b/bukkit/build.gradle index a2060c07..cc150952 100644 --- a/bukkit/build.gradle +++ b/bukkit/build.gradle @@ -10,7 +10,7 @@ dependencies { compileOnly 'org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT' compileOnly 'org.jetbrains:annotations:24.0.1' compileOnly 'de.themoep:minedown-adventure:1.7.2-SNAPSHOT' - compileOnly 'net.william278:annotaml:2.0.4' + compileOnly 'net.william278:annotaml:2.0.5' compileOnly 'net.william278:DesertWell:2.0.4' compileOnly 'com.github.MilkBowl:VaultAPI:1.7.1' compileOnly 'me.clip:placeholderapi:2.11.3' diff --git a/bukkit/src/test/java/net/william278/huskhomes/BukkitPluginTests.java b/bukkit/src/test/java/net/william278/huskhomes/BukkitPluginTests.java index d8fe5804..28e014cb 100644 --- a/bukkit/src/test/java/net/william278/huskhomes/BukkitPluginTests.java +++ b/bukkit/src/test/java/net/william278/huskhomes/BukkitPluginTests.java @@ -89,6 +89,13 @@ public class BukkitPluginTests { Assertions.assertNotNull(BukkitUser.adapt(player, plugin)); } + @Test + @DisplayName("Config Reload & Read Test") + public void testConfigReload() { + plugin.reloadConfig(); + Assertions.assertNotNull(plugin.getConfig()); + } + } @Nested diff --git a/common/build.gradle b/common/build.gradle index e8905f07..710baae0 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -7,7 +7,7 @@ dependencies { api 'org.apache.commons:commons-text:1.10.0' api 'de.themoep:minedown-adventure:1.7.2-SNAPSHOT' api 'com.google.code.gson:gson:2.10.1' - api 'net.william278:annotaml:2.0.4' + api 'net.william278:annotaml:2.0.5' api 'net.william278:PagineDown:1.1' api 'net.william278:DesertWell:2.0.4' api 'dev.dejvokep:boosted-yaml:1.3.1' diff --git a/common/src/main/java/net/william278/huskhomes/config/Settings.java b/common/src/main/java/net/william278/huskhomes/config/Settings.java index 6be3e8c0..e229e69a 100644 --- a/common/src/main/java/net/william278/huskhomes/config/Settings.java +++ b/common/src/main/java/net/william278/huskhomes/config/Settings.java @@ -100,7 +100,7 @@ public class Settings { @YamlComment("Names of tables to use on your database. Don't modify this unless you know what you're doing!") @YamlKey("database.table_names") - private Map tableNames = Database.Table.getConfigMap(); + private TreeMap tableNames = new TreeMap<>(Database.Table.getConfigMap()); // General settings @@ -113,7 +113,7 @@ public class Settings { @YamlKey("general.max_public_homes") private int maxPublicHomes = 10; - @YamlComment("Whether permission limits (i.e. huskhomes.max_homes.)" + @YamlComment("Whether permission limits (i.e. huskhomes.max_homes.) " + "should stack if the user inherits multiple nodes.") @YamlKey("general.stack_permission_limits") private boolean stackPermissionLimits = false; @@ -176,7 +176,7 @@ public class Settings { @YamlComment("Which sound effects to play for various actions") @YamlKey("general.sound_effects") - private Map soundEffects = SoundEffectAction.getConfigMap(); + private TreeMap soundEffects = new TreeMap<>(SoundEffectAction.getConfigMap()); @YamlComment("Whether to provide modern, rich TAB suggestions for commands (if available)") @YamlKey("general.brigadier_tab_completion") @@ -252,12 +252,7 @@ public class Settings { @YamlComment("Set a cooldown between performing actions (in seconds). Docs: https://william278.net/docs/huskhomes/cooldowns/") @YamlKey("cooldowns.cooldown_times") - private Map cooldownTimes = Arrays.stream(TransactionResolver.Action.values()) - .filter(action -> action.getDefaultCooldown() > 0) - .collect(Collectors.toMap( - action -> action.name().toLowerCase(Locale.ENGLISH), - TransactionResolver.Action::getDefaultCooldown) - ); + private TreeMap cooldownTimes = new TreeMap<>(TransactionResolver.Action.getCooldownsConfigMap()); // Economy settings @@ -271,12 +266,7 @@ public class Settings { @YamlComment("Charge money for perform certain actions. Docs: https://william278.net/docs/huskhomes/economy-hook/") @YamlKey("economy.costs") - private Map economyCosts = Arrays.stream(TransactionResolver.Action.values()) - .filter(action -> action.getDefaultCost() > 0) - .collect(Collectors.toMap( - action -> action.name().toLowerCase(Locale.ENGLISH), - TransactionResolver.Action::getDefaultCost) - ); + private TreeMap economyCosts = new TreeMap<>(TransactionResolver.Action.getEconomyCostsConfigMap()); // Mapping plugins @YamlComment("Display public homes/warps on your Dynmap, BlueMap or Pl3xMap. Docs: https://william278.net/docs/huskhomes/map-hooks") @@ -302,6 +292,7 @@ public class Settings { private Settings() { } + @NotNull public String getLanguage() { return language; } @@ -310,10 +301,12 @@ public class Settings { return checkForUpdates; } + @NotNull public Database.Type getDatabaseType() { return databaseType; } + @NotNull public String getMySqlHost() { return mySqlHost; } @@ -322,18 +315,22 @@ public class Settings { return mySqlPort; } + @NotNull public String getMySqlDatabase() { return mySqlDatabase; } + @NotNull public String getMySqlUsername() { return mySqlUsername; } + @NotNull public String getMySqlPassword() { return mySqlPassword; } + @NotNull public String getMySqlConnectionParameters() { return mySqlConnectionParameters; } @@ -358,13 +355,16 @@ public class Settings { return mySqlConnectionPoolTimeout; } - public Map getTableNames() { + @NotNull + public TreeMap getTableNames() { + final TreeMap tableNames = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + tableNames.putAll(this.tableNames); return tableNames; } @NotNull public String getTableName(@NotNull Database.Table table) { - return Optional.ofNullable(getTableNames().get(table.name().toLowerCase())).orElse(table.getName()); + return Optional.ofNullable(getTableNames().get(table.name())).orElse(table.getDefaultName()); } public int getMaxHomes() { @@ -391,6 +391,7 @@ public class Settings { return teleportWarmupTime; } + @NotNull public Locales.DisplaySlot getTeleportWarmupDisplay() { return teleportWarmupDisplay; } @@ -435,21 +436,30 @@ public class Settings { return playSoundEffects; } + @NotNull + public TreeMap getSoundEffects() { + final TreeMap soundEffects = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + soundEffects.putAll(this.soundEffects); + return soundEffects; + } + public Optional getSoundEffect(@NotNull SoundEffectAction action) { if (!doPlaySoundEffects()) { return Optional.empty(); } - return Optional.ofNullable(soundEffects.get(action.name().toLowerCase())); + return Optional.ofNullable(getSoundEffects().get(action.name())); } public boolean doCrossServer() { return crossServer; } + @NotNull public Broker.Type getBrokerType() { return messageBrokerType; } + @NotNull public String getClusterId() { return clusterId; } @@ -458,6 +468,7 @@ public class Settings { return globalSpawn; } + @NotNull public String getGlobalSpawnName() { return globalSpawnName; } @@ -466,6 +477,7 @@ public class Settings { return globalRespawning; } + @NotNull public String getRedisHost() { return redisHost; } @@ -474,6 +486,7 @@ public class Settings { return redisPort; } + @NotNull public String getRedisPassword() { return redisPassword; } @@ -511,11 +524,18 @@ public class Settings { return cooldowns; } + @NotNull + public TreeMap getCooldownTimes() { + final TreeMap cooldownTimes = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + cooldownTimes.putAll(this.cooldownTimes); + return cooldownTimes; + } + public long getCooldown(@NotNull TransactionResolver.Action action) { if (!doCooldowns()) { return 0; } - final Integer cooldown = cooldownTimes.get(action.name().toLowerCase(Locale.ENGLISH)); + final Integer cooldown = getCooldownTimes().get(action.name()); if (cooldown != null && cooldown > 0) { return cooldown; } @@ -530,11 +550,18 @@ public class Settings { return freeHomeSlots; } + @NotNull + public TreeMap getEconomyCosts() { + final TreeMap economyCosts = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + economyCosts.putAll(this.economyCosts); + return economyCosts; + } + public Optional getEconomyCost(@NotNull TransactionResolver.Action action) { if (!doEconomy()) { return Optional.empty(); } - final Double cost = economyCosts.get(action.name().toLowerCase(Locale.ENGLISH)); + final Double cost = getEconomyCosts().get(action.name()); if (cost != null && cost > 0d) { return Optional.of(cost); } @@ -554,11 +581,12 @@ public class Settings { } public boolean isCommandDisabled(Command type) { - return disabledCommands.stream().anyMatch(disabled -> { - final String command = (disabled.startsWith("/") ? disabled.substring(1) : disabled); - return command.equalsIgnoreCase(type.getName()) - || type.getAliases().stream().anyMatch(alias -> alias.equalsIgnoreCase(command)); - }); + return disabledCommands.stream() + .anyMatch(disabled -> { + final String command = (disabled.startsWith("/") ? disabled.substring(1) : disabled); + return command.equalsIgnoreCase(type.getName()) + || type.getAliases().stream().anyMatch(alias -> alias.equalsIgnoreCase(command)); + }); } public boolean doBrigadierTabCompletion() { @@ -573,6 +601,7 @@ public class Settings { TELEPORTATION_WARMUP("block.note_block.banjo"), TELEPORTATION_CANCELLED("entity.item.break"), TELEPORT_REQUEST_RECEIVED("entity.experience_orb.pickup"); + private final String defaultEffect; SoundEffectAction(@NotNull String defaultEffect) { @@ -581,11 +610,10 @@ public class Settings { @NotNull public static Map getConfigMap() { - return Arrays.stream(values()) - .collect(Collectors.toMap( - SoundEffectAction::name, - action -> action.defaultEffect) - ); + return Arrays.stream(values()).collect(Collectors.toMap( + action -> action.name().toLowerCase(Locale.ENGLISH), + action -> action.defaultEffect + )); } } diff --git a/common/src/main/java/net/william278/huskhomes/database/Database.java b/common/src/main/java/net/william278/huskhomes/database/Database.java index 98d4225f..9f2cdd60 100644 --- a/common/src/main/java/net/william278/huskhomes/database/Database.java +++ b/common/src/main/java/net/william278/huskhomes/database/Database.java @@ -474,17 +474,16 @@ public abstract class Database { } @NotNull - public String getName() { + public String getDefaultName() { return defaultName; } @NotNull public static Map getConfigMap() { - return Arrays.stream(values()) - .collect(Collectors.toMap( - Table::name, - action -> action.defaultName - )); + return Arrays.stream(values()).collect(Collectors.toMap( + table -> table.name().toLowerCase(Locale.ENGLISH), + Table::getDefaultName + )); } } diff --git a/common/src/main/java/net/william278/huskhomes/util/TransactionResolver.java b/common/src/main/java/net/william278/huskhomes/util/TransactionResolver.java index 02b7d5b9..507ad11f 100644 --- a/common/src/main/java/net/william278/huskhomes/util/TransactionResolver.java +++ b/common/src/main/java/net/william278/huskhomes/util/TransactionResolver.java @@ -26,8 +26,8 @@ import org.jetbrains.annotations.NotNull; import java.time.Duration; import java.time.Instant; -import java.util.Optional; -import java.util.StringJoiner; +import java.util.*; +import java.util.stream.Collectors; /** * Class for validating and performing economy and cooldown transactions involving {@link Action}s. @@ -229,5 +229,27 @@ public interface TransactionResolver { return defaultCooldown; } + // Get the default economy action cost map for the config + @NotNull + public static Map getEconomyCostsConfigMap() { + return Arrays.stream(values()) + .filter(action -> action.getDefaultCost() > 0) + .collect(Collectors.toMap( + action -> action.name().toLowerCase(Locale.ENGLISH), + Action::getDefaultCost + )); + } + + // Get the default cooldown action time map for the config + @NotNull + public static Map getCooldownsConfigMap() { + return Arrays.stream(values()) + .filter(action -> action.getDefaultCooldown() > 0) + .collect(Collectors.toMap( + action -> action.name().toLowerCase(Locale.ENGLISH), + Action::getDefaultCooldown + )); + } + } } diff --git a/docs/Config-Files.md b/docs/Config-Files.md index c389e584..0fed214a 100644 --- a/docs/Config-Files.md +++ b/docs/Config-Files.md @@ -44,13 +44,13 @@ database: timeout: 20000 # Names of tables to use on your database. Don't modify this unless you know what you're doing! table_names: - position_data: huskhomes_position_data - warp_data: huskhomes_warps - saved_position_data: huskhomes_saved_positions - player_cooldowns_data: huskhomes_user_cooldowns home_data: huskhomes_homes - teleport_data: huskhomes_teleports + player_cooldowns_data: huskhomes_user_cooldowns player_data: huskhomes_users + position_data: huskhomes_position_data + saved_position_data: huskhomes_saved_positions + teleport_data: huskhomes_teleports + warp_data: huskhomes_warps general: # The maximum homes a user can create. Override with the huskhomes.max_homes. permission. max_homes: 10 @@ -60,7 +60,7 @@ general: stack_permission_limits: false # Whether users require a permission (huskhomes.command.warp.) to use warps permission_restrict_warps: false - # Whether running /sethome or /setwarp when a home/warp already exists should overwrite it. + # Whether running /sethome or /setwarp when a home/warp already exists should overwrite. overwrite_existing_homes_warps: true # How long a player has to stand still and not take damage for when teleporting (in seconds) teleport_warmup_time: 5 @@ -78,7 +78,7 @@ general: allow_unicode_descriptions: true # Whether /back should work to teleport the user to where they died back_command_return_by_death: true - # Whether /back should work with other plugins that use the PlayerTeleportEvent (this can cause conflicts) + # Whether /back should work with other plugins that use the PlayerTeleportEvent (can cause conflicts) back_command_save_teleport_event: false # How many items should be displayed per-page in chat menu lists list_items_per_page: 12 @@ -88,10 +88,10 @@ general: play_sound_effects: true # Which sound effects to play for various actions sound_effects: - teleportation_cancelled: entity.item.break - teleportation_warmup: block.note_block.banjo - teleportation_complete: entity.enderman.teleport teleport_request_received: entity.experience_orb.pickup + teleportation_cancelled: entity.item.break + teleportation_complete: entity.enderman.teleport + teleportation_warmup: block.note_block.banjo # Whether to provide modern, rich TAB suggestions for commands (if available) brigadier_tab_completion: true cross_server: @@ -121,12 +121,12 @@ rtp: spawn_radius: 500 # Mean of the normal distribution used to calculate the distance from the center of the world distribution_mean: 0.75 - # Standard deviation of the normal distribution used to calculate the distance from the center of the world + # Standard deviation of the normal distribution for distributing players randomly distribution_deviation: 2.0 # List of worlds in which /rtp is disabled. Please note that /rtp does not work well in the nether. restricted_worlds: - - world_nether - - world_the_end + - world_nether + - world_the_end cooldowns: # Whether to apply a cooldown between performing certain actions enabled: true @@ -140,8 +140,8 @@ economy: free_home_slots: 5 # Charge money for perform certain actions. Docs: https://william278.net/docs/huskhomes/economy-hook/ costs: - make_home_public: 50.0 additional_home_slot: 100.0 + make_home_public: 50.0 random_teleport: 25.0 map_hook: # Display public homes/warps on your Dynmap, BlueMap or Pl3xMap. Docs: https://william278.net/docs/huskhomes/map-hooks diff --git a/paper/build.gradle b/paper/build.gradle index bd094734..e1274c32 100644 --- a/paper/build.gradle +++ b/paper/build.gradle @@ -11,7 +11,7 @@ dependencies { compileOnly 'org.bstats:bstats-bukkit:3.0.2' compileOnly 'org.jetbrains:annotations:24.0.1' compileOnly 'de.themoep:minedown-adventure:1.7.2-SNAPSHOT' - compileOnly 'net.william278:annotaml:2.0.4' + compileOnly 'net.william278:annotaml:2.0.5' compileOnly 'net.william278:DesertWell:2.0.4' compileOnly 'me.lucko:commodore:2.2' compileOnly 'com.github.MilkBowl:VaultAPI:1.7.1'