Solved Snowball Damage

Discussion in 'Plugin Development' started by TheAJ471, Dec 22, 2013.

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

    TheAJ471

    So how would I make a snowball do damage to a player. I tried this, but it didnt work. How would i fix it?
    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler(priority=EventPriority.HIGH)
    3. public void onSnowBall(EntityDamageByEntityEvent event)
    4. {
    5. if(event.getCause() == DamageCause.PROJECTILE){
    6. if(event.getDamager() instanceof Snowball){
    7. if(event.getEntity() instanceof Player){
    8. event.setDamage(2);
     
  2. Offline

    C0mExpert

    Maybe a newbie question, is a Snowball an entity ?

    Just made this, let me know when it doesn't work.

    Code:java
    1. package me.comexpert.help;
    2.  
    3. import org.bukkit.entity.*;
    4. import org.bukkit.event.*;
    5. import org.bukkit.event.entity.*;
    6. import org.bukkit.plugin.java.*;
    7.  
    8. public class Help extends JavaPlugin {
    9.  
    10. @EventHandler(priority=EventPriority.HIGH)
    11. public void onSnowBall(EntityDamageByEntityEvent event) {
    12.  
    13. if(event.getDamager() instanceof Snowball && event.getEntity() instanceof Player){
    14. Player player = (Player) event.getEntity();
    15. player.setHealth(player.getHealth()-2);
    16. }
    17. }
    18. }
    19.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  3. Offline

    TheAJ471

    C0mExpert On player.getHealth()-2); there is an error on .getHealth that is 'The method getHealth() is ambiguous for the type Player'

    C0mExpert Ok so I figured it out. Where you have
    Code:java
    1. player.setHealth(player.getHealth()-2);

    I replaced it with
    Code:java
    1. player.damage(2);
    2.  

    And now it works. So it is this all together
    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler(priority=EventPriority.HIGH)
    3. public void onSnowBall(EntityDamageByEntityEvent event) {
    4.  
    5. if(event.getDamager() instanceof Snowball && event.getEntity() instanceof Player){
    6. Player player = (Player) event.getEntity();
    7. player.damage(2);
    8. }
    9. }


    Also thanks for the help! [diamond]

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  4. Offline

    viper_monster

    TheAJ471 build against Bukkit, not CraftBukkit
     
  5. Offline

    C0mExpert

    TheAJ471
    You welcome, if you need more help just PM or tahg me ;)

    Greetings,

    C0mExpert
     
  6. Offline

    maciekmm

Thread Status:
Not open for further replies.

Share This Page