Merge pull request #150 from kezz/add-more-action-events

Add events for player sneaking and sprinting changes
This commit is contained in:
TheMode 2021-02-23 07:03:51 +01:00 committed by GitHub
commit ad4354ea0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 96 additions and 4 deletions

View File

@ -0,0 +1,15 @@
package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.PlayerEvent;
import org.jetbrains.annotations.NotNull;
/**
* Called when a player starts sneaking.
*/
public class PlayerStartSneakingEvent extends PlayerEvent {
public PlayerStartSneakingEvent(@NotNull Player player) {
super(player);
}
}

View File

@ -0,0 +1,15 @@
package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.PlayerEvent;
import org.jetbrains.annotations.NotNull;
/**
* Called when a player starts sprinting.
*/
public class PlayerStartSprintingEvent extends PlayerEvent {
public PlayerStartSprintingEvent(@NotNull Player player) {
super(player);
}
}

View File

@ -0,0 +1,15 @@
package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.PlayerEvent;
import org.jetbrains.annotations.NotNull;
/**
* Called when a player stops sneaking.
*/
public class PlayerStopSneakingEvent extends PlayerEvent {
public PlayerStopSneakingEvent(@NotNull Player player) {
super(player);
}
}

View File

@ -0,0 +1,15 @@
package net.minestom.server.event.player;
import net.minestom.server.entity.Player;
import net.minestom.server.event.PlayerEvent;
import org.jetbrains.annotations.NotNull;
/**
* Called when a player stops sprinting.
*/
public class PlayerStopSprintingEvent extends PlayerEvent {
public PlayerStopSprintingEvent(@NotNull Player player) {
super(player);
}
}

View File

@ -1,6 +1,10 @@
package net.minestom.server.listener;
import net.minestom.server.entity.Player;
import net.minestom.server.event.player.PlayerStartSneakingEvent;
import net.minestom.server.event.player.PlayerStartSprintingEvent;
import net.minestom.server.event.player.PlayerStopSneakingEvent;
import net.minestom.server.event.player.PlayerStopSprintingEvent;
import net.minestom.server.network.packet.client.play.ClientEntityActionPacket;
public class EntityActionListener {
@ -9,18 +13,46 @@ public class EntityActionListener {
ClientEntityActionPacket.Action action = packet.action;
switch (action) {
case START_SNEAKING:
player.setSneaking(true);
EntityActionListener.setSneaking(player, true);
break;
case STOP_SNEAKING:
player.setSneaking(false);
EntityActionListener.setSneaking(player, false);
break;
case START_SPRINTING:
player.setSprinting(true);
EntityActionListener.setSprinting(player, true);
break;
case STOP_SPRINTING:
player.setSprinting(false);
EntityActionListener.setSprinting(player, false);
break;
// TODO do remaining actions
}
}
private static void setSneaking(Player player, boolean sneaking) {
boolean oldState = player.isSneaking();
player.setSneaking(sneaking);
if (oldState != sneaking) {
if (sneaking) {
player.callEvent(PlayerStartSneakingEvent.class, new PlayerStartSneakingEvent(player));
} else {
player.callEvent(PlayerStopSneakingEvent.class, new PlayerStopSneakingEvent(player));
}
}
}
private static void setSprinting(Player player, boolean sprinting) {
boolean oldState = player.isSprinting();
player.setSprinting(sprinting);
if (oldState != sprinting) {
if (sprinting) {
player.callEvent(PlayerStartSprintingEvent.class, new PlayerStartSprintingEvent(player));
} else {
player.callEvent(PlayerStopSprintingEvent.class, new PlayerStopSprintingEvent(player));
}
}
}
}