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; + } +}