Potion effect on hit

Discussion in 'Plugin Development' started by LavaBall, Feb 21, 2015.

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

    LavaBall

    Hi There i need help with a plugin. What i need is when someone throws a snowball or an egg that the person it hits gets given a potion effect like nausea or what ever? ok but i can't get it right i have this so far:

    package me.bukkit.digdiggydigger;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;

    public class Snowballs {

    @EventHandler (priority = EventPriority.NORMAL)
    public void onEntityDamageByEntity (EntityDamageByEntityEvent event) {
    if (event.getDamager() instanceof Player && event.getEntity() instanceof Player) { //If both the damaged entity and damager are players
    Player attacker = (Player) event.getDamager();
    Player damaged = (Player) event.getEntity();
    if (attacker.getItemInHand().getTypeId() == Material.SNOW_BALL.getId()) {
    PotionEffect potionEffect = new PotionEffect(PotionEffectType.NAUSEA, 60, 2);
    potionEffect.apply(damaged);
    }
    }
    }
    }


    Can you let me know what is wrong with this code here and how to make it full working please!!

    Thanks a lot

    -LavaBall!
     
  2. Offline

    FerusGrim

    Nope, nope, nope.

    Please use the [/syntax] tags, and fix that spacing.

    Also, just to note, EntityDamageByEntityEvent#getDamager() is going to return the Snowball, not the player.

    Code:java
    1. if (event.getDamager() instanceof Snowball) {
    2. Snowball snowball = (Snowball) event.getDamager();
    3. if (snowball.getShooter() instanceof Player) {
    4. Player attacker = (Player) snowball.getShooter();
    5. }
    6. }


    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Feb 21, 2015
    MordorKing78 likes this.
  3. Offline

    LavaBall

    Code:java
    1. package me.bukkit.digdiggydigger;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.EventPriority;
    7. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    8. import org.bukkit.potion.PotionEffect;
    9. import org.bukkit.potion.PotionEffectType;
    10.  
    11. public class Snowballs {
    12.  
    13. @EventHandler (priority = EventPriority.NORMAL)
    14. public void onEntityDamageByEntity (EntityDamageByEntityEvent event) {
    15. if (event.getDamager() instanceof Player && event.getEntity() instanceof Player) { //If both the damaged entity and damager are players
    16. Player attacker = (Player) event.getDamager();
    17. Player damaged = (Player) event.getEntity();
    18. if (attacker.getItemInHand().getTypeId() == Material.SNOW_BALL.getId()) { //If the attacker is wielding a diamond sword, you can customize this to any thing you want
    19. PotionEffect potionEffect = new PotionEffect(PotionEffectType.POISON, 60, 2); //Replace this with your potion effect of choice
    20. potionEffect.apply(damaged); //Apply the potion effect
    21. }
    22. }
    23. }
    24.  
    25. }


    wait zoo where do i put your piece of code? and sorry i have very small knowledge of java so please bare with:D

    EDIT by Timtower: merged posts
     
  4. Offline

    FerusGrim

    Assuming that this event is fired when a Snowball hits a player, the property EntityDamageByEntityEvent#getDamager is going to return that Snowball Entity, not the Player who threw it.

    Replace your if-checks with my code.

    Also, rather than comparing the Ids of the Materials (for some reason), just compare the Materials themselves.

    Code:java
    1. attacker.getItemInHand() == Material.SNOW_BALL


    EDIT: ^ You shouldn't need that, though, considering you're already checking if the attacking Entity is a Snowball instance.
     
  5. Offline

    LavaBall

    ignore that last msg:D Ok So could you please post what the FULL code would be? because like i said I'm not the most knowledgable with Java:D
     
  6. Offline

    FerusGrim

    Then I also recommend you check this out.
     
  7. Offline

    LavaBall

    No I'm not that much of a noob:D ok just forget that lol so would it look a bit like this?


    package me.bukkit.digdiggydigger;

    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Snowball;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;

    public class Snowballs {

    @EventHandler (priority = EventPriority.NORMAL)
    public void onEntityDamageByEntity (EntityDamageByEntityEvent event) {
    if (event.getDamager() instanceof Snowball) {
    Snowball snowball = (Snowball) event.getDamager();
    if (snowball.getShooter() instanceof Player) {
    Player attacker = (Player) snowball.getShooter();

    if (attacker.getItemInHand() == Material.SNOW_BALL) { //If the attacker is wielding a diamond sword, you can customize this to any thing you want
    PotionEffect potionEffect = new PotionEffect(PotionEffectType.POISON, 60, 2); //Replace this with your potion effect of choice
    potionEffect.apply(damaged); //Apply the potion effect
    }
    }
    }
    }

    }
     
  8. Offline

    FerusGrim

    You don't need the Entity#getItemInHand check. By that point, you've already established that a Player object is the ProjectileSource for a Snowball object. Not to mention, that if-check will fail if the Snowball thrown is the last Snowball in that ItemStack, because Entity#getItemInHand would return Material.AIR (Or something else, if the player swapped hotkeys by the time the Snowball reached its target).
     
  9. Offline

    LavaBall

    ok so what would you suggest i do to make this piece of code correct once i have removed the Entity#getItemInHand check? Because now i have this:

    package me.bukkit.digdiggydigger;

    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Snowball;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;

    public class Snowballs {

    @EventHandler (priority = EventPriority.NORMAL)
    public void onEntityDamageByEntity (EntityDamageByEntityEvent event) {
    if (event.getDamager() instanceof Snowball) {
    Snowball snowball = (Snowball) event.getDamager();
    if (snowball.getShooter() instanceof Player) {
    Player attacker = (Player) snowball.getShooter();

    if (attacker == Material.SNOW_BALL) { //If the attacker is wielding a diamond sword, you can customize this to any thing you want
    PotionEffect potionEffect = new PotionEffect(PotionEffectType.POISON, 60, 2); //Replace this with your potion effect of choice
    potionEffect.apply(damaged); //Apply the potion effect
    }
    }
    }
    }

    }
     
  10. Offline

    Fhbgsdhkfbl

    Please use [/syntax] ..... It'll make your code easier to read...
     
  11. Offline

    _Filip

    i dont think ne1 here wants to read that mess
     
  12. Offline

    SuperOriginal

    If(player is equal to snowball)....
     
    ProMCKingz likes this.
  13. Offline

    LavaBall

    don't worry guys i can't get a straight answer:/
     
  14. Offline

    ProMCKingz

    @LavaBall
    like @SuperOriginal said; you are checking if player is a snowball, with this code:
    Code:
    if (event.getDamager() instanceof Snowball) {
    You want something on the basis of:
    Code:
    if (Event.getShooter().getItemInHand() == Material.SNOW_BALL)
     
  15. Offline

    Konato_K

    @ProMCKingz
    - There is no getShooter method in that event
    - You can't compare an ItemStack with a Material
    - That wouldn't work if they switched items before the snowball lands
    - Even if they don't switch item, it still wouldn't work for the last snowball
     
  16. Offline

    LavaBall

    Look Guys Thanks for trying to help but i just need the code?
     
  17. Offline

    SuperOriginal

    We've probably given more suggestions than we need to. Feel free to post updated code, and we can help from there. If you haven't changed anything, then try to understand what has been suggested above.
     
  18. Offline

    BurnerDiamond

    @LavaBall

    Bukkit is not what you think it is. People are spending their time to help you out, they are not making the plugin for you. We don't have to tell you the answers we just decide to help you since we're a community.

    Re-read all of the posts a love and if you still don't get it.... Go back to the Java docs.
     
  19. Offline

    Konato_K

    @LavaBall We are not a writing code service.
     
  20. Offline

    LavaBall

    ok sorry guys i get it ill try somewhere else:)
     
Thread Status:
Not open for further replies.

Share This Page