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. Offline

    Jakeob22

    If I want to check if somebody is breaking a block, how would I do so?

    If ( X ) {
    number = 1;
    }

    What would I put for "X" if I wanted to check if they broke a dirt block? The number is just something to put into the if statement. I don't like them to be empty. On another note, how long does it take bukkit to add a submission into the released plugins section?
     
  2. Offline

    Daniel Heppner

    Umm. Look at the Javadocs.
     
  3. Offline

    Perdog

    Well because I'm in a helpful mood tonight :p

    Code:java
    1. public void onBlockBreak (BlockBreakEvent event) {
    2. Block block = event.getBlock();
    3. Player player = event.getPlayer();
    4. if (block.getType() == Material.DIAMOND_ORE) {
    5. player.sendMessage("No diamond for you!");
    6. event.setCancelled(true);
    7. }
    8. }


    That should work, there may be some errors because I typed it out from memory
     
    Daniel Heppner likes this.
  4. Offline

    Jakeob22

    I found on the javadocs. Looks good. Question though, how can I get the coords of the block that was broken, for later?
     
  5. Offline

    Perdog

    Code:java
    1. public void onBlockBreak (BlockBreakEvent event) {
    2. Block block = event.getBlock;
    3. Location loc = block.getLocation();
    4. }

    That will save the blocks location in a variable, which depending on how much later you want to use it, could be completely useless
     
  6. Offline

    Jakeob22

    Thanks. ;)
    I'm using it in the same if statement and saving it and an int. I'm new to this, but I think I can get this to work. Thanks! :D
     
  7. Offline

    Daniel Heppner

    Feel free to PM me if you need any more help. Or hop on IRC.
    (I know I didn't help you in the first place, but I can still offer it, right?)

    EDIT: 1,200th post! :D
     
    Perdog likes this.
  8. Offline

    Jakeob22

    Actually, do you know how I could call a spider or silverfish to spawn at that location?
     
  9. Offline

    Perdog

    Code:java
    1. //stuff from above
    2. loc.getWorld().spawnCreature(loc, CreatureType.SPIDER);

    I think that's the way it's done, but again I haven't typed that out in my eclipse first so it may not be right
     
  10. Offline

    Jakeob22

    Does anybody know how I would declare all of this in the main class? I just want it to be that if you break a block it plays through some code. I'm getting errors saying it couldn't pass the event to my plugin. :(
     
  11. If you show us the error, we can probally help you out.
     
  12. Offline

    Jakeob22

    The plugin's name is "TheyHearYou" Just so you know ;)

    Oh... could it be that I need the class to be TheyHearYouBlockListener? xD

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 21, 2016
  13. The name of the class doesn't really matter. Just make sure it extends BlockListener (ex: public class MyClass extends BlockListener)
     
  14. Offline

    Jakeob22

    Ha, it worked. xD Thanks, guys!

    I have 1 more question now. How do I check if a player kills a spider? :)
    I made a PlayerListener now. (Rather than the BlockListener)
    If anybody could help, that would be great. :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 21, 2016
    Perdog and r3Fuze like this.
  15. You need an EntityDeathEvent inside an EntityListener, then you just check if event.getAttacker is instanceof Player.
     
    Jakeob22 likes this.
  16. Offline

    Jakeob22

    :D Fast reply, and that should get my plugin rolling. Do you know where I can get a list of all of the listener types?
     
  17. Something like this ?
     
  18. Offline

    Perdog

    You'd need an entity listener, not a playerlistener

    Code:java
    1. public class EL extends EntityListener {
    2. public void onEntityDeath (EntityDeathEvent event) {
    3. Entity entity = event.getEntity();
    4. Entity player = entity.getLastDamageCause();
    5. if (entity instanceof Spider) {
    6. if (player instanceof Player) {
    7. player.sendMessage("You killed a spider");
    8. }
    9. }
    10. }
    11. }

    That would tell the person that killed the spider that they killed it :p
     
  19. Offline

    coldandtired

    Ignore, Perdog has shown me the light :)
     
  20. Offline

    Perdog

    Damn I'm always late :p And there's a getAttacker now???
     
  21. Oh, woops. Actually theres not. I dont know where I got that from.
     
  22. Offline

    Jakeob22

    If I did
    Location spid = "X"

    How would I use X to get the location of the dead spider?

    I didn't need the getAttacker, so it worked out. ;)
     
  23. If it's inside the EntityDeathEvent then just use event.getEntity().getLocation()
     
  24. Offline

    coldandtired

    Actually, I'm pretty sure this wouldn't work.
    Try
    Code:Java
    1. Entity entity = ((EntityDamageByEntityEvent)event.getEntity().getLastDamageCause()).getDamager();
    2. Player player = (Player)entity;
     
  25. Offline

    Perdog

    The same way you did for your block, just take the entity variable that I gave you and get its location

    Why would you use an entitydamage event for the death of something?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 21, 2016
  26. That line is just a hacky way of getting the last attacker of the dead entity.
     
  27. Offline

    Perdog

    I think a way better way would be (Even better then the one that I first put)
    Code:java
    1. DamageCause cause = event.getEntity().getLastDamageCause().getCause();
    2. if (cause instanceof Player) {
    3. Player player = (Player) event;
    4. }
     
  28. That wont work since DamageCause is just an enum and it doesn't extend Entity and therefore can't be a player.
     
  29. Offline

    coldandtired

    Neither of those would work!

    The EntityDeathEvent doesn't contain a method for who killed the entity, only who was killed.
     
  30. Offline

    Perdog

    I know, but it does have an event to get the cause, and I know there is a way to translate that cause to the entity that did it. I think your way works, but there has to be a cleaner way to do it
     
Thread Status:
Not open for further replies.

Share This Page