/** * Perform a permissions check, throwing an exception if the required * permissions are not present. * * @param commander the source to check against * @param permission The permission to check * @throws CommandException if the source does not have permission */ public void checkPermission(CommandSource commander, String permission) throws CommandException { if (!commander.hasPermission(permission)) { throw new CommandException(t("You do not have permission to use this command!")); } }
@Override public CommandResult process(CommandSource source, String commandLine) throws CommandException { final String[] argSplit = commandLine.split(" ", 2); Optional<CommandMapping> cmdOptional = get(argSplit[0], source); if (!cmdOptional.isPresent()) { throw new CommandNotFoundException(t("commands.generic.notFound"), argSplit[0]); // TODO: Fix properly to use a SpongeTranslation?? } final String arguments = argSplit.length > 1 ? argSplit[1] : ""; CommandMapping mapping = cmdOptional.get(); Optional<PluginContainer> pluginOwner = Sponge.getCommandManager().getOwner(mapping); pluginOwner.ifPresent(pluginContainer -> Sponge.getCauseStackManager().pushCause(pluginContainer)); final CommandCallable spec = mapping.getCallable(); Sponge.getCauseStackManager().pushCause(spec); try { return spec.process(source, arguments); } catch (CommandNotFoundException e) { throw new CommandException(t("No such child command: %s", e.getCommand())); } finally { if (pluginOwner.isPresent()) { Sponge.getCauseStackManager().popCause(); } Sponge.getCauseStackManager().popCause(); } }
@Override public CommandResult execute(CommandSource src, CommandContext args) throws CommandException { CommandMapping mapping = args.<CommandMapping>getOne(getUntranslatedKey()).orElse(null); if (mapping == null) { if (this.fallbackExecutor == null) { throw new CommandException(t("Invalid subcommand state -- no more than one mapping may be provided for child arg %s", getKey())); } return this.fallbackExecutor.execute(src, args); } if (mapping.getCallable() instanceof CommandSpec) { CommandSpec spec = ((CommandSpec) mapping.getCallable()); spec.checkPermission(src); return spec.getExecutor().execute(src, args); } final String arguments = args.<String>getOne(getUntranslatedKey() + "_args").orElse(""); return mapping.getCallable().process(src, arguments); }
/** * Perform a permissions check, throwing an exception if the required permissions are not present. * * @param commander the source to check against * @param permission The permission to check * @throws CommandException if the source does not have permission */ public void checkPermission(CommandSource commander, String permission) throws CommandException { if (!commander.hasPermission(permission)) { throw new CommandException(t("You do not have permission to use this command!")); } }
@Override public CommandResult process(CommandSource source, String commandLine) throws CommandException { final String[] argSplit = commandLine.split(" ", 2); Optional<CommandMapping> cmdOptional = get(argSplit[0], source); if (!cmdOptional.isPresent()) { throw new CommandNotFoundException(t("commands.generic.notFound"), argSplit[0]); // TODO: Fix properly to use a SpongeTranslation?? } final String arguments = argSplit.length > 1 ? argSplit[1] : ""; final CommandCallable spec = cmdOptional.get().getCallable(); try { return spec.process(source, arguments); } catch (CommandNotFoundException e) { throw new CommandException(t("No such child command: %s", e.getCommand())); } }
@Override public CommandResult execute(CommandSource src, CommandContext args) throws CommandException { if (!(src instanceof Player)) { throw new CommandException(settings.getText().getPlayersOnly()); } if (!settings.getGeneral().getMail().isEnabled()) { throw new CommandException(settings.getText().getMailNotEnabled()); } Player player = (Player) src; Optional<Account> optAccount = plugin.getDatabase().getAccount(player); if (optAccount.isPresent()) { if (optAccount.get().isLoggedIn()) { throw new CommandException(settings.getText().getAlreadyLoggedIn()); } } else { throw new CommandException(settings.getText().getAccountNotLoaded()); } Account account = optAccount.get(); Optional<String> optEmail = account.getMail(); if (!optEmail.isPresent()) { throw new CommandException(settings.getText().getUncommittedMailAddress()); } prepareSend(player, account, optEmail.get()); return CommandResult.success(); }
@Override public CommandResult execute(CommandSource src, CommandContext args) throws CommandException { if (!(src instanceof Player)) { throw new CommandException(settings.getText().getPlayersOnly()); } Collection<String> passwords = args.getAll("password"); List<String> indexPasswords = Lists.newArrayList(passwords); String password = indexPasswords.get(0); if (!password.equals(indexPasswords.get(1))) { //Check if the first two passwords are equal to prevent typos throw new CommandException(settings.getText().getUnequalPasswords()); } if (password.length() < settings.getGeneral().getMinPasswordLength()) { throw new CommandException(settings.getText().getTooShortPassword()); } Task.builder() //we are executing a SQL Query which is blocking .async() .execute(new RegisterTask(plugin, protectionManager, (Player) src, password)) .name("Register Query") .submit(plugin); return CommandResult.success(); }
throw new CommandException(Format.error("You do not have permission to use this command."));
private void uuidRegister(UUID account, CommandSource src, String password) throws CommandException { //check if the account is an UUID Optional<Player> player = Sponge.getServer().getPlayer(account); if (player.isPresent()) { throw new CommandException(settings.getText().getForceRegisterOnline()); } else { Task.builder() //Async as it could run a SQL query .async() .execute(new ForceRegTask(plugin, src, account, password)) .submit(plugin); } }
public static CommandSpec getCommand() { return CommandSpec.builder() .permission("prism.extinguish") .arguments(GenericArguments.integer(Text.of("radius"))) .executor((source, args) -> { if (!(source instanceof Player)) { throw new CommandException(Format.error("You must be a player to use this command.")); } int radius = args.<Integer>getOne("radius").get(); int changes = WorldUtil.removeAroundFromLocation( BlockTypes.FIRE, ((Player) source).getLocation(), radius); source.sendMessage(Format.message(String.format("Removed %d matches within %d blocks", changes, radius))); return CommandResult.success(); }) .build(); } }
@Override public CommandResult execute(CommandSource src, CommandContext args) throws CommandException { if (!(src instanceof Player)) { throw new CommandException(settings.getText().getPlayersOnly()); throw new CommandException(settings.getText().getNotLoggedIn()); String password = indexPasswords.get(0); if (!password.equals(indexPasswords.get(1))) { throw new CommandException(settings.getText().getUnequalPasswords());
.executor((source, args) -> { if (!(source instanceof Player)) { throw new CommandException(Format.error("You must be a player to use this command.")); throw new CommandException(Format.error("You have no valid actions to undo."));
@Override public CommandResult execute(CommandSource src, CommandContext args) throws CommandException { if (!(src instanceof Player)) { throw new CommandException(settings.getText().getPlayersOnly()); } Task.builder() //we are executing a SQL Query which is blocking .async() .execute(new RegisterTask(plugin, protectionManager, (Player) src)) .name("Register Query") .submit(plugin); return CommandResult.success(); }
@Override public CommandResult execute(CommandSource src, CommandContext args) throws CommandException { Player player = args.<Player>getOne("account").get(); if (plugin.getDatabase().isLoggedIn(player)) { throw new CommandException(settings.getText().getForceLoginAlreadyLoggedIn()); } Task.builder() //we are executing a SQL Query which is blocking .async() .execute(new ForceLoginTask(plugin, attemptManager, protectionManager, player, src)) .name("Force Login Query") .submit(plugin); return CommandResult.success(); }
@Override public CommandResult execute(CommandSource src, CommandContext args) throws CommandException { CommandMapping mapping = args.<CommandMapping>getOne(getUntranslatedKey()).orElse(null); if (mapping == null) { if (this.fallbackExecutor != null) { return this.fallbackExecutor.execute(src, args); } else { throw new CommandException(t("Invalid subcommand state -- no more than one mapping may be provided for child arg %s", getKey())); } } if (mapping.getCallable() instanceof CommandSpec) { CommandSpec spec = ((CommandSpec) mapping.getCallable()); spec.checkPermission(src); return spec.getExecutor().execute(src, args); } else { final String arguments = args.<String>getOne(getUntranslatedKey() + "_args").orElse(""); return mapping.getCallable().process(src, arguments); } }
@Override public CommandResult execute(CommandSource src, CommandContext args) throws CommandException { if (!(src instanceof Player)) { throw new CommandException(settings.getText().getPlayersOnly()); } String email = args.<String>getOne("email").get(); plugin.getDatabase().getAccount((Player) src).ifPresent(account -> { account.setMail(email); src.sendMessage(settings.getText().getMailSet()); Task.builder() .async() .execute(() -> plugin.getDatabase().save(account)) .submit(plugin); }); return CommandResult.success(); }
public static CommandSpec getCommand() { return CommandSpec.builder() .permission("prism.inspect") .executor((source, args) -> { if (source instanceof Player) { Player player = (Player) source; if (Prism.getInstance().getActiveWands().contains(player.getUniqueId())) { Prism.getInstance().getActiveWands().remove(player.getUniqueId()); source.sendMessage(Format.heading("Inspection wand disabled.")); } else { Prism.getInstance().getActiveWands().add(player.getUniqueId()); source.sendMessage(Format.heading("Inspection wand enabled.")); } return CommandResult.success(); } else { throw new CommandException(Format.error("You must be a player to use this command.")); } }).build(); } }
@Override public CommandResult execute(CommandSource src, CommandContext args) throws CommandException { if (!(src instanceof Player)) { throw new CommandException(settings.getText().getPlayersOnly()); throw new CommandException(settings.getText().getAlreadyLoggedIn()); .submit(plugin); throw new CommandException(settings.getText().getMaxAttempts());
@Listener public void onGamePreInitialization(GamePreInitializationEvent event) { Sponge.getCommandManager().register(this, CommandSpec.builder() .description(Text.of("Teleports a player to another world")) .arguments(seq(playerOrSource(Text.of("target")), onlyOne(world(Text.of("world"))))) .permission(PLUGIN_ID + ".command.tpworld") .executor((src, args) -> { final Optional<WorldProperties> optWorldProperties = args.getOne("world"); final Optional<World> optWorld = Sponge.getServer().getWorld(optWorldProperties.get().getWorldName()); if (!optWorld.isPresent()) { throw new CommandException(Text.of("World [", Text.of(TextColors.AQUA, optWorldProperties.get().getWorldName()), "] " + "was not found.")); } for (Player target : args.<Player>getAll("target")) { target.setLocation(new Location<>(optWorld.get(), optWorld.get().getProperties() .getSpawnPosition())); } return CommandResult.success(); }) .build() , "tpworld"); Sponge.getCommandManager().register(this, new CommandSpawn(), "spawn"); }
@Override public CommandResult execute(CommandSource src, CommandContext args) throws CommandException { if (!(src instanceof Player)) { throw new CommandException(settings.getText().getPlayersOnly()); } Player player = (Player) src; if (!plugin.getDatabase().isLoggedIn(player)) { throw new CommandException(settings.getText().getNotLoggedIn()); } Account account = plugin.getDatabase().getAccount(player).get(); src.sendMessage(settings.getText().getLoggedOut()); account.setLoggedIn(false); Task.builder().execute(() -> protectionManager.protect(player)).submit(plugin); Task.builder() .async() .execute(() -> { //flushes the ip update plugin.getDatabase().save(account); }) .submit(plugin); return CommandResult.success(); }