Skip to content

Commit

Permalink
Add cause to PlayerOpenSignEvent
Browse files Browse the repository at this point in the history
Also fire the event for plugin-opened signs
  • Loading branch information
Machine-Maker committed Jul 2, 2023
1 parent 171ba7c commit 092fd25
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 8 deletions.
45 changes: 40 additions & 5 deletions patches/api/0420-Add-event-for-player-editing-sign.patch
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
+
+/**
Expand All @@ -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;
+ }
+
+ /**
Expand All @@ -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;
Expand All @@ -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,
+ }
+}
49 changes: 46 additions & 3 deletions patches/server/0980-Add-event-for-player-editing-sign.patch
Original file line number Diff line number Diff line change
Expand Up @@ -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 adb7220be617d6d9f2cdd7fbe4fa2dd24cc7d142..56c0f0344da5cda0eea04130b3d02b512db3dcd7 100644
--- a/src/main/java/net/minecraft/world/item/ItemStack.java
+++ b/src/main/java/net/minecraft/world/item/ItemStack.java
@@ -492,7 +492,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 d00e3627a4da6369e08f3495f04af342ae28e71f..0c8f41ce6d570d71d00aa54556e58e4fe7480a41 100644
--- a/src/main/java/org/bukkit/craftbukkit/block/CraftSign.java
+++ b/src/main/java/org/bukkit/craftbukkit/block/CraftSign.java
@@ -130,6 +130,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
}

0 comments on commit 092fd25

Please sign in to comment.