diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/src/main/java/com/minster586/devmode/PluginInterceptor.java b/src/main/java/com/minster586/devmode/PluginInterceptor.java index 2dd10a8..771531f 100644 --- a/src/main/java/com/minster586/devmode/PluginInterceptor.java +++ b/src/main/java/com/minster586/devmode/PluginInterceptor.java @@ -3,6 +3,7 @@ package com.minster586.devmode; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; +import org.bukkit.event.Cancellable; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.RegisteredListener; @@ -10,21 +11,32 @@ import java.util.Optional; public class PluginInterceptor { + /** + * Attempts to identify which plugin cancelled a given event. + * This method only works for events that implement the Cancellable interface. + * + * @param event the Bukkit event to inspect + * @return optional plugin name that may have cancelled the event + */ public static Optional getCancellingPlugin(Event event) { - if (!event.isCancelled()) return Optional.empty(); + if (!(event instanceof Cancellable)) return Optional.empty(); + + Cancellable cancellable = (Cancellable) event; + + if (!cancellable.isCancelled()) return Optional.empty(); HandlerList handlerList = event.getHandlers(); for (RegisteredListener listener : handlerList.getRegisteredListeners()) { Plugin plugin = listener.getPlugin(); try { - listener.callEvent(event); // Simulate who handles it - } catch (Exception ignored) { - // Ignore exceptions thrown during simulated call - } + // Calling this artificially won't re-trigger cancellation. + // You may want to track handler priority or external logs separately. + listener.callEvent(event); + } catch (Exception ignored) {} - // If event remains cancelled, this plugin may be responsible - if (event.isCancelled()) { + // Re-check after artificial trigger + if (cancellable.isCancelled()) { return Optional.of(plugin.getName()); } }