-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from Nixuge/main
Legacy Packet Adapter
- Loading branch information
Showing
22 changed files
with
363 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,9 @@ | |
*.iml | ||
|
||
.gradle | ||
build/ | ||
build/ | ||
|
||
.classpath | ||
.project | ||
.settings/ | ||
bin/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
plugins { | ||
id("net.megavex.scoreboardlibrary.base-conventions") | ||
} | ||
|
||
dependencies { | ||
compileOnly(project(":scoreboard-library-packet-adapter-base")) | ||
} |
41 changes: 41 additions & 0 deletions
41
...java/net/megavex/scoreboardlibrary/implementation/packetAdapter/legacy/ChatColorUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package net.megavex.scoreboardlibrary.implementation.packetAdapter.legacy; | ||
|
||
import net.kyori.adventure.text.format.NamedTextColor; | ||
import net.megavex.scoreboardlibrary.implementation.packetAdapter.util.reflect.ReflectUtil; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.lang.invoke.MethodHandle; | ||
import java.lang.invoke.MethodHandles; | ||
import java.lang.reflect.Method; | ||
|
||
public final class ChatColorUtil { | ||
private static final MethodHandle FROM_NAME_METHOD, GET_INDEX_METHOD; | ||
|
||
static { | ||
Class<?> enumChatFormatClass = ReflectUtil.getClassOrThrow(LegacyMinecraftClasses.server("EnumChatFormat")); | ||
MethodHandles.Lookup lookup = MethodHandles.lookup(); | ||
|
||
try { | ||
Method fromNameMethod = enumChatFormatClass.getMethod("b", String.class); | ||
FROM_NAME_METHOD = lookup.unreflect(fromNameMethod); | ||
|
||
Method getIndexMethod = enumChatFormatClass.getMethod("b"); | ||
GET_INDEX_METHOD = lookup.unreflect(getIndexMethod); | ||
} catch (NoSuchMethodException | IllegalAccessException e) { | ||
throw new ExceptionInInitializerError(e); | ||
} | ||
} | ||
|
||
private ChatColorUtil() { | ||
} | ||
|
||
public static int getColorIndex(@NotNull NamedTextColor color) { | ||
String name = NamedTextColor.NAMES.key(color); | ||
try { | ||
Object format = FROM_NAME_METHOD.invoke(name); | ||
return (int) GET_INDEX_METHOD.invoke(format); | ||
} catch (Throwable e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...megavex/scoreboardlibrary/implementation/packetAdapter/legacy/LegacyMinecraftClasses.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package net.megavex.scoreboardlibrary.implementation.packetAdapter.legacy; | ||
|
||
import org.bukkit.Bukkit; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
// TODO: should be moved to MinecraftReflection at some point | ||
public final class LegacyMinecraftClasses { | ||
private static final String NMS_VERSION_STRING = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3]; | ||
|
||
private LegacyMinecraftClasses() { | ||
} | ||
|
||
public static @NotNull String server(String path) { | ||
return "net.minecraft.server." + NMS_VERSION_STRING + "." + path; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...net/megavex/scoreboardlibrary/implementation/packetAdapter/legacy/LegacyPacketSender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package net.megavex.scoreboardlibrary.implementation.packetAdapter.legacy; | ||
|
||
import net.megavex.scoreboardlibrary.implementation.packetAdapter.PacketSender; | ||
import net.megavex.scoreboardlibrary.implementation.packetAdapter.util.reflect.MinecraftClasses; | ||
import net.megavex.scoreboardlibrary.implementation.packetAdapter.util.reflect.ReflectUtil; | ||
import org.bukkit.entity.Player; | ||
|
||
import java.lang.invoke.MethodHandle; | ||
import java.lang.invoke.MethodHandles; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Method; | ||
|
||
public final class LegacyPacketSender implements PacketSender<Object> { | ||
public static final LegacyPacketSender INSTANCE = new LegacyPacketSender(); | ||
|
||
private static final MethodHandle GET_HANDLE_METHOD, PLAYER_CONNECTION_FIELD, SEND_PACKET_METHOD; | ||
|
||
static { | ||
Class<?> packetClass = ReflectUtil.getClassOrThrow(LegacyMinecraftClasses.server("Packet")); | ||
Class<?> craftPlayerClass = ReflectUtil.getClassOrThrow(MinecraftClasses.craftBukkit("entity.CraftPlayer")); | ||
MethodHandles.Lookup lookup = MethodHandles.lookup(); | ||
|
||
try { | ||
Method getHandleMethod = craftPlayerClass.getMethod("getHandle"); | ||
GET_HANDLE_METHOD = lookup.unreflect(getHandleMethod); | ||
|
||
Field playerConnectionField = getHandleMethod.getReturnType().getField("playerConnection"); | ||
PLAYER_CONNECTION_FIELD = lookup.unreflectGetter(playerConnectionField); | ||
|
||
Method sendPacketMethod = playerConnectionField.getType().getMethod("sendPacket", packetClass); | ||
SEND_PACKET_METHOD = lookup.unreflect(sendPacketMethod); | ||
} catch (NoSuchMethodException | IllegalAccessException | NoSuchFieldException e) { | ||
throw new ExceptionInInitializerError(e); | ||
} | ||
} | ||
|
||
private LegacyPacketSender() { | ||
} | ||
|
||
@Override | ||
public void sendPacket(Player player, Object packet) { | ||
try { | ||
Object handle = GET_HANDLE_METHOD.invoke(player); | ||
Object playerConnection = PLAYER_CONNECTION_FIELD.invoke(handle); | ||
SEND_PACKET_METHOD.invoke(playerConnection, packet); | ||
} catch (Throwable e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.