mirror of
https://github.com/Winds-Studio/Leaf.git
synced 2025-12-19 15:09:25 +00:00
596 lines
26 KiB
Diff
596 lines
26 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: violetc <58360096+s-yh-china@users.noreply.github.com>
|
|
Date: Tue, 26 Sep 2023 19:00:41 +0800
|
|
Subject: [PATCH] Leaves: Protocol Core
|
|
|
|
Original license: GPLv3
|
|
Original project: https://github.com/LeavesMC/Leaves
|
|
|
|
diff --git a/src/main/java/net/minecraft/network/protocol/common/ServerboundCustomPayloadPacket.java b/src/main/java/net/minecraft/network/protocol/common/ServerboundCustomPayloadPacket.java
|
|
index af86f752c33a2990405fea058b7c41c437ba9d46..bada9fae1e7178162429e1f5a1608b9c4a680a6c 100644
|
|
--- a/src/main/java/net/minecraft/network/protocol/common/ServerboundCustomPayloadPacket.java
|
|
+++ b/src/main/java/net/minecraft/network/protocol/common/ServerboundCustomPayloadPacket.java
|
|
@@ -20,7 +20,12 @@ public record ServerboundCustomPayloadPacket(CustomPacketPayload payload) implem
|
|
|
|
private static CustomPacketPayload readPayload(ResourceLocation id, FriendlyByteBuf buf) {
|
|
FriendlyByteBuf.Reader<? extends CustomPacketPayload> packetdataserializer_a = (FriendlyByteBuf.Reader) ServerboundCustomPayloadPacket.KNOWN_TYPES.get(id);
|
|
-
|
|
+ // Leaves start - protocol
|
|
+ CustomPacketPayload leavesPayload = top.leavesmc.leaves.protocol.core.LeavesProtocolManager.getPayload(id, buf);
|
|
+ if (leavesPayload != null) {
|
|
+ return leavesPayload;
|
|
+ }
|
|
+ // Leaves end - protocol
|
|
return (CustomPacketPayload) (packetdataserializer_a != null ? (CustomPacketPayload) packetdataserializer_a.apply(buf) : readUnknownPayload(id, buf));
|
|
}
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
index 86bb3df2915495387c43cf425e3183682c9e0344..f2cef17007767a148d4957e654af8a655920790e 100644
|
|
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
@@ -1711,6 +1711,8 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
|
|
GameTestTicker.SINGLETON.tick();
|
|
}
|
|
|
|
+ top.leavesmc.leaves.protocol.core.LeavesProtocolManager.handleTick(); // Leaves - protocol
|
|
+
|
|
for (int i = 0; i < this.tickables.size(); ++i) {
|
|
((Runnable) this.tickables.get(i)).run();
|
|
}
|
|
diff --git a/src/main/java/net/minecraft/server/network/ServerCommonPacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerCommonPacketListenerImpl.java
|
|
index 61cd58a5bb3d228b29b9cc4db11be03d4c833223..1610f21bbfbca0d12ca12373e56d90a54ac7c9f7 100644
|
|
--- a/src/main/java/net/minecraft/server/network/ServerCommonPacketListenerImpl.java
|
|
+++ b/src/main/java/net/minecraft/server/network/ServerCommonPacketListenerImpl.java
|
|
@@ -136,6 +136,7 @@ public abstract class ServerCommonPacketListenerImpl implements ServerCommonPack
|
|
|
|
@Override
|
|
public void handleCustomPayload(ServerboundCustomPayloadPacket packet) {
|
|
+ top.leavesmc.leaves.protocol.core.LeavesProtocolManager.handlePayload(player, packet.payload()); // Leaves - protocol
|
|
// Paper start - Brand support
|
|
if (packet.payload() instanceof net.minecraft.network.protocol.common.custom.BrandPayload brandPayload) {
|
|
this.player.clientBrandName = brandPayload.brand();
|
|
@@ -153,6 +154,7 @@ public abstract class ServerCommonPacketListenerImpl implements ServerCommonPack
|
|
String channels = payload.toString(com.google.common.base.Charsets.UTF_8);
|
|
for (String channel : channels.split("\0")) {
|
|
this.getCraftPlayer().addChannel(channel);
|
|
+ top.leavesmc.leaves.protocol.core.LeavesProtocolManager.handleMinecraftRegister(channel, player); // Leaves - protocol
|
|
}
|
|
} catch (Exception ex) {
|
|
ServerGamePacketListenerImpl.LOGGER.error("Couldn\'t register custom payload", ex);
|
|
diff --git a/src/main/java/net/minecraft/server/players/PlayerList.java b/src/main/java/net/minecraft/server/players/PlayerList.java
|
|
index 42e6754ae5432a41f54064961bd5d66891a30612..9a3dbd337728374e3adb1071769f7824c2698fe3 100644
|
|
--- a/src/main/java/net/minecraft/server/players/PlayerList.java
|
|
+++ b/src/main/java/net/minecraft/server/players/PlayerList.java
|
|
@@ -362,6 +362,7 @@ public abstract class PlayerList {
|
|
}
|
|
|
|
player.didPlayerJoinEvent = true; // Gale - EMC - do not process chat/commands before player has joined
|
|
+ top.leavesmc.leaves.protocol.core.LeavesProtocolManager.handlePlayerJoin(player); // Leaves - protocol
|
|
|
|
final net.kyori.adventure.text.Component jm = playerJoinEvent.joinMessage();
|
|
|
|
@@ -620,6 +621,7 @@ public abstract class PlayerList {
|
|
return this.remove(entityplayer, net.kyori.adventure.text.Component.translatable("multiplayer.player.left", net.kyori.adventure.text.format.NamedTextColor.YELLOW, io.papermc.paper.configuration.GlobalConfiguration.get().messages.useDisplayNameInQuitMessage ? entityplayer.getBukkitEntity().displayName() : io.papermc.paper.adventure.PaperAdventure.asAdventure(entityplayer.getDisplayName())));
|
|
}
|
|
public net.kyori.adventure.text.Component remove(ServerPlayer entityplayer, net.kyori.adventure.text.Component leaveMessage) {
|
|
+ top.leavesmc.leaves.protocol.core.LeavesProtocolManager.handlePlayerLeave(entityplayer); // Leaves - protocol
|
|
// Paper end - Fix kick event leave message not being sent
|
|
org.purpurmc.purpur.task.BossBarTask.removeFromAll(entityplayer.getBukkitEntity()); // Purpur
|
|
ServerLevel worldserver = entityplayer.serverLevel();
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
|
|
index 3c3b98d13587ce21bffa14a79aed963a7ce1f176..db96be7e6f30ecab1349c1fe68fcdd2f4e27167d 100644
|
|
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
|
|
@@ -482,6 +482,7 @@ public final class CraftServer implements Server {
|
|
MapPalette.setMapColorCache(new CraftMapColorCache(this.logger));
|
|
}
|
|
datapackManager = new io.papermc.paper.datapack.PaperDatapackManager(console.getPackRepository()); // Paper
|
|
+ top.leavesmc.leaves.protocol.core.LeavesProtocolManager.init(); // Leaves - protocol
|
|
}
|
|
|
|
public boolean getCommandBlockOverride(String command) {
|
|
@@ -1107,6 +1108,7 @@ public final class CraftServer implements Server {
|
|
org.purpurmc.purpur.PurpurConfig.registerCommands(); // Purpur
|
|
this.overrideAllCommandBlockCommands = this.commandsConfiguration.getStringList("command-block-overrides").contains("*");
|
|
this.ignoreVanillaPermissions = this.commandsConfiguration.getBoolean("ignore-vanilla-permissions");
|
|
+ top.leavesmc.leaves.protocol.core.LeavesProtocolManager.handleServerReload(); // Leaves - protocol
|
|
|
|
int pollCount = 0;
|
|
|
|
diff --git a/src/main/java/top/leavesmc/leaves/protocol/core/LeavesProtocol.java b/src/main/java/top/leavesmc/leaves/protocol/core/LeavesProtocol.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..f884f54f5a1f0665c290e014f088aae857cecef8
|
|
--- /dev/null
|
|
+++ b/src/main/java/top/leavesmc/leaves/protocol/core/LeavesProtocol.java
|
|
@@ -0,0 +1,12 @@
|
|
+package top.leavesmc.leaves.protocol.core;
|
|
+
|
|
+import java.lang.annotation.ElementType;
|
|
+import java.lang.annotation.Retention;
|
|
+import java.lang.annotation.RetentionPolicy;
|
|
+import java.lang.annotation.Target;
|
|
+
|
|
+@Target(ElementType.TYPE)
|
|
+@Retention(RetentionPolicy.RUNTIME)
|
|
+public @interface LeavesProtocol {
|
|
+ String[] namespace();
|
|
+}
|
|
diff --git a/src/main/java/top/leavesmc/leaves/protocol/core/LeavesProtocolManager.java b/src/main/java/top/leavesmc/leaves/protocol/core/LeavesProtocolManager.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..740039a14fe915e7edd30273de628efd92512e4a
|
|
--- /dev/null
|
|
+++ b/src/main/java/top/leavesmc/leaves/protocol/core/LeavesProtocolManager.java
|
|
@@ -0,0 +1,361 @@
|
|
+package top.leavesmc.leaves.protocol.core;
|
|
+
|
|
+import net.minecraft.network.FriendlyByteBuf;
|
|
+import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket;
|
|
+import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
|
|
+import net.minecraft.resources.ResourceLocation;
|
|
+import net.minecraft.server.level.ServerPlayer;
|
|
+import org.apache.commons.lang.ArrayUtils;
|
|
+import org.jetbrains.annotations.NotNull;
|
|
+
|
|
+import org.bukkit.event.player.PlayerKickEvent;
|
|
+
|
|
+import java.io.File;
|
|
+import java.io.IOException;
|
|
+import java.lang.reflect.Constructor;
|
|
+import java.lang.reflect.InvocationTargetException;
|
|
+import java.lang.reflect.Method;
|
|
+import java.lang.reflect.Modifier;
|
|
+import java.net.JarURLConnection;
|
|
+import java.net.URL;
|
|
+import java.net.URLDecoder;
|
|
+import java.nio.charset.StandardCharsets;
|
|
+import java.util.ArrayList;
|
|
+import java.util.Arrays;
|
|
+import java.util.Collections;
|
|
+import java.util.Enumeration;
|
|
+import java.util.HashMap;
|
|
+import java.util.HashSet;
|
|
+import java.util.LinkedHashSet;
|
|
+import java.util.List;
|
|
+import java.util.Map;
|
|
+import java.util.Set;
|
|
+import java.util.jar.JarEntry;
|
|
+import java.util.jar.JarFile;
|
|
+
|
|
+public class LeavesProtocolManager {
|
|
+
|
|
+ private static final Map<LeavesProtocol, Map<ProtocolHandler.PayloadReceiver, Constructor<? extends CustomPacketPayload>>> KNOWN_TYPES = new HashMap<>();
|
|
+ private static final Map<LeavesProtocol, Map<ProtocolHandler.PayloadReceiver, Method>> KNOW_RECEIVERS = new HashMap<>();
|
|
+
|
|
+ private static final List<Method> TICKERS = new ArrayList<>();
|
|
+ private static final List<Method> PLAYER_JOIN = new ArrayList<>();
|
|
+ private static final List<Method> PLAYER_LEAVE = new ArrayList<>();
|
|
+ private static final List<Method> RELOAD_SERVER = new ArrayList<>();
|
|
+ private static final Map<LeavesProtocol, Map<ProtocolHandler.MinecraftRegister, Method>> MINECRAFT_REGISTER = new HashMap<>();
|
|
+
|
|
+ public static void init() {
|
|
+ for (Class<?> clazz : getClasses("top.leavesmc.leaves.protocol")) {
|
|
+ final LeavesProtocol protocol = clazz.getAnnotation(LeavesProtocol.class);
|
|
+ if (protocol != null) {
|
|
+ Set<Method> methods;
|
|
+ try {
|
|
+ Method[] publicMethods = clazz.getMethods();
|
|
+ Method[] privateMethods = clazz.getDeclaredMethods();
|
|
+ methods = new HashSet<>(publicMethods.length + privateMethods.length, 1.0f);
|
|
+ Collections.addAll(methods, publicMethods);
|
|
+ Collections.addAll(methods, privateMethods);
|
|
+ } catch (NoClassDefFoundError e) {
|
|
+ e.printStackTrace();
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ Map<ProtocolHandler.PayloadReceiver, Constructor<? extends CustomPacketPayload>> map = KNOWN_TYPES.getOrDefault(protocol, new HashMap<>());
|
|
+ for (final Method method : methods) {
|
|
+ if (method.isBridge() || method.isSynthetic() || !Modifier.isStatic(method.getModifiers())) {
|
|
+ continue;
|
|
+ }
|
|
+
|
|
+ method.setAccessible(true);
|
|
+
|
|
+ final ProtocolHandler.Init init = method.getAnnotation(ProtocolHandler.Init.class);
|
|
+ if (init != null) {
|
|
+ try {
|
|
+ method.invoke(null);
|
|
+ } catch (InvocationTargetException | IllegalAccessException e) {
|
|
+ e.printStackTrace();
|
|
+ }
|
|
+ continue;
|
|
+ }
|
|
+
|
|
+ final ProtocolHandler.PayloadReceiver receiver = method.getAnnotation(ProtocolHandler.PayloadReceiver.class);
|
|
+ if (receiver != null) {
|
|
+ try {
|
|
+ map.put(receiver, receiver.payload().getConstructor(ResourceLocation.class, FriendlyByteBuf.class));
|
|
+ } catch (NoSuchMethodException e) {
|
|
+ e.printStackTrace();
|
|
+ continue;
|
|
+ }
|
|
+
|
|
+ if (!KNOW_RECEIVERS.containsKey(protocol)) {
|
|
+ KNOW_RECEIVERS.put(protocol, new HashMap<>());
|
|
+ }
|
|
+
|
|
+ KNOW_RECEIVERS.get(protocol).put(receiver, method);
|
|
+ continue;
|
|
+ }
|
|
+
|
|
+ final ProtocolHandler.Ticker ticker = method.getAnnotation(ProtocolHandler.Ticker.class);
|
|
+ if (ticker != null) {
|
|
+ TICKERS.add(method);
|
|
+ continue;
|
|
+ }
|
|
+
|
|
+ final ProtocolHandler.PlayerJoin playerJoin = method.getAnnotation(ProtocolHandler.PlayerJoin.class);
|
|
+ if (playerJoin != null) {
|
|
+ PLAYER_JOIN.add(method);
|
|
+ continue;
|
|
+ }
|
|
+
|
|
+ final ProtocolHandler.PlayerLeave playerLeave = method.getAnnotation(ProtocolHandler.PlayerLeave.class);
|
|
+ if (playerLeave != null) {
|
|
+ PLAYER_LEAVE.add(method);
|
|
+ continue;
|
|
+ }
|
|
+
|
|
+ final ProtocolHandler.ReloadServer reloadServer = method.getAnnotation(ProtocolHandler.ReloadServer.class);
|
|
+ if (reloadServer != null) {
|
|
+ RELOAD_SERVER.add(method);
|
|
+ continue;
|
|
+ }
|
|
+
|
|
+ final ProtocolHandler.MinecraftRegister minecraftRegister = method.getAnnotation(ProtocolHandler.MinecraftRegister.class);
|
|
+ if (minecraftRegister != null) {
|
|
+ if (!MINECRAFT_REGISTER.containsKey(protocol)) {
|
|
+ MINECRAFT_REGISTER.put(protocol, new HashMap<>());
|
|
+ }
|
|
+
|
|
+ MINECRAFT_REGISTER.get(protocol).put(minecraftRegister, method);
|
|
+ }
|
|
+ }
|
|
+ KNOWN_TYPES.put(protocol, map);
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public static CustomPacketPayload getPayload(ResourceLocation id, FriendlyByteBuf buf) {
|
|
+ for (LeavesProtocol protocol : KNOWN_TYPES.keySet()) {
|
|
+ if (!ArrayUtils.contains(protocol.namespace(), id.getNamespace())) {
|
|
+ continue;
|
|
+ }
|
|
+
|
|
+ Map<ProtocolHandler.PayloadReceiver, Constructor<? extends CustomPacketPayload>> map = KNOWN_TYPES.get(protocol);
|
|
+ for (ProtocolHandler.PayloadReceiver receiver : map.keySet()) {
|
|
+ if (receiver.ignoreId() || ArrayUtils.contains(receiver.payloadId(), id.getPath())) {
|
|
+ try {
|
|
+ return map.get(receiver).newInstance(id, buf);
|
|
+ } catch (InvocationTargetException | InstantiationException | IllegalAccessException e) {
|
|
+ e.printStackTrace();
|
|
+ buf.readBytes(buf.readableBytes());
|
|
+ return new ErrorPayload(id, protocol.namespace(), receiver.payloadId());
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ return null;
|
|
+ }
|
|
+
|
|
+ public static void handlePayload(ServerPlayer player, CustomPacketPayload payload) {
|
|
+ if (payload instanceof ServerboundCustomPayloadPacket.UnknownPayload) {
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ if (payload instanceof ErrorPayload errorPayload) {
|
|
+ player.connection.disconnect("Payload " + Arrays.toString(errorPayload.packetID) + " from " + Arrays.toString(errorPayload.protocolID) + " error", PlayerKickEvent.Cause.INVALID_PAYLOAD);
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ for (LeavesProtocol protocol : KNOW_RECEIVERS.keySet()) {
|
|
+ if (!ArrayUtils.contains(protocol.namespace(), payload.id().getNamespace())) {
|
|
+ continue;
|
|
+ }
|
|
+
|
|
+ Map<ProtocolHandler.PayloadReceiver, Method> map = KNOW_RECEIVERS.get(protocol);
|
|
+ for (ProtocolHandler.PayloadReceiver receiver : map.keySet()) {
|
|
+ if (payload.getClass() == receiver.payload()) {
|
|
+ if (receiver.ignoreId() || ArrayUtils.contains(receiver.payloadId(), payload.id().getPath())) {
|
|
+ try {
|
|
+ map.get(receiver).invoke(null, player, payload);
|
|
+ } catch (InvocationTargetException | IllegalAccessException e) {
|
|
+ e.printStackTrace();
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public static void handleTick() {
|
|
+ if (!TICKERS.isEmpty()) {
|
|
+ try {
|
|
+ for (Method method : TICKERS) {
|
|
+ method.invoke(null);
|
|
+ }
|
|
+ } catch (InvocationTargetException | IllegalAccessException e) {
|
|
+ e.printStackTrace();
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public static void handlePlayerJoin(ServerPlayer player) {
|
|
+ if (!PLAYER_JOIN.isEmpty()) {
|
|
+ try {
|
|
+ for (Method method : PLAYER_JOIN) {
|
|
+ method.invoke(null, player);
|
|
+ }
|
|
+ } catch (InvocationTargetException | IllegalAccessException e) {
|
|
+ e.printStackTrace();
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public static void handlePlayerLeave(ServerPlayer player) {
|
|
+ if (!PLAYER_LEAVE.isEmpty()) {
|
|
+ try {
|
|
+ for (Method method : PLAYER_LEAVE) {
|
|
+ method.invoke(null, player);
|
|
+ }
|
|
+ } catch (InvocationTargetException | IllegalAccessException e) {
|
|
+ e.printStackTrace();
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public static void handleServerReload() {
|
|
+ if (!RELOAD_SERVER.isEmpty()) {
|
|
+ try {
|
|
+ for (Method method : RELOAD_SERVER) {
|
|
+ method.invoke(null);
|
|
+ }
|
|
+ } catch (InvocationTargetException | IllegalAccessException e) {
|
|
+ e.printStackTrace();
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public static void handleMinecraftRegister(String channelId, ServerPlayer player) {
|
|
+ for (LeavesProtocol protocol : MINECRAFT_REGISTER.keySet()) {
|
|
+ String[] channel = channelId.split(":");
|
|
+ if (!ArrayUtils.contains(protocol.namespace(), channel[0])) {
|
|
+ continue;
|
|
+ }
|
|
+
|
|
+ Map<ProtocolHandler.MinecraftRegister, Method> map = MINECRAFT_REGISTER.get(protocol);
|
|
+ for (ProtocolHandler.MinecraftRegister register : map.keySet()) {
|
|
+ if (register.ignoreId() || register.channelId().equals(channel[1]) ||
|
|
+ ArrayUtils.contains(register.channelIds(), channel[1])) {
|
|
+ try {
|
|
+ map.get(register).invoke(null, player);
|
|
+ } catch (InvocationTargetException | IllegalAccessException e) {
|
|
+ e.printStackTrace();
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public static Set<Class<?>> getClasses(String pack) {
|
|
+ Set<Class<?>> classes = new LinkedHashSet<Class<?>>();
|
|
+ String packageDirName = pack.replace('.', '/');
|
|
+ Enumeration<URL> dirs;
|
|
+ try {
|
|
+ dirs = Thread.currentThread().getContextClassLoader().getResources(packageDirName);
|
|
+ while (dirs.hasMoreElements()) {
|
|
+ URL url = dirs.nextElement();
|
|
+ String protocol = url.getProtocol();
|
|
+ if ("file".equals(protocol)) {
|
|
+ String filePath = URLDecoder.decode(url.getFile(), StandardCharsets.UTF_8);
|
|
+ findClassesInPackageByFile(pack, filePath, classes);
|
|
+ } else if ("jar".equals(protocol)) {
|
|
+ JarFile jar;
|
|
+ try {
|
|
+ jar = ((JarURLConnection) url.openConnection()).getJarFile();
|
|
+ Enumeration<JarEntry> entries = jar.entries();
|
|
+ findClassesInPackageByJar(pack, entries, packageDirName, classes);
|
|
+ } catch (IOException e) {
|
|
+ e.printStackTrace();
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ } catch (IOException e) {
|
|
+ e.printStackTrace();
|
|
+ }
|
|
+ return classes;
|
|
+ }
|
|
+
|
|
+ private static void findClassesInPackageByFile(String packageName, String packagePath, Set<Class<?>> classes) {
|
|
+ File dir = new File(packagePath);
|
|
+ if (!dir.exists() || !dir.isDirectory()) {
|
|
+ return;
|
|
+ }
|
|
+ File[] dirfiles = dir.listFiles((file) -> file.isDirectory() || file.getName().endsWith(".class"));
|
|
+ if (dirfiles != null) {
|
|
+ for (File file : dirfiles) {
|
|
+ if (file.isDirectory()) {
|
|
+ findClassesInPackageByFile(packageName + "." + file.getName(), file.getAbsolutePath(), classes);
|
|
+ } else {
|
|
+ String className = file.getName().substring(0, file.getName().length() - 6);
|
|
+ try {
|
|
+ classes.add(Class.forName(packageName + '.' + className));
|
|
+ } catch (ClassNotFoundException e) {
|
|
+ e.printStackTrace();
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+
|
|
+ private static void findClassesInPackageByJar(String packageName, Enumeration<JarEntry> entries, String packageDirName, Set<Class<?>> classes) {
|
|
+ while (entries.hasMoreElements()) {
|
|
+ JarEntry entry = entries.nextElement();
|
|
+ String name = entry.getName();
|
|
+ if (name.charAt(0) == '/') {
|
|
+ name = name.substring(1);
|
|
+ }
|
|
+ if (name.startsWith(packageDirName)) {
|
|
+ int idx = name.lastIndexOf('/');
|
|
+ if (idx != -1) {
|
|
+ packageName = name.substring(0, idx).replace('/', '.');
|
|
+ }
|
|
+ if (name.endsWith(".class") && !entry.isDirectory()) {
|
|
+ String className = name.substring(packageName.length() + 1, name.length() - 6);
|
|
+ try {
|
|
+ classes.add(Class.forName(packageName + '.' + className));
|
|
+ } catch (ClassNotFoundException e) {
|
|
+ e.printStackTrace();
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public record ErrorPayload(ResourceLocation id, String[] protocolID,
|
|
+ String[] packetID) implements CustomPacketPayload {
|
|
+ @Override
|
|
+ public void write(@NotNull FriendlyByteBuf buf) {
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public record EmptyPayload(ResourceLocation id) implements CustomPacketPayload {
|
|
+
|
|
+ public EmptyPayload(ResourceLocation location, FriendlyByteBuf buf) {
|
|
+ this(location);
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public void write(@NotNull FriendlyByteBuf buf) {
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public record LeavesPayload(FriendlyByteBuf data, ResourceLocation id) implements CustomPacketPayload {
|
|
+
|
|
+ public LeavesPayload(ResourceLocation location, FriendlyByteBuf buf) {
|
|
+ this(new FriendlyByteBuf(buf.readBytes(buf.readableBytes())), location);
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public void write(FriendlyByteBuf buf) {
|
|
+ buf.writeBytes(data);
|
|
+ }
|
|
+ }
|
|
+}
|
|
diff --git a/src/main/java/top/leavesmc/leaves/protocol/core/ProtocolHandler.java b/src/main/java/top/leavesmc/leaves/protocol/core/ProtocolHandler.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..92ad6e9b1c0d9640b80c1ebe739c613d989eec21
|
|
--- /dev/null
|
|
+++ b/src/main/java/top/leavesmc/leaves/protocol/core/ProtocolHandler.java
|
|
@@ -0,0 +1,63 @@
|
|
+package top.leavesmc.leaves.protocol.core;
|
|
+
|
|
+import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
|
|
+
|
|
+import java.lang.annotation.ElementType;
|
|
+import java.lang.annotation.Retention;
|
|
+import java.lang.annotation.RetentionPolicy;
|
|
+import java.lang.annotation.Target;
|
|
+
|
|
+public class ProtocolHandler {
|
|
+
|
|
+ @Target(ElementType.METHOD)
|
|
+ @Retention(RetentionPolicy.RUNTIME)
|
|
+ public @interface Init {
|
|
+
|
|
+ }
|
|
+
|
|
+ @Target(ElementType.METHOD)
|
|
+ @Retention(RetentionPolicy.RUNTIME)
|
|
+ public @interface PayloadReceiver {
|
|
+
|
|
+ Class<? extends CustomPacketPayload> payload();
|
|
+
|
|
+ String[] payloadId();
|
|
+
|
|
+ boolean ignoreId() default false;
|
|
+ }
|
|
+
|
|
+ @Target(ElementType.METHOD)
|
|
+ @Retention(RetentionPolicy.RUNTIME)
|
|
+ public @interface Ticker {
|
|
+ int delay() default 0;
|
|
+ }
|
|
+
|
|
+ @Target(ElementType.METHOD)
|
|
+ @Retention(RetentionPolicy.RUNTIME)
|
|
+ public @interface PlayerJoin {
|
|
+
|
|
+ }
|
|
+
|
|
+ @Target(ElementType.METHOD)
|
|
+ @Retention(RetentionPolicy.RUNTIME)
|
|
+ public @interface PlayerLeave {
|
|
+
|
|
+ }
|
|
+
|
|
+ @Target(ElementType.METHOD)
|
|
+ @Retention(RetentionPolicy.RUNTIME)
|
|
+ public @interface ReloadServer {
|
|
+
|
|
+ }
|
|
+
|
|
+ @Target(ElementType.METHOD)
|
|
+ @Retention(RetentionPolicy.RUNTIME)
|
|
+ public @interface MinecraftRegister {
|
|
+
|
|
+ String channelId() default "";
|
|
+
|
|
+ String[] channelIds() default {};
|
|
+
|
|
+ boolean ignoreId() default false;
|
|
+ }
|
|
+}
|
|
diff --git a/src/main/java/top/leavesmc/leaves/protocol/core/ProtocolUtils.java b/src/main/java/top/leavesmc/leaves/protocol/core/ProtocolUtils.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..5282c5ad3d26d06ab685ddaaf6fd9a4d49559717
|
|
--- /dev/null
|
|
+++ b/src/main/java/top/leavesmc/leaves/protocol/core/ProtocolUtils.java
|
|
@@ -0,0 +1,36 @@
|
|
+package top.leavesmc.leaves.protocol.core;
|
|
+
|
|
+import net.minecraft.network.FriendlyByteBuf;
|
|
+import net.minecraft.network.protocol.common.ClientboundCustomPayloadPacket;
|
|
+import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
|
|
+import net.minecraft.resources.ResourceLocation;
|
|
+import net.minecraft.server.level.ServerPlayer;
|
|
+import org.jetbrains.annotations.NotNull;
|
|
+
|
|
+import java.util.function.Consumer;
|
|
+
|
|
+public class ProtocolUtils {
|
|
+
|
|
+ public static void sendEmptyPayloadPacket(ServerPlayer player, ResourceLocation id) {
|
|
+ player.connection.send(new ClientboundCustomPayloadPacket(new LeavesProtocolManager.EmptyPayload(id)));
|
|
+ }
|
|
+
|
|
+ public static void sendPayloadPacket(ServerPlayer player, ResourceLocation id, Consumer<FriendlyByteBuf> consumer) {
|
|
+ player.connection.send(new ClientboundCustomPayloadPacket(new CustomPacketPayload() {
|
|
+ @Override
|
|
+ public void write(@NotNull FriendlyByteBuf buf) {
|
|
+ consumer.accept(buf);
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ @NotNull
|
|
+ public ResourceLocation id() {
|
|
+ return id;
|
|
+ }
|
|
+ }));
|
|
+ }
|
|
+
|
|
+ public static void sendPayloadPacket(ServerPlayer player, CustomPacketPayload payload) {
|
|
+ player.connection.send(new ClientboundCustomPayloadPacket(payload));
|
|
+ }
|
|
+}
|