Splash Potion Help

Discussion in 'Spigot Plugin Development' started by Creepermanthe3rd, Apr 15, 2017.

Thread Status:
Not open for further replies.
  1. Hello, ive had alot of trouble trying to figure out how to test when a player splash's a speed potion
     
  2. 1. Interact event
    2. Check if left click (throw)
    3. Check if item in hand is potion
    4. Check if potion is speedpotion
    5. Your code
     
  3. Better to use PotionSplashEvent
     
  4. Can anyone give me some example code of potion splash so like when a PLAYEr splashes a Speed pot it will either cancel the event or remove the effect
     
  5. well you're strait up asking for a spoonfeed

    public void onPotionSplash(PotionSplashEvent e){
    if(e.getPotionEffect().equals(speed)){
    yada yada
    }
    }

    The code was written on the browser and is untested

    and if you paste this into eclipse I can pretty much guarantee it wont work
     
    • Like Like x 1
  6. Code (Text):

        @EventHandler
        public void onPotionSplash(PotionSplashEvent e) {
            Bukkit.broadcastMessage("splas");
            if(e.getPotion().equals(PotionEffectType.SPEED)) {
                Bukkit.broadcastMessage("speed");
    When I throw a potion it says splash, when I throw a speed it doesnt say Speed though. Any ideas why? (Debugging btw)
     
  7. wait you know your shit

    I thought you didnt know what to do since it seemed like you needed a spoonfeed
    ok uhh try this:

    {
    @EventHandler
    public void onPotionSplash(PotionSplashEvent e) {
    Bukkit.broadcastMessage("splas");
    if(e.getPotion().getEffects().contains(PotionEffectType.SPEED)) {
    Bukkit.broadcastMessage("speed");
    }}}
     
  8. Incorrect.

    I've produced this code:
    Code (Text):
    @EventHandler
    public void onPotionSplash(PotionSplashEvent event) {
        if (event.getEntity().getShooter() instanceof Player) {
            Player player = (Player) event.getEntity().getShooter();
               
            for (PotionEffect potionEffect : event.getPotion().getEffects()) {
                if (potionEffect.getType().equals(PotionEffectType.SPEED)) {
                    player.sendMessage(ChatColor.GREEN + "You just threw a splash potion of Speed.");
                }
            }
        }
    }
     
    • Like Like x 1
  9. how on earth is mine incorrect? You just did it differently.
     
    • Agree Agree x 1
  10. Test your code and view the outcome. ;)

    You code is very sloppy and unorganized.
     
  11. I know


    by the way, I didn't test it but according to all laws of code and my knowledge, it should work

    but then I could be stupid
     
  12. THANK YOU SOO MUCH Man. Been stuck on this for fucking hours.
     
    • Friendly Friendly x 1
  13. Test it then you'll find out if you're stupid or not. :p
    No problem ;)
     
    • Like Like x 1
    • Agree Agree x 1
  14. Do you know how I could see if the potion is coming from a dispenser?
     
  15. Also if a player splashes another player what would i use to say message the player who was affected?
     
  16. I'll produce an example now.
    You'd have to loop through the method getAffectedEntities.
     
  17. if(person that throwed instanceof entity){
    }
    else{
    yada yada
    }
     
  18. I've created an example of the two requests that you were asking for:
    Code (Text):
    @EventHandler
    public void onPotionSplash(PotionSplashEvent event) {
        if (event.getEntity().getShooter() instanceof Player) {
            Player player = (Player) event.getEntity().getShooter();
               
            for (PotionEffect potionEffect : event.getPotion().getEffects()) {
                if (potionEffect.getType().equals(PotionEffectType.SPEED)) {
                    player.sendMessage(ChatColor.GREEN + "You just threw a splash potion of Speed.");
                       
                    for (LivingEntity livingEntities : event.getAffectedEntities()) {
                        if (livingEntities instanceof Player) {
                            Player targetPlayer = (Player) livingEntities;
                            if (targetPlayer != player) {
                                targetPlayer.sendMessage(ChatColor.RED + "You were affected by " + player.getName() + "'s splash potion of speed.");
                            }
                        }
                    }
                }
            }
        }
           
        if (event.getEntity().getShooter() instanceof BlockProjectileSource) {
            BlockProjectileSource bps = (BlockProjectileSource) event.getEntity().getShooter();
            if (bps.getBlock().getType() == Material.DISPENSER) {
                for (PotionEffect potionEffect : event.getPotion().getEffects()) {
                    Bukkit.broadcastMessage(ChatColor.GREEN + "A splash potion of " + potionEffect.getType().getName().toLowerCase() + " was released from a dispenser.");
                }
            }
        }
    }
     
    • Like Like x 1
    • Useful Useful x 1
Thread Status:
Not open for further replies.