Skip to content

Commit

Permalink
Fix items being leaked through Allays & Armor Stands (#162)
Browse files Browse the repository at this point in the history
* Disallow entity interaction in the chunk editor

* Catch PlayerArmorStandManipulateEvent in ChunkEditor
  • Loading branch information
WasabiThumb authored Dec 16, 2024
1 parent 9d77ca8 commit fe87088
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions src/main/java/codes/wasabi/xclaim/gui/ChunkEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,25 @@ private Events() {
listener.register();
}

void tryRegisterPaperEvents() {
// PAPER ONLY: Item frame change event
Class<? extends PlayerEvent> itemFrameEventClass;
try {
itemFrameEventClass = Class.forName("io.papermc.paper.event.player.PlayerItemFrameChangeEvent")
.asSubclass(PlayerEvent.class);
} catch (ClassNotFoundException | ClassCastException ignored) {
return;
void tryRegisterConditionalEvents() {
// Register events that are not guaranteed to exist in Spigot 1.8+
final String[] miscPlayerEvents = new String[] {
"io.papermc.paper.event.player.PlayerItemFrameChangeEvent",
"org.bukkit.event.player.PlayerArmorStandManipulateEvent"
};
for (String className : miscPlayerEvents) {
try {
this.registerMiscPlayerEvent(Class.forName(className).asSubclass(PlayerEvent.class));
} catch (ClassNotFoundException | ClassCastException ignored) { }
}
}

private void registerMiscPlayerEvent(@NotNull Class<? extends PlayerEvent> clazz) {
Bukkit.getPluginManager().registerEvent(
itemFrameEventClass,
clazz,
this,
EventPriority.NORMAL,
(Listener ignored, Event event) -> this.onItemFrameChange((PlayerEvent) event),
(Listener ignored, Event event) -> this.onMiscPlayerEvent((PlayerEvent) event),
XClaim.instance
);
}
Expand Down Expand Up @@ -309,6 +314,12 @@ public void onInteract(@NotNull PlayerInteractEvent event) {
}
}

@EventHandler
public void onInteractEntity(@NotNull PlayerInteractEntityEvent event) {
Player ply = event.getPlayer();
if (getEditing(ply) != null) event.setCancelled(true);
}

@EventHandler
public void onLeave(@NotNull PlayerQuitEvent event) {
Player ply = event.getPlayer();
Expand Down Expand Up @@ -419,8 +430,7 @@ public void onMove(@NotNull PlayerMoveEvent event) {
}
}

// PAPER ONLY
public void onItemFrameChange(@NotNull PlayerEvent event) {
public void onMiscPlayerEvent(@NotNull PlayerEvent event) {
Player ply = event.getPlayer();
if (getEditing(ply) != null && event instanceof Cancellable) {
((Cancellable) event).setCancelled(true);
Expand Down Expand Up @@ -454,7 +464,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();
EVENTS.tryRegisterConditionalEvents();
}

private static final Map<UUID, Claim> editingMap = new HashMap<>();
Expand Down

0 comments on commit fe87088

Please sign in to comment.