From b8e0ca43316e2ad7490555f5602d99061e9fea47 Mon Sep 17 00:00:00 2001 From: BONNe1704 Date: Thu, 10 Jan 2019 11:36:06 +0200 Subject: [PATCH] Rework ChallengesAddon main class. Add dependencies to AcidIsland and BSkyBlock addons in pom.xml. Use proper way how to get GameMode admin and user commands. Init Settings object and implement onReload() method. Add check on disabled game modes, to avoid loading challenges in addons, where it should be disabled by settings. --- pom.xml | 12 ++ .../bentobox/challenges/ChallengesAddon.java | 199 ++++++++++++++---- 2 files changed, 165 insertions(+), 46 deletions(-) diff --git a/pom.xml b/pom.xml index 3e5504c..f959305 100644 --- a/pom.xml +++ b/pom.xml @@ -93,6 +93,18 @@ 1.0 provided + + world.bentobox + bskyblock + 1.0 + provided + + + world.bentobox + acidisland + 1.0 + provided + world.bentobox level diff --git a/src/main/java/world/bentobox/challenges/ChallengesAddon.java b/src/main/java/world/bentobox/challenges/ChallengesAddon.java index 5fa9b55..68923a2 100644 --- a/src/main/java/world/bentobox/challenges/ChallengesAddon.java +++ b/src/main/java/world/bentobox/challenges/ChallengesAddon.java @@ -2,12 +2,13 @@ package world.bentobox.challenges; import org.bukkit.Bukkit; +import world.bentobox.acidisland.AcidIsland; +import world.bentobox.bentobox.api.configuration.Config; import world.bentobox.challenges.commands.ChallengesCommand; import world.bentobox.challenges.commands.admin.Challenges; import world.bentobox.challenges.listeners.ResetListener; import world.bentobox.challenges.listeners.SaveListener; import world.bentobox.bentobox.api.addons.Addon; -import world.bentobox.bentobox.api.commands.CompositeCommand; /** * Add-on to BSkyBlock that enables challenges @@ -16,90 +17,196 @@ import world.bentobox.bentobox.api.commands.CompositeCommand; */ public class ChallengesAddon extends Addon { +// --------------------------------------------------------------------- +// Section: Variables +// --------------------------------------------------------------------- + private ChallengesManager challengesManager; - private String permissionPrefix = "addon"; + private ChallengesImportManager importManager; + + private Settings settings; + private boolean hooked; + +// --------------------------------------------------------------------- +// Section: Constants +// --------------------------------------------------------------------- + + + /** + * Permission prefix for addon. + */ + private static final String PERMISSION_PREFIX = "addon"; + + +// --------------------------------------------------------------------- +// Section: Methods +// --------------------------------------------------------------------- + + + /** + * {@inheritDoc} + */ @Override public void onLoad() { // Save default config.yml - saveDefaultConfig(); + this.saveDefaultConfig(); + // Load the plugin's config + this.loadSettings(); } + + /** + * {@inheritDoc} + */ @Override public void onEnable() { // Check if it is enabled - it might be loaded, but not enabled. - if (getPlugin() == null || !getPlugin().isEnabled()) { + if (this.getPlugin() == null || !this.getPlugin().isEnabled()) { Bukkit.getLogger().severe("BentoBox is not available or disabled!"); this.setState(State.DISABLED); return; } // Challenges Manager - challengesManager = new ChallengesManager(this); + this.challengesManager = new ChallengesManager(this); // Challenge import setup - importManager = new ChallengesImportManager(this); + this.importManager = new ChallengesImportManager(this); - // Register commands - run one tick later to allow all addons to load - // AcidIsland hook in - getPlugin().getAddonsManager().getAddonByName("AcidIsland").ifPresent(a -> { - CompositeCommand acidIslandCmd = getPlugin().getCommandsManager().getCommand("ai"); - if (acidIslandCmd != null) { - new ChallengesCommand(this, acidIslandCmd); - CompositeCommand acidCmd = getPlugin().getCommandsManager().getCommand("acid"); - new Challenges(this, acidCmd); - hooked = true; + + // Integrate into AcidIsland. + if (this.settings.getDisabledGameModes().isEmpty() || + !this.settings.getDisabledGameModes().contains("AcidIsland")) + { + this.getPlugin().getAddonsManager().getAddonByName("AcidIsland").ifPresent( + addon -> { + AcidIsland acidIsland = (AcidIsland) addon; + + new Challenges(this, + this.getPlugin().getCommandsManager().getCommand( + acidIsland.getSettings().getAdminCommand())); + + new ChallengesCommand(this, + this.getPlugin().getCommandsManager().getCommand( + acidIsland.getSettings().getIslandCommand())); + + this.hooked = true; + }); + } + + // Integrate into BSkyBlock. + if (this.settings.getDisabledGameModes().isEmpty() || + !this.settings.getDisabledGameModes().contains("BSkyBlock")) + { + this.getPlugin().getAddonsManager().getAddonByName("BSkyBlock").ifPresent( + addon -> { +// BSkyBlock skyBlock = (BSkyBlock) addon; +// SkyBlock addon cannot change commands ;( + + new Challenges(this, + this.getPlugin().getCommandsManager().getCommand("bsbadmin")); + + new ChallengesCommand(this, + this.getPlugin().getCommandsManager().getCommand("island")); + + this.hooked = true; + }); + } + + if (this.hooked) { + // Try to find Level addon and if it does not exist, display a warning + if (!this.getAddonByName("Level").isPresent()) { + this.logWarning("Level add-on not found so level challenges will not work!"); } - }); - getPlugin().getAddonsManager().getAddonByName("BSkyBlock").ifPresent(a -> { - // BSkyBlock hook in - CompositeCommand bsbIslandCmd = getPlugin().getCommandsManager().getCommand("island"); - if (bsbIslandCmd != null) { - new ChallengesCommand(this, bsbIslandCmd); - CompositeCommand bsbAdminCmd = getPlugin().getCommandsManager().getCommand("bsbadmin"); - new Challenges(this, bsbAdminCmd); - hooked = true; - } - }); - // If the add-on never hooks in, then it is useless - if (!hooked) { - logError("Challenges could not hook into AcidIsland or BSkyBlock so will not do anything!"); + + // Register the reset listener + this.registerListener(new ResetListener(this)); + // Register the autosave listener. + this.registerListener(new SaveListener(this)); + } else { + this.logError("Challenges could not hook into AcidIsland or BSkyBlock so will not do anything!"); this.setState(State.DISABLED); - return; } - // Try to find Level addon and if it does not exist, display a warning - if (!getAddonByName("Level").isPresent()) { - logWarning("Level add-on not found so level challenges will not work!"); - } - // Register the reset listener - this.registerListener(new ResetListener(this)); - // Register the autosave listener. - this.registerListener(new SaveListener(this)); - // Done } + + /** + * {@inheritDoc} + */ @Override - public void onDisable(){ - if (challengesManager != null) { - challengesManager.save(); + public void onReload() + { + if (this.hooked) { + this.challengesManager.save(); + + this.loadSettings(); + this.getLogger().info("Challenges addon reloaded."); } } - public ChallengesManager getChallengesManager() { - return challengesManager; + + /** + * {@inheritDoc} + */ + @Override + public void onDisable() { + if (this.hooked) { + this.challengesManager.save(); + } } + + /** + * This method loads addon configuration settings in memory. + */ + private void loadSettings() { + this.settings = new Config<>(this, Settings.class).loadConfigObject(); + + if (this.settings == null) { + // Disable + this.logError("Challenges settings could not load! Addon disabled."); + this.setState(State.DISABLED); + } + } + + +// --------------------------------------------------------------------- +// Section: Getters +// --------------------------------------------------------------------- + + + /** + * @return challengesManager + */ + public ChallengesManager getChallengesManager() { + return this.challengesManager; + } + + + /** + * @return Permission Prefix. + */ @Override public String getPermissionPrefix() { - return permissionPrefix ; + return PERMISSION_PREFIX; } + /** * @return the importManager */ public ChallengesImportManager getImportManager() { - return importManager; + return this.importManager; } + + /** + * @return the challenge settings. + */ + public Settings getChallengesSettings() + { + return this.settings; + } }