From 9db73b7bae2ff9224462045d7385b9e9e55316b4 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Sun, 21 Apr 2024 23:10:26 +0200 Subject: [PATCH] Adds an option for disabling startup fixes Also fixes configuration default values not being used when the option is missing from the configuration file. --- .../stargate/config/ConfigOption.java | 5 ++++ .../stargate/config/StargateConfig.java | 11 +++----- .../stargate/config/StargateGateConfig.java | 9 +++++++ .../stargate/utility/PortalFileHelper.java | 27 +++++++++++-------- src/main/resources/config.yml | 5 ++++ 5 files changed, 39 insertions(+), 18 deletions(-) diff --git a/src/main/java/net/knarcraft/stargate/config/ConfigOption.java b/src/main/java/net/knarcraft/stargate/config/ConfigOption.java index 5c41c71..b1f4137 100644 --- a/src/main/java/net/knarcraft/stargate/config/ConfigOption.java +++ b/src/main/java/net/knarcraft/stargate/config/ConfigOption.java @@ -66,6 +66,11 @@ public enum ConfigOption { */ DESTROYED_BY_EXPLOSION("gates.integrity.destroyedByExplosion", "Whether stargates should be destroyed by explosions", false), + /** + * Whether to fix incorrect signs, buttons or openings during startup + */ + APPLY_STARTUP_FIXES("gates.integrity.applyStartupFixes", "Whether Stargates should fix incorrect signs, buttons or openings during startup", true), + /** * Whether to verify each portal's gate layout after each load */ diff --git a/src/main/java/net/knarcraft/stargate/config/StargateConfig.java b/src/main/java/net/knarcraft/stargate/config/StargateConfig.java index 169a7eb..9caf1ce 100644 --- a/src/main/java/net/knarcraft/stargate/config/StargateConfig.java +++ b/src/main/java/net/knarcraft/stargate/config/StargateConfig.java @@ -418,13 +418,10 @@ public final class StargateConfig { //Load the option using its correct data type switch (option.getDataType()) { case STRING_LIST -> optionValue = newConfig.getStringList(configNode); - case STRING -> { - String value = newConfig.getString(configNode); - optionValue = value != null ? value.trim() : ""; - } - case BOOLEAN -> optionValue = newConfig.getBoolean(configNode); - case INTEGER -> optionValue = newConfig.getInt(configNode); - case DOUBLE -> optionValue = newConfig.getDouble(configNode); + case STRING -> optionValue = newConfig.getString(configNode, (String) option.getDefaultValue()).trim(); + case BOOLEAN -> optionValue = newConfig.getBoolean(configNode, (boolean) option.getDefaultValue()); + case INTEGER -> optionValue = newConfig.getInt(configNode, (int) option.getDefaultValue()); + case DOUBLE -> optionValue = newConfig.getDouble(configNode, (double) option.getDefaultValue()); default -> throw new IllegalArgumentException("Invalid config data type encountered"); } configOptions.put(option, optionValue); diff --git a/src/main/java/net/knarcraft/stargate/config/StargateGateConfig.java b/src/main/java/net/knarcraft/stargate/config/StargateGateConfig.java index f3dc071..32ff7a4 100644 --- a/src/main/java/net/knarcraft/stargate/config/StargateGateConfig.java +++ b/src/main/java/net/knarcraft/stargate/config/StargateGateConfig.java @@ -195,6 +195,15 @@ public final class StargateGateConfig { return (boolean) configOptions.get(ConfigOption.DESTROYED_BY_EXPLOSION); } + /** + * Gets whether to destroy portals when any blocks are broken by explosions + * + * @return

Whether to destroy portals when any blocks are broken by explosions

+ */ + public boolean applyStartupFixes() { + return (boolean) configOptions.get(ConfigOption.APPLY_STARTUP_FIXES); + } + /** * Gets the default portal network to use if no other network is given * diff --git a/src/main/java/net/knarcraft/stargate/utility/PortalFileHelper.java b/src/main/java/net/knarcraft/stargate/utility/PortalFileHelper.java index 9a9962e..b983ecc 100644 --- a/src/main/java/net/knarcraft/stargate/utility/PortalFileHelper.java +++ b/src/main/java/net/knarcraft/stargate/utility/PortalFileHelper.java @@ -236,16 +236,19 @@ public final class PortalFileHelper { Stargate.logInfo(String.format("{%s} Loaded %d stargates with %d set as always-on", world.getName(), portalCount, openCount)); - //Re-draw the signs in case a bug in the config prevented the portal from loading and has been fixed since - Stargate.debug("PortalFileHelper::doPostLoadTasks::update", - String.format("Updating portal signs/buttons for %s", world)); - for (Portal portal : PortalRegistry.getAllPortals()) { - if (portal.isRegistered() && portal.getWorld() != null && portal.getWorld().equals(world) && - world.getWorldBorder().isInside(portal.getSignLocation())) { - portal.drawSign(); - updatePortalButton(portal); - Stargate.debug("UpdateSignsButtons", String.format("Updated sign and button for portal %s", - portal.getName())); + + if (Stargate.getGateConfig().applyStartupFixes()) { + //Re-draw the signs in case a bug in the config prevented the portal from loading and has been fixed since + Stargate.debug("PortalFileHelper::doPostLoadTasks::update", + String.format("Updating portal signs/buttons for %s", world)); + for (Portal portal : PortalRegistry.getAllPortals()) { + if (portal.isRegistered() && portal.getWorld() != null && portal.getWorld().equals(world) && + world.getWorldBorder().isInside(portal.getSignLocation())) { + portal.drawSign(); + updatePortalButton(portal); + Stargate.debug("UpdateSignsButtons", String.format("Updated sign and button for portal %s", + portal.getName())); + } } } //Save the portals to disk to update with any changes @@ -299,7 +302,9 @@ public final class PortalFileHelper { //Register the portal, and close it in case it wasn't properly closed when the server stopped boolean buttonLocationChanged = updateButtonVector(portal); PortalHandler.registerPortal(portal); - portal.getPortalOpener().closePortal(true); + if (Stargate.getGateConfig().applyStartupFixes()) { + portal.getPortalOpener().closePortal(true); + } return buttonLocationChanged; } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index eeaf860..ad27cad 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -106,6 +106,11 @@ gates: # This is more resource intensive, and should really only be used if verifyPortals is true. # Or if using an easily destroyable open/closed material. protectEntrance: false + + # Should things like outdated signs, invalid button materials and not properly closed Stargates be fixed at startup? + # It is generally recommended to enable this, but for huge servers, the amount of chunks loaded might require way + # too much RAM. + applyStartupFixes: true # +----------------------------------------------------------------------------------------------+ # # | Aesthetic Tweaks | #