Solved Throwing A Potion?

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

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

    thebiologist13

    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:
    Code:
    java.lang.IllegalArgumentException: Projectile not supported
        at org.apache.commons.lang.Validate.notNull(Validate.java:203)
        at org.bukkit.craftbukkit.entity.CraftLivingEntity.launchProjectile(CraftLivingEntity.java:292)
        at com.github.thebiologist13.Spawner.spawnTheEntity(Spawner.java:888)
        at com.github.thebiologist13.Spawner.spawn(Spawner.java:339)
        at com.github.thebiologist13.Spawner.tick(Spawner.java:274)
        at com.github.thebiologist13.CustomSpawners$1.run(CustomSpawners.java:148)
        at org.bukkit.craftbukkit.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:126)
        at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:512)
        at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:212)
        at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:476)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:408)
        at net.minecraft.server.ThreadServerApplication.run
    Can villagers not throw potions?

    Thanks!
    ~thebiologist13
     
  2. ThrownPotion potion = (ThrownPotion)World.SpawnEntity(location, entityType.thrownpotion);

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

    theCodeBro

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

    chasechocolate

  5. Offline

    theCodeBro

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

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

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

    thebiologist13

    I forgot I posted this XD

    I figured it out and this is how I do it now:
    Code:java
    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
    6.  
    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();
    14.  
    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. }
    28.  
    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
    32.  
    33. nmsWorld.addEntity(ent); //Spawn the entity
    34. return ent.getBukkitEntity();

    Code:java
    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!
    ~thebiologist13
     
Thread Status:
Not open for further replies.

Share This Page