fixed plugin watcher
fixed plugin watcher
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/target
|
@@ -3,6 +3,7 @@ package com.minster586.devmode;
|
|||||||
import org.bukkit.event.Event;
|
import org.bukkit.event.Event;
|
||||||
import org.bukkit.event.HandlerList;
|
import org.bukkit.event.HandlerList;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.event.Cancellable;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
import org.bukkit.plugin.RegisteredListener;
|
import org.bukkit.plugin.RegisteredListener;
|
||||||
|
|
||||||
@@ -10,21 +11,32 @@ import java.util.Optional;
|
|||||||
|
|
||||||
public class PluginInterceptor {
|
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<String> getCancellingPlugin(Event event) {
|
public static Optional<String> 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();
|
HandlerList handlerList = event.getHandlers();
|
||||||
for (RegisteredListener listener : handlerList.getRegisteredListeners()) {
|
for (RegisteredListener listener : handlerList.getRegisteredListeners()) {
|
||||||
Plugin plugin = listener.getPlugin();
|
Plugin plugin = listener.getPlugin();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
listener.callEvent(event); // Simulate who handles it
|
// Calling this artificially won't re-trigger cancellation.
|
||||||
} catch (Exception ignored) {
|
// You may want to track handler priority or external logs separately.
|
||||||
// Ignore exceptions thrown during simulated call
|
listener.callEvent(event);
|
||||||
}
|
} catch (Exception ignored) {}
|
||||||
|
|
||||||
// If event remains cancelled, this plugin may be responsible
|
// Re-check after artificial trigger
|
||||||
if (event.isCancelled()) {
|
if (cancellable.isCancelled()) {
|
||||||
return Optional.of(plugin.getName());
|
return Optional.of(plugin.getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user