Solved Summoning Splash or Lingering Potions.

Discussion in 'Spigot Plugin Development' started by Geekenex, Apr 2, 2020.

Thread Status:
Not open for further replies.
  1. Hey! I'm trying to make an item where if you right click it into the air, a lingering potion of poison will be propelled in the direction you are looking maybe like 5 blocks or so away onto the ground. However after lots of research and trying I'm still not successful.
    The problem is that the potion doesn't go in the direction you are looking and the potion isn't a potion of poison.
    Would anyone of you know how I can successfully create this item?
    - Thanks

    Here is my code:
    Code (Text):
    @EventHandler
      public void spiderWand(PlayerInteractEvent e){
        Player player = e.getPlayer();
        ItemStack item = e.getItem();
        if(e.getAction().equals(Action.RIGHT_CLICK_AIR)) {
            if(item.getType().equals(Material.NETHER_BRICK)) {
               
                ItemStack potion = new ItemStack(Material.SPLASH_POTION);
                PotionMeta meta = (PotionMeta) potion.getItemMeta();
                meta.setColor(Color.BLUE);
                meta.addCustomEffect(new PotionEffect(PotionEffectType.POISON, 1, 0), true);
                potion.setItemMeta(meta);
                Vector vector = new Vector();
                vector.setX(player.getLocation().getX());
                vector.setY(player.getLocation().getY());
                vector.setZ(player.getLocation().getZ());
                vector.multiply(0.7);
                ThrownPotion thrownPotion = (ThrownPotion) player.getWorld().spawnEntity(player.getLocation(), EntityType.SPLASH_POTION);
                thrownPotion.setVelocity(vector);
                thrownPotion.setItem(potion);
                thrownPotion.setBounce(true);
               
               
               
                }
               
        }  
              }    
     
  2. You are using the player’s location as vector instead of it’s direction. player#getLocation#getDirection() is the vector for the velocity you are looking for.

    The potion doesn’t have a poison effect because you set the potion duration to 1 which is 1 tick (very short).
     
  3. First of all, you are taking the players position and multiply that by 0.7, which gives you a vector that points from the world center point to 70% of where the players stands and then you put that vector onto the potion.
    If that did not make sense to you, maybe watch a short video about Vector mathematics - it helps a lot with understanding how they work.

    As @MightyOne already mentioned, you can use player.getLocation().getDirection() instead. If you want to get a vector direction, that starts at a given position and ends at a given position, you take the end point position (vector) and subtract the start position (vector) from it.

    It would be wise, to create the ItemStack as a private final variable inside your class, so that you don't recreate it, every time the method runs.
     
  4. Thanks! I looked into vectors more and read your responses and I solved the problem :)
    Your help was greatly appreciated.
     
Thread Status:
Not open for further replies.