From 24828a3a1b136d339f6a18d80b99f33382bc8bc7 Mon Sep 17 00:00:00 2001 From: BONNe Date: Mon, 20 Sep 2021 09:27:58 +0300 Subject: [PATCH] Fixes a bug when global commands does not displays in tab-complete. Remove DefaultsCommand.java as it is not used anymore. --- .../bentobox/challenges/ChallengesAddon.java | 25 +-- .../ChallengesGlobalPlayerCommand.java | 8 +- .../admin/ChallengesAdminCommand.java | 3 - .../admin/ChallengesGlobalAdminCommand.java | 10 +- .../commands/admin/DefaultsCommand.java | 168 ------------------ 5 files changed, 30 insertions(+), 184 deletions(-) delete mode 100644 src/main/java/world/bentobox/challenges/commands/admin/DefaultsCommand.java diff --git a/src/main/java/world/bentobox/challenges/ChallengesAddon.java b/src/main/java/world/bentobox/challenges/ChallengesAddon.java index 5b18baa..1882d57 100644 --- a/src/main/java/world/bentobox/challenges/ChallengesAddon.java +++ b/src/main/java/world/bentobox/challenges/ChallengesAddon.java @@ -73,6 +73,12 @@ public class ChallengesAddon extends Addon { */ private boolean levelProvided; + /** + * List of hooked gamemode addons. + */ + private final List hookedGameModes = new ArrayList<>(); + + // --------------------------------------------------------------------- // Section: Constants // --------------------------------------------------------------------- @@ -113,6 +119,12 @@ public class ChallengesAddon extends Addon { this.saveDefaultConfig(); // Load the plugin's config this.loadSettings(); + + if (this.settings.isUseCommonGUI()) + { + new ChallengesGlobalPlayerCommand(this, this.hookedGameModes); + new ChallengesGlobalAdminCommand(this, this.hookedGameModes); + } } @@ -151,7 +163,7 @@ public class ChallengesAddon extends Addon { // Web content loading this.webManager = new WebManager(this); - List hookedGameModes = new ArrayList<>(); + this.hookedGameModes.clear(); this.getPlugin().getAddonsManager().getGameModeAddons().forEach(gameModeAddon -> { if (!this.settings.getDisabledGameModes().contains( @@ -163,7 +175,7 @@ public class ChallengesAddon extends Addon { new ChallengesAdminCommand(this, command)); this.hooked = true; - hookedGameModes.add(gameModeAddon); + this.hookedGameModes.add(gameModeAddon); CHALLENGES_WORLD_PROTECTION.addGameModeAddon(gameModeAddon); CHALLENGES_ISLAND_PROTECTION.addGameModeAddon(gameModeAddon); @@ -173,15 +185,6 @@ public class ChallengesAddon extends Addon { }); if (this.hooked) { - - // Create general challenge commands - - if (this.settings.isUseCommonGUI()) - { - new ChallengesGlobalPlayerCommand(this, hookedGameModes); - new ChallengesGlobalAdminCommand(this, hookedGameModes); - } - // Register the reset listener this.registerListener(new ResetListener(this)); // Register the autosave listener. diff --git a/src/main/java/world/bentobox/challenges/commands/ChallengesGlobalPlayerCommand.java b/src/main/java/world/bentobox/challenges/commands/ChallengesGlobalPlayerCommand.java index 4f5954e..257da85 100644 --- a/src/main/java/world/bentobox/challenges/commands/ChallengesGlobalPlayerCommand.java +++ b/src/main/java/world/bentobox/challenges/commands/ChallengesGlobalPlayerCommand.java @@ -9,6 +9,7 @@ import world.bentobox.bentobox.api.user.User; import world.bentobox.challenges.ChallengesAddon; import world.bentobox.challenges.config.SettingsUtils.GuiMode; import world.bentobox.challenges.panel.user.GameModePanel; +import world.bentobox.challenges.utils.Constants; import world.bentobox.challenges.utils.Utils; @@ -52,7 +53,12 @@ public class ChallengesGlobalPlayerCommand extends CompositeCommand { // It is not necessary to check 0, as in that case addon will not be hooked. - if (this.gameModeAddons.size() == 1) + if (this.gameModeAddons.isEmpty()) + { + Utils.sendMessage(user, user.getTranslation(Constants.ERRORS + "not-hooked")); + return false; + } + else if (this.gameModeAddons.size() == 1) { this.gameModeAddons.get(0).getPlayerCommand().ifPresent(compositeCommand -> user.performCommand(compositeCommand.getTopLabel() + " " + diff --git a/src/main/java/world/bentobox/challenges/commands/admin/ChallengesAdminCommand.java b/src/main/java/world/bentobox/challenges/commands/admin/ChallengesAdminCommand.java index afbca4a..22817e2 100644 --- a/src/main/java/world/bentobox/challenges/commands/admin/ChallengesAdminCommand.java +++ b/src/main/java/world/bentobox/challenges/commands/admin/ChallengesAdminCommand.java @@ -37,9 +37,6 @@ public class ChallengesAdminCommand extends CompositeCommand // This method reloads challenges addon new ReloadChallenges(getAddon(), this); - // Defaults processing command - new DefaultsCommand(this.getAddon(), this); - // Complete challenge command new CompleteCommand(this.getAddon(), this); diff --git a/src/main/java/world/bentobox/challenges/commands/admin/ChallengesGlobalAdminCommand.java b/src/main/java/world/bentobox/challenges/commands/admin/ChallengesGlobalAdminCommand.java index 6c4bf79..17b6b50 100644 --- a/src/main/java/world/bentobox/challenges/commands/admin/ChallengesGlobalAdminCommand.java +++ b/src/main/java/world/bentobox/challenges/commands/admin/ChallengesGlobalAdminCommand.java @@ -6,8 +6,11 @@ import java.util.List; import world.bentobox.bentobox.api.addons.GameModeAddon; import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.util.Util; import world.bentobox.challenges.ChallengesAddon; import world.bentobox.challenges.panel.user.GameModePanel; +import world.bentobox.challenges.utils.Constants; +import world.bentobox.challenges.utils.Utils; /** @@ -49,7 +52,12 @@ public class ChallengesGlobalAdminCommand extends CompositeCommand { // For single game mode just open correct gui. - if (this.gameModeAddons.size() == 1) + if (this.gameModeAddons.isEmpty()) + { + Utils.sendMessage(user, user.getTranslation(Constants.ERRORS + "not-hooked")); + return false; + } + else if (this.gameModeAddons.size() == 1) { this.gameModeAddons.get(0).getAdminCommand().ifPresent(compositeCommand -> user.performCommand(compositeCommand.getTopLabel() + " " + diff --git a/src/main/java/world/bentobox/challenges/commands/admin/DefaultsCommand.java b/src/main/java/world/bentobox/challenges/commands/admin/DefaultsCommand.java deleted file mode 100644 index 171dae3..0000000 --- a/src/main/java/world/bentobox/challenges/commands/admin/DefaultsCommand.java +++ /dev/null @@ -1,168 +0,0 @@ -package world.bentobox.challenges.commands.admin; - - -import java.util.Collections; -import java.util.List; -import java.util.Optional; - -import world.bentobox.bentobox.api.addons.Addon; -import world.bentobox.bentobox.api.commands.CompositeCommand; -import world.bentobox.bentobox.api.user.User; -import world.bentobox.bentobox.util.Util; -import world.bentobox.challenges.ChallengesAddon; - - -/** - * This method generates default challenges file. - */ -public class DefaultsCommand extends CompositeCommand -{ - - /** - * Constructor that inits generate defaults command. - * - * @param addon Addon that inits this command - * @param cmd Parent command - */ - public DefaultsCommand(Addon addon, CompositeCommand cmd) - { - super(addon, cmd, "defaults"); - this.addon = (ChallengesAddon) addon; - } - - - /** - * {@inheritDoc} - */ - @Override - public void setup() - { - this.setPermission("admin.challenges"); - this.setParametersHelp("challenges.commands.admin.defaults.parameters"); - this.setDescription("challenges.commands.admin.defaults.description"); - - // Register sub commands - // This method reloads challenges addon - new ImportCommand(this); - // Import ASkyBlock Challenges - new GenerateCommand(this); - } - - - /** - * {@inheritDoc} - */ - @Override - public boolean execute(User user, String label, List args) - { - return this.showHelp(this, user); - } - - -// --------------------------------------------------------------------- -// Section: Private Classes -// --------------------------------------------------------------------- - - - /** - * This class allows to process import command. - */ - private class ImportCommand extends CompositeCommand - { - /** - * Default constructor for import method. - * @param parent composite command - */ - private ImportCommand(CompositeCommand parent) - { - super(DefaultsCommand.this.addon, parent, "import"); - } - - - /** - * {@inheritDoc} - */ - @Override - public void setup() - { - this.setPermission("admin.challenges"); - this.setParametersHelp("challenges.commands.admin.defaults-import.parameters"); - this.setDescription("challenges.commands.admin.defaults-import.description"); - } - - - /** - * {@inheritDoc} - */ - @Override - public boolean execute(User user, String label, List args) - { - DefaultsCommand.this.addon.getImportManager().loadDownloadedChallenges(user, this.getWorld(), "default"); - return true; - } - } - - - /** - * This class allows to process generate command. - */ - private class GenerateCommand extends CompositeCommand - { - /** - * Default constructor for generate method. - * @param parent composite command - */ - private GenerateCommand(CompositeCommand parent) - { - super(DefaultsCommand.this.addon, parent, "generate"); - } - - - /** - * {@inheritDoc} - */ - @Override - public void setup() - { - this.setPermission("admin.challenges"); - this.setParametersHelp("challenges.commands.admin.defaults-generate.parameters"); - this.setDescription("challenges.commands.admin.defaults-generate.description"); - } - - - /** - * {@inheritDoc} - */ - @Override - public boolean execute(User user, String label, List args) - { - DefaultsCommand.this.addon.getImportManager().generateDatabaseFile( - user, - this.getWorld(), - "defaults"); - - return true; - } - - - /** - * {@inheritDoc} - */ - @Override - public Optional> tabComplete(User user, String alias, List args) - { - String lastArg = !args.isEmpty() ? args.get(args.size() - 1) : ""; - return Optional.of(Util.tabLimit(Collections.singletonList("overwrite"), lastArg)); - } - } - - -// --------------------------------------------------------------------- -// Section: Variables -// --------------------------------------------------------------------- - - /** - * Holds challenges addon as variable. - */ - private ChallengesAddon addon; -}