JorelAli / CommandAPI

A Bukkit/Spigot API for the command UI introduced in Minecraft 1.13

Home Page:https://commandapi.jorel.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add Option for Automatic Exception on Empty EntitySelectorArgument Lists

learliet opened this issue · comments

Description

There should be an option available when utilizing the EntitySelectorArgument to trigger an exception automatically when the resulting list is empty. This behavior mirrors the default implementation where argument.entity.notfound.entity or argument.entity.notfound.player is respectively returned.

Expected code

val myCommand = CommandAPICommand("myCommand")
  .withArguments(EntitySelectorArgument.ManyPlayers("players", false)) // allowEmpty flag
  .executes(CommandExecutor { sender, args ->
      // automatically returns "argument.entity.notfound.player" if empty
      val players = args[0] as List<Player> 
      // [...]
  })

Extra details

No response

I like this idea, and it should be fairly trivial to implement. In our existing multiple entity selector code, we have this:

case ENTITYSELECTOR_MANY_ENTITIES:
try {
List<org.bukkit.entity.Entity> result = new ArrayList<>();
for (Entity entity : argument.findEntities(cmdCtx.getSource())) {
result.add(entity.getBukkitEntity());
}
yield result;
} catch (CommandSyntaxException e) {
yield new ArrayList<org.bukkit.entity.Entity>();
}
case ENTITYSELECTOR_MANY_PLAYERS:
try {
List<Player> result = new ArrayList<>();
for (ServerPlayer player : argument.findPlayers(cmdCtx.getSource())) {
result.add(player.getBukkitEntity());
}
yield result;
} catch (CommandSyntaxException e) {
yield new ArrayList<Player>();
}

In this implementation, we catch the exception when the list of entities would be empty and instead return an empty array. Adding a parameter to allow this exception to bubble up should be available as an option.

It would be nice to get this implemented for our next release given how simple this is to implement.

Implemented in version 9.4.0.