Potion effect arguments

An image of a potion argument with a list of potion effect suggestions

The PotionEffectArgument class represents Minecraft potion effects. When used, this argument is casted to Bukkit's PotionEffectType class, or alternatively a NamespacedKey object if the PotionEffectArgument.NamespacedKey argument is used to create a PotionEffectArgument.

Example - Giving a player a potion effect

Say we wanted to have a command that gives a player a potion effect. For this command, we'll use the following syntax:

/potion <target> <potion> <duration> <strength>

In this example, we utilize some of the other arguments that we've described earlier, such as the PlayerArgument and TimeArgument. Since duration for the PotionEffect constructor is in ticks, this is perfectly fit for the TimeArgument, which is represented in ticks.

new CommandAPICommand("potion")
    .withArguments(new PlayerArgument("target"))
    .withArguments(new PotionEffectArgument("potion"))
    .withArguments(new TimeArgument("duration"))
    .withArguments(new IntegerArgument("strength"))
    .executes((sender, args) -> {
        Player target = (Player) args.get("target");
        PotionEffectType potion = (PotionEffectType) args.get("potion");
        int duration = (int) args.get("duration");
        int strength = (int) args.get("strength");
        
        // Add the potion effect to the target player
        target.addPotionEffect(new PotionEffect(potion, duration, strength));
    })
    .register();
new CommandAPICommand("potion")
    .withArguments(new PlayerArgument("target"))
    .withArguments(new PotionEffectArgument.NamespacedKey("potion"))
    .withArguments(new TimeArgument("duration"))
    .withArguments(new IntegerArgument("strength"))
    .executes((sender, args) -> {
        Player target = (Player) args.get("target");
        NamespacedKey potionKey = (NamespacedKey) args.get("potion");
        int duration = (int) args.get("duration");
        int strength = (int) args.get("strength");

        PotionEffectType potion = PotionEffectType.getByKey(potionKey);

        // Add the potion effect to the target player
        target.addPotionEffect(new PotionEffect(potion, duration, strength));
    })
    .register();
CommandAPICommand("potion")
    .withArguments(PlayerArgument("target"))
    .withArguments(PotionEffectArgument("potion"))
    .withArguments(TimeArgument("duration"))
    .withArguments(IntegerArgument("strength"))
    .executes(CommandExecutor { _, args ->
        val target = args["target"] as Player
        val potion = args["potion"] as PotionEffectType
        val duration = args["duration"] as Int
        val strength = args["strength"] as Int

        // Add the potion effect to the target player
        target.addPotionEffect(PotionEffect(potion, duration, strength))
    })
    .register()
CommandAPICommand("potion")
    .withArguments(PlayerArgument("target"))
    .withArguments(PotionEffectArgument.NamespacedKey("potion"))
    .withArguments(TimeArgument("duration"))
    .withArguments(IntegerArgument("strength"))
    .executes(CommandExecutor { _, args ->
        val target = args["target"] as Player
        val potionKey = args["potion"] as NamespacedKey
        val duration = args["duration"] as Int
        val strength = args["strength"] as Int

        val potion = PotionEffectType.getByKey(potionKey)!!

        // Add the potion effect to the target player
        target.addPotionEffect(PotionEffect(potion, duration, strength))
    })
    .register()
commandAPICommand("potion") {
    playerArgument("target")
    potionEffectArgument("potion")
    timeArgument("duration")
    integerArgument("strength")
    anyExecutor { _, args ->
        val target = args["target"] as Player
        val potion = args["potion"] as PotionEffectType
        val duration = args["duration"] as Int
        val strength = args["strength"] as Int

        // Add the potion effect to the target player
        target.addPotionEffect(PotionEffect(potion, duration, strength))
    }
}
commandAPICommand("potion") {
    playerArgument("target")
    potionEffectArgument("potion", true)
    timeArgument("duration")
    integerArgument("strength")
    anyExecutor { _, args ->
        val target = args["target"] as Player
        val potionKey = args["potion"] as NamespacedKey
        val duration = args["duration"] as Int
        val strength = args["strength"] as Int

        val potion = PotionEffectType.getByKey(potionKey)!!

        // Add the potion effect to the target player
        target.addPotionEffect(PotionEffect(potion, duration, strength))
    }
}