Solved Attacking Bats

Discussion in 'Spigot Plugin Development' started by OopsyPoopsy, Apr 21, 2017.

  1. Hello.
    I'm trying to make it so when a player attacks a bat, it will send them a message but when the player hits the bat, nothing happens.

    Code (Java):

    @SuppressWarnings("deprecation")

    @EventHandler

    public void onBatHit(EntityDamageByEntityEvent e) {

    Player p = (Player)e.getDamager();

    Player p2 = (Player)e.getEntity();

    Entity entity = e.getEntity();


    EntityType batentity = EntityType.BAT;

    String batname = batentity.toString();


    Location loc = p.getLocation();

    Location loc1 = loc.add(0.0,4.0,0.0);


    if (e.getEntity() == p2) {

    p.getLocation().getWorld().spawnCreature(loc1, batentity);

    return;

    }

    if (e.getEntityType() == EntityType.BAT) {

    e.setCancelled(true);

    p.sendMessage("hm");

    return;
     
     
  2. is a bat a player? did you even check for a stacktrace?
     
  3. Why are you casting entity to player for P2? You also don't know for sure the damager is a player in all circumstances.


    You should do, if
    Code (Text):

    if (e.getEntity() instanceof BAT && e.getDamager() instanceof PLAYER)
    {
    Player p = (Player) e.getDamager();

    //then do whatever you want to do when a bat is hit

    }
     
    The entity could never be both a player and a bat, thus if you tried to run it it would surely error trying to cast a bat to a player (or creeper or any other entity that wasn't a player)


    Sent from my iPhone using Tapatalk
     
  4. I tried this and it still did not work.

    Code (Java):

    @SuppressWarnings("deprecation")

    [USER=17846]@EventHandler[/USER]

    public void onBatHit(EntityDamageByEntityEvent e) {

    Player p = (Player)e.getDamager();

    Player p2 = (Player)e.getEntity();


    EntityType batentity = EntityType.BAT;


    Location loc = p.getLocation();

    Location loc1 = loc.add(0.0,4.0,0.0);


    if (e.getEntity() == p2) {

    p.getLocation().getWorld().spawnCreature(loc1, batentity);

    return;

    }

    if (e.getEntity() instanceof Bat && e.getDamager() instanceof Player)

    {



    Bukkit.broadcastMessage("hm");
     
     
  5. Strahan

    Benefactor

    It's better to learn the right way to do things than to suppress deprecation warnings.[/code][/QUOTE]

    You say that, but the code you just posted still is casting to player without validation.

    PS and what's the point of making the batentity variable? It seems pretty unnecessary as wherever you use that, you can just use EntityType.BAT.
     
  6. I have that code because once p hits p2, then it will summon a bat.
     
  7. The instanceof checks need to be the very first thing, you're blindly casting the entity and damager to player when in reality they could be anything. This event fires whenever any entity damages another entity. So if a skeleton shoots an armor stand it fires. If a player hits a creeper it fires.

    If you only want this to affect bats you need the instanceof stuff at the top then your code below.


    Sent from my iPhone using Tapatalk
     
  8. He did put the validation. But he just copied my example code and stuck it at the bottom below his blind casts heh.


    Sent from my iPhone using Tapatalk
     
  9. I fixed it, thank you so much for your help!

    Code (Java):

    @SuppressWarnings("deprecation")

    @EventHandler

    public void onBatHit(EntityDamageByEntityEvent e) {


    EntityType batentity = EntityType.BAT;


    Location loc = e.getDamager().getLocation();

    Location loc1 = loc.add(0.0,4.0,0.0);


    if (e.getDamager() instanceof Player) {

    if (e.getEntity() instanceof Player) {

    e.getDamager().getLocation().getWorld().spawnCreature(loc1, batentity);

    return;

    }

    if (e.getEntity() instanceof Bat && e.getDamager() instanceof Player) {

    Player p = (Player)e.getDamager();



    Bukkit.broadcastMessage("hm");


    }

    }

    }

    }
     
     
  10. Okay you're trying to do two different things.
    If a player hits another player it Summons a bat?

    if(e.getEntity() instanceof Player && e.getDamager() instanceof Player) {
    //this would be any player damaging any other player
    }

    Then in a second block of code
    If(e.getEntity() instanceof BAT && e.getDamager() instanceof Player)
    {
    //this would be a player hitting a bat
    }


    Sent from my iPhone using Tapatalk