1.15.2 Infinite splash potions

Discussion in 'Spigot Plugin Development' started by DasGandlaf, May 15, 2020.

  1. Hello, I want to make a plugin, where when you throw a splash potion, you get that exact one back infinitely.

    Here's what I have:
    Code (Text):
       
    @EventHandler
        public void onProjectileLaunch(PotionSplashEvent event)
        {
            Projectile projectile = event.getEntity();

            if (projectile instanceof ThrownPotion)
            {
                if(event.getEntity().getShooter() instanceof Player)
                {
                    Player player = (Player) event.getEntity();
                    ((Player) event.getEntity().getShooter()).getInventory().addItem(event.getEntity().getItem());
                    player.sendMessage("you threw a potion");
                }
            }
        }
    I have this type of formatting because I came from c++ and can't read the traditional java formatting properly.

    I don't know why this doesn't work. I don't get the message "you threw a potion" and I don't get any errors.
    Thanks for the replys! :)
     
  2. If you do not get a message it means your query of the instance does not work. I would reccomend using the event ProjectileThrowEvent instead of PotionThrowEvent. :)
     
    • Informative Informative x 1
  3. Then this should work right ?:
    Code (Text):
        @EventHandler
        public void onProjectileLaunch(ProjectileLaunchEvent event)
        {
            Projectile projectile = event.getEntity();

            if (projectile instanceof ThrownPotion)
            {
                if(event.getEntity().getShooter() instanceof Player)
                {
                    Player player = (Player) event.getEntity();
                    player.getInventory().addItem(((Player) event.getEntity().getShooter()).getInventory().getItemInMainHand());
                    player.sendMessage("you threw a potion");
                }
            }
        }
    }
    but it doesn't :/
     
  4. you're casting the player as being an entity

    this
    Code (Java):
    Player player = (Player) event.getEntity()
    should be this
    Code (Java):
    Player player = (Player) event.getEntity().getShooter()
    then you can do
    Code (Java):
    player.getInventory().addItem(event.getEntity()); // I think anyway
    some of the code is probs wrong cause I'm not in an IDE but you get the gist
     
  5. Code (Text):
    player.getInventory().addItem(event.getEntity());
    I tried this but it didn't work :/ I casted it then to itemstack and it still doesnt work.
     
  6. You should be able to use ThrownPotion#getItem() after casting event.getEntity() to ThrownPotion to get the correct ItemStack and add that to the player's inventory.
     
    • Agree Agree x 1