Solved [Please Help] Error

Discussion in 'Plugin Development' started by Wolf_Jacob, Mar 15, 2013.

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

    Wolf_Jacob

    Thanks from the help of what you guys gave me and what i had i changed it around and got it to work...

    Thanks
     
  2. Offline

    kreashenz

    It shouldn't be Player player = player.getPlayer(); because the player variable hasn't been completely initialized.. It SHOULD be Player player = event.getPlayer();
     
  3. Offline

    Wolf_Jacob

    once i do that it tell me to add a cast to getPlayer();
     
  4. Offline

    kreashenz

    Wolf_Jacob so try Player player = ((Player)event.getPlayer());
     
  5. Wolf_Jacob
    "Unresolved compilation problem: The local variable player may not have been initialized"
    That means that you have compile errors which you did not fix, they appear in the color red in your IDE.
    Do not export jar when your IDE tells you that there are errors.

    Now, ignore what kreashenz said because that'll give you even more problems, instead you must first store event.getEntity() and then check if that's instanceof Player then cast the entity to Player...
    Because that event triggers when any entity is damaged... for example, if a cow gets damaged your code assumes that the cow entity is a player and tries to cast it, but fails because a cow is obviously not a player.

    And you have the same problem with the casting of event, you can't guarantee that the event triggered is entity-by-entity damage, entities can be damaged by blocks and themselves too you know... you should directly listen to the event you need.

    So use this:
    Code:
    @EventHandler
    public void eventEntityDamageEntity(EntityDamageByEntityEvent event) {
        Entity ent = event.getEntity();
        if(ent instanceof Player) {
            Player player = (Player)ent;
            // do stuff.
        }
    }
     
  6. Offline

    Barinade

    public void onEntityDamage(EntityDamageEvent event) {
    EntityDamageByEntityEvent dam = (EntityDamageByEntityEvent) event;
    Player player = player.getPlayer();

    Player player = (Player) player.getPlayer(); seems more suitable, lmfao.

    Kidding.
    Do this
    @EventHandler
    public void onEntityDamage(EntityDamageEvent event) {
    if (!(event.getEntity() instanceof Player))
    return;
    Player player = (Player) event.getEntity();
    int blockId = player.getItemInHand().getType().getId();
    if (blockId == 332) {
    if (dam.getDamage() == 0) {
    dam.setDamage(3);
    } else {
    event.setCancelled(true);
    }
    }
    }

    This only works when the player is damaged naturally, like fall or fire, pvp and mob damage is EntityDamageByEntityEvent, in which case you would do the same thing
     
  7. It triggers for all kind of damages actually, EntityDamageByEntityEvent is an extension of EntityDamageEvent.

    If you're going to write code for other people at least use [code] tags :p
     
  8. Offline

    kreashenz

    Digi He messages me telling me it didn't work. It was a guess, so sorry for being wrong :p
     
  9. Offline

    Barinade

    @EventHandler
    public void onEntityDamage(EntityDamageByEntityEvent event) {
    if (!(event.getEntity() instanceof Player))
    return;
    Player player = (Player) event.getEntity();
    int blockId = player.getItemInHand().getType().getId();
    if (blockId == 332) {
    if (event.getDamage() == 0) {
    event.setDamage(10);
    } else {
    event.setCancelled(true);
    }
    }
    }
     
  10. Offline

    Wolf_Jacob

    Barinade isnt that what i have already?? i want to be able to shoot a snowball with a hoe which is the first method with the PlayerInteract then after that do the Entity damage
     
  11. Offline

    drtshock

    No harm in trying. Everyone has to learn some how :)
     
  12. Offline

    kreashenz

    drtshock Yup.. Just like the way I learn, I have to be a hands-on learner, I can't learn just from people saying to do stuff.. I have to see it.
     
    drtshock likes this.
  13. Offline

    Barinade

    If you're nerfing damage altogether then use EntityDamageEvent, if you just want it for pvp/mobs use EntityDamageByEntity event
     
Thread Status:
Not open for further replies.

Share This Page