Skip to content

Commit

Permalink
Added 1.18 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
sammwyy committed Apr 14, 2022
1 parent 78273e2 commit 56a6ea3
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/dev/_2lstudios/hamsterapi/Debug.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions src/dev/_2lstudios/hamsterapi/HamsterAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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.");
}
Expand Down
74 changes: 74 additions & 0 deletions src/dev/_2lstudios/hamsterapi/Version.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
27 changes: 26 additions & 1 deletion src/dev/_2lstudios/hamsterapi/hamsterplayer/HamsterPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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();
}

Expand All @@ -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);
}
Expand All @@ -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;
}

Expand Down

0 comments on commit 56a6ea3

Please sign in to comment.