Add cause to PlayerOpenSignEvent (#9441)

Also fire the event for plugin-opened signs
This commit is contained in:
Jake Potrebic 2023-07-26 09:25:14 -07:00 committed by GitHub
parent 1837f6c677
commit a40e48f3fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 91 additions and 13 deletions

View file

@ -6,10 +6,10 @@ Subject: [PATCH] Add event for player editing sign
diff --git a/src/main/java/io/papermc/paper/event/player/PlayerOpenSignEvent.java b/src/main/java/io/papermc/paper/event/player/PlayerOpenSignEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..4f81a6dbe6694d40f27445f5d17aed48ad75498a
index 0000000000000000000000000000000000000000..a82c0db092684bcb5e3c99f8158eb407268b0b5a
--- /dev/null
+++ b/src/main/java/io/papermc/paper/event/player/PlayerOpenSignEvent.java
@@ -0,0 +1,69 @@
@@ -0,0 +1,104 @@
+package io.papermc.paper.event.player;
+
+import org.bukkit.block.Sign;
@ -18,6 +18,7 @@ index 0000000000000000000000000000000000000000..4f81a6dbe6694d40f27445f5d17aed48
+import org.bukkit.event.Cancellable;
+import org.bukkit.event.HandlerList;
+import org.bukkit.event.player.PlayerEvent;
+import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.NotNull;
+
+/**
@ -28,13 +29,16 @@ index 0000000000000000000000000000000000000000..4f81a6dbe6694d40f27445f5d17aed48
+public class PlayerOpenSignEvent extends PlayerEvent implements Cancellable {
+ private static final HandlerList HANDLER_LIST = new HandlerList();
+ private boolean cancel = false;
+ @NotNull private final Sign sign;
+ @NotNull private final Side side;
+ private final Sign sign;
+ private final Side side;
+ private final Cause cause;
+
+ public PlayerOpenSignEvent(@NotNull Player editor, @NotNull Sign sign, @NotNull Side side) {
+ @ApiStatus.Internal
+ public PlayerOpenSignEvent(final @NotNull Player editor, final @NotNull Sign sign, final @NotNull Side side, final @NotNull Cause cause) {
+ super(editor);
+ this.sign = sign;
+ this.side = side;
+ this.cause = cause;
+ }
+
+ /**
@ -58,6 +62,15 @@ index 0000000000000000000000000000000000000000..4f81a6dbe6694d40f27445f5d17aed48
+ return side;
+ }
+
+ /**
+ * The cause of this sign open.
+ *
+ * @return the cause
+ */
+ public @NotNull Cause getCause() {
+ return cause;
+ }
+
+ @Override
+ public boolean isCancelled() {
+ return cancel;
@ -78,4 +91,26 @@ index 0000000000000000000000000000000000000000..4f81a6dbe6694d40f27445f5d17aed48
+ public static HandlerList getHandlerList() {
+ return HANDLER_LIST;
+ }
+
+ /**
+ * The cause of the {@link PlayerOpenSignEvent}.
+ */
+ public enum Cause {
+ /**
+ * The event was triggered by the placement of a sign.
+ */
+ PLACE,
+ /**
+ * The event was triggered by an interaction with a sign.
+ */
+ INTERACT,
+ /**
+ * The event was triggered via a plugin with {@link org.bukkit.entity.HumanEntity#openSign(Sign, Side)}
+ */
+ PLUGIN,
+ /**
+ * Fallback cause for any unknown cause.
+ */
+ UNKNOWN,
+ }
+}

View file

@ -4,24 +4,67 @@ Date: Mon, 12 Jun 2023 12:56:46 -0400
Subject: [PATCH] Add event for player editing sign
diff --git a/src/main/java/net/minecraft/world/item/ItemStack.java b/src/main/java/net/minecraft/world/item/ItemStack.java
index ae9b821d71d2868ba4eb3c73558fdcd6caf264e3..86304357ca687036cf2448f8cd7c639bdc309e25 100644
--- a/src/main/java/net/minecraft/world/item/ItemStack.java
+++ b/src/main/java/net/minecraft/world/item/ItemStack.java
@@ -493,7 +493,7 @@ public final class ItemStack {
try {
if (world.getBlockEntity(SignItem.openSign) instanceof SignBlockEntity tileentitysign) {
if (world.getBlockState(SignItem.openSign).getBlock() instanceof SignBlock blocksign) {
- blocksign.openTextEdit(entityhuman, tileentitysign, true);
+ blocksign.openTextEdit(entityhuman, tileentitysign, true, io.papermc.paper.event.player.PlayerOpenSignEvent.Cause.PLACE); // Paper
}
}
} finally {
diff --git a/src/main/java/net/minecraft/world/level/block/SignBlock.java b/src/main/java/net/minecraft/world/level/block/SignBlock.java
index 5ab7df0042391cb621ed78a187dc20333e344186..f7fda6fb4e908869310c783e68f7ad7025840592 100644
index 5ab7df0042391cb621ed78a187dc20333e344186..1bc8fbb66c43e9844c66bce26cd349d7fcbc4cfe 100644
--- a/src/main/java/net/minecraft/world/level/block/SignBlock.java
+++ b/src/main/java/net/minecraft/world/level/block/SignBlock.java
@@ -144,6 +144,16 @@ public abstract class SignBlock extends BaseEntityBlock implements SimpleWaterlo
@@ -97,7 +97,7 @@ public abstract class SignBlock extends BaseEntityBlock implements SimpleWaterlo
} else if (bl3) {
return InteractionResult.SUCCESS;
} else if (!this.otherPlayerIsEditingSign(player, signBlockEntity) && player.mayBuild() && this.hasEditableText(player, signBlockEntity, bl2)) {
- this.openTextEdit(player, signBlockEntity, bl2);
+ this.openTextEdit(player, signBlockEntity, bl2, io.papermc.paper.event.player.PlayerOpenSignEvent.Cause.INTERACT); // Paper
return InteractionResult.SUCCESS;
} else {
return InteractionResult.PASS;
@@ -143,7 +143,22 @@ public abstract class SignBlock extends BaseEntityBlock implements SimpleWaterlo
return woodType;
}
+ @io.papermc.paper.annotation.DoNotUse @Deprecated // Paper
public void openTextEdit(Player player, SignBlockEntity blockEntity, boolean front) {
+ // Paper start
+ this.openTextEdit(player, blockEntity, front, io.papermc.paper.event.player.PlayerOpenSignEvent.Cause.UNKNOWN);
+ }
+ public void openTextEdit(Player player, SignBlockEntity blockEntity, boolean front, io.papermc.paper.event.player.PlayerOpenSignEvent.Cause cause) {
+ org.bukkit.entity.Player bukkitPlayer = (org.bukkit.entity.Player) player.getBukkitEntity();
+ org.bukkit.block.Block bukkitBlock = org.bukkit.craftbukkit.block.CraftBlock.at(blockEntity.getLevel(), blockEntity.getBlockPos());
+ org.bukkit.craftbukkit.block.CraftSign<?> bukkitSign = (org.bukkit.craftbukkit.block.CraftSign<?>) org.bukkit.craftbukkit.block.CraftBlockStates.getBlockState(bukkitBlock);
+ io.papermc.paper.event.player.PlayerOpenSignEvent event = new io.papermc.paper.event.player.PlayerOpenSignEvent(
+ bukkitPlayer,
+ bukkitSign,
+ front ? org.bukkit.block.sign.Side.FRONT : org.bukkit.block.sign.Side.BACK);
+ front ? org.bukkit.block.sign.Side.FRONT : org.bukkit.block.sign.Side.BACK,
+ cause);
+ if (!event.callEvent()) return;
+ // Paper end
blockEntity.setAllowedPlayerEditor(player.getUUID());
player.openTextEdit(blockEntity, front);
}
diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftSign.java b/src/main/java/org/bukkit/craftbukkit/block/CraftSign.java
index f48b5204e59ab0c840fc08ecb9abf5facbae9be1..ed29fd2271585a100c1933b787005e9be54bd715 100644
--- a/src/main/java/org/bukkit/craftbukkit/block/CraftSign.java
+++ b/src/main/java/org/bukkit/craftbukkit/block/CraftSign.java
@@ -128,6 +128,10 @@ public class CraftSign<T extends SignBlockEntity> extends CraftBlockEntityState<
SignBlockEntity handle = ((CraftSign<?>) sign).getTileEntity();
+ // Paper start
+ io.papermc.paper.event.player.PlayerOpenSignEvent event = new io.papermc.paper.event.player.PlayerOpenSignEvent((Player) player, sign, side, io.papermc.paper.event.player.PlayerOpenSignEvent.Cause.PLUGIN);
+ if (!event.callEvent()) return;
+ // Paper end
handle.setAllowedPlayerEditor(player.getUniqueId()); // Paper
((org.bukkit.craftbukkit.entity.CraftHumanEntity) player).getHandle().openTextEdit(handle, Side.FRONT == side); // Paper - change move open sign to HumanEntity
}

View file

@ -5,7 +5,7 @@ Subject: [PATCH] Add Sign#getInteractableSideFor
diff --git a/src/main/java/net/minecraft/world/level/block/entity/SignBlockEntity.java b/src/main/java/net/minecraft/world/level/block/entity/SignBlockEntity.java
index 0521240dddde12d78cc05deda7fac11690f5d155..f356196aaeb498a6d2c9ad2112329cef5a3103d6 100644
index e34f7426df5d6c94fcc4101b28702e6c1d9fccff..9eea2982e92e9bc7a53962dc6b21de60f9e5a4c7 100644
--- a/src/main/java/net/minecraft/world/level/block/entity/SignBlockEntity.java
+++ b/src/main/java/net/minecraft/world/level/block/entity/SignBlockEntity.java
@@ -64,13 +64,18 @@ public class SignBlockEntity extends BlockEntity implements CommandSource { // C
@ -30,10 +30,10 @@ index 0521240dddde12d78cc05deda7fac11690f5d155..f356196aaeb498a6d2c9ad2112329cef
float f1 = (float) (Mth.atan2(d1, d0) * 57.2957763671875D) - 90.0F;
diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftSign.java b/src/main/java/org/bukkit/craftbukkit/block/CraftSign.java
index f48b5204e59ab0c840fc08ecb9abf5facbae9be1..d34ed7d5abc53c1a09a5fe8050f6c54e27697e76 100644
index ed29fd2271585a100c1933b787005e9be54bd715..3b9078bad9c0bccc37f44224aa6653d827057a3a 100644
--- a/src/main/java/org/bukkit/craftbukkit/block/CraftSign.java
+++ b/src/main/java/org/bukkit/craftbukkit/block/CraftSign.java
@@ -146,6 +146,14 @@ public class CraftSign<T extends SignBlockEntity> extends CraftBlockEntityState<
@@ -150,6 +150,14 @@ public class CraftSign<T extends SignBlockEntity> extends CraftBlockEntityState<
}
// Paper end

View file

@ -21,10 +21,10 @@ index 6c9d163b9f857806461dc72e54713f1a4f3a5c31..0bd8ea3143b2e9755d492af4596622d1
}
}
diff --git a/src/main/java/net/minecraft/world/level/block/SignBlock.java b/src/main/java/net/minecraft/world/level/block/SignBlock.java
index f7fda6fb4e908869310c783e68f7ad7025840592..14e393e252ffe7d04e0aafb85bd319bed76c330d 100644
index 1bc8fbb66c43e9844c66bce26cd349d7fcbc4cfe..e20c24c86db023ad269f15fe04963608e3fb3fe5 100644
--- a/src/main/java/net/minecraft/world/level/block/SignBlock.java
+++ b/src/main/java/net/minecraft/world/level/block/SignBlock.java
@@ -166,6 +166,6 @@ public abstract class SignBlock extends BaseEntityBlock implements SimpleWaterlo
@@ -171,6 +171,6 @@ public abstract class SignBlock extends BaseEntityBlock implements SimpleWaterlo
@Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level world, BlockState state, BlockEntityType<T> type) {