From 08d73f232b69045a37adc7f9808a730e138d3c6f Mon Sep 17 00:00:00 2001 From: tastybento Date: Tue, 15 Aug 2023 17:25:26 -0700 Subject: [PATCH] Adds a config section in BentoBox to run commands when it is loaded Commands are run as console. --- .../world/bentobox/bentobox/BentoBox.java | 2 + .../world/bentobox/bentobox/Settings.java | 57 +++++++++++-------- src/main/resources/config.yml | 39 ++++--------- 3 files changed, 46 insertions(+), 52 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/BentoBox.java b/src/main/java/world/bentobox/bentobox/BentoBox.java index 56d02eb37..48a45b937 100644 --- a/src/main/java/world/bentobox/bentobox/BentoBox.java +++ b/src/main/java/world/bentobox/bentobox/BentoBox.java @@ -251,6 +251,8 @@ public class BentoBox extends JavaPlugin implements Listener { // Tell all addons that everything is loaded isLoaded = true; this.addonsManager.allLoaded(); + // Run ready commands + settings.getReadyCommands().forEach(cmd -> Bukkit.getServer().dispatchCommand(getServer().getConsoleSender(), cmd)); // Fire plugin ready event - this should go last after everything else Bukkit.getPluginManager().callEvent(new BentoBoxReadyEvent()); instance.log("All blueprints loaded."); diff --git a/src/main/java/world/bentobox/bentobox/Settings.java b/src/main/java/world/bentobox/bentobox/Settings.java index 384447caf..758cfaf78 100644 --- a/src/main/java/world/bentobox/bentobox/Settings.java +++ b/src/main/java/world/bentobox/bentobox/Settings.java @@ -1,12 +1,18 @@ package world.bentobox.bentobox; +import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import org.bukkit.Material; +import com.google.common.collect.ImmutableList; + import world.bentobox.bentobox.api.configuration.ConfigComment; import world.bentobox.bentobox.api.configuration.ConfigEntry; import world.bentobox.bentobox.api.configuration.ConfigObject; @@ -22,26 +28,6 @@ import world.bentobox.bentobox.database.DatabaseSetup.DatabaseType; @StoreAt(filename="config.yml") // Explicitly call out what name this should have. @ConfigComment("BentoBox v[version] configuration file.") @ConfigComment("") -@ConfigComment("This configuration file contains settings that mainly apply to or manage the following elements:") -@ConfigComment(" * Data storage") -@ConfigComment(" * Gamemodes (commands, ...)") -@ConfigComment(" * Internet connectivity (web-based content-enriched features, ...)") -@ConfigComment("") -@ConfigComment("Note that this configuration file is dynamic:") -@ConfigComment(" * It gets updated with the newest settings and comments after BentoBox loaded its settings from it.") -@ConfigComment(" * Upon updating BentoBox, new settings will be automatically added into this configuration file.") -@ConfigComment(" * Said settings are distinguishable by a dedicated comment, which looks like this:") -@ConfigComment(" Added since X.Y.Z.") -@ConfigComment(" * They are provided with default values that should not cause issues on live production servers.") -@ConfigComment(" * You can however edit this file while the server is online.") -@ConfigComment(" You will therefore need to run the following command in order to take the changes into account: /bentobox reload.") -@ConfigComment("") -@ConfigComment("Here are a few pieces of advice before you get started:") -@ConfigComment(" * You should check out our Wiki, which may provide you useful tips or insights about BentoBox's features.") -@ConfigComment(" Link: https://github.com/BentoBoxWorld/BentoBox/wiki") -@ConfigComment(" * You should edit this configuration file while the server is offline.") -@ConfigComment(" * Moreover, whenever you update BentoBox, you should do so on a test server first.") -@ConfigComment(" This will allow you to configure the new settings beforehand instead of applying them inadvertently on a live production server.") public class Settings implements ConfigObject { /* GENERAL */ @@ -56,10 +42,17 @@ public class Settings implements ConfigObject { @ConfigEntry(path = "general.use-economy") private boolean useEconomy = true; + /* COMMANDS */ + @ConfigComment("Console commands to run when BentoBox has loaded all worlds and addons.") + @ConfigComment("Commands are run as the console.") + @ConfigComment("e.g. set aliases for worlds in Multiverse here, or anything you need to") + @ConfigComment("run after the plugin is fully loaded.") + @ConfigEntry(path = "general.ready-commands", since = "1.24.2") + private List readyCommands = new ArrayList<>(); + // Database - @ConfigComment("JSON, MYSQL, MARIADB, MONGODB, SQLITE, POSTGRESQL and YAML(deprecated).") + @ConfigComment("JSON, MYSQL, MARIADB, MONGODB, SQLITE, and POSTGRESQL.") @ConfigComment("Transition database options are:") - @ConfigComment(" YAML2JSON, YAML2MARIADB, YAML2MYSQL, YAML2MONGODB, YAML2SQLITE") @ConfigComment(" JSON2MARIADB, JSON2MYSQL, JSON2MONGODB, JSON2SQLITE, JSON2POSTGRESQL") @ConfigComment(" MYSQL2JSON, MARIADB2JSON, MONGODB2JSON, SQLITE2JSON, POSTGRESQL2JSON") @ConfigComment("If you need others, please make a feature request.") @@ -70,7 +63,7 @@ public class Settings implements ConfigObject { @ConfigComment(" SQLite versions 3.28 or later") @ConfigComment(" PostgreSQL versions 9.4 or later") @ConfigComment("Transition options enable migration from one database type to another. Use /bbox migrate.") - @ConfigComment("YAML and JSON are file-based databases.") + @ConfigComment("JSON is a file-based database.") @ConfigComment("MYSQL might not work with all implementations: if available, use a dedicated database type (e.g. MARIADB).") @ConfigComment("BentoBox uses HikariCP for connecting with SQL databases.") @ConfigComment("If you use MONGODB, you must also run the BSBMongo plugin (not addon).") @@ -983,8 +976,8 @@ public class Settings implements ConfigObject { { return maximumPoolSize; } - - + + /** * Gets safe spot search range. * @@ -1038,4 +1031,18 @@ public class Settings implements ConfigObject { { this.safeSpotSearchRange = safeSpotSearchRange; } + + /** + * @return an immutable list of readyCommands + */ + public List getReadyCommands() { + return ImmutableList.copyOf(Objects.requireNonNullElse(readyCommands, Collections.emptyList())); + } + + /** + * @param readyCommands the readyCommands to set + */ + public void setReadyCommands(List readyCommands) { + this.readyCommands = readyCommands; + } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 3be69031b..997ce06c6 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,25 +1,5 @@ # BentoBox {$version} configuration file. # -# This configuration file contains settings that mainly apply to or manage the following elements: -# * Data storage -# * Gamemodes (commands, ...) -# * Internet connectivity (web-based content-enriched features, ...) -# -# Note that this configuration file is dynamic: -# * It gets updated with the newest settings and comments after BentoBox loaded its settings from it. -# * Upon updating BentoBox, new settings will be automatically added into this configuration file. -# * Said settings are distinguishable by a dedicated comment, which looks like this: -# Added since X.Y.Z. -# * They are provided with default values that should not cause issues on live production servers. -# * You can however edit this file while the server is online. -# You will therefore need to run the following command in order to take the changes into account: /bentobox reload. -# -# Here are a few pieces of advice before you get started: -# * You should check out our Wiki, which may provide you useful tips or insights about BentoBox's features. -# Link: https://github.com/BentoBoxWorld/BentoBox/wiki -# * You should edit this configuration file while the server is offline. -# * Moreover, whenever you update BentoBox, you should do so on a test server first. -# This will allow you to configure the new settings beforehand instead of applying them inadvertently on a live production server. general: # Default language for new players. # This is the filename in the locale folder without .yml. @@ -28,10 +8,15 @@ general: # Use economy or not. If true, an economy plugin is required. If false, no money is used or given. # If there is no economy plugin present anyway, money will be automatically disabled. use-economy: true + # Console commands to run when BentoBox has loaded all worlds and addons. + # Commands are run as the console. + # e.g. set aliases for worlds in Multiverse here, or anything you need to + # run after the plugin is fully loaded. + # Added since 1.24.2. + ready-commands: [] database: - # JSON, MYSQL, MARIADB, MONGODB, SQLITE, POSTGRESQL and YAML(deprecated). + # JSON, MYSQL, MARIADB, MONGODB, SQLITE, and POSTGRESQL. # Transition database options are: - # YAML2JSON, YAML2MARIADB, YAML2MYSQL, YAML2MONGODB, YAML2SQLITE # JSON2MARIADB, JSON2MYSQL, JSON2MONGODB, JSON2SQLITE, JSON2POSTGRESQL # MYSQL2JSON, MARIADB2JSON, MONGODB2JSON, SQLITE2JSON, POSTGRESQL2JSON # If you need others, please make a feature request. @@ -42,7 +27,7 @@ general: # SQLite versions 3.28 or later # PostgreSQL versions 9.4 or later # Transition options enable migration from one database type to another. Use /bbox migrate. - # YAML and JSON are file-based databases. + # JSON is a file-based database. # MYSQL might not work with all implementations: if available, use a dedicated database type (e.g. MARIADB). # BentoBox uses HikariCP for connecting with SQL databases. # If you use MONGODB, you must also run the BSBMongo plugin (not addon). @@ -59,11 +44,11 @@ general: # This helps prevent issues if the server crashes. # Data is also saved at important points in the game. backup-period: 5 - # How many players will be saved in one tick. Default is 20 + # How many players will be saved in one tick. Default is 200 # Reduce if you experience lag while saving. # Do not set this too low or data might get lost! max-saved-players-per-tick: 20 - # How many islands will be saved in one tick. Default is 20 + # How many islands will be saved in one tick. Default is 200 # Reduce if you experience lag while saving. # Do not set this too low or data might get lost! max-saved-islands-per-tick: 20 @@ -82,8 +67,8 @@ general: prefix-character: '' # Custom connection datasource properties that will be applied to connection pool. # Check available values to your SQL driver implementation. - # Example: ") - # custom-properties: + # Example: + # custom-properties: # cachePrepStmts: 'true' # prepStmtCacheSize: '250' # prepStmtCacheSqlLimit: '2048'