Java Code Examples for org.bukkit.event.player.PlayerMoveEvent#getEntityFrom()

The following examples show how to use org.bukkit.event.player.PlayerMoveEvent#getEntityFrom() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PlayerMovementListener.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
private final void handleMovementHigh(final PlayerMoveEvent event) {
    Player player = event.getPlayer();
    EntityLocation originalFrom = event.getEntityFrom();
    EntityLocation originalTo = event.getEntityTo();

    EntityLocation oldTo = this.lastToLocation.get(player);
    if(oldTo != null && !oldTo.equals(originalFrom)) {
        // If this movement does not start where the last known movement ended,
        // we have to make up the missing movement. We do that by (potentially) firing
        // two coarse events for this one event, a "fake" one for the missing movement
        // and a "real" one for the current movement.

        // First, modify this event to look like the missing event, and fire
        // a coarse event that wraps it.
        event.setFrom(oldTo);
        event.setTo(originalFrom);
        this.updateLastToLocation(event);
        if(this.callCoarsePlayerMove(event)) {
            // If the fake coarse event was cancelled, we don't need to fire
            // the real one, so just return. Note that the wrapped event won't
            // actually be cancelled, rather its to location will be modified
            // to return the player to the oldTo location. Also note that if
            // the original event was already cancelled before the coarse event
            // fired, then we will never get here, and both the fake and real
            // events will go through.
            this.updateLastToLocation(event);
            return;
        }

        // Restore the event to its real state
        event.setFrom(originalFrom);
        event.setTo(originalTo);
    }

    this.updateLastToLocation(event);
    if(this.callCoarsePlayerMove(event)) {
        this.updateLastToLocation(event);
    }
}
 
Example 2
Source File: PlayerMovementListener.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Fire a CoarsePlayerMoveEvent that wraps the given event, only if it crosses
 * a block boundary, or the PoseFlags change.
 * @param event         The movement event to potentially wrap
 * @return True if the original event was not cancelled, and a coarse event was fired,
 *         and that coarse event was cancelled. In this case, the wrapped event won't
 *         actually be cancelled, but callers should treat it like it is.
 */
private boolean callCoarsePlayerMove(final PlayerMoveEvent event) {
    // Don't fire coarse events for teleports that are not "in-game"
    // e.g. /jumpto command
    if(event instanceof PlayerTeleportEvent) {
        PlayerTeleportEvent teleportEvent = (PlayerTeleportEvent) event;
        if(teleportEvent.getCause() != TeleportCause.ENDER_PEARL &&
           teleportEvent.getCause() != TeleportCause.UNKNOWN) {
            return false;
        }
    }

    // If the movement does not cross a block boundary, and no PoseFlags changed, we don't care about it
    final EntityLocation from = event.getEntityFrom();
    final EntityLocation to = event.getEntityTo();
    if(from.position().coarseEquals(to.position()) && from.poseFlags().equals(to.poseFlags())) {
        return false;
    }

    // Remember whether the original event was already cancelled
    boolean wasCancelled = event.isCancelled();

    CoarsePlayerMoveEvent generalEvent = new CoarsePlayerMoveEvent(event, event.getPlayer(), from, to);
    this.eventBus.callEvent(generalEvent);

    if(!wasCancelled && generalEvent.isCancelled()) {
        // When a coarse event is cancelled, we have our own logic for resetting the
        // player's position, so we un-cancel the event and instead modify its
        // to location to put the player where we want them.
        resetPosition(event);
        return true;
    } else {
        return false;
    }
}