Search in sources :

Example 6 with PlayerMoveEvent

use of org.bukkit.event.player.PlayerMoveEvent in project AuthMeReloaded by AuthMe.

the class PlayerListenerTest method shouldAllowMovementWithinRadius.

@Test
public void shouldAllowMovementWithinRadius() {
    // given
    given(settings.getProperty(RestrictionSettings.ALLOW_UNAUTHED_MOVEMENT)).willReturn(true);
    given(settings.getProperty(RestrictionSettings.ALLOWED_MOVEMENT_RADIUS)).willReturn(12);
    World world = mock(World.class);
    Player player = mock(Player.class);
    given(player.getWorld()).willReturn(world);
    Location from = new Location(world, 200, 70, 200);
    Location to = new Location(world, 199, 69, 201);
    PlayerMoveEvent event = spy(new PlayerMoveEvent(player, from, to));
    given(player.getLocation()).willReturn(from);
    given(listenerService.shouldCancelEvent(player)).willReturn(true);
    given(settings.getProperty(RestrictionSettings.NO_TELEPORT)).willReturn(false);
    // sqrt(10^2 + 2^2 + 4^2) = 11 < 12 (allowed movement radius)
    Location spawn = new Location(world, 190, 72, 204);
    given(spawnLoader.getSpawnLocation(player)).willReturn(spawn);
    // when
    listener.onPlayerMove(event);
    // then
    verify(listenerService).shouldCancelEvent(player);
    verify(player, never()).teleport(any(Location.class));
    verify(spawnLoader).getSpawnLocation(player);
    verifyNoModifyingCalls(event);
}
Also used : Player(org.bukkit.entity.Player) PlayerMoveEvent(org.bukkit.event.player.PlayerMoveEvent) World(org.bukkit.World) Location(org.bukkit.Location) Test(org.junit.Test)

Example 7 with PlayerMoveEvent

use of org.bukkit.event.player.PlayerMoveEvent in project AuthMeReloaded by AuthMe.

the class PlayerListenerTest method shouldAllowFalling.

@Test
public void shouldAllowFalling() {
    // given
    given(settings.getProperty(RestrictionSettings.ALLOW_UNAUTHED_MOVEMENT)).willReturn(false);
    Player player = mock(Player.class);
    Location from = new Location(null, 100, 90, 200);
    Location to = new Location(null, 100, 88, 200);
    PlayerMoveEvent event = spy(new PlayerMoveEvent(player, from, to));
    // when
    listener.onPlayerMove(event);
    // then
    verifyNoModifyingCalls(event);
}
Also used : Player(org.bukkit.entity.Player) PlayerMoveEvent(org.bukkit.event.player.PlayerMoveEvent) Location(org.bukkit.Location) Test(org.junit.Test)

Example 8 with PlayerMoveEvent

use of org.bukkit.event.player.PlayerMoveEvent in project AuthMeReloaded by AuthMe.

the class PlayerListenerTest method shouldCancelEventForDisabledUnauthedMovement.

@Test
public void shouldCancelEventForDisabledUnauthedMovement() {
    // given
    given(settings.getProperty(RestrictionSettings.ALLOW_UNAUTHED_MOVEMENT)).willReturn(false);
    Player player = mock(Player.class);
    World world = mock(World.class);
    Location from = new Location(world, 200, 70, 200);
    Location to = new Location(world, 199, 70, 199);
    PlayerMoveEvent event = spy(new PlayerMoveEvent(player, from, to));
    given(listenerService.shouldCancelEvent(player)).willReturn(true);
    // when
    listener.onPlayerMove(event);
    // then
    verify(listenerService).shouldCancelEvent(player);
    verify(event).setTo(from);
}
Also used : Player(org.bukkit.entity.Player) PlayerMoveEvent(org.bukkit.event.player.PlayerMoveEvent) World(org.bukkit.World) Location(org.bukkit.Location) Test(org.junit.Test)

Example 9 with PlayerMoveEvent

use of org.bukkit.event.player.PlayerMoveEvent in project AuthMeReloaded by AuthMe.

the class PlayerListenerTest method shouldTeleportPlayerInDifferentWorldToSpawn.

@Test
public void shouldTeleportPlayerInDifferentWorldToSpawn() {
    // given
    given(settings.getProperty(RestrictionSettings.ALLOW_UNAUTHED_MOVEMENT)).willReturn(true);
    given(settings.getProperty(RestrictionSettings.ALLOWED_MOVEMENT_RADIUS)).willReturn(20);
    World playerWorld = mock(World.class);
    Player player = mock(Player.class);
    given(player.getWorld()).willReturn(playerWorld);
    Location from = new Location(null, 200, 70, 200);
    Location to = new Location(null, 199, 70, 199);
    PlayerMoveEvent event = spy(new PlayerMoveEvent(player, from, to));
    given(listenerService.shouldCancelEvent(player)).willReturn(true);
    given(settings.getProperty(RestrictionSettings.NO_TELEPORT)).willReturn(false);
    World world = mock(World.class);
    Location spawn = new Location(world, 0, 90, 0);
    given(spawnLoader.getSpawnLocation(player)).willReturn(spawn);
    // when
    listener.onPlayerMove(event);
    // then
    verify(listenerService).shouldCancelEvent(player);
    verify(player).teleport(spawn);
    verify(spawnLoader).getSpawnLocation(player);
    verifyNoModifyingCalls(event);
}
Also used : Player(org.bukkit.entity.Player) PlayerMoveEvent(org.bukkit.event.player.PlayerMoveEvent) World(org.bukkit.World) Location(org.bukkit.Location) Test(org.junit.Test)

Example 10 with PlayerMoveEvent

use of org.bukkit.event.player.PlayerMoveEvent in project Glowstone by GlowstoneMC.

the class PlayerUpdateHandler method handle.

@Override
public void handle(GlowSession session, PlayerUpdateMessage message) {
    GlowPlayer player = session.getPlayer();
    Location oldLocation = player.getLocation();
    Location newLocation = oldLocation.clone();
    message.update(newLocation);
    // don't let players reach an illegal position
    if (Math.abs(newLocation.getBlockX()) > 32000000 || Math.abs(newLocation.getBlockZ()) > 32000000) {
        session.getPlayer().kickPlayer("Illegal position");
        return;
    }
    /*
          don't let players move more than 100 blocks in a single packet
          if they move greater than 10 blocks, but less than 100, just warn
          this is NOT robust hack prevention - only to prevent client
          confusion about where its actual location is (e.g. during login)
        */
    if (message.moved() && !player.isDead()) {
        if (player.teleportedTo != null) {
            if (newLocation.equals(player.teleportedTo)) {
                player.endTeleport();
                return;
            } else {
                // outdated location, so skip packet
                return;
            }
        } else {
            double distance = newLocation.distanceSquared(oldLocation);
            if (distance > 100 * 100) {
                session.getPlayer().kickPlayer("You moved too quickly :( (Hacking?)");
                return;
            } else if (distance > 100) {
                GlowServer.logger.warning(session.getPlayer().getName() + " moved too quickly!");
            }
        }
    }
    // call move event if movement actually occurred and there are handlers registered
    if (!oldLocation.equals(newLocation) && PlayerMoveEvent.getHandlerList().getRegisteredListeners().length > 0) {
        PlayerMoveEvent event = EventFactory.callEvent(new PlayerMoveEvent(player, oldLocation, newLocation));
        if (event.isCancelled()) {
            // tell client they're back where they started
            session.send(new PositionRotationMessage(oldLocation));
            return;
        }
        if (!event.getTo().equals(newLocation)) {
            // teleport to the set destination: fires PlayerTeleportEvent and
            // handles if the destination is in another world
            player.teleport(event.getTo(), TeleportCause.PLUGIN);
            return;
        }
        if (!Objects.equals(session.getPlayer().getLocation(), oldLocation)) {
            // plugin changed location on move event
            return;
        }
    }
    // move event was not fired or did nothing, simply update location
    player.setRawLocation(newLocation);
    // do stuff with onGround if we need to
    if (player.isOnGround() != message.isOnGround()) {
        if (message.isOnGround() && player.getVelocity().getY() > 0) {
            // jump
            if (player.isSprinting()) {
                player.addExhaustion(0.2f);
            } else {
                player.addExhaustion(0.05f);
            }
        }
        player.setOnGround(message.isOnGround());
    }
    // Checks if the player is still wearing the Elytra
    ItemStack chestplate = player.getInventory().getChestplate();
    boolean hasElytra = chestplate != null && chestplate.getType() == Material.ELYTRA && chestplate.getDurability() < chestplate.getType().getMaxDurability();
    if (player.isGliding() && (player.isOnGround() || !hasElytra)) {
        player.setGliding(false);
    }
    player.addMoveExhaustion(newLocation);
    // track movement stats
    Vector delta = newLocation.clone().subtract(oldLocation).toVector();
    delta.setX(Math.abs(delta.getX()));
    delta.setY(Math.abs(delta.getY()));
    delta.setZ(Math.abs(delta.getZ()));
    int flatDistance = (int) Math.round(Math.sqrt(NumberConversions.square(delta.getX()) + NumberConversions.square(delta.getZ())) * 100.0);
    if (message.isOnGround()) {
        if (flatDistance > 0) {
            if (player.isSprinting()) {
                player.incrementStatistic(Statistic.SPRINT_ONE_CM, flatDistance);
            } else if (player.isSneaking()) {
                player.incrementStatistic(Statistic.CROUCH_ONE_CM, flatDistance);
            } else {
                player.incrementStatistic(Statistic.WALK_ONE_CM, flatDistance);
            }
        }
    } else if (player.isFlying()) {
        if (flatDistance > 0) {
            player.incrementStatistic(Statistic.FLY_ONE_CM, flatDistance);
        }
    } else if (player.isInWater()) {
        if (flatDistance > 0) {
            player.incrementStatistic(Statistic.SWIM_ONE_CM, flatDistance);
        }
    }
}
Also used : PositionRotationMessage(net.glowstone.net.message.play.game.PositionRotationMessage) GlowPlayer(net.glowstone.entity.GlowPlayer) PlayerMoveEvent(org.bukkit.event.player.PlayerMoveEvent) ItemStack(org.bukkit.inventory.ItemStack) Vector(org.bukkit.util.Vector) Location(org.bukkit.Location)

Aggregations

PlayerMoveEvent (org.bukkit.event.player.PlayerMoveEvent)10 Location (org.bukkit.Location)8 Test (org.junit.Test)8 Player (org.bukkit.entity.Player)7 World (org.bukkit.World)4 GlowPlayer (net.glowstone.entity.GlowPlayer)1 PositionRotationMessage (net.glowstone.net.message.play.game.PositionRotationMessage)1 Event (org.bukkit.event.Event)1 EventHandler (org.bukkit.event.EventHandler)1 Listener (org.bukkit.event.Listener)1 BlockBreakEvent (org.bukkit.event.block.BlockBreakEvent)1 PlayerEvent (org.bukkit.event.player.PlayerEvent)1 PlayerInteractEvent (org.bukkit.event.player.PlayerInteractEvent)1 ItemStack (org.bukkit.inventory.ItemStack)1 Vector (org.bukkit.util.Vector)1