Hook farmland destroyed due to stepping ?

Discussion in 'Plugin Development' started by Digi, Oct 2, 2011.

Thread Status:
Not open for further replies.
  1. How can I hook when farmland is destroyed due to stepping on it (as player, mob or block) ?

    I tried some BLOCK_* events but none of them trigger for that :/
  2. Offline


    Try this:

    1. public void onPlayerInteract(PlayerInteractEvent event)
    2. {
    3. if (event.getAction().equals(Action.PHYSICAL))
    4. {
    5. if (event.getClickedBlock().getTypeId() == 60)
    6. {
    7. if (event.getPlayer() != null)
    8. {
    9. System.out.println(event.getPlayer().getName()+" destroyed farmland by stepping on it!");
    10. }
    11. else
    12. {
    13. System.out.println("A non-player destroyed farmland by stepping on it!");
    14. }
    15. }
    16. }
    17. }
  3. Oh I see, thanks, it works :}

    A side question, a curiosity... is there a difference between getAction().equals(Action.PHYSICAL) and getAction() == Action.PHYSICAL ?
  4. Offline


    Yeah, using .equals implicitly casts both values to String, which works but is inefficient. Much better to use == here; direct comparisons are allowed with enum types.
  5. I see, so when *should* I use .equals() ?
  6. Offline


    When comparing two objects which really are Strings (I'm including string literals in that).
  7. Offline

    Simon Welker

    Does this work (the non-player part) for flowing water destroying farmland too? Or is there any other Event I could cancel to prevent it from doing so? Can't find any in the Javadoc.

    Thanks in advance!
  8. Offline

    Simon Welker

    Because I'm a lazyhat and don't have Eclipse right at the moment, and you might have known. Sigh, now I'll be extra disappointed if it doesn't work! xD
    Will do.
  9. Offline


    Nope, it won't work.

    Well, the name says for itself:

    You probably may want to use BlockPhysicsEvent
    public void onBlockPhysics (BlockPhysicsEvent event)
  10. Well, I've tested myself since I'm having the tools at hand anway... the water doesn't seem to ruin the farmland block tough.
    And no, it doesn't trigger for water hitting the wheat planted.

    The onPlayerInteract() method triggers for mobs stepping on farmland too, so the name is confusing :p
  11. Offline


    Haha, I didn't know that :)
  12. Offline

    Simon Welker

    Uh? So it works actually using the onPlayerInteract method? I'm just trying to write some code to get it to work. Or do you mean that for you, water flowing on farmland doesn't ruin it? If so, it does, it needs some time to though.

    It seems it works!

    public class FarmlandPlayerListener extends PlayerListener {
        public void onPlayerInteract(PlayerInteractEvent event) {
            Block block = event.getPlayer().getLocation().getBlock().getRelative(BlockFace.DOWN);
            if (event.getAction() == (Action.PHYSICAL) && block.getType() == Material.SOIL) {
                if (event.getPlayer() == null) {
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: May 20, 2016
  13. I didn't say I cancelled it :) I only printed a message in my code and nothing triggered.
  14. Offline

    Simon Welker

    Well, with that code I posted, on some weird way, it really works. Grass blocks still go back to dirt blocks if water flows on them, but Farmland blocks do not anymore. I'm wondering why the heck "event.getPlayer().getLocation()" actually works, as event.getPlayer() apparently is really null when triggered by WaterFlow. Wow. I'll test it again without my small plugin, then I can definitely confirm this.

    Am I an idiot? Water doesn't kill farmland blocks at all, hahaha! I programmed a plugin for nothing and wasted your time too xD Please forgive me! :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: May 20, 2016
  15. Yes, that's what I was trying to tell you, farmland doesn't get ruined by water, just the wheat on top and that doesn't trigger the event :p
Thread Status:
Not open for further replies.

Share This Page