diff --git a/src/dev/_2lstudios/hamsterapi/Debug.java b/src/dev/_2lstudios/hamsterapi/Debug.java index 925ad9b..897e8ad 100644 --- a/src/dev/_2lstudios/hamsterapi/Debug.java +++ b/src/dev/_2lstudios/hamsterapi/Debug.java @@ -10,6 +10,10 @@ public static void init(HamsterAPI plugin) { sender = plugin.getServer().getConsoleSender(); } + public static boolean isEnabled() { + return sender != null; + } + public static void log(String prefix, String message) { if (sender != null) { sender.sendMessage( diff --git a/src/dev/_2lstudios/hamsterapi/HamsterAPI.java b/src/dev/_2lstudios/hamsterapi/HamsterAPI.java index 6b81b24..edb2c68 100644 --- a/src/dev/_2lstudios/hamsterapi/HamsterAPI.java +++ b/src/dev/_2lstudios/hamsterapi/HamsterAPI.java @@ -33,7 +33,7 @@ public static synchronized HamsterAPI getInstance() { return instance; } - private static String getVersion(Server server) { + public static String getVersion(Server server) { final String packageName = server.getClass().getPackage().getName(); return packageName.substring(packageName.lastIndexOf('.') + 1); } @@ -72,7 +72,7 @@ public void onEnable() { if (this.getConfig().getBoolean("debug")) { Debug.init(this); - Debug.info("Debug mode is enabled in HamsterAPI."); + Debug.info("Debug mode is enabled in HamsterAPI (" + Version.getCurrentVersion().toString() + ")"); Debug.warn("It is recommended not to use this mode in production."); Debug.crit("Debug mode can affect server performance while it is active."); } diff --git a/src/dev/_2lstudios/hamsterapi/Version.java b/src/dev/_2lstudios/hamsterapi/Version.java new file mode 100644 index 0000000..1dfc5ae --- /dev/null +++ b/src/dev/_2lstudios/hamsterapi/Version.java @@ -0,0 +1,74 @@ +package dev._2lstudios.hamsterapi; + +import org.bukkit.Bukkit; + +public class Version { + private int first = 0; + private int second = 0; + private int third = 0; + + private static Version currentVersion; + + public Version(String versionText) { + String[] splittedVersion = versionText.split("[.]"); + + for (int i = 0; i < splittedVersion.length; i++) { + String part = splittedVersion[i]; + int value = Integer.parseInt(part); + + if (i == 0) { + first = value; + } + + if (i == 1) { + second = value; + } + + if (i == 2) { + third = value; + } + } + } + + public static Version getCurrentVersion() { + if (currentVersion == null) { + currentVersion = new Version( + HamsterAPI.getVersion(Bukkit.getServer()) + .split("v")[1] + .split("_R")[0] + .replace("_", ".") + ); + } + return currentVersion; + } + + public boolean isMajor(Version version) { + return + (first > version.first) + || + (first == version.first && second > version.second) + || + (first == version.first && second == version.second && third > version.third) + ; + } + + public boolean isMajor(String versionText) { + return this.isMajor(new Version(versionText)); + } + + public boolean isMinor(Version version) { + return version.isMajor(this); + } + + public boolean isMinor(String versionText) { + return this.isMinor(new Version(versionText)); + } + + public String toString() { + String ver = first + "." + second; + if (third != 0) { + ver += "." + third; + } + return ver; + } +} diff --git a/src/dev/_2lstudios/hamsterapi/hamsterplayer/HamsterPlayer.java b/src/dev/_2lstudios/hamsterapi/hamsterplayer/HamsterPlayer.java index 0860140..cb8fb4c 100644 --- a/src/dev/_2lstudios/hamsterapi/hamsterplayer/HamsterPlayer.java +++ b/src/dev/_2lstudios/hamsterapi/hamsterplayer/HamsterPlayer.java @@ -9,7 +9,9 @@ import org.bukkit.Server; import org.bukkit.entity.Player; +import dev._2lstudios.hamsterapi.Debug; import dev._2lstudios.hamsterapi.HamsterAPI; +import dev._2lstudios.hamsterapi.Version; import dev._2lstudios.hamsterapi.enums.HamsterHandler; import dev._2lstudios.hamsterapi.handlers.HamsterChannelHandler; import dev._2lstudios.hamsterapi.handlers.HamsterDecoderHandler; @@ -207,12 +209,25 @@ public void setup() if (!setup) { final Reflection reflection = hamsterAPI.getReflection(); final Object handle = player.getClass().getMethod("getHandle").invoke(player); + Debug.info("Invoked player getHandle (" + this.player.getName() + ")"); this.playerConnection = reflection.getField(handle, reflection.getPlayerConnection()); + Debug.info("Getting playerConection field (" + this.player.getName() + ")"); + this.networkManager = reflection.getField(playerConnection, reflection.getNetworkManager()); + Debug.info("Getting networkManager field (" + this.player.getName() + ")"); + this.channel = (Channel) reflection.getField(networkManager, Channel.class); + Debug.info("Getting Channel from networkManager field (" + this.player.getName() + ")"); + this.iChatBaseComponentClass = reflection.getIChatBaseComponent(); - this.sendPacketMethod = this.playerConnection.getClass().getMethod("sendPacket", reflection.getPacket()); + + + this.sendPacketMethod = this.playerConnection.getClass().getMethod( + Version.getCurrentVersion().isMinor("1.18") ? "sendPacket" : "a" + , reflection.getPacket()); + Debug.info("Getting sendPacket method from playerConnection field (" + this.player.getName() + ")"); + this.toChatBaseComponent = iChatBaseComponentClass.getDeclaredClasses()[0].getMethod("a", String.class); this.setup = true; } @@ -225,6 +240,7 @@ public void inject() throws IllegalAccessException, InvocationTargetException, N setup(); if (!channel.isActive()) { + Debug.warn("Trying to inject a player with NIO channel closed (" + this.player.getName() + ")"); throw new ClosedChannelException(); } @@ -234,16 +250,21 @@ public void inject() throws IllegalAccessException, InvocationTargetException, N if (pipeline.get("decompress") != null) { pipeline.addAfter("decompress", HamsterHandler.HAMSTER_DECODER, hamsterDecoderHandler); + Debug.info("Added HAMSTER_DECODER in pipeline after decompress (" + this.player.getName() + ")"); } else if (pipeline.get("splitter") != null) { pipeline.addAfter("splitter", HamsterHandler.HAMSTER_DECODER, hamsterDecoderHandler); + Debug.info("Added HAMSTER_DECODER in pipeline after spliter (" + this.player.getName() + ")"); } else { + Debug.crit("No ChannelHandler was found on the pipeline to inject HAMSTER_DECODER (" + this.player.getName() + ")"); throw new IllegalAccessException( "No ChannelHandler was found on the pipeline to inject " + HamsterHandler.HAMSTER_DECODER); } if (pipeline.get("decoder") != null) { pipeline.addAfter("decoder", HamsterHandler.HAMSTER_CHANNEL, hamsterChannelHandler); + Debug.info("Added HAMSTER_CHANNEL in pipeline after decoder (" + this.player.getName() + ")"); } else { + Debug.crit("No ChannelHandler was found on the pipeline to inject HAMSTER_CHANNEL (" + this.player.getName() + ")"); throw new IllegalAccessException( "No ChannelHandler was found on the pipeline to inject " + hamsterChannelHandler); } @@ -259,6 +280,10 @@ public boolean tryInject() { inject(); } catch (final IllegalAccessException | InvocationTargetException | NoSuchMethodException | NoSuchFieldException | ClosedChannelException e) { + if (Debug.isEnabled()) { + Debug.crit("Exception throwed while injecting:"); + e.printStackTrace(); + } return false; }