aikar / commands

Java Command Dispatch Framework - (Bukkit, Spigot, Paper, Sponge, Bungee, JDA, Velocity supported, generically usable anywhere)
https://acfspigot.emc.gs
MIT License
566 stars 147 forks source link

Optional annotation does not properly work for World #254

Open DarkEyeDragon opened 4 years ago

DarkEyeDragon commented 4 years ago
@Default
@CommandPermission("rtp.teleport.self")
@CommandCompletion("@players")
public void onTeleport(CommandSender sender, @Optional @CommandPermission("rtp.teleport.other") OnlinePlayer target, @Optional @CommandPermission("rtp.teleport.world") World world) {
    Player player;
    if (target == null && sender instanceof Player) {
        player = (Player) sender;
    } else {
        player = target.getPlayer();
    }
    if (world == null) {
        world = configHandler.getDefaultWorld();
    }
    final World finalWorld = world;

    //Add it to the Scheduler to not falsely trigger the "Moved to quickly" warning
    new BukkitRunnable() {
        @Override
        public void run() {
            teleport(player, finalWorld, sender.hasPermission("rtp.teleport.bypass"));
        }
    }.runTaskLater(plugin, 1);
}

image

Machine-Maker commented 4 years ago

So I think the issue here, is that you have two optional args. The wiki says you can July have 1 (the last arg).

What I would do to get around this is to instead use @Default with some crazy string no one would type in. The write a custom resolver to handle World.class and if what was passed to the resolver was that string in @Default (aka the player left it blank), then handle that, other wise, look for a world with that name.

DarkEyeDragon commented 4 years ago

From the wiki:

Can only be used (currently) at end of the command for non Player arguments. Makes it so that if nothing is entered to resolve this context, it will simply return null.

What I got from this line is that it only works for the last argument (unless other optionals are from the type Player). Unless it actually means when the player is the command sender? In that case the current issue would make sense.

It seems to work fine when the player executes the command but the console doesn't allow it.

Which is weird behavior imo and should be adressed either way.