Solved Snowball potion effect event

Discussion in 'Plugin Development' started by phildachil, Jan 19, 2014.

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

    phildachil

    Trying to make it so when a snowball hits near a player, the player(s) within a certain radius will get a potion effect.

    Here is my code so far:

    Code:java
    1. publicclass Bombs implements Listener {
    2.  
    3. @EventHandler
    4. public void onProjectileHit(ProjectileHitEvent e) {
    5. Projectile p = e.getEntity();
    6. if (!(p instanceof Snowball)) return;
    7. Snowball s = (Snowball) p;
    8. s.getWorld().createExplosion(s.getLocation(), 0);
    9. for (Entity en : s.getNearbyEntities(3, 3, 3)) {
    10. if (en instanceof Player) {
    11. Player pl = (Player) en;
    12. if (!(pl == e.getEntity().getShooter())) pl.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 400, 0));
    13.  
    14. }
    15. }
    16. }
    17. }
    18.  


    Would really appreciate some help!
     
  2. Offline

    MrInspector

    Use EntityDamageByEntityEvent
     
    Garris0n likes this.
  3. Offline

    phildachil

    instead of what?
     
  4. Offline

    MrInspector

    ProjectileHitEvent
     
  5. Offline

    phildachil

    Code:java
    1. publicvoid EntityDamageByEntityEvent(Entity damager, Entity damagee, EntityDamageEvent.DamageCause cause, int damage) {


    this?
     
  6. Offline

    Garris0n

    Do you know how events work?
     
    Unknown_Stalker_ and MrInspector like this.
  7. Offline

    MrInspector

    err :p

    Code:java
    1. @EventHandler
    2. public void onProjectileHit(EntityDamageByEntityEvent e) {
     
  8. Offline

    phildachil

    It's what I want to learn :S But I'm finding it hard to find any tutorials so I'm just testing for myself.


    Would this work? I have a feeling it doesn't since I think it applies the potion effect to the shooter.
    Code:java
    1.  
    2. @EventHandler
    3. public void onProjectileHit(EntityDamageByEntityEvent e) {
    4. Projectile p = (Projectile) e.getEntity();
    5. if (!(p instanceof Snowball)) return;
    6. Snowball s = (Snowball) p;
    7. s.getWorld().createExplosion(s.getLocation(), 0);
    8. for (Entity en : s.getNearbyEntities(3, 3, 3)) {
    9. if (en instanceof Player) {
    10. Player pl = (Player) en;
    11. if (!(pl == ((Projectile) e.getEntity()).getShooter())) pl.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 400, 0));
    12.  
    13. }
    14. }
    15. }
    16.  
    17.  
    18. }
    19.  
     
  9. Offline

    Garris0n

    You should first check if the projectile is an instance of Projectile before casting. To kill two birds with one stone, just do the snowball check and cast afterwards ignoring the Projectile part entirely. As for the rest, yeah that should work just fine, though I'd use .equals() on the player just because.
     
  10. Offline

    phildachil

    Ehm, doesnt this check if the projectile is a snowball

    Code:java
    1. if(!(p instanceof Snowball)) return;
    2.  
     
  11. Offline

    Garris0n

    Yes but you cast it to a projectile without checking first. Remove that.
     
  12. Offline

    phildachil

    Oh I misread your comment :S
    Alright so this?
    Code:java
    1. @EventHandler
    2. public void onProjectileHit(EntityDamageByEntityEvent e) {
    3. if (!(e.getEntity() instanceof Snowball)) return;
    4. Projectile p = (Projectile) e.getEntity();
    5. Snowball s = (Snowball) p;
    6. s.getWorld().createExplosion(s.getLocation(), 0);
    7. for (Entity en : s.getNearbyEntities(3, 3, 3)) {
    8. if (en instanceof Player) {
    9. Player pl = (Player) en;
    10. if (!(pl == ((Projectile) e.getEntity()).getShooter())) pl.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 400, 0));
    11.  
    12. }
    13. }
    14. }
    15.  
    16. }
     
  13. Offline

    Dippoakabob

    If you've already checked that it's an instance of a Snowball, there's no need to cast it to a projectile first.
    After your Snowball check, cast the e.getEntity() right to a snowball.
     
  14. Offline

    phildachil

    There's no 'need'. Does it matter that it's there?
     
  15. Offline

    Dippoakabob

    Nope, just was a little suggestion. Does your code not run still?
     
  16. Offline

    phildachil

    Actually the first code was fine, I was just messing up in my main class T_T
     
  17. Offline

    Dippoakabob

    Happens all the time :p
    Feel free to message or tag me if you have any issues.
     
Thread Status:
Not open for further replies.

Share This Page