From 97341ce657e599d620918ffc4c9abe274d810d93 Mon Sep 17 00:00:00 2001 From: BONNe Date: Sun, 26 Apr 2020 01:57:14 +0300 Subject: [PATCH] Add DefaultPlayerCommand and DefaultAdminCommand (#1293) Helps to implement #498. Also added 4 new WorldSettings methods: * getAdminCommandAliases() * getPlayerCommandAliases() * getDefaultPlayerAction() * getDefaultNewPlayerAction() Co-Authored-By: Florian CUNY --- .../commands/admin/DefaultAdminCommand.java | 118 +++++++++++++++ .../commands/island/DefaultPlayerCommand.java | 141 ++++++++++++++++++ .../api/configuration/WorldSettings.java | 57 +++++++ 3 files changed, 316 insertions(+) create mode 100644 src/main/java/world/bentobox/bentobox/api/commands/admin/DefaultAdminCommand.java create mode 100644 src/main/java/world/bentobox/bentobox/api/commands/island/DefaultPlayerCommand.java diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/DefaultAdminCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/DefaultAdminCommand.java new file mode 100644 index 000000000..50ff10120 --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/DefaultAdminCommand.java @@ -0,0 +1,118 @@ +package world.bentobox.bentobox.api.commands.admin; + + +import java.util.List; + +import world.bentobox.bentobox.api.addons.GameModeAddon; +import world.bentobox.bentobox.api.commands.CompositeCommand; +import world.bentobox.bentobox.api.commands.admin.blueprints.AdminBlueprintCommand; +import world.bentobox.bentobox.api.commands.admin.deaths.AdminDeathsCommand; +import world.bentobox.bentobox.api.commands.admin.purge.AdminPurgeCommand; +import world.bentobox.bentobox.api.commands.admin.range.AdminRangeCommand; +import world.bentobox.bentobox.api.commands.admin.resets.AdminResetsCommand; +import world.bentobox.bentobox.api.commands.admin.team.AdminTeamAddCommand; +import world.bentobox.bentobox.api.commands.admin.team.AdminTeamDisbandCommand; +import world.bentobox.bentobox.api.commands.admin.team.AdminTeamKickCommand; +import world.bentobox.bentobox.api.commands.admin.team.AdminTeamSetownerCommand; +import world.bentobox.bentobox.api.localization.TextVariables; +import world.bentobox.bentobox.api.user.User; + + +/** + * This is default Admin command for console and op. It contains all necessary parts that + * for main command. + * @since 1.13.0 + * @author BONNe + */ +public abstract class DefaultAdminCommand extends CompositeCommand +{ + /** + * This is the top-level command constructor for commands that have no parent. + * + * @param addon - GameMode addon + */ + public DefaultAdminCommand(GameModeAddon addon) + { + // Register command with alias from config. + super(addon, + addon.getWorldSettings().getAdminCommandAliases().split(" ")[0], + addon.getWorldSettings().getAdminCommandAliases().split(" ")); + } + + + /** + * Setups anything that is necessary for default main admin command. + * @see world.bentobox.bentobox.api.commands.BentoBoxCommand#setup() + */ + @Override + public void setup() + { + this.setPermission("admin.*"); + this.setOnlyPlayer(false); + + this.setParametersHelp("commands.admin.help.parameters"); + this.setDescription("commands.admin.help.description"); + + new AdminVersionCommand(this); + new AdminTeleportCommand(this, "tp"); + new AdminTeleportCommand(this, "tpnether"); + new AdminTeleportCommand(this, "tpend"); + new AdminGetrankCommand(this); + new AdminSetrankCommand(this); + new AdminInfoCommand(this); + // Team commands + new AdminTeamAddCommand(this); + new AdminTeamKickCommand(this); + new AdminTeamDisbandCommand(this); + new AdminTeamSetownerCommand(this); + // Schems + new AdminBlueprintCommand(this); + // Register/unregister islands + new AdminRegisterCommand(this); + new AdminUnregisterCommand(this); + // Range + new AdminRangeCommand(this); + // Resets + new AdminResetsCommand(this); + // Delete + new AdminDeleteCommand(this); + // Why + new AdminWhyCommand(this); + // Deaths + new AdminDeathsCommand(this); + // Reload + new AdminReloadCommand(this); + // Spawn + new AdminSetspawnCommand(this); + // Reset flags + new AdminResetFlagsCommand(this); + // Trash + //new AdminTrashCommand(this); + //new AdminEmptyTrashCommand(this); + //new AdminSwitchtoCommand(this); + // Switch + new AdminSwitchCommand(this); + // Purge + new AdminPurgeCommand(this); + // Settings + new AdminSettingsCommand(this); + } + + + /** + * Defines what will be executed when this command is run. + * @see world.bentobox.bentobox.api.commands.BentoBoxCommand#execute(User, String, List) + */ + @Override + public boolean execute(User user, String label, List args) + { + if (user != null && !args.isEmpty()) + { + user.sendMessage("general.errors.unknown-command", TextVariables.LABEL, getTopLabel()); + return false; + } + + // By default run the attached help command, if it exists (it should) + return this.showHelp(this, user); + } +} diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/DefaultPlayerCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/DefaultPlayerCommand.java new file mode 100644 index 000000000..8225e0ed0 --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/DefaultPlayerCommand.java @@ -0,0 +1,141 @@ +package world.bentobox.bentobox.api.commands.island; + + +import java.util.Collections; +import java.util.List; + +import world.bentobox.bentobox.api.addons.GameModeAddon; +import world.bentobox.bentobox.api.commands.CompositeCommand; +import world.bentobox.bentobox.api.commands.island.team.IslandTeamCommand; +import world.bentobox.bentobox.api.localization.TextVariables; +import world.bentobox.bentobox.api.user.User; + + +/** + * This is default player command class. It contains all necessary parts for main /[gamemode] command. + * @since 1.13.0 + * @author BONNe + */ +public abstract class DefaultPlayerCommand extends CompositeCommand +{ + /** + * This is the top-level command constructor for commands that have no parent. + * + * @param addon - GameMode addon + */ + public DefaultPlayerCommand(GameModeAddon addon) + { + // Register command with alias from config. + super(addon, + addon.getWorldSettings().getPlayerCommandAliases().split(" ")[0], + addon.getWorldSettings().getPlayerCommandAliases().split(" ")); + } + + + /** + * Setups anything that is necessary for default main user command. + * @see world.bentobox.bentobox.api.commands.BentoBoxCommand#setup() + */ + @Override + public void setup() + { + // Description + this.setDescription("commands.island.help.description"); + // Limit to player + this.setOnlyPlayer(true); + // Permission + this.setPermission("island"); + + // Set up default subcommands + + // Teleport commands + new IslandGoCommand(this); + new IslandSpawnCommand(this); + + // Allows to create/reset island. + new IslandCreateCommand(this); + new IslandResetCommand(this); + + // Displays info about the island. + new IslandInfoCommand(this); + + // Settings related commands + new IslandSettingsCommand(this); + new IslandSethomeCommand(this); + new IslandSetnameCommand(this); + new IslandResetnameCommand(this); + new IslandLanguageCommand(this); + + // Ban related commands + new IslandBanCommand(this); + new IslandUnbanCommand(this); + new IslandBanlistCommand(this); + + // Kicks visitors or coops/trusted from island + new IslandExpelCommand(this); + + // Tells owner of adjacent islands + new IslandNearCommand(this); + + // Team commands + new IslandTeamCommand(this); + } + + + /** + * Defines what will be executed when this command is run. + * @see world.bentobox.bentobox.api.commands.BentoBoxCommand#execute(User, String, List) + */ + @Override + public boolean execute(User user, String label, List args) + { + if (user == null) + { + return false; + } + + if (!args.isEmpty()) + { + user.sendMessage("general.errors.unknown-command", TextVariables.LABEL, this.getTopLabel()); + return false; + } + + // Check if user has an island. + if (this.getIslands().getIsland(this.getWorld(), user.getUniqueId()) != null) + { + // Default command if user has an island. + String command = this.getAddon().getWorldSettings().getDefaultPlayerAction(); + + // If command exists, the call it. + // Otherwise, just use "go" command. + if (command != null && this.getSubCommand(command).isPresent()) + { + return this.getSubCommand(command).get().call(user, label, Collections.emptyList()); + } + else + { + return this.getSubCommand("go"). + map(goCmd -> goCmd.call(user, goCmd.getLabel(), Collections.emptyList())). + orElse(false); + } + } + else + { + // Default command if user does not have an island. + String command = this.getAddon().getWorldSettings().getDefaultNewPlayerAction(); + + // If command exists, the call it. + // Otherwise, just use "create" command. + if (command != null && this.getSubCommand(command).isPresent()) + { + return this.getSubCommand(command).get().call(user, label, Collections.emptyList()); + } + else + { + return this.getSubCommand("create"). + map(createCmd -> createCmd.call(user, createCmd.getLabel(), Collections.emptyList())). + orElse(false); + } + } + } +} diff --git a/src/main/java/world/bentobox/bentobox/api/configuration/WorldSettings.java b/src/main/java/world/bentobox/bentobox/api/configuration/WorldSettings.java index 5d029d626..caaf9b169 100644 --- a/src/main/java/world/bentobox/bentobox/api/configuration/WorldSettings.java +++ b/src/main/java/world/bentobox/bentobox/api/configuration/WorldSettings.java @@ -467,4 +467,61 @@ public interface WorldSettings extends ConfigObject { return true; } + + /** + * Returns all aliases for main admin command. + * It is assumed that all aliases are split with whitespace between them. + * String cannot be empty. + * Default value: {@code getFriendlyName() + "admin"} (to retain backward compatibility). + * @return String value + * @since 1.13.0 + */ + default String getAdminCommandAliases() + { + return this.getFriendlyName().toLowerCase() + "admin"; + } + + + /** + * Returns all aliases for main player command. + * It is assumed that all aliases are split with whitespace between them. + * String cannot be empty. + * Default value: {@code getFriendlyName()} (to retain backward compatibility). + * @return String value + * @since 1.13.0 + */ + default String getPlayerCommandAliases() + { + return this.getFriendlyName().toLowerCase(); + } + + + /** + * Returns sub-command for users when they execute main user command and they have an + * island. + * If defined sub-command does not exist in accessible user command list, then it will + * still call "go" sub-command. + * Default value: {@code "go"} (to retain backward compatibility) + * @return name of default sub-command for main command if user does have an island. + * @since 1.13.0 + */ + default String getDefaultPlayerAction() + { + return "go"; + } + + + /** + * Returns default sub-command for users when they execute main user command and they + * do not have an island. + * If defined sub-command does not exist in accessible user command list, then it will + * still call "create" sub-command. + * Default value: {@code "create"} (to retain backward compatibility) + * @return name of default sub-command for main command if user does not have an island. + * @since 1.13.0 + */ + default String getDefaultNewPlayerAction() + { + return "create"; + } }