Solved Throwing A Potion?

Discussion in 'Plugin Development' started by thebiologist13, Sep 29, 2012.

Thread Status:
Not open for further replies.
  1. Offline


    Hi all!

    I don't expect this to be a hard question, but how do you throw a splash potion? I tried having my plugin spawn a villager then use launchProjectile(ThrownPotion.class), thereafter removing the villager (I only want the potion to be thrown, not the entity that threw it). I got this error though:
    java.lang.IllegalArgumentException: Projectile not supported
        at org.apache.commons.lang.Validate.notNull(
        at org.bukkit.craftbukkit.entity.CraftLivingEntity.launchProjectile(
        at com.github.thebiologist13.Spawner.spawnTheEntity(
        at com.github.thebiologist13.Spawner.spawn(
        at com.github.thebiologist13.Spawner.tick(
        at com.github.thebiologist13.CustomSpawners$
        at org.bukkit.craftbukkit.scheduler.CraftScheduler.mainThreadHeartbeat(
        at net.minecraft.server.MinecraftServer.q(
        at net.minecraft.server.DedicatedServer.q(
        at net.minecraft.server.MinecraftServer.p(
    Can villagers not throw potions?

  2. ThrownPotion potion = (ThrownPotion)World.SpawnEntity(location, entityType.thrownpotion);

    //set type of potion
    //set velocity
  3. Offline


    How to set the Potion effect it will give?
  4. Offline


  5. Offline


    tried, but didn't work. Here's My Code:

    ThrownPotion p = (ThrownPotion)player.launchProjectile(ThrownPotion.class);

    p.getEffects().add(new PotionEffect(PotionEffectType.HARM, 1, 0));
  6. Offline


    I forgot I posted this XD

    I figured it out and this is how I do it now:
    1. World world = spawnLocation.getWorld(); //Gets the world of location to spawn.
    2. PotionEffect effect = spawnType.getPotionEffectBukkit(); //Gets the potion effect.
    3. PotionType type = PotionType.getByEffect(effect.getType()); //Creates a potion type
    4. Potion p = new Potion(type);
    5. int data = p.toDamageValue(); //Gets the damage value of the potion
    7. //Now it gets complicated :P
    8. //First I get the NMS world representation.
    9. net.minecraft.server.v1_5_R2.World nmsWorld = ((CraftWorld) world).getHandle();
    10. //And make a NMS entity potion.
    11. EntityPotion ent = new EntityPotion(nmsWorld, spawnLocation.getX(), spawnLocation.getY(), spawnLocation.getZ(), new net.minecraft.server.v1_5_R2.ItemStack(373, 1, data));
    12. //Then a NBTTagCompound for the custom data.
    13. NBTTagCompound nbt = new NBTTagCompound();
    15. ent.b(nbt); //Gets all the normal NBT tags
    16. NBTTagCompound potionTag = nbt.getCompound("Potion"); //Potion tag of entity
    17. NBTTagCompound tagTag = new NBTTagCompound();
    18. if(potionTag == null) { //Makes sure it has the tag
    19. potionTag = new NBTTagCompound();
    20. potionTag.setShort("id", (short) 373); //Splash potion ID
    21. potionTag.setShort("Damage", (short) data); //Data value from before.
    22. potionTag.setByte("Count", (byte) 1); //Amount
    23. tagTag.set("CustomPotionEffects", makePotion(effect)); //makePotion(PotionEffect) is below
    24. } else {
    25. tagTag = potionTag.getCompound("tag");
    26. tagTag.set("CustomPotionEffects", makePotion(effect));
    27. }
    29. potionTag.setCompound("tag", tagTag); //Set the modified custom potion NBT to the compound
    30. nbt.setCompound("Potion", potionTag);
    31. ent.a(nbt); //Apply the new NBT to the entity
    33. nmsWorld.addEntity(ent); //Spawn the entity
    34. return ent.getBukkitEntity();

    1. //This only supports 1 potion effect on the splash potion.
    2. public NBTTagList makePotion(PotionEffect effect) {
    3. NBTTagList list = new NBTTagList();
    4. NBTTagCompound potionType = new NBTTagCompound();
    5. potionType.setByte("Id", (byte) effect.getType().getId());
    6. potionType.setByte("Amplifier", (byte) effect.getAmplifier());
    7. potionType.setInt("Duration", effect.getDuration());
    8. potionType.setByte("Ambient", (byte) 0); //Not ambient
    9. list.add(potionType);
    10. return list;
    11. }

    This may not be the most efficient way to do this, but it works. :)

    Hope I could help!
Thread Status:
Not open for further replies.

Share This Page