1
0
Fork 0
mirror of https://github.com/PaperMC/Paper.git synced 2024-12-22 22:45:31 +01:00
PaperMC/patches/api/0069-Add-PlayerJumpEvent.patch

118 lines
3.6 KiB
Diff
Raw Normal View History

2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Zach Brown <zach.brown@destroystokyo.com>
Date: Thu, 28 Sep 2017 17:21:32 -0400
Subject: [PATCH] Add PlayerJumpEvent
diff --git a/src/main/java/com/destroystokyo/paper/event/player/PlayerJumpEvent.java b/src/main/java/com/destroystokyo/paper/event/player/PlayerJumpEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..1d07c3d6bf3b9283371ca45698178979113085fa
2021-06-11 14:02:28 +02:00
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/player/PlayerJumpEvent.java
@@ -0,0 +1,105 @@
2021-06-11 14:02:28 +02:00
+package com.destroystokyo.paper.event.player;
+
+import com.google.common.base.Preconditions;
+import org.bukkit.Location;
+import org.bukkit.entity.Player;
+import org.bukkit.event.Cancellable;
+import org.bukkit.event.HandlerList;
+import org.bukkit.event.player.PlayerEvent;
2024-02-01 10:15:57 +01:00
+import org.bukkit.event.player.PlayerMoveEvent;
+import org.jetbrains.annotations.ApiStatus;
+import org.jspecify.annotations.NullMarked;
2021-06-11 14:02:28 +02:00
+
+/**
+ * Called when the server detects the player is jumping.
+ * <p>
+ * Added to avoid the overhead and special case logic that many plugins use
2024-02-01 10:15:57 +01:00
+ * when checking for jumps via {@link PlayerMoveEvent}, this event is fired whenever
2021-06-11 14:02:28 +02:00
+ * the server detects that the player is jumping.
+ */
+@NullMarked
2021-06-11 14:02:28 +02:00
+public class PlayerJumpEvent extends PlayerEvent implements Cancellable {
2024-02-01 10:15:57 +01:00
+
+ private static final HandlerList HANDLER_LIST = new HandlerList();
+
+ private final Location to;
+ private Location from;
2021-06-11 14:02:28 +02:00
+
2024-02-01 10:15:57 +01:00
+ private boolean cancelled;
+
+ @ApiStatus.Internal
+ public PlayerJumpEvent(final Player player, final Location from, final Location to) {
2021-06-11 14:02:28 +02:00
+ super(player);
+ this.from = from;
+ this.to = to;
+ }
+
+ /**
2024-02-01 10:15:57 +01:00
+ * {@inheritDoc}
2021-06-11 14:02:28 +02:00
+ * <p>
+ * If a jump event is cancelled, the player will be moved or
2024-02-01 10:15:57 +01:00
+ * teleported back to the Location as defined by {@link #getFrom()}. This will not
2021-06-11 14:02:28 +02:00
+ * fire an event
+ *
2024-02-01 10:15:57 +01:00
+ * @return {@code true} if this event is cancelled
2021-06-11 14:02:28 +02:00
+ */
+ @Override
2021-06-11 14:02:28 +02:00
+ public boolean isCancelled() {
2024-02-01 10:15:57 +01:00
+ return this.cancelled;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
2024-02-01 10:15:57 +01:00
+ * {@inheritDoc}
2021-06-11 14:02:28 +02:00
+ * <p>
+ * If a jump event is cancelled, the player will be moved or
2024-02-01 10:15:57 +01:00
+ * teleported back to the Location as defined by {@link #getFrom()}. This will not
2021-06-11 14:02:28 +02:00
+ * fire an event
+ *
2024-02-01 10:15:57 +01:00
+ * @param cancel {@code true} if you wish to cancel this event
2021-06-11 14:02:28 +02:00
+ */
+ @Override
+ public void setCancelled(final boolean cancel) {
2024-02-01 10:15:57 +01:00
+ this.cancelled = cancel;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * Gets the location this player jumped from
+ *
+ * @return Location the player jumped from
+ */
+ public Location getFrom() {
2024-02-01 10:15:57 +01:00
+ return this.from;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * Sets the location to mark as where the player jumped from
+ *
+ * @param from New location to mark as the players previous location
+ */
+ public void setFrom(final Location from) {
2024-02-01 10:15:57 +01:00
+ Preconditions.checkArgument(from != null, "Cannot use null from location!");
+ Preconditions.checkArgument(from.getWorld() != null, "Cannot use from location with null world!");
2021-06-11 14:02:28 +02:00
+ this.from = from;
+ }
+
+ /**
+ * Gets the location this player jumped to
2024-02-01 10:15:57 +01:00
+ * <p>
2021-06-11 14:02:28 +02:00
+ * This information is based on what the client sends, it typically
+ * has little relation to the arc of the jump at any given point.
+ *
+ * @return Location the player jumped to
+ */
+ public Location getTo() {
2024-03-20 22:33:34 +01:00
+ return this.to.clone();
2021-06-11 14:02:28 +02:00
+ }
+
+ @Override
+ public HandlerList getHandlers() {
2024-02-01 10:15:57 +01:00
+ return HANDLER_LIST;
2021-06-11 14:02:28 +02:00
+ }
+
+ public static HandlerList getHandlerList() {
2024-02-01 10:15:57 +01:00
+ return HANDLER_LIST;
2021-06-11 14:02:28 +02:00
+ }
+}