An if statement for breaking a block?

Discussion in 'Plugin Development' started by Jakeob22, Dec 2, 2011.

Thread Status:
Not open for further replies.
  1. I just tested it and it does actually work, but as you said, theres probally a better way of doing it.

    Code I used:
    Code:java
    1. public void onEntityDeath(EntityDeathEvent event) {
    2. Entity entity = ((EntityDamageByEntityEvent) event.getEntity().getLastDamageCause()).getDamager();
    3. Player player = (Player) entity;
    4. if(event.getEntity() instanceof Spider) {
    5. player.sendMessage("You killed a spider");
    6. } else {
    7. player.sendMessage("You killed something else");
    8. }
    9. }

    You would ofcourse need to test if entity is instanceof Player and stuff.
     
  2. Offline

    coldandtired

    By coincidence, I was messing around with this code in my own plugin about two hours ago. :)

    I was surprised that I couldn't find a better way. The first thing I tried was using the damage event to put the killer into a Map and then reading it in the death event! Compared to that a little cast is much nicer.

    Your code above is pretty similar to what I used, I just had it check that the DamageCause was EntityAttack before doing the rest.
     
  3. Offline

    Jakeob22

    It doesn't matter! I got mine working the way I wanted. xD Thanks again!
     
    r3Fuze likes this.
  4. What code did you use ?
     
  5. Offline

    Drakonix

    The right way of doing it :)

    Code:JAVA
    1.  
    2. public void onEntityDeath(EntityDeathEvent event) {
    3. Entity e = event.getEntity();
    4. if (event.getEntity().getLastDamageCause() instanceof EntityDamageByEntityEvent) {
    5. Entity dmgr = ((EntityDamageByEntityEvent) event.getEntity()
    6. .getLastDamageCause()).getDamager();
    7.  
    8. if (dmgr instanceof Player) {
    9. ((Player) dmgr).sendMessage("You killed " + e.toString());
    10.  
    11. }
    12.  
    13. }
    14. }
    15.  
     
  6. Offline

    Jakeob22

    I made an onCreatureSpawn event. How do I cancel the spawning of an animal? I have this so far:

    Code:
        public void onCreatureSpawn(CreatureSpawnEvent event) {
            creature = event.getCreatureType();
    
            if (creature == CreatureType.CHICKEN){
    // Here is where I want to cancel the event ;)
            }
    
        }
     
  7. Offline

    Perdog

    Instead of constantly coming here for help you should look through the javadocs and stuff. I'm not trying to be mean but it's almost like you aren't even trying to do it yourself :p

    It's as simple as
    Code:
    event.setCancelled(true);
     
    Jakeob22 likes this.
  8. Offline

    Jakeob22

    I've tried browsing the javadocs. That's how I found CreatureType.CHICKEN

    When I searched them, I only found that spawning was cancelable. I tried to look it up, but I couldn't find it. Thanks for your help everybody! I'm probably just not using them correctly.
     
  9. Offline

    Perdog

    The best thing to do, and what I did when I was starting out, is to just type event. and then scroll down through all the options to see what all can be done with that, then when you find what might be what you need select it then press . again and scroll through those options. Adding the javadocs to your imports and scrolling through them is also another really good way to learn :)
     
Thread Status:
Not open for further replies.

Share This Page