From d0b89d995c13cd8d3c680d67ebefe39421e9a6fa Mon Sep 17 00:00:00 2001 From: Telesphoreo Date: Sun, 20 Mar 2022 20:05:50 -0500 Subject: [PATCH] Add global gamerules --- .../java/dev/plex/command/impl/DebugCMD.java | 14 +++++- .../plex/services/impl/GameRuleService.java | 14 +++++- src/main/java/dev/plex/util/PlexUtils.java | 40 +++++++++------ src/main/resources/config.yml | 50 +++++++------------ 4 files changed, 67 insertions(+), 51 deletions(-) diff --git a/src/main/java/dev/plex/command/impl/DebugCMD.java b/src/main/java/dev/plex/command/impl/DebugCMD.java index 3cf0a55..b22326f 100644 --- a/src/main/java/dev/plex/command/impl/DebugCMD.java +++ b/src/main/java/dev/plex/command/impl/DebugCMD.java @@ -9,6 +9,7 @@ import dev.plex.rank.enums.Rank; import dev.plex.util.PlexLog; import dev.plex.util.PlexUtils; import java.util.List; +import java.util.Locale; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import org.bukkit.Bukkit; @@ -44,8 +45,17 @@ public class DebugCMD extends PlexCommand { for (World world : Bukkit.getWorlds()) { - PlexUtils.commitGameRules(world); - PlexLog.debug("Set gamerules for world: " + world.getName()); + PlexUtils.commitGlobalGameRules(world); + PlexLog.log("Set global gamerules for world: " + world.getName()); + } + for (String world : plugin.config.getConfigurationSection("worlds").getKeys(false)) + { + World bukkitWorld = Bukkit.getWorld(world); + if (bukkitWorld != null) + { + PlexUtils.commitSpecificGameRules(bukkitWorld); + PlexLog.log("Set specific gamerules for world: " + world.toLowerCase(Locale.ROOT)); + } } return mmString("Re-applied game all the game rules!"); } diff --git a/src/main/java/dev/plex/services/impl/GameRuleService.java b/src/main/java/dev/plex/services/impl/GameRuleService.java index f7f97b9..9da2300 100644 --- a/src/main/java/dev/plex/services/impl/GameRuleService.java +++ b/src/main/java/dev/plex/services/impl/GameRuleService.java @@ -3,6 +3,7 @@ package dev.plex.services.impl; import dev.plex.services.AbstractService; import dev.plex.util.PlexLog; import dev.plex.util.PlexUtils; +import java.util.Locale; import org.bukkit.Bukkit; import org.bukkit.World; @@ -18,8 +19,17 @@ public class GameRuleService extends AbstractService { for (World world : Bukkit.getWorlds()) { - PlexUtils.commitGameRules(world); - PlexLog.debug("Set gamerules for world: " + world.getName()); + PlexUtils.commitGlobalGameRules(world); + PlexLog.log("Set global gamerules for world: " + world.getName()); + } + for (String world : plugin.config.getConfigurationSection("worlds").getKeys(false)) + { + World bukkitWorld = Bukkit.getWorld(world); + if (bukkitWorld != null) + { + PlexUtils.commitSpecificGameRules(bukkitWorld); + PlexLog.log("Set specific gamerules for world: " + world.toLowerCase(Locale.ROOT)); + } } } diff --git a/src/main/java/dev/plex/util/PlexUtils.java b/src/main/java/dev/plex/util/PlexUtils.java index bf661a6..8729441 100644 --- a/src/main/java/dev/plex/util/PlexUtils.java +++ b/src/main/java/dev/plex/util/PlexUtils.java @@ -239,28 +239,40 @@ public class PlexUtils extends PlexBase } } - public static void commitGameRules(World world) + public static void commitGlobalGameRules(World world) + { + for (String s : Plex.get().config.getStringList("global_gamerules")) + { + readGameRules(world, s); + } + } + + public static void commitSpecificGameRules(World world) { for (String s : Plex.get().config.getStringList("worlds." + world.getName().toLowerCase(Locale.ROOT) + ".gameRules")) { - String gameRule = s.split(";")[0]; - T value = (T)s.split(";")[1]; - GameRule rule = (GameRule)GameRule.getByName(gameRule); - if (rule != null && check(value).getClass().equals(rule.getType())) - { - world.setGameRule(rule, value); - PlexLog.debug("Setting game rule " + gameRule + " for world " + world.getName() + " with value " + value); - } - else - { - PlexLog.error(String.format("Failed to set game rule %s for world %s with value %s!", gameRule, world.getName().toLowerCase(Locale.ROOT), value)); - } + readGameRules(world, s); + } + } + + private static void readGameRules(World world, String s) + { + String gameRule = s.split(";")[0]; + T value = (T)s.split(";")[1]; + GameRule rule = (GameRule)GameRule.getByName(gameRule); + if (rule != null && check(value).getClass().equals(rule.getType())) + { + world.setGameRule(rule, value); + PlexLog.debug("Setting game rule " + gameRule + " for world " + world.getName() + " with value " + value); + } + else + { + PlexLog.error(String.format("Failed to set game rule %s for world %s with value %s!", gameRule, world.getName().toLowerCase(Locale.ROOT), value)); } } public static Object check(T value) { - if (value.toString().equalsIgnoreCase("true") || value.toString().equalsIgnoreCase("false")) { return Boolean.parseBoolean(value.toString()); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 832a51a..821540c 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -47,25 +47,31 @@ data: password: "" # See https://plex.us.org/docs/customization/config#worlds for documentation +# These gamerules apply to all worlds on the server +global_gamerules: + - "doWeatherCycle;true" + - "doDaylightCycle;true" + - "doMobSpawning;false" + - "keepInventory;true" + - "doFireTick;false" + - "doMobLoot;false" + - "mobGriefing;false" + - "doTileDrops;false" + - "commandBlockOutput;false" + - "naturalRegeneration;true" + - "announceAdvancements;false" + - "showDeathMessages;false" + - "sendCommandFeedback;false" + worlds: flatlands: name: "Flatlands" permission: "plex.world.flatlands" noEdit: "&cYou can't edit this world!" gameRules: + # The gamerules here override the global gamerules - "doWeatherCycle;false" - "doDaylightCycle;false" - - "doMobSpawning;false" - - "keepInventory;true" - - "doFireTick;false" - - "doMobLoot;false" - - "mobGriefing;false" - - "doTileDrops;false" - - "commandBlockOutput;false" - - "naturalRegeneration;true" - - "announceAdvancements;false" - - "showDeathMessages;false" - - "sendCommandFeedback;false" parameters: grass_block: 1 dirt: 32 @@ -80,17 +86,6 @@ worlds: gameRules: - "doWeatherCycle;false" - "doDaylightCycle;false" - - "doMobSpawning;false" - - "keepInventory;true" - - "doFireTick;false" - - "doMobLoot;false" - - "mobGriefing;false" - - "doTileDrops;false" - - "commandBlockOutput;false" - - "naturalRegeneration;true" - - "announceAdvancements;false" - - "showDeathMessages;false" - - "sendCommandFeedback;false" parameters: grass_block: 1 dirt: 32 @@ -105,17 +100,6 @@ worlds: gameRules: - "doWeatherCycle;false" - "doDaylightCycle;false" - - "doMobSpawning;false" - - "keepInventory;true" - - "doFireTick;false" - - "doMobLoot;false" - - "mobGriefing;false" - - "doTileDrops;false" - - "commandBlockOutput;false" - - "naturalRegeneration;true" - - "announceAdvancements;false" - - "showDeathMessages;false" - - "sendCommandFeedback;false" parameters: grass_block: 1 dirt: 32