Add clicked position to PlayerUseUnknownEntityEvent (#9604)

This commit is contained in:
Nassim Jahnke 2023-08-16 17:58:07 +10:00
parent f5719c0b4a
commit 84993c1413
6 changed files with 99 additions and 39 deletions

View File

@ -3,6 +3,10 @@ From: Jedediah Smith <jedediah@silencegreys.com>
Date: Sat, 2 Apr 2016 05:08:36 -0400 Date: Sat, 2 Apr 2016 05:08:36 -0400
Subject: [PATCH] Add PlayerUseUnknownEntityEvent 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 <nassim@njahnke.dev>
diff --git a/src/main/java/com/destroystokyo/paper/event/player/PlayerUseUnknownEntityEvent.java b/src/main/java/com/destroystokyo/paper/event/player/PlayerUseUnknownEntityEvent.java 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 new file mode 100644
@ -16,43 +20,79 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+import org.bukkit.event.HandlerList; +import org.bukkit.event.HandlerList;
+import org.bukkit.event.player.PlayerEvent; +import org.bukkit.event.player.PlayerEvent;
+import org.bukkit.inventory.EquipmentSlot; +import org.bukkit.inventory.EquipmentSlot;
+import org.bukkit.util.Vector;
+import org.jetbrains.annotations.NotNull; +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 { +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 int entityId;
+ private final boolean attack; + 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); + super(who);
+ this.entityId = entityId; + this.entityId = entityId;
+ this.attack = attack; + this.attack = attack;
+ this.hand = hand; + 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() { + public int getEntityId() {
+ return this.entityId; + 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() { + public boolean isAttack() {
+ return this.attack; + 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; + 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 + @NotNull
+ @Override + @Override
+ public HandlerList getHandlers() { + public HandlerList getHandlers() {
+ return handlers; + return HANDLERS;
+ } + }
+ +
+ @NotNull + @NotNull
+ public static HandlerList getHandlerList() { + public static HandlerList getHandlerList() {
+ return handlers; + return HANDLERS;
+ } + }
+} +}

View File

@ -3,24 +3,30 @@ From: Jedediah Smith <jedediah@silencegreys.com>
Date: Sat, 2 Apr 2016 05:09:16 -0400 Date: Sat, 2 Apr 2016 05:09:16 -0400
Subject: [PATCH] Add PlayerUseUnknownEntityEvent Subject: [PATCH] Add PlayerUseUnknownEntityEvent
== AT == Adds the PlayerUseUnknownEntityEvent to be used by plugins dealing with
public net.minecraft.network.protocol.game.ServerboundInteractPacket$ActionType virtual entities/entities that are not actually known to the server.
Co-authored-by: Nassim Jahnke <nassim@njahnke.dev>
diff --git a/src/main/java/net/minecraft/network/protocol/game/ServerboundInteractPacket.java b/src/main/java/net/minecraft/network/protocol/game/ServerboundInteractPacket.java 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 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/network/protocol/game/ServerboundInteractPacket.java --- a/src/main/java/net/minecraft/network/protocol/game/ServerboundInteractPacket.java
+++ b/src/main/java/net/minecraft/network/protocol/game/ServerboundInteractPacket.java +++ b/src/main/java/net/minecraft/network/protocol/game/ServerboundInteractPacket.java
@@ -0,0 +0,0 @@ import net.minecraft.world.entity.Entity; @@ -0,0 +0,0 @@ public class ServerboundInteractPacket implements Packet<ServerGamePacketListene
import net.minecraft.world.phys.Vec3; buf.writeEnum(this.hand);
}
public class ServerboundInteractPacket implements Packet<ServerGamePacketListener> { }
- private final int entityId; +
- private final ServerboundInteractPacket.Action action; + // Paper start - PlayerUseUnknownEntityEvent
+ private final int entityId; public final int getEntityId() { return this.entityId; } // Paper - add accessor + public int getEntityId() {
+ private final ServerboundInteractPacket.Action action; public final ServerboundInteractPacket.ActionType getActionType() { return this.action.getType(); } // Paper - add accessor + return this.entityId;
private final boolean usingSecondaryAction; + }
static final ServerboundInteractPacket.Action ATTACK_ACTION = new ServerboundInteractPacket.Action() { +
@Override + 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 diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java --- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
@ -29,37 +35,38 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
}); });
} }
} }
+ // Paper start - fire event + // Paper start - PlayerUseUnknownEntityEvent
+ else { + else {
+ packet.dispatch(new net.minecraft.network.protocol.game.ServerboundInteractPacket.Handler() { + packet.dispatch(new net.minecraft.network.protocol.game.ServerboundInteractPacket.Handler() {
+ @Override + @Override
+ public void onInteraction(net.minecraft.world.InteractionHand hand) { + public void onInteraction(net.minecraft.world.InteractionHand hand) {
+ ServerGamePacketListenerImpl.this.callPlayerUseUnknownEntityEvent(packet, hand); + ServerGamePacketListenerImpl.this.callPlayerUseUnknownEntityEvent(packet, hand, null);
+ } + }
+ +
+ @Override + @Override
+ public void onInteraction(net.minecraft.world.InteractionHand hand, net.minecraft.world.phys.Vec3 pos) { + 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 + @Override
+ public void onAttack() { + 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.cserver.getPluginManager().callEvent(new com.destroystokyo.paper.event.player.PlayerUseUnknownEntityEvent(
+ this.getCraftPlayer(), + this.getCraftPlayer(),
+ packet.getEntityId(), + packet.getEntityId(),
+ packet.getActionType() == ServerboundInteractPacket.ActionType.ATTACK, + packet.isAttack(),
+ hand == InteractionHand.MAIN_HAND ? EquipmentSlot.HAND : EquipmentSlot.OFF_HAND + 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 @Override
public void handleClientCommand(ServerboundClientCommandPacket packet) { public void handleClientCommand(ServerboundClientCommandPacket packet) {

View File

@ -574,11 +574,11 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+import com.destroystokyo.paper.profile.CraftPlayerProfile; +import com.destroystokyo.paper.profile.CraftPlayerProfile;
+import com.destroystokyo.paper.profile.PlayerProfile; +import com.destroystokyo.paper.profile.PlayerProfile;
import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.google.common.util.concurrent.ThreadFactoryBuilder;
import io.papermc.paper.math.Position; import io.papermc.paper.math.BlockPosition;
import com.google.gson.JsonArray; import io.papermc.paper.math.FinePosition;
@@ -0,0 +0,0 @@ import net.minecraft.world.level.Level; @@ -0,0 +0,0 @@ import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraft.world.level.chunk.ChunkStatus; import net.minecraft.world.level.chunk.ChunkStatus;
import net.minecraft.world.phys.Vec3;
import org.apache.commons.lang.exception.ExceptionUtils; import org.apache.commons.lang.exception.ExceptionUtils;
+import com.mojang.authlib.GameProfile; +import com.mojang.authlib.GameProfile;
import org.bukkit.Location; import org.bukkit.Location;

View File

@ -16,9 +16,9 @@ diff --git a/src/main/java/io/papermc/paper/util/MCUtil.java b/src/main/java/io/
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/io/papermc/paper/util/MCUtil.java --- a/src/main/java/io/papermc/paper/util/MCUtil.java
+++ b/src/main/java/io/papermc/paper/util/MCUtil.java +++ b/src/main/java/io/papermc/paper/util/MCUtil.java
@@ -0,0 +0,0 @@ import it.unimi.dsi.fastutil.objects.ReferenceArrayList; @@ -0,0 +0,0 @@ import net.minecraft.core.BlockPos;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
import net.minecraft.core.Vec3i;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
+import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.CompoundTag;
+import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.Component;

View File

@ -3487,17 +3487,21 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+package io.papermc.paper.util; +package io.papermc.paper.util;
+ +
+import com.google.common.util.concurrent.ThreadFactoryBuilder; +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 io.papermc.paper.math.Position;
+import it.unimi.dsi.fastutil.objects.ObjectRBTreeSet; +import it.unimi.dsi.fastutil.objects.ObjectRBTreeSet;
+import java.lang.ref.Cleaner; +import java.lang.ref.Cleaner;
+import net.minecraft.core.BlockPos; +import net.minecraft.core.BlockPos;
+import net.minecraft.core.Direction; +import net.minecraft.core.Direction;
+import net.minecraft.core.Vec3i;
+import net.minecraft.server.MinecraftServer; +import net.minecraft.server.MinecraftServer;
+import net.minecraft.server.level.ServerLevel; +import net.minecraft.server.level.ServerLevel;
+import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.Entity;
+import net.minecraft.world.level.ChunkPos; +import net.minecraft.world.level.ChunkPos;
+import net.minecraft.world.level.ClipContext; +import net.minecraft.world.level.ClipContext;
+import net.minecraft.world.level.Level; +import net.minecraft.world.level.Level;
+import net.minecraft.world.phys.Vec3;
+import org.apache.commons.lang.exception.ExceptionUtils; +import org.apache.commons.lang.exception.ExceptionUtils;
+import org.bukkit.Location; +import org.bukkit.Location;
+import org.bukkit.block.BlockFace; +import org.bukkit.block.BlockFace;
@ -3960,6 +3964,14 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ return new BlockPos(vec.getBlockX(), vec.getBlockY(), vec.getBlockZ()); + 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) { + public static boolean isEdgeOfChunk(BlockPos pos) {
+ final int modX = pos.getX() & 15; + final int modX = pos.getX() & 15;
+ final int modZ = pos.getZ() & 15; + final int modZ = pos.getZ() & 15;

View File

@ -15850,9 +15850,9 @@ diff --git a/src/main/java/io/papermc/paper/util/MCUtil.java b/src/main/java/io/
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/io/papermc/paper/util/MCUtil.java --- a/src/main/java/io/papermc/paper/util/MCUtil.java
+++ b/src/main/java/io/papermc/paper/util/MCUtil.java +++ b/src/main/java/io/papermc/paper/util/MCUtil.java
@@ -0,0 +0,0 @@ package io.papermc.paper.util; @@ -0,0 +0,0 @@ import com.google.common.util.concurrent.ThreadFactoryBuilder;
import io.papermc.paper.math.BlockPosition;
import com.google.common.util.concurrent.ThreadFactoryBuilder; import io.papermc.paper.math.FinePosition;
import io.papermc.paper.math.Position; import io.papermc.paper.math.Position;
+import com.google.gson.JsonArray; +import com.google.gson.JsonArray;
+import com.google.gson.JsonObject; +import com.google.gson.JsonObject;
@ -15864,6 +15864,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+import it.unimi.dsi.fastutil.objects.ReferenceArrayList; +import it.unimi.dsi.fastutil.objects.ReferenceArrayList;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
import net.minecraft.core.Vec3i;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
+import net.minecraft.server.level.ChunkHolder; +import net.minecraft.server.level.ChunkHolder;
+import net.minecraft.server.level.ChunkMap; +import net.minecraft.server.level.ChunkMap;
@ -15877,9 +15878,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
+import net.minecraft.world.level.chunk.ChunkAccess; +import net.minecraft.world.level.chunk.ChunkAccess;
+import net.minecraft.world.level.chunk.ChunkStatus; +import net.minecraft.world.level.chunk.ChunkStatus;
import net.minecraft.world.phys.Vec3;
import org.apache.commons.lang.exception.ExceptionUtils; import org.apache.commons.lang.exception.ExceptionUtils;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.block.BlockFace;
@@ -0,0 +0,0 @@ import org.spigotmc.AsyncCatcher; @@ -0,0 +0,0 @@ import org.spigotmc.AsyncCatcher;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;