Snowball damage

Discussion in 'Plugin Development' started by Anrza, Jul 11, 2014.

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

    Anrza

    Not the best thread name ever, but in absence of better ideas, it'll do.

    I have this code:
    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void onPlayerInteract(PlayerInteractEvent event) {
    4. if (event.getAction() == Action.RIGHT_CLICK_AIR){
    5. Player player = (Player) event.getPlayer();
    6. if (event.getPlayer().isSneaking()){
    7. if (event.getPlayer().getItemInHand().getType().equals(Material.IRON_AXE)){
    8. ItemStack is = new ItemStack(Material.AIR,0);
    9. player.setItemInHand(is);
    10. Location loc = player.getEyeLocation();
    11. Snowball ball = (Snowball)loc.getWorld().spawnEntity(loc, EntityType.SNOWBALL);
    12. ball.setShooter(player);
    13. ball.setVelocity(player.getLocation().getDirection());
    14. String string = ball.getUniqueId().toString();
    15. map.put(string, 10);
    16. }
    17. }
    18.  
    19. }
    20. }
    21. @SuppressWarnings("deprecation")
    22. @EventHandler
    23. public void onEntityDamageByEntityEvent(EntityDamageByEntityEvent event) {
    24. if (event.getDamager().getType().equals(EntityType.SNOWBALL)); {
    25. System.out.println("Worked so far");
    26. EntityDamageEvent hitEntity = (EntityDamageEvent) event.getEntity();
    27. String ball = (String) event.getDamager().getUniqueId().toString();
    28. Integer damage = (Integer) map.get(ball);
    29. hitEntity.setDamage(damage);
    30. }
    31.  
    32. }


    I basically want it to throw a snowball when you right click and iron axe while sneaking (throw it). That works fine. Then I'm trying to set the damage it will deal by getUniqueId, using it as a string for a hashmap and setting the value of it to the amount of damage I want it to deal.
    In the next part, it's pretty clear what I want it to do, but it doesn't deal any damage and wont even print the debug message "Worked so far".
    This is the error I get when the snowball hits an entity:
    Can someone tell me what I'm doing wrong, please?
     
  2. Anrza could you copy & paste line 53 of WeaponThrowing.java from your Workspace please?
     
  3. Code:
    EntityDamageEvent hitEntity = (EntityDamageEvent) event.getEntity();
    You're casting EntityDamageEvent to an entity? That's the error I believe.
    I think you mean:
    Code:
    Entity hitEntity = event.getEntity();
    By the way, you're going to get a NullPointerException if the player has no item and they right click air as the item will be null. Make sure to check if event.getItem() != null first.
    Also, don't use event.getPlayer().getItemInHand(), use event.getItem().
     
  4. Offline

    Anrza

    Joshuaknight1998 KingFaris11
    Code:java
    1. EntityDamageEvent hitEntity = (EntityDamageEvent) event.getEntity();

    Ah, you both solved this in different ways :p
    Yea, that was kinda improvised.
    And thanks, now I know how to read error reports :3

    So, how do you suggest I fix this?
     
    KingFaris11 likes this.
  5. I already posted the code of what you should have. :p

    Suggestion: You should also check if the map contains the Snowball, as players shooting normal snowballs will get affected by this too.

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

    Anrza

    KingFaris11
    Yes, thank you for the code and the advice, but the code gives me the error "The method setDamage(Intiger) is undefined for the type Entity".
    There's a quick fix "Add cast to 'hitEntity'", and while Eclipse doesn't complain about the code, I get this error message in the console:
    Since it refers to line 58, which is:
    Code:java
    1. ((EntityDamageEvent) hitEntity).setDamage(damage);

    I suppose the problem has to do with that.
     
  7. You're doing it again ;( ;(
    You cannot cast EntityDamageEvent to an entity... You're meant to do:
    event.setDamage(damage);
    No "hitEntity" rubbish.

    It seems you don't know the basics of Java, I suggest you learn that at least. You should know what you can and can't cast.
     
  8. Offline

    Anrza

    KingFaris11
    Thanks... yea, I've been hearing that I should learn stuff lately. I guess I will. Thank you very much for helping me!
     
    KingFaris11 likes this.
Thread Status:
Not open for further replies.

Share This Page