commands icon indicating copy to clipboard operation
commands copied to clipboard

Optional annotation does not properly work for World

Open DarkEyeDragon opened this issue 4 years ago • 2 comments

@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

DarkEyeDragon avatar Mar 03 '20 19:03 DarkEyeDragon

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.

Machine-Maker avatar Mar 03 '20 19:03 Machine-Maker

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.

DarkEyeDragon avatar Mar 03 '20 19:03 DarkEyeDragon