From 314cc5215b7f678adb7486a420150ad31b8faba4 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Fri, 8 Apr 2016 20:35:54 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E5=AE=89=E5=85=A8=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Yum/listeners/ThreadSafetyListener.java | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 src/main/java/pw/yumc/Yum/listeners/ThreadSafetyListener.java diff --git a/src/main/java/pw/yumc/Yum/listeners/ThreadSafetyListener.java b/src/main/java/pw/yumc/Yum/listeners/ThreadSafetyListener.java new file mode 100644 index 0000000..b853c49 --- /dev/null +++ b/src/main/java/pw/yumc/Yum/listeners/ThreadSafetyListener.java @@ -0,0 +1,107 @@ +package pw.yumc.Yum.listeners; + +import org.bukkit.Bukkit; +import org.bukkit.event.Event; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.CreatureSpawnEvent; +import org.bukkit.event.entity.ItemSpawnEvent; +import org.bukkit.event.inventory.InventoryOpenEvent; +import org.bukkit.event.player.PlayerMoveEvent; +import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.event.player.PlayerTeleportEvent; +import org.bukkit.event.server.PluginDisableEvent; +import org.bukkit.event.server.PluginEnableEvent; +import org.bukkit.event.world.ChunkLoadEvent; +import org.bukkit.event.world.ChunkUnloadEvent; +import org.bukkit.event.world.WorldLoadEvent; +import org.bukkit.event.world.WorldSaveEvent; +import org.bukkit.event.world.WorldUnloadEvent; + +import pw.yumc.Yum.Yum; + +/** + * Fork From LagMonitor: https://github.com/games647/LagMonitor.git + * We can listen to events which are intended to run sync to the main thread. + * If those events are fired on a async task the operation was likely not thread-safe. + */ +public class ThreadSafetyListener implements Listener { + + private final Thread mainThread = Thread.currentThread(); + + public ThreadSafetyListener(final Yum yum) { + Bukkit.getPluginManager().registerEvents(this, yum); + } + + @EventHandler + public void onChunkLoad(final ChunkLoadEvent chunkLoadEvent) { + checkSafety(chunkLoadEvent); + } + + @EventHandler + public void onChunkUnload(final ChunkUnloadEvent chunkUnloadEvent) { + checkSafety(chunkUnloadEvent); + } + + @EventHandler + public void onCreatureSpawn(final CreatureSpawnEvent creatureSpawnEvent) { + checkSafety(creatureSpawnEvent); + } + + @EventHandler + public void onInventoryOpen(final InventoryOpenEvent inventoryOpenEvent) { + checkSafety(inventoryOpenEvent); + } + + @EventHandler + public void onItemSpawn(final ItemSpawnEvent itemSpawnEvent) { + checkSafety(itemSpawnEvent); + } + + @EventHandler + public void onPlayerMove(final PlayerMoveEvent moveEvent) { + checkSafety(moveEvent); + } + + @EventHandler + public void onPlayerQuit(final PlayerQuitEvent quitEvent) { + checkSafety(quitEvent); + } + + @EventHandler + public void onPlayerTeleport(final PlayerTeleportEvent teleportEvent) { + checkSafety(teleportEvent); + } + + @EventHandler + public void onPluginDisable(final PluginDisableEvent pluginDisableEvent) { + checkSafety(pluginDisableEvent); + } + + @EventHandler + public void onPluginEnable(final PluginEnableEvent pluginEnableEvent) { + checkSafety(pluginEnableEvent); + } + + @EventHandler + public void onWorldLoad(final WorldLoadEvent worldLoadEvent) { + checkSafety(worldLoadEvent); + } + + @EventHandler + public void onWorldSave(final WorldSaveEvent worldSaveEvent) { + checkSafety(worldSaveEvent); + } + + @EventHandler + public void onWorldUnload(final WorldUnloadEvent worldUnloadEvent) { + checkSafety(worldUnloadEvent); + } + + private void checkSafety(final Event eventType) { + if (Thread.currentThread() != mainThread && !eventType.isAsynchronous()) { + final String eventName = eventType.getEventName(); + throw new IllegalAccessError("[Yum 线程安全]: 请勿异步调用一个同步事件 " + eventName); + } + } +}