package world.bentobox.challenges.commands; 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.ChallengesPanel; import world.bentobox.challenges.utils.Constants; import world.bentobox.challenges.utils.Utils; public class ChallengesPlayerCommand extends CompositeCommand { public ChallengesPlayerCommand(ChallengesAddon addon, CompositeCommand cmd) { super(addon, cmd, addon.getChallengesSettings().getPlayerMainCommand().split(" ")[0], addon.getChallengesSettings().getPlayerMainCommand().split(" ")); } /** * {@inheritDoc} */ @Override public boolean canExecute(User user, String label, List args) { if (!this.getIWM().inWorld(user.getWorld()) || !Util.sameWorld(this.getWorld(), user.getWorld())) { // Not a GameMode world. Utils.sendMessage(user, user.getWorld(), "general.errors.wrong-world"); return false; } if (!((ChallengesAddon) this.getAddon()).getChallengesManager().hasAnyChallengeData(this.getWorld())) { // Do not open gui if there is no challenges. this.getAddon().logError("There are no challenges set up in " + this.getWorld() + "!"); // Show admin better explanation. if (user.isOp() || user.hasPermission(this.getPermissionPrefix() + "admin.challenges")) { String topLabel = this.getIWM().getAddon(this.getWorld()). map(GameModeAddon::getAdminCommand). map(optionalAdminCommand -> optionalAdminCommand.map(CompositeCommand::getTopLabel).orElse(this.getTopLabel())). orElse(this.getTopLabel()); Utils.sendMessage(user, this.getWorld(), Constants.ERRORS + "no-challenges-admin", "[command]", topLabel + " " + this.getAddon().getChallengesSettings().getAdminMainCommand().split(" ")[0]); } else { Utils.sendMessage(user, this.getWorld(), Constants.ERRORS + "no-challenges"); } return false; } if (this.getIslands().getIsland(this.getWorld(), user) == null) { // Do not open gui if there is no island for this player. Utils.sendMessage(user, this.getWorld(), "general.errors.no-island"); return false; } else if (ChallengesAddon.CHALLENGES_WORLD_PROTECTION.isSetForWorld(this.getWorld()) && !this.getIslands().locationIsOnIsland(user.getPlayer(), user.getLocation())) { // Do not open gui if player is not on the island, but challenges requires island for // completion. Utils.sendMessage(user, this.getWorld(), Constants.ERRORS + "not-on-island"); return false; } return true; } @Override public boolean execute(User user, String label, List args) { // Open up the challenges GUI if (user.isPlayer()) { ChallengesPanel.open(this.getAddon(), this.getWorld(), user, this.getTopLabel(), this.getPermissionPrefix()); return true; } // Show help showHelp(this, user); return false; } @Override public void setup() { this.setPermission("challenges"); this.setParametersHelp("challenges.commands.user.main.parameters"); this.setDescription("challenges.commands.user.main.description"); new CompleteChallengeCommand(this.getAddon(), this); this.setOnlyPlayer(true); } }