com.velocitypowered.api.command.Command Java Examples

The following examples show how to use com.velocitypowered.api.command.Command. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VelocityCommand.java    From Velocity with MIT License 6 votes vote down vote up
@Override
public void execute(CommandSource source, String @NonNull [] args) {
  if (args.length == 0) {
    usage(source);
    return;
  }

  Command command = subcommands.get(args[0].toLowerCase(Locale.US));
  if (command == null) {
    usage(source);
    return;
  }
  @SuppressWarnings("nullness")
  String[] actualArgs = Arrays.copyOfRange(args, 1, args.length);
  command.execute(source, actualArgs);
}
 
Example #2
Source File: VelocityCommand.java    From Velocity with MIT License 6 votes vote down vote up
@Override
public List<String> suggest(CommandSource source, String @NonNull [] currentArgs) {
  if (currentArgs.length == 0) {
    return subcommands.entrySet().stream()
            .filter(e -> e.getValue().hasPermission(source, new String[0]))
            .map(Map.Entry::getKey)
            .collect(ImmutableList.toImmutableList());
  }

  if (currentArgs.length == 1) {
    return subcommands.entrySet().stream()
        .filter(e -> e.getKey().regionMatches(true, 0, currentArgs[0], 0,
            currentArgs[0].length()))
        .filter(e -> e.getValue().hasPermission(source, new String[0]))
        .map(Map.Entry::getKey)
        .collect(ImmutableList.toImmutableList());
  }

  Command command = subcommands.get(currentArgs[0].toLowerCase(Locale.US));
  if (command == null) {
    return ImmutableList.of();
  }
  @SuppressWarnings("nullness")
  String[] actualArgs = Arrays.copyOfRange(currentArgs, 1, currentArgs.length);
  return command.suggest(source, actualArgs);
}
 
Example #3
Source File: VelocityCommand.java    From Velocity with MIT License 5 votes vote down vote up
/**
 * Initializes the command object for /velocity.
 * @param server the Velocity server
 */
public VelocityCommand(VelocityServer server) {
  this.subcommands = ImmutableMap.<String, Command>builder()
      .put("version", new Info(server))
      .put("plugins", new Plugins(server))
      .put("reload", new Reload(server))
      .build();
}
 
Example #4
Source File: VelocityCommand.java    From Velocity with MIT License 5 votes vote down vote up
@Override
public boolean hasPermission(CommandSource source, String @NonNull [] args) {
  if (args.length == 0) {
    return subcommands.values().stream().anyMatch(e -> e.hasPermission(source, args));
  }
  Command command = subcommands.get(args[0].toLowerCase(Locale.US));
  if (command == null) {
    return true;
  }
  @SuppressWarnings("nullness")
  String[] actualArgs = Arrays.copyOfRange(args, 1, args.length);
  return command.hasPermission(source, actualArgs);
}
 
Example #5
Source File: VelocityCommandManager.java    From Velocity with MIT License 5 votes vote down vote up
@Override
public void register(final Command command, final String... aliases) {
  Preconditions.checkNotNull(aliases, "aliases");
  Preconditions.checkNotNull(command, "executor");
  for (int i = 0, length = aliases.length; i < length; i++) {
    final String alias = aliases[i];
    Preconditions.checkNotNull(alias, "alias at index %s", i);
    this.commands.put(alias.toLowerCase(Locale.ENGLISH), command);
  }
}
 
Example #6
Source File: VelocityCommandManager.java    From Velocity with MIT License 5 votes vote down vote up
@Override
public boolean execute(CommandSource source, String cmdLine) {
  Preconditions.checkNotNull(source, "invoker");
  Preconditions.checkNotNull(cmdLine, "cmdLine");

  String[] split = cmdLine.split(" ", -1);
  if (split.length == 0) {
    return false;
  }

  String alias = split[0];
  Command command = commands.get(alias.toLowerCase(Locale.ENGLISH));
  if (command == null) {
    return false;
  }

  @SuppressWarnings("nullness")
  String[] actualArgs = Arrays.copyOfRange(split, 1, split.length);
  try {
    if (!command.hasPermission(source, actualArgs)) {
      return false;
    }

    command.execute(source, actualArgs);
    return true;
  } catch (Exception e) {
    throw new RuntimeException("Unable to invoke command " + cmdLine + " for " + source, e);
  }
}
 
Example #7
Source File: VelocityCommandManager.java    From Velocity with MIT License 5 votes vote down vote up
/**
 * Determines if the {@code source} has permission to run the {@code cmdLine}.
 * @param source the source to check against
 * @param cmdLine the command to run
 * @return {@code true} if the command can be run, otherwise {@code false}
 */
public boolean hasPermission(CommandSource source, String cmdLine) {
  Preconditions.checkNotNull(source, "source");
  Preconditions.checkNotNull(cmdLine, "cmdLine");

  String[] split = cmdLine.split(" ", -1);
  if (split.length == 0) {
    // No command available.
    return false;
  }

  String alias = split[0];
  Command command = commands.get(alias.toLowerCase(Locale.ENGLISH));
  if (command == null) {
    // No such command.
    return false;
  }

  @SuppressWarnings("nullness")
  String[] actualArgs = Arrays.copyOfRange(split, 1, split.length);
  try {
    return command.hasPermission(source, actualArgs);
  } catch (Exception e) {
    throw new RuntimeException(
        "Unable to invoke suggestions for command " + alias + " for " + source, e);
  }
}
 
Example #8
Source File: Main.java    From TAB with Apache License 2.0 4 votes vote down vote up
@Subscribe
	public void onProxyInitialization(ProxyInitializeEvent event) {
		try {
			Class.forName("org.yaml.snakeyaml.Yaml");
			me.neznamy.tab.shared.ProtocolVersion.SERVER_VERSION = me.neznamy.tab.shared.ProtocolVersion.values()[1];
			Shared.mainClass = this;
			Shared.separatorType = "server";
			Shared.command = new TabCommand();
			server.getCommandManager().register("btab", new Command() {
				public void execute(CommandSource sender, String[] args) {
					if (Shared.disabled) {
						if (args.length == 1 && args[0].toLowerCase().equals("reload")) {
							if (sender.hasPermission("tab.reload")) {
								Shared.unload();
								Shared.load(false);
								if (Shared.disabled) {
									if (sender instanceof Player) {
										sender.sendMessage(TextComponent.of(Placeholders.color(Configs.reloadFailed.replace("%file%", Shared.brokenFile))));
									}
								} else {
									sender.sendMessage(TextComponent.of(Placeholders.color(Configs.reloaded)));
								}
							} else {
								sender.sendMessage(TextComponent.of(Placeholders.color(Configs.no_perm)));
							}
						} else {
							if (sender.hasPermission("tab.admin")) {
								sender.sendMessage(TextComponent.of(Placeholders.color("&m                                                                                ")));
								sender.sendMessage(TextComponent.of(Placeholders.color(" &c&lPlugin is disabled due to a broken configuration file (" + Shared.brokenFile + ")")));
								sender.sendMessage(TextComponent.of(Placeholders.color(" &8>> &3&l/tab reload")));
								sender.sendMessage(TextComponent.of(Placeholders.color("      - &7Reloads plugin and config")));
								sender.sendMessage(TextComponent.of(Placeholders.color("&m                                                                                ")));
							}
						}
					} else {
						Shared.command.execute(sender instanceof Player ? Shared.getPlayer(((Player)sender).getUniqueId()) : null, args);
					}
				}
/*				public List<String> suggest(CommandSource sender, String[] args) {
					List<String> sug = command.complete(sender instanceof Player ? Shared.getPlayer(((Player)sender).getUniqueId()) : null, args);
					if (sug == null) {
						sug = new ArrayList<String>();
						for (Player p : server.getAllPlayers()) {
							sug.add(p.getUsername());
						}
					}
					return sug;
				}*/
			});
			registerPackets();
			plm = new PluginMessenger(this);
			Shared.load(true);
		} catch (ClassNotFoundException e) {
			sendConsoleMessage("&c[TAB] The plugin requires Velocity 1.1.0 and up to work ! Get it at https://ci.velocitypowered.com/job/velocity-1.1.0/");
		}
	}
 
Example #9
Source File: VelocityCommandManager.java    From Velocity with MIT License 4 votes vote down vote up
/**
 * Offer suggestions to fill in the command.
 * @param source the source for the command
 * @param cmdLine the partially completed command
 * @return a {@link List}, possibly empty
 */
public List<String> offerSuggestions(CommandSource source, String cmdLine) {
  Preconditions.checkNotNull(source, "source");
  Preconditions.checkNotNull(cmdLine, "cmdLine");

  String[] split = cmdLine.split(" ", -1);
  if (split.length == 0) {
    // No command available.
    return ImmutableList.of();
  }

  String alias = split[0];
  if (split.length == 1) {
    // Offer to fill in commands.
    ImmutableList.Builder<String> availableCommands = ImmutableList.builder();
    for (Map.Entry<String, Command> entry : commands.entrySet()) {
      if (entry.getKey().regionMatches(true, 0, alias, 0, alias.length())
          && entry.getValue().hasPermission(source, new String[0])) {
        availableCommands.add("/" + entry.getKey());
      }
    }
    return availableCommands.build();
  }

  Command command = commands.get(alias.toLowerCase(Locale.ENGLISH));
  if (command == null) {
    // No such command, so we can't offer any tab complete suggestions.
    return ImmutableList.of();
  }

  @SuppressWarnings("nullness")
  String[] actualArgs = Arrays.copyOfRange(split, 1, split.length);
  try {
    if (!command.hasPermission(source, actualArgs)) {
      return ImmutableList.of();
    }

    return ImmutableList.copyOf(command.suggest(source, actualArgs));
  } catch (Exception e) {
    throw new RuntimeException(
        "Unable to invoke suggestions for command " + alias + " for " + source, e);
  }
}
 
Example #10
Source File: VelocityCommandExecutor.java    From LuckPerms with MIT License 4 votes vote down vote up
private ForwardingCommand(Command delegate) {
    this.delegate = delegate;
}