Detecting when player moves to the next block?

Discussion in 'Plugin Development' started by Pocketkid2, Feb 5, 2015.

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

    Pocketkid2

    I've got some code here that is supopsed to return true if the player has moved to a different block, using a PlayerMoveEvent.

    Code:
    public boolean changedBlock(PlayerMoveEvent event) {
            if (event.getTo().getBlockX() != event.getFrom().getBlockX()) {
                if (event.getTo().getBlockZ() != event.getFrom().getBlockZ()) {
                    return true;
                }
            }
            return false;
        }
    This is called in a PlayerMoveEvent in my plugin, and a debug message is showed when it goes through. The problem is, when I test it, I have to walk around a lot to get it to trigger. What's going on? It doesn't trigger as much as it is supposed to!
     
  2. Offline

    Sheepii

    Code:
    public boolean changedBlock(PlayerMoveEvent event) {
            Block current = event.getPlayer().getLocation().getBlock();
            Block last = null;
            if (current != last) {
                last = current;
                return true;
            }
            return false;
        }
    
    Try this.
     
  3. Offline

    Pocketkid2

    If you are assigning last to null, wouldn't that if statement always happen?

    Yep, that doesn't work. It just returns true every time.

    Can anyone help?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  4. Offline

    Lolmewn

    You're only returning true when X AND Z change. Try doing X OR Z.
     
  5. Offline

    Pocketkid2

    Still no work
    EDIT: Here's my code:
    Code:
    public boolean changedBlock(PlayerMoveEvent event) {
            if (event.getFrom().getBlockX() != event.getTo().getBlockX() || event.getFrom().getBlockZ() != event.getTo().getBlockZ()) {
                return true;
            }
            return false;
        }
    
    Never mind, it works! I just need to fix some of my other code to make it work perfectly...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  6. Offline

    Lolmewn

    @Pocketkid2 thought so :) don't forget to mark the thread as solved!
     
Thread Status:
Not open for further replies.

Share This Page