From 1174f76da6e796093221bbb3f10ee278274560da Mon Sep 17 00:00:00 2001 From: Wasabi_Thumbs Date: Sat, 7 Dec 2024 17:50:11 -0500 Subject: [PATCH] Patch item frame modification while in chunk editor (#156) --- .../codes/wasabi/xclaim/gui/ChunkEditor.java | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/main/java/codes/wasabi/xclaim/gui/ChunkEditor.java b/src/main/java/codes/wasabi/xclaim/gui/ChunkEditor.java index 358204b..daf5150 100644 --- a/src/main/java/codes/wasabi/xclaim/gui/ChunkEditor.java +++ b/src/main/java/codes/wasabi/xclaim/gui/ChunkEditor.java @@ -23,9 +23,7 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.HumanEntity; import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; +import org.bukkit.event.*; import org.bukkit.event.block.Action; import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.PlayerDeathEvent; @@ -52,6 +50,24 @@ private Events() { listener.register(); } + void tryRegisterPaperEvents() { + // PAPER ONLY: Item frame change event + Class itemFrameEventClass; + try { + itemFrameEventClass = Class.forName("io.papermc.paper.event.player.PlayerItemFrameChangeEvent") + .asSubclass(PlayerEvent.class); + } catch (ClassNotFoundException | ClassCastException ignored) { + return; + } + Bukkit.getPluginManager().registerEvent( + itemFrameEventClass, + this, + EventPriority.NORMAL, + (Listener ignored, Event event) -> this.onItemFrameChange((PlayerEvent) event), + XClaim.instance + ); + } + @EventHandler public void onDrop(@NotNull PlayerDropItemEvent event) { Player ply = event.getPlayer(); @@ -403,6 +419,14 @@ public void onMove(@NotNull PlayerMoveEvent event) { } } + // PAPER ONLY + public void onItemFrameChange(@NotNull PlayerEvent event) { + Player ply = event.getPlayer(); + if (getEditing(ply) != null && event instanceof Cancellable) { + ((Cancellable) event).setCancelled(true); + } + } + } private static ItemStack CLAIM_STACK; @@ -430,6 +454,7 @@ public static void initialize() { KEY_INVENTORY = Objects.requireNonNull(Platform.get().createNamespacedKey(XClaim.instance, "ce_inventory")); EVENTS = new Events(); Bukkit.getPluginManager().registerEvents(EVENTS, XClaim.instance); + EVENTS.tryRegisterPaperEvents(); } private static final Map editingMap = new HashMap<>();