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 clicked position to PlayerUseUnknownEntityEvent #9604

Merged
merged 5 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
58 changes: 49 additions & 9 deletions patches/api/0036-Add-PlayerUseUnknownEntityEvent.patch
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,96 @@ From: Jedediah Smith <[email protected]>
Date: Sat, 2 Apr 2016 05:08:36 -0400
Subject: [PATCH] Add PlayerUseUnknownEntityEvent

Adds the PlayerUseUnknownEntityEvent to be used by plugins dealing with
virtual entities/entities that are not actually known to the server.

Co-authored-by: Nassim Jahnke <[email protected]>

diff --git a/src/main/java/com/destroystokyo/paper/event/player/PlayerUseUnknownEntityEvent.java b/src/main/java/com/destroystokyo/paper/event/player/PlayerUseUnknownEntityEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..09cfdf48ead8f03f3497646537292174241b0868
index 0000000000000000000000000000000000000000..16291c34a87fb0c610a8b2fef10a1899693cb3b8
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/player/PlayerUseUnknownEntityEvent.java
@@ -0,0 +1,46 @@
@@ -0,0 +1,82 @@
+package com.destroystokyo.paper.event.player;
+
+import org.bukkit.entity.Player;
+import org.bukkit.event.HandlerList;
+import org.bukkit.event.player.PlayerEvent;
+import org.bukkit.inventory.EquipmentSlot;
+import org.bukkit.util.Vector;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Represents an event that is called when a player right-clicks an unknown entity.
+ * Useful for plugins dealing with virtual entities (entities that don't actually spawned on the server).
+ * <br>
+ * This event may be called multiple times per interaction with different interaction hands
+ * and with or without the clicked position.
+ */
+public class PlayerUseUnknownEntityEvent extends PlayerEvent {
+
+ private static final HandlerList handlers = new HandlerList();
+ private static final HandlerList HANDLERS = new HandlerList();
+ private final int entityId;
+ private final boolean attack;
+ @NotNull private final EquipmentSlot hand;
+ private final @NotNull EquipmentSlot hand;
+ private final @Nullable Vector clickedPosition;
+
+ public PlayerUseUnknownEntityEvent(@NotNull Player who, int entityId, boolean attack, @NotNull EquipmentSlot hand) {
+ public PlayerUseUnknownEntityEvent(@NotNull Player who, int entityId, boolean attack, @NotNull EquipmentSlot hand, @Nullable Vector clickedPosition) {
+ super(who);
+ this.entityId = entityId;
+ this.attack = attack;
+ this.hand = hand;
+ this.clickedPosition = clickedPosition;
+ }
+
+ /**
+ * Returns the entity id of the unknown entity that was interacted with.
+ *
+ * @return the entity id of the entity that was interacted with
+ */
+ public int getEntityId() {
+ return this.entityId;
+ }
+
+ /**
+ * Returns whether the interaction was an attack.
+ *
+ * @return true if the player is attacking the entity, false if the player is interacting with the entity
+ */
+ public boolean isAttack() {
+ return this.attack;
+ }
+
+ @NotNull
+ public EquipmentSlot getHand() {
+ /**
+ * Returns the hand used to perform this interaction.
+ *
+ * @return the hand used to interact
+ */
+ public @NotNull EquipmentSlot getHand() {
+ return this.hand;
+ }
+
+ /**
+ * Returns the position relative to the entity that was clicked, or null if not available.
+ * See {@link org.bukkit.event.player.PlayerInteractAtEntityEvent} for more details.
+ *
+ * @return the position relative to the entity that was clicked, or null if not available
+ * @see org.bukkit.event.player.PlayerInteractAtEntityEvent
+ */
+ public @Nullable Vector getClickedRelativePosition() {
+ return clickedPosition.clone();
+ }
+
+ @NotNull
+ @Override
+ public HandlerList getHandlers() {
+ return handlers;
+ return HANDLERS;
+ }
+
+ @NotNull
+ public static HandlerList getHandlerList() {
+ return handlers;
+ return HANDLERS;
+ }
+}
16 changes: 14 additions & 2 deletions patches/server/0009-MC-Utils.patch
Original file line number Diff line number Diff line change
Expand Up @@ -3480,24 +3480,28 @@ index 0000000000000000000000000000000000000000..cea9c098ade00ee87b8efc8164ab72f5
+}
diff --git a/src/main/java/io/papermc/paper/util/MCUtil.java b/src/main/java/io/papermc/paper/util/MCUtil.java
new file mode 100644
index 0000000000000000000000000000000000000000..6779a0cc401231c53545dd22827b404be80b2ad0
index 0000000000000000000000000000000000000000..9572294a50110f2452090da1f32e0a73edc3db05
--- /dev/null
+++ b/src/main/java/io/papermc/paper/util/MCUtil.java
@@ -0,0 +1,522 @@
@@ -0,0 +1,534 @@
+package io.papermc.paper.util;
+
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
+import io.papermc.paper.math.BlockPosition;
+import io.papermc.paper.math.FinePosition;
+import io.papermc.paper.math.Position;
+import it.unimi.dsi.fastutil.objects.ObjectRBTreeSet;
+import java.lang.ref.Cleaner;
+import net.minecraft.core.BlockPos;
+import net.minecraft.core.Direction;
+import net.minecraft.core.Vec3i;
+import net.minecraft.server.MinecraftServer;
+import net.minecraft.server.level.ServerLevel;
+import net.minecraft.world.entity.Entity;
+import net.minecraft.world.level.ChunkPos;
+import net.minecraft.world.level.ClipContext;
+import net.minecraft.world.level.Level;
+import net.minecraft.world.phys.Vec3;
+import org.apache.commons.lang.exception.ExceptionUtils;
+import org.bukkit.Location;
+import org.bukkit.block.BlockFace;
Expand Down Expand Up @@ -3960,6 +3964,14 @@ index 0000000000000000000000000000000000000000..6779a0cc401231c53545dd22827b404b
+ return new BlockPos(vec.getBlockX(), vec.getBlockY(), vec.getBlockZ());
+ }
+
+ public static FinePosition toPosition(Vec3 vector) {
+ return Position.fine(vector.x, vector.y, vector.z);
+ }
+
+ public static BlockPosition toPosition(Vec3i vector) {
+ return Position.block(vector.getX(), vector.getY(), vector.getZ());
+ }
+
+ public static boolean isEdgeOfChunk(BlockPos pos) {
+ final int modX = pos.getX() & 15;
+ final int modZ = pos.getZ() & 15;
Expand Down
19 changes: 10 additions & 9 deletions patches/server/0019-Rewrite-chunk-system.patch
Original file line number Diff line number Diff line change
Expand Up @@ -15847,12 +15847,12 @@ index cea9c098ade00ee87b8efc8164ab72f5279758f0..197224e31175252d8438a8df585bbb65
+ }
}
diff --git a/src/main/java/io/papermc/paper/util/MCUtil.java b/src/main/java/io/papermc/paper/util/MCUtil.java
index 6779a0cc401231c53545dd22827b404be80b2ad0..750ed5844b16dcee6c4dda7a7422777ed1bd3f5c 100644
index 7afb13b7457755fac3aed4b0260413a280ed29e6..5e17a83df2f3606aff375fe054266d03f9a0b3b6 100644
--- a/src/main/java/io/papermc/paper/util/MCUtil.java
+++ b/src/main/java/io/papermc/paper/util/MCUtil.java
@@ -2,16 +2,29 @@ package io.papermc.paper.util;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
@@ -4,17 +4,30 @@ import com.google.common.util.concurrent.ThreadFactoryBuilder;
import io.papermc.paper.math.BlockPosition;
import io.papermc.paper.math.FinePosition;
import io.papermc.paper.math.Position;
+import com.google.gson.JsonArray;
+import com.google.gson.JsonObject;
Expand All @@ -15864,6 +15864,7 @@ index 6779a0cc401231c53545dd22827b404be80b2ad0..750ed5844b16dcee6c4dda7a7422777e
+import it.unimi.dsi.fastutil.objects.ReferenceArrayList;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.Vec3i;
import net.minecraft.server.MinecraftServer;
+import net.minecraft.server.level.ChunkHolder;
+import net.minecraft.server.level.ChunkMap;
Expand All @@ -15877,10 +15878,10 @@ index 6779a0cc401231c53545dd22827b404be80b2ad0..750ed5844b16dcee6c4dda7a7422777e
import net.minecraft.world.level.Level;
+import net.minecraft.world.level.chunk.ChunkAccess;
+import net.minecraft.world.level.chunk.ChunkStatus;
import net.minecraft.world.phys.Vec3;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.bukkit.Location;
import org.bukkit.block.BlockFace;
@@ -22,8 +35,11 @@ import org.spigotmc.AsyncCatcher;
@@ -26,8 +39,11 @@ import org.spigotmc.AsyncCatcher;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand All @@ -15892,7 +15893,7 @@ index 6779a0cc401231c53545dd22827b404be80b2ad0..750ed5844b16dcee6c4dda7a7422777e
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.LinkedBlockingQueue;
@@ -516,6 +532,100 @@ public final class MCUtil {
@@ -528,6 +544,100 @@ public final class MCUtil {
}
}

Expand Down Expand Up @@ -20638,7 +20639,7 @@ index ca788f0dcec4a117b410fe8348969e056b138b1e..a6ac76707da39cf86113003b1f326433
public boolean remove(Object object) {
int i = this.findIndex((T)object);
diff --git a/src/main/java/net/minecraft/util/worldupdate/WorldUpgrader.java b/src/main/java/net/minecraft/util/worldupdate/WorldUpgrader.java
index 12e72ad737b1219fcdf88d344d41621d9fd5feec..e0bfeebeaac1aaea64bc07cdfdf7790e3e43ca7b 100644
index 495b52bfab14478f8285cc5471335a41244c199e..e16ef1b7c0bfe6d6194c09f6787a50fd9b28f55e 100644
--- a/src/main/java/net/minecraft/util/worldupdate/WorldUpgrader.java
+++ b/src/main/java/net/minecraft/util/worldupdate/WorldUpgrader.java
@@ -186,7 +186,11 @@ public class WorldUpgrader {
Expand Down Expand Up @@ -22796,7 +22797,7 @@ index d01388bbadf3069357cf52463f4104a1be4d2b56..b3dfa35bc41191883814c78693a0d958
// Spigot start
private final org.bukkit.World.Spigot spigot = new org.bukkit.World.Spigot()
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
index cf87f197d860567746dcae2a15946fed0b1a1d85..3b1dbfe6f03e62ac9e12066d41516de157f99719 100644
index f76db40188007b6ab475d259b4cbe0c7ef804677..49ca3592012cca981b96434c9807440672a8c165 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
@@ -195,6 +195,48 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
Expand Down
57 changes: 32 additions & 25 deletions patches/server/0083-Add-PlayerUseUnknownEntityEvent.patch
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,70 @@ From: Jedediah Smith <[email protected]>
Date: Sat, 2 Apr 2016 05:09:16 -0400
Subject: [PATCH] Add PlayerUseUnknownEntityEvent

== AT ==
public net.minecraft.network.protocol.game.ServerboundInteractPacket$ActionType
Adds the PlayerUseUnknownEntityEvent to be used by plugins dealing with
virtual entities/entities that are not actually known to the server.

Co-authored-by: Nassim Jahnke <[email protected]>

diff --git a/src/main/java/net/minecraft/network/protocol/game/ServerboundInteractPacket.java b/src/main/java/net/minecraft/network/protocol/game/ServerboundInteractPacket.java
index a5d57cc862036012d83b090bb1b3ccf4115a88b3..21068f766b75c414d5818073b7dca083d8ff4409 100644
index 644a0fdea6576647539b96528717dbaeab498d93..221e64a66ff12a8de5c75992fc26a54a03b317e7 100644
--- a/src/main/java/net/minecraft/network/protocol/game/ServerboundInteractPacket.java
+++ b/src/main/java/net/minecraft/network/protocol/game/ServerboundInteractPacket.java
@@ -10,8 +10,8 @@ import net.minecraft.world.entity.Entity;
import net.minecraft.world.phys.Vec3;

public class ServerboundInteractPacket implements Packet<ServerGamePacketListener> {
- private final int entityId;
- private final ServerboundInteractPacket.Action action;
+ private final int entityId; public final int getEntityId() { return this.entityId; } // Paper - add accessor
+ private final ServerboundInteractPacket.Action action; public final ServerboundInteractPacket.ActionType getActionType() { return this.action.getType(); } // Paper - add accessor
private final boolean usingSecondaryAction;
static final ServerboundInteractPacket.Action ATTACK_ACTION = new ServerboundInteractPacket.Action() {
@Override
@@ -169,4 +169,14 @@ public class ServerboundInteractPacket implements Packet<ServerGamePacketListene
buf.writeEnum(this.hand);
}
}
+
+ // Paper start - PlayerUseUnknownEntityEvent
+ public int getEntityId() {
+ return this.entityId;
+ }
+
+ public boolean isAttack() {
+ return this.action.getType() == ActionType.ATTACK;
+ }
+ // Paper end - PlayerUseUnknownEntityEvent
}
diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
index cad7df80d4914b533ce9db1a10b5923b5ffe578b..ae66a8a222bc27986bab0f7896e0eacfd6044b31 100644
index cad7df80d4914b533ce9db1a10b5923b5ffe578b..39f2db938edad3db6fa83d9f8431a28176d4011a 100644
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
@@ -2543,8 +2543,37 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Tic
@@ -2543,8 +2543,38 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Tic
});
}
}
+ // Paper start - fire event
+ // Paper start - PlayerUseUnknownEntityEvent
+ else {
+ packet.dispatch(new net.minecraft.network.protocol.game.ServerboundInteractPacket.Handler() {
+ @Override
+ public void onInteraction(net.minecraft.world.InteractionHand hand) {
+ ServerGamePacketListenerImpl.this.callPlayerUseUnknownEntityEvent(packet, hand);
+ ServerGamePacketListenerImpl.this.callPlayerUseUnknownEntityEvent(packet, hand, null);
+ }
+
+ @Override
+ public void onInteraction(net.minecraft.world.InteractionHand hand, net.minecraft.world.phys.Vec3 pos) {
+ ServerGamePacketListenerImpl.this.callPlayerUseUnknownEntityEvent(packet, hand);
+ ServerGamePacketListenerImpl.this.callPlayerUseUnknownEntityEvent(packet, hand, pos);
+ }
+
+ @Override
+ public void onAttack() {
+ ServerGamePacketListenerImpl.this.callPlayerUseUnknownEntityEvent(packet, net.minecraft.world.InteractionHand.MAIN_HAND);
+ ServerGamePacketListenerImpl.this.callPlayerUseUnknownEntityEvent(packet, net.minecraft.world.InteractionHand.MAIN_HAND, null);
+ }
+ });
+ }
+
+ }

+ private void callPlayerUseUnknownEntityEvent(ServerboundInteractPacket packet, InteractionHand hand) {
+ private void callPlayerUseUnknownEntityEvent(ServerboundInteractPacket packet, InteractionHand hand, @Nullable net.minecraft.world.phys.Vec3 vector) {
+ this.cserver.getPluginManager().callEvent(new com.destroystokyo.paper.event.player.PlayerUseUnknownEntityEvent(
+ this.getCraftPlayer(),
+ packet.getEntityId(),
+ packet.getActionType() == ServerboundInteractPacket.ActionType.ATTACK,
+ hand == InteractionHand.MAIN_HAND ? EquipmentSlot.HAND : EquipmentSlot.OFF_HAND
+ ));
+ packet.isAttack(),
+ hand == InteractionHand.MAIN_HAND ? EquipmentSlot.HAND : EquipmentSlot.OFF_HAND,
+ vector != null ? new org.bukkit.util.Vector(vector.x, vector.y, vector.z) : null)
+ );
}
+ // Paper end
+ // Paper end - PlayerUseUnknownEntityEvent

@Override
public void handleClientCommand(ServerboundClientCommandPacket packet) {
4 changes: 2 additions & 2 deletions patches/server/0121-Properly-fix-item-duplication-bug.patch
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ index 9e182c4cdf54c9ca7701660df72052d5c8936a55..5c855a15f7847ca37c263755f643eeb1

@Override
diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
index 631eaf856daa09151e62e192296ba143213d6f29..12ce6ef8163fa46c1f5bc1f88011cd862b8bad89 100644
index 512129382123d0c06eb308a98a9ee1b268e3b486..c7870a1a81f9c2d84066a39d2606c96f4e4df0bf 100644
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
@@ -3160,7 +3160,7 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Tic
@@ -3161,7 +3161,7 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Tic
}

public final boolean isDisconnected() {
Expand Down
12 changes: 6 additions & 6 deletions patches/server/0140-Basic-PlayerProfile-API.patch
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ index 0000000000000000000000000000000000000000..7ac27392a8647ef7d0dc78efe78703e9
+ @NotNull GameProfile buildGameProfile();
+}
diff --git a/src/main/java/io/papermc/paper/util/MCUtil.java b/src/main/java/io/papermc/paper/util/MCUtil.java
index 750ed5844b16dcee6c4dda7a7422777ed1bd3f5c..39b19310eece90fa5398fbb160593fd4f966627a 100644
index 5e17a83df2f3606aff375fe054266d03f9a0b3b6..5448dce87bb5934d4192ce2eb0b99a7b8e1f1ecc 100644
--- a/src/main/java/io/papermc/paper/util/MCUtil.java
+++ b/src/main/java/io/papermc/paper/util/MCUtil.java
@@ -1,5 +1,7 @@
Expand All @@ -574,17 +574,17 @@ index 750ed5844b16dcee6c4dda7a7422777ed1bd3f5c..39b19310eece90fa5398fbb160593fd4
+import com.destroystokyo.paper.profile.CraftPlayerProfile;
+import com.destroystokyo.paper.profile.PlayerProfile;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import io.papermc.paper.math.Position;
import com.google.gson.JsonArray;
@@ -26,6 +28,7 @@ import net.minecraft.world.level.Level;
import net.minecraft.world.level.chunk.ChunkAccess;
import io.papermc.paper.math.BlockPosition;
import io.papermc.paper.math.FinePosition;
@@ -30,6 +32,7 @@ import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraft.world.level.chunk.ChunkStatus;
import net.minecraft.world.phys.Vec3;
import org.apache.commons.lang.exception.ExceptionUtils;
+import com.mojang.authlib.GameProfile;
import org.bukkit.Location;
import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.CraftWorld;
@@ -374,6 +377,10 @@ public final class MCUtil {
@@ -378,6 +381,10 @@ public final class MCUtil {
return run.get();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ also adding some additional logging in order to help work out what is causing
random disconnections for clients.

diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
index 79769e770e04f0852fabed7dc7367e87bd5be310..924f520702723b3f44ac990f6222102119d16e9c 100644
index 1f52d3cdb63319b0fe7aa29c34f502f4368646c2..9fdde607ee51da69371cce114c15074beb2d611a 100644
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
@@ -3119,14 +3119,18 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Tic
@@ -3120,14 +3120,18 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Tic

@Override
public void handleKeepAlive(ServerboundKeepAlivePacket packet) {
Expand Down
Loading