use of org.bukkit.command.Command in project MassiveCore by MassiveCraft.
the class TypeStringCommand method getTabList.
@Override
public Collection<String> getTabList(CommandSender sender, String arg) {
// Split the arg into a list of args #inception-the-movie!
String[] args = argAsArgs(arg);
// Tab completion of base commands
if (args.length <= 1)
return getKnownCommands().keySet();
// Get command alias and subargs
String alias = args[0];
// Attempt using the tab completion of that command.
Command command = getCommandSmart(alias);
if (command == null)
return Collections.emptySet();
List<String> subcompletions = command.tabComplete(sender, alias, Arrays.copyOfRange(args, 1, args.length));
String prefix = Txt.implode(Arrays.copyOfRange(args, 0, args.length - 1), " ") + " ";
List<String> ret = new MassiveList<>();
for (String subcompletion : subcompletions) {
String completion = prefix + subcompletion;
ret.add(completion);
}
return ret;
}
use of org.bukkit.command.Command in project NoCheatPlus by NoCheatPlus.
the class ChatListener method onPlayerCommandPreprocess.
/**
* We listen to PlayerCommandPreprocess events because commands can be used for spamming too.
*
* @param event
* the event
*/
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerCommandPreprocess(final PlayerCommandPreprocessEvent event) {
final Player player = event.getPlayer();
// Tell TickTask to update cached permissions.
final IPlayerData pData = DataManager.getPlayerData(player);
final ChatConfig cc = pData.getGenericInstance(ChatConfig.class);
// Checks that replace parts of the message (color).
if (color.isEnabled(player, pData)) {
event.setMessage(color.check(player, event.getMessage(), true));
}
// Left-trim is necessary because the server accepts leading spaces with commands.
final String message = event.getMessage();
final String lcMessage = StringUtil.leftTrim(message).toLowerCase();
// TODO: Remove bukkit: etc.
final String[] split = lcMessage.split(" ", 2);
final String alias = split[0].substring(1);
final Command command = CommandUtil.getCommand(alias);
// Could as well use an array and allow null on input of SimpleCharPrefixTree.
final List<String> messageVars = new ArrayList<String>();
messageVars.add(lcMessage);
// Message to run chat checks on.
String checkMessage = message;
if (command != null) {
messageVars.add("/" + command.getLabel().toLowerCase() + (split.length > 1 ? (" " + split[1]) : ""));
}
if (alias.indexOf(":") != -1) {
final int index = message.indexOf(":") + 1;
if (index < message.length()) {
checkMessage = message.substring(index);
messageVars.add(checkMessage.toLowerCase());
}
}
// Prevent commands from being used by players (e.g. /op /deop /reload).
if (cc.consoleOnlyCheck && consoleOnlyCommands.hasAnyPrefixWords(messageVars)) {
if (command == null || command.testPermission(player)) {
player.sendMessage(cc.consoleOnlyMessage);
}
event.setCancelled(true);
return;
}
// Handle as chat or command.
if (chatCommands.hasAnyPrefixWords(messageVars)) {
// TODO: Cut off the command ?.
if (textChecks(player, checkMessage, cc, pData, true, false)) {
event.setCancelled(true);
}
} else if (!commandExclusions.hasAnyPrefixWords(messageVars)) {
// Treat as command.
if (commands.isEnabled(player, pData) && commands.check(player, checkMessage, cc, pData, captcha)) {
event.setCancelled(true);
} else {
// TODO: Consider always checking these?
// Note that this checks for prefixes, not prefix words.
final MovingConfig mcc = pData.getGenericInstance(MovingConfig.class);
if (mcc.passableUntrackedCommandCheck && mcc.passableUntrackedCommandPrefixes.hasAnyPrefix(messageVars)) {
if (checkUntrackedLocation(player, message, mcc, pData)) {
event.setCancelled(true);
}
}
}
}
}
use of org.bukkit.command.Command in project NoCheatPlus by NoCheatPlus.
the class PermissionUtil method protectCommands.
/**
* Set up the command protection with the given permission message.
* @param permissionBase
* @param ignoredCommands
* @param invertIgnored
* @param ops
* @param permissionMessage
* @return
*/
public static List<CommandProtectionEntry> protectCommands(final String permissionBase, final Collection<String> ignoredCommands, final boolean invertIgnored, final boolean ops, final String permissionMessage) {
final Set<String> checked = new HashSet<String>();
for (String label : ignoredCommands) {
checked.add(CommandUtil.getCommandLabel(label, false));
}
final PluginManager pm = Bukkit.getPluginManager();
Permission rootPerm = pm.getPermission(permissionBase);
if (rootPerm == null) {
rootPerm = new Permission(permissionBase);
pm.addPermission(rootPerm);
}
final List<CommandProtectionEntry> changed = new LinkedList<CommandProtectionEntry>();
// Apply protection based on white-list or black-list.
for (final Command command : CommandUtil.getCommands()) {
final String lcLabel = command.getLabel().trim().toLowerCase();
if (checked.contains(lcLabel) || containsAnyAliases(checked, command)) {
if (!invertIgnored) {
continue;
}
} else if (invertIgnored) {
continue;
}
// Set the permission for the command.
String cmdPermName = command.getPermission();
final boolean cmdHadPerm;
if (cmdPermName == null) {
// Set a permission.
cmdPermName = permissionBase + "." + lcLabel;
command.setPermission(cmdPermName);
cmdHadPerm = false;
} else {
cmdHadPerm = true;
}
// Set permission default behavior.
Permission cmdPerm = pm.getPermission(cmdPermName);
final boolean permRegistered = cmdPerm != null;
if (!permRegistered) {
cmdPerm = new Permission(cmdPermName);
if (!cmdHadPerm) {
// NCP added the permission, allow root.
cmdPerm.addParent(rootPerm, true);
}
// else: permission was present, but not registered.
pm.addPermission(cmdPerm);
}
// Create change history entry.
if (cmdHadPerm && permRegistered) {
changed.add(new CommandProtectionEntry(command, lcLabel, cmdPermName, cmdPerm.getDefault(), command.getPermissionMessage()));
} else {
// (New Permission instances will not be touched on restore.)
changed.add(new CommandProtectionEntry(command, lcLabel, null, null, command.getPermissionMessage()));
}
// Change
cmdPerm.setDefault(ops ? PermissionDefault.OP : PermissionDefault.FALSE);
command.setPermissionMessage(permissionMessage);
}
return changed;
}
use of org.bukkit.command.Command in project Arcade2 by ShootGame.
the class GeneralListeners method onPlayerCommandPreprocess.
//
// Disable some commands
//
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
String prefix = BukkitCommands.BUKKIT_COMMAND_PREFIX.toLowerCase();
String message = event.getMessage();
if (!message.startsWith(prefix)) {
return;
}
String query = message.substring(1);
if (query.isEmpty()) {
return;
}
String label = query.split(" ", 2)[0].toLowerCase();
if (label.isEmpty()) {
return;
}
Command command = this.plugin.getServer().getCommandMap().getCommand(label);
if (command == null) {
return;
}
for (String disabled : DISABLED_COMMANDS) {
for (String alias : command.getAliases()) {
if (command.getName().equalsIgnoreCase(disabled) || alias.equalsIgnoreCase(disabled)) {
event.setCancelled(true);
event.getPlayer().sendMessage(String.format(COMMAND_MESSAGE, prefix + command.getName()));
return;
}
}
}
}
use of org.bukkit.command.Command in project Essentials by EssentialsX.
the class AlternativeCommandsHandler method addPlugin.
public final void addPlugin(final Plugin plugin) {
if (plugin.getDescription().getMain().contains("com.earth2me.essentials")) {
return;
}
final List<Command> commands = PluginCommandYamlParser.parse(plugin);
final String pluginName = plugin.getDescription().getName().toLowerCase(Locale.ENGLISH);
for (Command command : commands) {
final PluginCommand pc = (PluginCommand) command;
final List<String> labels = new ArrayList<>(pc.getAliases());
labels.add(pc.getName());
PluginCommand reg = ess.getServer().getPluginCommand(pluginName + ":" + pc.getName().toLowerCase(Locale.ENGLISH));
if (reg == null) {
reg = ess.getServer().getPluginCommand(pc.getName().toLowerCase(Locale.ENGLISH));
}
if (reg == null || !reg.getPlugin().equals(plugin)) {
continue;
}
for (String label : labels) {
List<PluginCommand> plugincommands = altcommands.get(label.toLowerCase(Locale.ENGLISH));
if (plugincommands == null) {
plugincommands = new ArrayList<>();
altcommands.put(label.toLowerCase(Locale.ENGLISH), plugincommands);
}
boolean found = false;
for (PluginCommand pc2 : plugincommands) {
if (pc2.getPlugin().equals(plugin)) {
found = true;
}
}
if (!found) {
plugincommands.add(reg);
}
}
}
}
Aggregations