diff --git a/src/main/java/net/minestom/server/event/player/PlayerStartSneakingEvent.java b/src/main/java/net/minestom/server/event/player/PlayerStartSneakingEvent.java new file mode 100644 index 000000000..198ca1093 --- /dev/null +++ b/src/main/java/net/minestom/server/event/player/PlayerStartSneakingEvent.java @@ -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); + } +} diff --git a/src/main/java/net/minestom/server/event/player/PlayerStartSprintingEvent.java b/src/main/java/net/minestom/server/event/player/PlayerStartSprintingEvent.java new file mode 100644 index 000000000..e517a80e3 --- /dev/null +++ b/src/main/java/net/minestom/server/event/player/PlayerStartSprintingEvent.java @@ -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); + } +} diff --git a/src/main/java/net/minestom/server/event/player/PlayerStopSneakingEvent.java b/src/main/java/net/minestom/server/event/player/PlayerStopSneakingEvent.java new file mode 100644 index 000000000..9601bd7e6 --- /dev/null +++ b/src/main/java/net/minestom/server/event/player/PlayerStopSneakingEvent.java @@ -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); + } +} diff --git a/src/main/java/net/minestom/server/event/player/PlayerStopSprintingEvent.java b/src/main/java/net/minestom/server/event/player/PlayerStopSprintingEvent.java new file mode 100644 index 000000000..cd15590e8 --- /dev/null +++ b/src/main/java/net/minestom/server/event/player/PlayerStopSprintingEvent.java @@ -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); + } +} diff --git a/src/main/java/net/minestom/server/listener/EntityActionListener.java b/src/main/java/net/minestom/server/listener/EntityActionListener.java index c43cbb91d..e99cffc75 100644 --- a/src/main/java/net/minestom/server/listener/EntityActionListener.java +++ b/src/main/java/net/minestom/server/listener/EntityActionListener.java @@ -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)); + } + } + } }