Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PlayerLandEvent #10859

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions patches/api/0484-Add-PlayerLandEvent.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: StillLutto <[email protected]>
Date: Sat, 8 Jun 2024 19:44:50 +0200
Subject: [PATCH] Add PlayerLandEvent


diff --git a/src/main/java/io/papermc/paper/event/player/PlayerLandEvent.java b/src/main/java/io/papermc/paper/event/player/PlayerLandEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..0a26286eb9d807ee202df60923166525a5f3af62
--- /dev/null
+++ b/src/main/java/io/papermc/paper/event/player/PlayerLandEvent.java
@@ -0,0 +1,87 @@
+package io.papermc.paper.event.player;
+
+import com.google.common.base.Preconditions;
+import org.bukkit.Location;
+import org.bukkit.block.Block;
+import org.bukkit.entity.Player;
+import org.bukkit.event.HandlerList;
+import org.bukkit.event.player.PlayerEvent;
+import org.bukkit.event.player.PlayerMoveEvent;
+import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Called when the server detects the player lands on the ground.
+ * <p>
+ * Added to avoid the overhead and special case logic that many plugins use
+ * when checking for player landing via {@link PlayerMoveEvent}, this event is fired whenever
+ * the server detects that the player lands on the ground.
+ */
+public class PlayerLandEvent extends PlayerEvent {
+
+ private static final HandlerList HANDLER_LIST = new HandlerList();
+
+ @NotNull private final Location to;
+ @NotNull private Location from;
+ @NotNull private Block mainSupportingBlock;
+
+ @ApiStatus.Internal
+ public PlayerLandEvent(@NotNull final Player player, @NotNull final Location from, @NotNull final Location to, final Block mainSupportingBlock) {
+ super(player);
+ this.from = from;
+ this.to = to;
+ this.mainSupportingBlock = mainSupportingBlock;
+ }
+
+ /**
+ * Gets the location where the player lands from
+ *
+ * @return Location where the player lands from
+ */
+ @NotNull
+ public Location getFrom() {
+ return this.from;
+ }
+
+ /**
+ * Sets the location to mark as where the player lands from
+ *
+ * @param from New location to mark as the players previous location
+ */
+ public void setFrom(@NotNull Location from) {
+ Preconditions.checkArgument(from != null, "Cannot use null from location!");
+ Preconditions.checkArgument(from.getWorld() != null, "Cannot use from location with null world!");
+ this.from = from;
+ }
+
+ /**
+ * Gets the location where the player lands
+ *
+ * @return Location where the player lands
+ */
+ @NotNull
+ public Location getTo() {
+ return this.to.clone();
+ }
+
+ /**
+ * Gets the location this player jumped to
+ *
+ * @return Location the player jumped to
+ */
+ @NotNull
+ public Block getMainSupportingBlock() {
+ return this.mainSupportingBlock;
+ }
+
+ @NotNull
+ @Override
+ public HandlerList getHandlers() {
+ return HANDLER_LIST;
+ }
+
+ @NotNull
+ public static HandlerList getHandlerList() {
+ return HANDLER_LIST;
+ }
+}
34 changes: 34 additions & 0 deletions patches/server/1053-Add-PlayerLandEvent.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: StillLutto <[email protected]>
Date: Sat, 8 Jun 2024 19:44:50 +0200
Subject: [PATCH] Add PlayerLandEvent


diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
index 4ae88bfcead40cd05f9514a48a922a37767cb3cf..8e6d05933857764ef582a31088f7a97eed8c4cc9 100644
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
@@ -193,6 +193,7 @@ import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.shapes.BooleanOp;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
+import org.bukkit.craftbukkit.block.CraftBlock;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use fully qualified imports when using a class not very often.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair, will change.

import org.slf4j.Logger;

// CraftBukkit start
@@ -1591,6 +1592,15 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
this.player.absMoveTo(d0, d1, d2, f, f1);
boolean flag4 = this.player.isAutoSpinAttack();

+ // Paper start - Add PlayerLandEvent
+ if (flag2 && !(this.player.getY() - d4 >= 0.0)) {
+ this.player.setOnGround(true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is up with setting the player's ground value here? As this is technically changing behavior?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I set the ground value here is because that method runs checkSupportingBlock() which updates Entity#mainSupportingBlockPos. The reason I'm not just taking advantage of this.player.setOnGroundWithKnownMovement() which happens just a few lines below is because I previously made this event cancellable, But after further testing, it had pretty buggy behaviour... I'm going to quickly just move this below the this.player.setOnGroundWithKnownMovement() in the next commit.

+ final org.bukkit.block.Block mainSupportingBlock = CraftBlock.at(this.player.level(), this.player.mainSupportingBlockPos.get());
+ io.papermc.paper.event.player.PlayerLandEvent event = new io.papermc.paper.event.player.PlayerLandEvent(player, from, to, mainSupportingBlock);
+ event.callEvent();
+ }
+ // Paper end - Add PlayerLandEvent
+
this.clientIsFloating = d11 >= -0.03125D && !flag2 && this.player.gameMode.getGameModeForPlayer() != GameType.SPECTATOR && !this.server.isFlightAllowed() && !this.player.getAbilities().mayfly && !this.player.hasEffect(MobEffects.LEVITATION) && !flag && !flag4 && this.noBlocksAround(this.player);
this.player.serverLevel().getChunkSource().move(this.player);
this.player.doCheckFallDamage(this.player.getX() - d3, this.player.getY() - d4, this.player.getZ() - d5, packet.isOnGround());