PaperMC / Paper

The most widely used, high performance Minecraft server that aims to fix gameplay and mechanics inconsistencies
https://papermc.io/
Other
9.99k stars 2.32k forks source link

Overriding player movement in PlayerMoveEvent causes a PlayerTeleportEvent #8144

Open Baylem opened 2 years ago

Baylem commented 2 years ago

Expected behavior

If I override player movement in a PlayerMoveEvent, it should not call a PlayerTeleportEvent. I believe this is a genuine oversight, while it is niche I do believe it needs to be addressed for consistency

@EventHandler
public void onPlayerMove(PlayerMoveEvent event) {
        // Why does this call PlayerTeleportEvent ?
        Location to = event.getFrom();
        to.setPitch(event.getTo().getPitch());
        to.setYaw(event.getTo().getYaw());
        event.setTo(to);
}

Observed/Actual behavior

When using the above logic to override the player movement, it calls PlayerTeleportEvent,

Steps/models to reproduce

public class Main extends JavaPlugin implements Listener {
    private static boolean shouldFreeze = false;

    @Override
    public void onEnable() {
        getServer().getPluginManager().registerEvents(this, this);

        getServer().getScheduler().runTaskTimerAsynchronously(this, () -> {
            shouldFreeze = !shouldFreeze;
        }, 20, 100);
    }

    @EventHandler(priority = EventPriority.MONITOR)
    public void onPlayerTeleport(PlayerTeleportEvent event) {
        getServer().broadcastMessage(event.getCause().toString());
    }

    @EventHandler
    public void onPlayerMove(PlayerMoveEvent event) {
        if(shouldFreeze) {
            // Why does this call a teleport event?
            Location to = event.getFrom();
            to.setPitch(event.getTo().getPitch());
            to.setYaw(event.getTo().getYaw());
            event.setTo(to);
        }
    }

}

Plugin and Datapack List

N/A

Paper version

[01:23:50 INFO]: This server is running Paper version git-Paper-61 (MC: 1.19) (Implementing API version 1.19-R0.1-SNAPSHOT) (Git: cd215af) You are running the latest version

Other

No response

Doc94 commented 2 years ago

Well normally if you try change the rotation of the player you need use Player#teleport then you call the event, the same in this case with change the destination of the PlayerMoveEvent, checking the upstream the logic its like this if the destination was changes then use the same Player#teleport for handle this change for apply all the logic related to this (not run if player is vehicle, dismount if is a passenger, wakeup, close inventory)....

i mean for handle this can be detect if the location only change the yar/pitch and if x,y,z are the same call the internal teleport for avoid call the event.. but not sure if is valid.

Doc94 commented 2 years ago

Thanks to @Owen1212055 i think can handle this now, xd

Created https://github.com/PaperMC/Paper/pull/8176 for this.