From 90eefd5f849bba326ed7531c85b89488fc1344c8 Mon Sep 17 00:00:00 2001 From: Hellxd Date: Thu, 7 Jul 2022 20:08:46 +0800 Subject: [PATCH 1/2] =?UTF-8?q?docs:=20=E7=A7=BB=E9=99=A4src=E5=86=85?= =?UTF-8?q?=E7=9A=84=E7=A4=BA=E4=BE=8B=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pluginhooker/examples/ExampleHook.java | 40 ------------------- .../examples/ExampleListener.java | 19 --------- 2 files changed, 59 deletions(-) delete mode 100644 src/main/java/io/github/dionatestserver/pluginhooker/examples/ExampleHook.java delete mode 100644 src/main/java/io/github/dionatestserver/pluginhooker/examples/ExampleListener.java diff --git a/src/main/java/io/github/dionatestserver/pluginhooker/examples/ExampleHook.java b/src/main/java/io/github/dionatestserver/pluginhooker/examples/ExampleHook.java deleted file mode 100644 index 7b4da30..0000000 --- a/src/main/java/io/github/dionatestserver/pluginhooker/examples/ExampleHook.java +++ /dev/null @@ -1,40 +0,0 @@ -package io.github.dionatestserver.pluginhooker.examples; - -import io.github.dionatestserver.pluginhooker.DionaPluginHooker; -import io.github.dionatestserver.pluginhooker.player.DionaPlayer; -import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; - -public class ExampleHook { - - private final Plugin pluginToHook; - - public ExampleHook(Plugin pluginToHook) { - this.pluginToHook = pluginToHook; - } - - public void hookPlugin() { - DionaPluginHooker.getPluginManager().addPlugin(pluginToHook); - } - - public void unHookPlugin() { - DionaPluginHooker.getPluginManager().removePlugin(pluginToHook); - } - - public void enablePluginForPlayer(Player player) { - DionaPlayer dionaPlayer = DionaPluginHooker.getPlayerManager().getDionaPlayer(player); - if (dionaPlayer == null) { - return; - } - dionaPlayer.enablePlugin(pluginToHook); - } - - public void disablePluginForPlayer(Player player) { - DionaPlayer dionaPlayer = DionaPluginHooker.getPlayerManager().getDionaPlayer(player); - if (dionaPlayer == null) { - return; - } - dionaPlayer.disablePlugin(pluginToHook); - } - -} diff --git a/src/main/java/io/github/dionatestserver/pluginhooker/examples/ExampleListener.java b/src/main/java/io/github/dionatestserver/pluginhooker/examples/ExampleListener.java deleted file mode 100644 index 78692e4..0000000 --- a/src/main/java/io/github/dionatestserver/pluginhooker/examples/ExampleListener.java +++ /dev/null @@ -1,19 +0,0 @@ -package io.github.dionatestserver.pluginhooker.examples; - -import io.github.dionatestserver.pluginhooker.events.DionaBukkitListenerEvent; -import io.github.dionatestserver.pluginhooker.events.DionaProtocolLibPacketEvent; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; - -public class ExampleListener implements Listener { - - @EventHandler - public void onBukkitEvent(DionaBukkitListenerEvent event) { - // do something - } - - @EventHandler - public void onProtocolLibEvent(DionaProtocolLibPacketEvent event) { - // do something - } -} From 8fcaed1e1a19c084e40bacb13afb675de6b85565 Mon Sep 17 00:00:00 2001 From: Hellxd Date: Thu, 7 Jul 2022 20:16:29 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Revert=20"feat:=20=E4=BD=BF=E7=94=A8MethodH?= =?UTF-8?q?andle=E6=9B=BF=E6=8D=A2plib=E5=9B=9E=E8=B0=83=E7=9A=84=E5=8F=8D?= =?UTF-8?q?=E5=B0=84"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 32f087c258f0bfc77250e0b70775360c9d2884c2. --- .../ProtocolLibCallbackHandler.java | 28 ++++++------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/src/main/java/io/github/dionatestserver/pluginhooker/hook/impl/protocollib/ProtocolLibCallbackHandler.java b/src/main/java/io/github/dionatestserver/pluginhooker/hook/impl/protocollib/ProtocolLibCallbackHandler.java index d411b18..02bd1bc 100644 --- a/src/main/java/io/github/dionatestserver/pluginhooker/hook/impl/protocollib/ProtocolLibCallbackHandler.java +++ b/src/main/java/io/github/dionatestserver/pluginhooker/hook/impl/protocollib/ProtocolLibCallbackHandler.java @@ -1,6 +1,5 @@ package io.github.dionatestserver.pluginhooker.hook.impl.protocollib; -import com.comphenix.protocol.concurrency.AbstractConcurrentListenerMultimap; import com.comphenix.protocol.concurrency.SortedCopyOnWriteArray; import com.comphenix.protocol.events.PacketEvent; import com.comphenix.protocol.events.PacketListener; @@ -12,17 +11,13 @@ import org.bukkit.Bukkit; import org.bukkit.plugin.Plugin; -import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodHandles; import java.lang.reflect.Field; import java.util.Collection; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; public class ProtocolLibCallbackHandler { - private MethodHandle listenersGetter; - private MethodHandle listenersSetter; + private Field mapListeners; public SortedPacketListenerList handleProtocolLibPacket(SortedPacketListenerList listenerList, PacketEvent event, boolean outbound) { DionaPlayer dionaPlayer = DionaPluginHooker.getPlayerManager().getDionaPlayer(event.getPlayer()); @@ -62,28 +57,21 @@ private SortedPacketListenerList deepCopyListenerList(SortedPacketListenerList s SortedPacketListenerList result = new SortedPacketListenerList(); try { - if (this.listenersGetter == null) { - Field mapListeners = SortedPacketListenerList.class.getSuperclass().getDeclaredField("mapListeners"); - mapListeners.setAccessible(true); - - MethodHandles.Lookup lookup = MethodHandles.lookup(); - this.listenersGetter = lookup.unreflectGetter(mapListeners); - this.listenersSetter = lookup.unreflectSetter(mapListeners); + if (this.mapListeners == null) { + this.mapListeners = SortedPacketListenerList.class.getSuperclass().getDeclaredField("mapListeners"); + this.mapListeners.setAccessible(true); } -// ConcurrentHashMap listeners = (ConcurrentHashMap) mapListeners.get(sortedPacketListenerList); - ConcurrentMap listeners = - (ConcurrentMap) listenersGetter.invokeExact(((AbstractConcurrentListenerMultimap) sortedPacketListenerList)); - ConcurrentMap resultMap = listeners.keySet().stream().collect( + ConcurrentHashMap listeners = (ConcurrentHashMap) mapListeners.get(sortedPacketListenerList); + ConcurrentHashMap resultMap = listeners.keySet().stream().collect( ConcurrentHashMap::new, (map, packetType) -> map.put(packetType, new SortedCopyOnWriteArray((Collection) listeners.get(packetType))), ConcurrentHashMap::putAll ); -// mapListeners.set(result, resultMap); - listenersSetter.invokeExact((AbstractConcurrentListenerMultimap) result, resultMap); + mapListeners.set(result, resultMap); return result; - } catch (Throwable e) { + } catch (Exception e) { e.printStackTrace(); } return null;