From 2ffb1ce886482a8a59858f15b62fa417a60e0f40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9D=8F=E9=BB=91?= Date: Mon, 27 Aug 2018 00:23:57 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=B3=A8=E5=86=8C=E6=89=AB?= =?UTF-8?q?=E6=8F=8F=E4=BB=85=E5=AF=B9=E4=BE=9D=E8=B5=96=20TabooLib=20?= =?UTF-8?q?=E7=9A=84=E6=8F=92=E4=BB=B6=E6=9C=89=E6=95=88=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E4=B8=80=E4=B8=AA=E4=B8=A2=E4=BA=86=E7=9A=84=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commands/internal/TCommandHandler.java | 11 ++-- .../taboolib/listener/TListenerHandler.java | 13 ++--- .../taboolib/socket/packet/PacketParser.java | 50 +++++++++++++++++++ 3 files changed, 64 insertions(+), 10 deletions(-) create mode 100644 src/main/java/me/skymc/taboolib/socket/packet/PacketParser.java diff --git a/src/main/java/me/skymc/taboolib/commands/internal/TCommandHandler.java b/src/main/java/me/skymc/taboolib/commands/internal/TCommandHandler.java index fee740c..16579ae 100644 --- a/src/main/java/me/skymc/taboolib/commands/internal/TCommandHandler.java +++ b/src/main/java/me/skymc/taboolib/commands/internal/TCommandHandler.java @@ -1,6 +1,7 @@ package me.skymc.taboolib.commands.internal; import com.ilummc.tlib.resources.TLocale; +import me.skymc.taboolib.TabooLib; import me.skymc.taboolib.fileutils.FileUtils; import me.skymc.taboolib.listener.TListener; import me.skymc.taboolib.methods.ReflectionUtils; @@ -133,10 +134,12 @@ public class TCommandHandler implements Listener { */ public static void registerCommands() { for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) { - try { - registerCommand(plugin); - } catch (Exception e) { - e.printStackTrace(); + if (plugin.equals(TabooLib.instance()) || plugin.getDescription().getDepend().contains("TabooLib")) { + try { + registerCommand(plugin); + } catch (Exception e) { + e.printStackTrace(); + } } } } diff --git a/src/main/java/me/skymc/taboolib/listener/TListenerHandler.java b/src/main/java/me/skymc/taboolib/listener/TListenerHandler.java index 5376aef..100e8ba 100644 --- a/src/main/java/me/skymc/taboolib/listener/TListenerHandler.java +++ b/src/main/java/me/skymc/taboolib/listener/TListenerHandler.java @@ -1,7 +1,7 @@ package me.skymc.taboolib.listener; -import com.ilummc.tlib.util.Ref; import com.ilummc.tlib.util.Strings; +import me.skymc.taboolib.TabooLib; import me.skymc.taboolib.fileutils.FileUtils; import org.bukkit.Bukkit; import org.bukkit.event.EventHandler; @@ -11,7 +11,6 @@ import org.bukkit.event.server.PluginDisableEvent; import org.bukkit.event.server.PluginEnableEvent; import org.bukkit.plugin.Plugin; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.*; @@ -29,10 +28,12 @@ public class TListenerHandler implements Listener { */ public static void setupListeners() { for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) { - try { - setupListener(plugin); - } catch (Exception e) { - e.printStackTrace(); + if (plugin.equals(TabooLib.instance()) || plugin.getDescription().getDepend().contains("TabooLib")) { + try { + setupListener(plugin); + } catch (Exception e) { + e.printStackTrace(); + } } } } diff --git a/src/main/java/me/skymc/taboolib/socket/packet/PacketParser.java b/src/main/java/me/skymc/taboolib/socket/packet/PacketParser.java new file mode 100644 index 0000000..0f733fd --- /dev/null +++ b/src/main/java/me/skymc/taboolib/socket/packet/PacketParser.java @@ -0,0 +1,50 @@ +package me.skymc.taboolib.socket.packet; + +import com.google.gson.JsonObject; +import me.skymc.taboolib.fileutils.FileUtils; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +/** + * @Author sky + * @Since 2018-08-22 23:07 + */ +public class PacketParser { + + private List> packets = new ArrayList<>(); + + public PacketParser() { + FileUtils.getClasses(PacketParser.class).stream().filter(clazz -> clazz.isAnnotationPresent(PacketType.class)).forEach(clazz -> packets.add(clazz)); + } + + public Packet parser(JsonObject json) { + if (!json.has("packet")) { + return null; + } + String packetType = json.get("packet").getAsString(); + Optional> packetFind = packets.stream().filter(packet -> packet.getAnnotation(PacketType.class).name().equals(packetType)).findFirst(); + if (!packetFind.isPresent()) { + return null; + } + try { + Packet packetObject = (Packet) packetFind.get().getConstructor(Integer.TYPE).newInstance(json.get("port").getAsInt()); + packetObject.unSerialize(json); + return packetObject; + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + // ********************************* + // + // Getter and Setter + // + // ********************************* + + public List> getPackets() { + return packets; + } +}