public static void broadcastCommandMessage( CommandSender source, String message, boolean sendToSource) { String result = source.getName() + ": " + message; if (source instanceof BlockCommandSender) { BlockCommandSender blockCommandSender = (BlockCommandSender) source; if (blockCommandSender .getBlock() .getWorld() .getGameRuleValue("commandBlockOutput") .equalsIgnoreCase("false")) { Bukkit.getConsoleSender().sendMessage(result); return; } } else if (source instanceof CommandMinecart) { CommandMinecart commandMinecart = (CommandMinecart) source; if (commandMinecart .getWorld() .getGameRuleValue("commandBlockOutput") .equalsIgnoreCase("false")) { Bukkit.getConsoleSender().sendMessage(result); return; } } Set<Permissible> users = Bukkit.getPluginManager() .getPermissionSubscriptions(Server.BROADCAST_CHANNEL_ADMINISTRATIVE); String colored = ChatColor.GRAY + "" + ChatColor.ITALIC + "[" + result + ChatColor.GRAY + ChatColor.ITALIC + "]"; if (sendToSource && !(source instanceof ConsoleCommandSender)) { source.sendMessage(message); } for (Permissible user : users) { if (user instanceof CommandSender) { CommandSender target = (CommandSender) user; if (target instanceof ConsoleCommandSender) { target.sendMessage(result); } else if (target != source) { target.sendMessage(colored); } } } }
/** * ********************************************************** onCommand() * ********************************************************** */ public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { if (sender instanceof Player) { Player player = (Player) sender; if (args.length == 0) { return false; } if (args[0].equals("getPos")) { this.getPos(player, cmd, label, args); } else if (args[0].equals("setPos1")) { this.setPos1(player, cmd, label, args); } else if (args[0].equals("setPos2")) { this.setPos2(player, cmd, label, args); } else if (args[0].equals("resetPos")) { this.resetPos(player, cmd, label, args); } else if (args[0].equals("teleport")) { this.cmdTeleport.commandHandler(sender, cmd, label, args, this.pos1, this.pos2); } else if (args[0].equals("license")) { this.cmdLicense.commandHandler(sender, cmd, label, args); } else if (args[0].equals("player")) { this.cmdPlayer.commandHandler(sender, cmd, label, args); } return true; } else { sender.sendMessage("You must be a player!"); } return false; }
/** * Executed on tab completion for this command, returning a list of options the player can tab * through. * * @param sender Source object which is executing this command * @param alias the alias being used * @param args All arguments passed to the command, split via ' ' * @return a list of tab-completions for the specified arguments. This will never be null. List * may be immutable. * @throws IllegalArgumentException if sender, alias, or args is null */ public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException { Validate.notNull(sender, "Sender cannot be null"); Validate.notNull(args, "Arguments cannot be null"); Validate.notNull(alias, "Alias cannot be null"); if (args.length == 0) { return ImmutableList.of(); } String lastWord = args[args.length - 1]; Player senderPlayer = sender instanceof Player ? (Player) sender : null; ArrayList<String> matchedPlayers = new ArrayList<String>(); for (Player player : sender.getServer().getOnlinePlayers()) { String name = player.getName(); if ((senderPlayer == null || senderPlayer.canSee(player)) && StringUtil.startsWithIgnoreCase(name, lastWord)) { matchedPlayers.add(name); } } Collections.sort(matchedPlayers, String.CASE_INSENSITIVE_ORDER); return matchedPlayers; }
@Override public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { String name = cmd.getName(); if (!commandMap.containsKey(name)) { // That command doesn't belong to us return true; } SearchResult result = searchCommand(name, args); if (result == null || result.container == null) { sender.sendMessage(messageBundle.getMessage("message-unknown-command")); return true; } CommandContainer command = result.container; int deepness = result.deepness; // Cut the args to be suitable to the sub-command-deepness String[] cutArgs = new String[args.length - deepness]; System.arraycopy(args, deepness, cutArgs, 0, args.length - deepness); CommandContext context = new CommandContext(cutArgs, command, sender); command.execute( context, messageBundle, permissionChecker, this.args.toArray(new Object[this.args.size()])); return true; }
@Override public void cmdStatus(CommandSender sender) { if (!(sender instanceof Player)) return; boolean bypass = Config.isPlayerBypassing(((Player) sender).getUniqueId()); sender.sendMessage( C_HEAD + "Border bypass is currently " + enabledColored(bypass) + C_HEAD + " for you."); }
/** * Tests the given {@link CommandSender} to see if they can perform this command. * * <p>If they do not have permission, they will be informed that they cannot do this. * * @param target User to test * @return true if they can use it, otherwise false */ public boolean testPermission(CommandSender target) { if (testPermissionSilent(target)) { return true; } if (permissionMessage == null) { target.sendMessage( ChatColor.RED + "I'm sorry, but you do not have permission to perform this command. Please contact the server administrators if you believe that this is in error."); } else if (permissionMessage.length() != 0) { for (String line : permissionMessage.replace("<permission>", permission).split("\n")) { target.sendMessage(line); } } return false; }
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { if (cmd.getName().equalsIgnoreCase("msreload")) { if (sender instanceof Player) { Player player = (Player) sender; if (player.hasPermission("MobspawnSettings.settings") == false) { sender.sendMessage("You don't have the required permissions MobspawnSettings.settings"); return true; } } MsConfig config = main.getMsConfig(); config.reloadConfiguration(false); sender.sendMessage("Reloaded"); return true; } return false; }
@Override public void showCommandError(final CommandSender sender, final String commandLabel, final Throwable exception) { sender.sendMessage(_("errorWithMessage", exception.getMessage())); if (ess.getSettings().isDebug()) { LOGGER.log(Level.WARNING, _("errorCallingCommand", commandLabel), exception); } }
@Override public void cmdStatus(CommandSender sender) { sender.sendMessage( C_HEAD + "DynMap border display is " + enabledColored(Config.DynmapBorderEnabled()) + C_HEAD + "."); }
@Override public int broadcast(String message, String permission) { int count = 0; for (Permissible permissible : getPluginManager().getPermissionSubscriptions(permission)) { if (permissible instanceof CommandSender && permissible.hasPermission(permission)) { ((CommandSender) permissible).sendMessage(message); ++count; } } return count; }
@Override public boolean execute(CommandSender sender, String commandLabel, String[] args) { if (exe != null) { if (!FOPMR_Rank.isEqualOrHigher(FOPMR_Rank.getRank(sender), rank)) { sender.sendMessage( ChatColor.RED + "You must be " + CUtils_Methods.aOrAn(rank.name) + " " + rank.name + " to use this command."); return true; } if (!exe.onCommand(sender, this, commandLabel, args)) { sender.sendMessage(this.usageMessage.replaceAll("<command>", command)); return false; } return true; } return false; }
/** * Tests the given {@link CommandSender} to see if they can perform this command. * * <p>No error is sent to the sender. * * @param target User to test * @return true if they can use it, otherwise false */ public boolean testPermissionSilent(CommandSender target) { if ((permission == null) || (permission.length() == 0)) { return true; } for (String p : permission.split(";")) { if (target.hasPermission(p)) { return true; } } return false; }
public boolean onCommand( final CommandSender sender, org.bukkit.command.Command cmd, String label, final String[] args) { if (cmd.getName().equalsIgnoreCase("enablemaps")) { if (args.length < 1 && !args[0].equalsIgnoreCase("battles") && !args[0].equalsIgnoreCase("arcade")) { sender.sendMessage(ChatColor.RED + "Invalid map type! Options: 'battles','arcade'"); return false; } String type = args[0]; if (type.equalsIgnoreCase("battles")) { if (!battleMapsLoaded) { loadMaps(BATTLE_MAPS_PACKAGE); sender.sendMessage(ChatColor.DARK_AQUA + "Loaded battles maps!"); battleMapsLoaded = true; } else { sender.sendMessage(ChatColor.RED + "Battles maps already loaded!"); } } if (type.equalsIgnoreCase("arcade")) { if (!arcadeMapsLoaded) { loadMaps(ARCADE_MAPS_PACKAGE); sender.sendMessage(ChatColor.DARK_AQUA + "Loaded arcade maps!"); arcadeMapsLoaded = true; } else { sender.sendMessage(ChatColor.RED + "Arcade maps already loaded!"); } } } return true; }
@Override public boolean dispatchCommand(CommandSender sender, String commandLine) throws CommandException { if (commandMap.dispatch(sender, commandLine)) { return true; } String firstword = commandLine; if (firstword.indexOf(' ') >= 0) { firstword = firstword.substring(0, firstword.indexOf(' ')); } sender.sendMessage(ChatColor.GRAY + "Unknown command \"" + firstword + "\", try \"help\""); return false; }
@Override public boolean onCommand(CommandSender sender, Command command, String label, String[] split) { Player player = (sender instanceof Player) ? (Player) sender : null; // if world name is passed inside quotation marks, handle that, and get List<String> instead of // String[] List<String> params = concatenateQuotedWorldName(split); String worldName = null; // is second parameter the command and first parameter a world name? definitely world name if it // was in quotation marks if (wasWorldQuotation || (params.size() > 1 && !subCommands.containsKey(params.get(0)) && subCommandsWithWorldNames.contains(params.get(1)))) worldName = params.get(0); // no command specified? show command examples / help if (params.isEmpty()) params.add(0, "commands"); // determined the command name String cmdName = (worldName == null) ? params.get(0).toLowerCase() : params.get(1).toLowerCase(); // remove command name and (if there) world name from front of param array params.remove(0); if (worldName != null) params.remove(0); // make sure command is recognized, default to showing command examples / help if not; also // check for specified page number if (!subCommands.containsKey(cmdName)) { int page = (player == null) ? 0 : 1; try { page = Integer.parseInt(cmdName); } catch (NumberFormatException ignored) { sender.sendMessage(WBCmd.C_ERR + "Command not recognized. Showing command list."); } cmdName = "commands"; params.add(0, Integer.toString(page)); } WBCmd subCommand = subCommands.get(cmdName); // check permission if (!Config.HasPermission(player, subCommand.permission)) return true; // if command requires world name when run by console, make sure that's in place if (player == null && subCommand.hasWorldNameInput && subCommand.consoleRequiresWorldName && worldName == null) { sender.sendMessage( WBCmd.C_ERR + "This command requires a world to be specified if run by the console."); subCommand.sendCmdHelp(sender); return true; } // make sure valid number of parameters has been provided if (params.size() < subCommand.minParams || params.size() > subCommand.maxParams) { if (subCommand.maxParams == 0) sender.sendMessage(WBCmd.C_ERR + "This command does not accept any parameters."); else sender.sendMessage(WBCmd.C_ERR + "You have not provided a valid number of parameters."); subCommand.sendCmdHelp(sender); return true; } // execute command subCommand.execute(sender, player, params, worldName); return true; }
@Override public boolean handleCommand(final CommandSender sender, final Command command, final String commandLabel, final String[] args) { boolean disabled = false; boolean overridden = false; ISettings settings = ess.getSettings(); settings.acquireReadLock(); try { disabled = settings.getData().getCommands().isDisabled(command.getName()); overridden = !disabled || settings.getData().getCommands().isOverridden(command.getName()); } finally { settings.unlock(); } // Allow plugins to override the command via onCommand if (!overridden && (!commandLabel.startsWith("e") || commandLabel.equalsIgnoreCase(command.getName()))) { final PluginCommand pc = getAlternative(commandLabel); if (pc != null) { executed(commandLabel, pc.getLabel()); try { return pc.execute(sender, commandLabel, args); } catch (final Exception ex) { final ArrayList<StackTraceElement> elements = new ArrayList<StackTraceElement>(Arrays.asList(ex.getStackTrace())); elements.remove(0); final ArrayList<StackTraceElement> toRemove = new ArrayList<StackTraceElement>(); for (final StackTraceElement e : elements) { if (e.getClassName().equals("net.ess3.Essentials")) { toRemove.add(e); } } elements.removeAll(toRemove); final StackTraceElement[] trace = elements.toArray(new StackTraceElement[elements.size()]); ex.setStackTrace(trace); ex.printStackTrace(); sender.sendMessage(ChatColor.RED + "An internal error occurred while attempting to perform this command"); return true; } } } try { IUser user = null; if (sender instanceof Player) { user = ess.getUserMap().getUser((Player)sender); LOGGER.log(Level.INFO, String.format("[PLAYER_COMMAND] %s: /%s %s ", ((Player)sender).getName(), commandLabel, EssentialsCommand.getFinalArg(args, 0))); } // Check for disabled commands if (disabled) { return true; } final String commandName = command.getName().toLowerCase(Locale.ENGLISH); IEssentialsCommand cmd = commands.get(commandName); if (cmd == null) { try { cmd = (IEssentialsCommand)classLoader.loadClass(commandPath + commandName).newInstance(); cmd.init(ess, commandName); cmd.setEssentialsModule(module); commands.put(commandName, cmd); } catch (Exception ex) { sender.sendMessage(_("commandNotLoaded", commandName)); LOGGER.log(Level.SEVERE, _("commandNotLoaded", commandName), ex); return true; } } // Check authorization if (sender != null && !cmd.isAuthorized(sender)) { LOGGER.log(Level.WARNING, _("deniedAccessCommand", user.getName())); user.sendMessage(_("noAccessCommand")); return true; } // Run the command try { if (user == null) { cmd.run(sender, command, commandLabel, args); } else { user.acquireReadLock(); try { cmd.run(user, command, commandLabel, args); } finally { user.unlock(); } } return true; } catch (NoChargeException ex) { return true; } catch (NotEnoughArgumentsException ex) { sender.sendMessage(command.getDescription()); sender.sendMessage(command.getUsage().replaceAll("<command>", commandLabel)); if (!ex.getMessage().isEmpty()) { sender.sendMessage(ex.getMessage()); } return true; } catch (Throwable ex) { showCommandError(sender, commandLabel, ex); return true; } } catch (Throwable ex) { LOGGER.log(Level.SEVERE, _("commandFailed", commandLabel), ex); return true; } }