On player changes block

Hello guys,

I’m trying to execute some code each time a player moves from one block to another, only horizontally (if x or z changes). So I came up with this but it’s not working as I expected. Is there any mistake in this or does it come from the following operations (in which case I’ll take care of) ?

@Listener
public void onPlayerMove(DisplaceEntityEvent event, @First Player player)
{
    if (event.getFromTransform().getLocation().getBlockPosition().getX() == event.getToTransform().getLocation().getBlockPosition().getX() && 
            event.getFromTransform().getLocation().getBlockPosition().getZ() == event.getToTransform().getLocation().getBlockPosition().getZ())
    {
        return;
    }
    Location<World> loc = player.getLocation();
    
    // and then do some stuff
}

This only works if player moves vertically I think…Nevermind…

I would change DisplaceEntityEvent to DisplaceEntityEvent.TargetPlayer

and .getBlockPosition().getX() to .getBlockX() and so on

1 Like

Not a bad idea. I did so but the problem is still there.

After getting the location, I test wether the player enters (or leaves) a special zone, if so I send him a message. And here it’s as if the message is always “one block too late”.

Any idea ? :confused:

I’m not sure. I guess it would depend on how you’re storing the information for that zone.

Well it works when I remove the first if so I assume the problem does not come from the “is inside zone” testing.

Shouldn’t that be event.getToTransform()? Otherwise the location will be where the player was and not where their going.

2 Likes

Oh yeah you’re right ! Thanks !

That solved my problem… well partially but the other part is probably due to the rest of my code. I’ll search by myself :wink:

Another question that is a bit related. In a totally different context, is it possible to, how could I say, like reduce the frequency at which the DiplaceEntityEvent is called (only for this listener of course, not globally) ?

I did that with a Hashtable<UUID, Date> lastChecks but is there a short and easy “built-in” way ?

I’m not aware of a built in way, you’ll have to handle that yourself.

Ok, thanks anyway !