mirror of
https://github.com/Winds-Studio/Leaf.git
synced 2025-12-19 15:09:25 +00:00
608 lines
30 KiB
Diff
608 lines
30 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com>
|
|
Date: Sat, 13 Jul 2024 21:23:12 +0800
|
|
Subject: [PATCH] Optimize LeavesProtocolManager init protocol
|
|
|
|
In original LeavesProtocolManager, it will init for all protocol support modules even they are disabled.
|
|
And the "protocol support event" will be fired in every tick and when player joined to do the module enable check
|
|
|
|
It is no necessary to check whether enable every tick..., so I changed the init part, it will only load enabled
|
|
modules and will do init again when server reload or config reload
|
|
|
|
diff --git a/src/main/java/org/leavesmc/leaves/protocol/AppleSkinProtocol.java b/src/main/java/org/leavesmc/leaves/protocol/AppleSkinProtocol.java
|
|
index c496c97c99cd352c2566731d3017cf1b14ee74ec..9b54e24bd094465625dca12f6ac5724f51775adb 100644
|
|
--- a/src/main/java/org/leavesmc/leaves/protocol/AppleSkinProtocol.java
|
|
+++ b/src/main/java/org/leavesmc/leaves/protocol/AppleSkinProtocol.java
|
|
@@ -31,6 +31,10 @@ public class AppleSkinProtocol {
|
|
|
|
private static final Set<ServerPlayer> players = new HashSet<>();
|
|
|
|
+ public static boolean shouldEnable() {
|
|
+ return org.dreeam.leaf.config.modules.network.ProtocolSupport.appleskinProtocol;
|
|
+ }
|
|
+
|
|
@Contract("_ -> new")
|
|
public static @NotNull ResourceLocation id(String path) {
|
|
return new ResourceLocation(PROTOCOL_ID, path);
|
|
@@ -38,49 +42,41 @@ public class AppleSkinProtocol {
|
|
|
|
@ProtocolHandler.PlayerJoin
|
|
public static void onPlayerLoggedIn(@NotNull ServerPlayer player) {
|
|
- if (org.dreeam.leaf.config.modules.network.ProtocolSupport.appleskinProtocol) {
|
|
- resetPlayerData(player);
|
|
- }
|
|
+ resetPlayerData(player);
|
|
}
|
|
|
|
@ProtocolHandler.PlayerLeave
|
|
public static void onPlayerLoggedOut(@NotNull ServerPlayer player) {
|
|
- if (org.dreeam.leaf.config.modules.network.ProtocolSupport.appleskinProtocol) {
|
|
- players.remove(player);
|
|
- resetPlayerData(player);
|
|
- }
|
|
+ players.remove(player);
|
|
+ resetPlayerData(player);
|
|
}
|
|
|
|
@ProtocolHandler.MinecraftRegister(ignoreId = true)
|
|
public static void onPlayerSubscribed(@NotNull ServerPlayer player) {
|
|
- if (org.dreeam.leaf.config.modules.network.ProtocolSupport.appleskinProtocol) {
|
|
- players.add(player);
|
|
- }
|
|
+ players.add(player);
|
|
}
|
|
|
|
@ProtocolHandler.Ticker
|
|
public static void tick() {
|
|
- if (org.dreeam.leaf.config.modules.network.ProtocolSupport.appleskinProtocol) {
|
|
- for (ServerPlayer player : players) {
|
|
- FoodData data = player.getFoodData();
|
|
-
|
|
- float saturation = data.getSaturationLevel();
|
|
- Float previousSaturation = previousSaturationLevels.get(player.getUUID());
|
|
- if (previousSaturation == null || saturation != previousSaturation) {
|
|
- ProtocolUtils.sendPayloadPacket(player, SATURATION_KEY, buf -> {
|
|
- buf.writeFloat(saturation);
|
|
- });
|
|
- previousSaturationLevels.put(player.getUUID(), saturation);
|
|
- }
|
|
-
|
|
- float exhaustion = data.getExhaustionLevel();
|
|
- Float previousExhaustion = previousExhaustionLevels.get(player.getUUID());
|
|
- if (previousExhaustion == null || Math.abs(exhaustion - previousExhaustion) >= MINIMUM_EXHAUSTION_CHANGE_THRESHOLD) {
|
|
- ProtocolUtils.sendPayloadPacket(player, EXHAUSTION_KEY, buf -> {
|
|
- buf.writeFloat(exhaustion);
|
|
- });
|
|
- previousExhaustionLevels.put(player.getUUID(), exhaustion);
|
|
- }
|
|
+ for (ServerPlayer player : players) {
|
|
+ FoodData data = player.getFoodData();
|
|
+
|
|
+ float saturation = data.getSaturationLevel();
|
|
+ Float previousSaturation = previousSaturationLevels.get(player.getUUID());
|
|
+ if (previousSaturation == null || saturation != previousSaturation) {
|
|
+ ProtocolUtils.sendPayloadPacket(player, SATURATION_KEY, buf -> {
|
|
+ buf.writeFloat(saturation);
|
|
+ });
|
|
+ previousSaturationLevels.put(player.getUUID(), saturation);
|
|
+ }
|
|
+
|
|
+ float exhaustion = data.getExhaustionLevel();
|
|
+ Float previousExhaustion = previousExhaustionLevels.get(player.getUUID());
|
|
+ if (previousExhaustion == null || Math.abs(exhaustion - previousExhaustion) >= MINIMUM_EXHAUSTION_CHANGE_THRESHOLD) {
|
|
+ ProtocolUtils.sendPayloadPacket(player, EXHAUSTION_KEY, buf -> {
|
|
+ buf.writeFloat(exhaustion);
|
|
+ });
|
|
+ previousExhaustionLevels.put(player.getUUID(), exhaustion);
|
|
}
|
|
}
|
|
}
|
|
diff --git a/src/main/java/org/leavesmc/leaves/protocol/AsteorBarProtocol.java b/src/main/java/org/leavesmc/leaves/protocol/AsteorBarProtocol.java
|
|
index ed8d9888a24d3ae6cf8fe2f8b269554102e451df..1bdd77078c7345db1a675fbdc26b37e96bf468b0 100644
|
|
--- a/src/main/java/org/leavesmc/leaves/protocol/AsteorBarProtocol.java
|
|
+++ b/src/main/java/org/leavesmc/leaves/protocol/AsteorBarProtocol.java
|
|
@@ -30,6 +30,10 @@ public class AsteorBarProtocol {
|
|
|
|
private static final Set<ServerPlayer> players = new HashSet<>();
|
|
|
|
+ public static boolean shouldEnable() {
|
|
+ return org.dreeam.leaf.config.modules.network.ProtocolSupport.asteorBarProtocol;
|
|
+ }
|
|
+
|
|
@Contract("_ -> new")
|
|
public static @NotNull ResourceLocation id(String path) {
|
|
return ResourceLocation.fromNamespaceAndPath(PROTOCOL_ID, path);
|
|
@@ -37,51 +41,43 @@ public class AsteorBarProtocol {
|
|
|
|
@ProtocolHandler.PlayerJoin
|
|
public static void onPlayerLoggedIn(@NotNull ServerPlayer player) {
|
|
- if (org.dreeam.leaf.config.modules.network.ProtocolSupport.asteorBarProtocol) {
|
|
- resetPlayerData(player);
|
|
- }
|
|
+ resetPlayerData(player);
|
|
}
|
|
|
|
@ProtocolHandler.PlayerLeave
|
|
public static void onPlayerLoggedOut(@NotNull ServerPlayer player) {
|
|
- if (org.dreeam.leaf.config.modules.network.ProtocolSupport.asteorBarProtocol) {
|
|
- players.remove(player);
|
|
- resetPlayerData(player);
|
|
- }
|
|
+ players.remove(player);
|
|
+ resetPlayerData(player);
|
|
}
|
|
|
|
@ProtocolHandler.MinecraftRegister(ignoreId = true)
|
|
public static void onPlayerSubscribed(@NotNull ServerPlayer player) {
|
|
- if (org.dreeam.leaf.config.modules.network.ProtocolSupport.asteorBarProtocol) {
|
|
- players.add(player);
|
|
- }
|
|
+ players.add(player);
|
|
}
|
|
|
|
@ProtocolHandler.Ticker
|
|
public static void tick() {
|
|
- if (org.dreeam.leaf.config.modules.network.ProtocolSupport.asteorBarProtocol) {
|
|
- for (ServerPlayer player : players) {
|
|
- FoodData data = player.getFoodData();
|
|
-
|
|
- float saturation = data.getSaturationLevel();
|
|
- Float previousSaturation = previousSaturationLevels.get(player.getUUID());
|
|
- if (previousSaturation == null || saturation != previousSaturation) {
|
|
- ProtocolUtils.sendPayloadPacket(player, NETWORK_KEY, buf -> {
|
|
- buf.writeByte(1);
|
|
- buf.writeFloat(saturation);
|
|
- });
|
|
- previousSaturationLevels.put(player.getUUID(), saturation);
|
|
- }
|
|
-
|
|
- float exhaustion = data.getExhaustionLevel();
|
|
- Float previousExhaustion = previousExhaustionLevels.get(player.getUUID());
|
|
- if (previousExhaustion == null || Math.abs(exhaustion - previousExhaustion) >= THRESHOLD) {
|
|
- ProtocolUtils.sendPayloadPacket(player, NETWORK_KEY, buf -> {
|
|
- buf.writeByte(0);
|
|
- buf.writeFloat(exhaustion);
|
|
- });
|
|
- previousExhaustionLevels.put(player.getUUID(), exhaustion);
|
|
- }
|
|
+ for (ServerPlayer player : players) {
|
|
+ FoodData data = player.getFoodData();
|
|
+
|
|
+ float saturation = data.getSaturationLevel();
|
|
+ Float previousSaturation = previousSaturationLevels.get(player.getUUID());
|
|
+ if (previousSaturation == null || saturation != previousSaturation) {
|
|
+ ProtocolUtils.sendPayloadPacket(player, NETWORK_KEY, buf -> {
|
|
+ buf.writeByte(1);
|
|
+ buf.writeFloat(saturation);
|
|
+ });
|
|
+ previousSaturationLevels.put(player.getUUID(), saturation);
|
|
+ }
|
|
+
|
|
+ float exhaustion = data.getExhaustionLevel();
|
|
+ Float previousExhaustion = previousExhaustionLevels.get(player.getUUID());
|
|
+ if (previousExhaustion == null || Math.abs(exhaustion - previousExhaustion) >= THRESHOLD) {
|
|
+ ProtocolUtils.sendPayloadPacket(player, NETWORK_KEY, buf -> {
|
|
+ buf.writeByte(0);
|
|
+ buf.writeFloat(exhaustion);
|
|
+ });
|
|
+ previousExhaustionLevels.put(player.getUUID(), exhaustion);
|
|
}
|
|
}
|
|
}
|
|
diff --git a/src/main/java/org/leavesmc/leaves/protocol/ChatImageProtocol.java b/src/main/java/org/leavesmc/leaves/protocol/ChatImageProtocol.java
|
|
index 87ffe8a81a8bab7d20ff9551b105487d47616ee1..b026b9fefd85d16b0294738034bfae1220d47eac 100644
|
|
--- a/src/main/java/org/leavesmc/leaves/protocol/ChatImageProtocol.java
|
|
+++ b/src/main/java/org/leavesmc/leaves/protocol/ChatImageProtocol.java
|
|
@@ -7,7 +7,6 @@ import net.minecraft.network.protocol.Packet;
|
|
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
|
|
import net.minecraft.resources.ResourceLocation;
|
|
import net.minecraft.server.level.ServerPlayer;
|
|
-import org.dreeam.leaf.config.modules.network.ProtocolSupport;
|
|
import org.jetbrains.annotations.Contract;
|
|
import org.jetbrains.annotations.NotNull;
|
|
import org.leavesmc.leaves.protocol.chatimage.ChatImageIndex;
|
|
@@ -30,7 +29,12 @@ public class ChatImageProtocol {
|
|
public static int MAX_STRING = 532767;
|
|
private static final Gson gson = new Gson();
|
|
|
|
- public record FileInfoChannelPacket(String message) implements LeavesCustomPayload<LeavesProtocolManager.LeavesPayload> {
|
|
+ public static boolean shouldEnable() {
|
|
+ return org.dreeam.leaf.config.modules.network.ProtocolSupport.chatImageProtocol;
|
|
+ }
|
|
+
|
|
+ public record FileInfoChannelPacket(
|
|
+ String message) implements LeavesCustomPayload<LeavesProtocolManager.LeavesPayload> {
|
|
private static final ResourceLocation FILE_INFO = ChatImageProtocol.id("file_info");
|
|
|
|
@New
|
|
@@ -49,7 +53,8 @@ public class ChatImageProtocol {
|
|
}
|
|
}
|
|
|
|
- public record DownloadFileChannelPacket(String message) implements LeavesCustomPayload<LeavesProtocolManager.LeavesPayload> {
|
|
+ public record DownloadFileChannelPacket(
|
|
+ String message) implements LeavesCustomPayload<LeavesProtocolManager.LeavesPayload> {
|
|
/**
|
|
* 发送文件分块到客户端通道(Map)
|
|
*/
|
|
@@ -72,7 +77,8 @@ public class ChatImageProtocol {
|
|
|
|
}
|
|
|
|
- public record FileChannelPacket(String message) implements LeavesCustomPayload<LeavesProtocolManager.LeavesPayload> {
|
|
+ public record FileChannelPacket(
|
|
+ String message) implements LeavesCustomPayload<LeavesProtocolManager.LeavesPayload> {
|
|
/**
|
|
* 客户端发送文件分块到服务器通道(Map)
|
|
*/
|
|
@@ -97,7 +103,6 @@ public class ChatImageProtocol {
|
|
|
|
@ProtocolHandler.PayloadReceiver(payload = FileChannelPacket.class, payloadId = "file_channel")
|
|
public static void serverFileChannelReceived(ServerPlayer player, String res) {
|
|
- if (!ProtocolSupport.chatImageProtocol) return;
|
|
ChatImageIndex title = gson.fromJson(res, ChatImageIndex.class);
|
|
HashMap<Integer, String> blocks = SERVER_BLOCK_CACHE.containsKey(title.url) ? SERVER_BLOCK_CACHE.get(title.url) : new HashMap<>();
|
|
blocks.put(title.index, res);
|
|
@@ -123,7 +128,6 @@ public class ChatImageProtocol {
|
|
|
|
@ProtocolHandler.PayloadReceiver(payload = FileInfoChannelPacket.class, payloadId = "file_info")
|
|
public static void serverFileInfoChannelReceived(ServerPlayer player, String url) {
|
|
- if (!ProtocolSupport.chatImageProtocol) return;
|
|
if (SERVER_BLOCK_CACHE.containsKey(url) && FILE_COUNT_MAP.containsKey(url)) {
|
|
HashMap<Integer, String> list = SERVER_BLOCK_CACHE.get(url);
|
|
Integer total = FILE_COUNT_MAP.get(url);
|
|
diff --git a/src/main/java/org/leavesmc/leaves/protocol/XaeroMapProtocol.java b/src/main/java/org/leavesmc/leaves/protocol/XaeroMapProtocol.java
|
|
index 9e35dfaf8bb5511b4cd0a71175d7ecb6d835042f..5ef19098512ae8a070dea270a68c27695c34624b 100644
|
|
--- a/src/main/java/org/leavesmc/leaves/protocol/XaeroMapProtocol.java
|
|
+++ b/src/main/java/org/leavesmc/leaves/protocol/XaeroMapProtocol.java
|
|
@@ -16,6 +16,10 @@ public class XaeroMapProtocol {
|
|
private static final ResourceLocation MINIMAP_KEY = idMini("main");
|
|
private static final ResourceLocation WORLDMAP_KEY = idWorld("main");
|
|
|
|
+ public static boolean shouldEnable() {
|
|
+ return org.dreeam.leaf.config.modules.network.ProtocolSupport.xaeroMapProtocol;
|
|
+ }
|
|
+
|
|
@Contract("_ -> new")
|
|
public static @NotNull ResourceLocation idMini(String path) {
|
|
return new ResourceLocation(PROTOCOL_ID_MINI, path);
|
|
@@ -27,7 +31,7 @@ public class XaeroMapProtocol {
|
|
}
|
|
|
|
public static void onSendWorldInfo(@NotNull ServerPlayer player) {
|
|
- if (org.dreeam.leaf.config.modules.network.ProtocolSupport.xaeroMapProtocol) {
|
|
+ if (shouldEnable()) {
|
|
ProtocolUtils.sendPayloadPacket(player, MINIMAP_KEY, buf -> {
|
|
buf.writeByte(0);
|
|
buf.writeInt(org.dreeam.leaf.config.modules.network.ProtocolSupport.xaeroMapServerID);
|
|
diff --git a/src/main/java/org/leavesmc/leaves/protocol/core/LeavesProtocolManager.java b/src/main/java/org/leavesmc/leaves/protocol/core/LeavesProtocolManager.java
|
|
index b76eb38942171d22dcd767ea353f012e5920f1f5..ec901ea7103ce5a3e1d6fa1efd8135ce020e18ce 100644
|
|
--- a/src/main/java/org/leavesmc/leaves/protocol/core/LeavesProtocolManager.java
|
|
+++ b/src/main/java/org/leavesmc/leaves/protocol/core/LeavesProtocolManager.java
|
|
@@ -10,30 +10,21 @@ import org.bukkit.event.player.PlayerKickEvent;
|
|
import org.jetbrains.annotations.NotNull;
|
|
import org.leavesmc.leaves.LeavesLogger;
|
|
|
|
-import java.io.File;
|
|
-import java.io.IOException;
|
|
import java.lang.reflect.Constructor;
|
|
import java.lang.reflect.Executable;
|
|
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.Iterator;
|
|
-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 {
|
|
|
|
@@ -51,8 +42,16 @@ public class LeavesProtocolManager {
|
|
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 reload() {
|
|
+ handleServerReload();
|
|
+ cleanProtocols(); // Do cleanup
|
|
+ init();
|
|
+ }
|
|
+
|
|
public static void init() {
|
|
- for (Class<?> clazz : getClasses("org.leavesmc.leaves.protocol")) {
|
|
+ boolean shouldEnable;
|
|
+
|
|
+ for (Class<?> clazz : org.dreeam.leaf.config.LeafConfig.getClasses("org.leavesmc.leaves.protocol")) {
|
|
final LeavesProtocol protocol = clazz.getAnnotation(LeavesProtocol.class);
|
|
if (protocol != null) {
|
|
Set<Method> methods;
|
|
@@ -62,7 +61,12 @@ public class LeavesProtocolManager {
|
|
methods = new HashSet<>(publicMethods.length + privateMethods.length, 1.0f);
|
|
Collections.addAll(methods, publicMethods);
|
|
Collections.addAll(methods, privateMethods);
|
|
- } catch (NoClassDefFoundError error) {
|
|
+
|
|
+ Object instance = clazz.getConstructor().newInstance();
|
|
+ Method method = clazz.getMethod("shouldEnable");
|
|
+ shouldEnable = (boolean) method.invoke(instance);
|
|
+ } catch (NoClassDefFoundError | InvocationTargetException | InstantiationException |
|
|
+ IllegalAccessException | NoSuchMethodException error) {
|
|
LOGGER.severe("Failed to load class " + clazz.getName() + " due to missing dependencies, " + error.getCause() + ": " + error.getMessage());
|
|
return;
|
|
}
|
|
@@ -75,6 +79,16 @@ public class LeavesProtocolManager {
|
|
|
|
method.setAccessible(true);
|
|
|
|
+ final ProtocolHandler.ReloadServer reloadServer = method.getAnnotation(ProtocolHandler.ReloadServer.class);
|
|
+ if (reloadServer != null) {
|
|
+ RELOAD_SERVER.add(method);
|
|
+ continue;
|
|
+ }
|
|
+
|
|
+ if (!shouldEnable) {
|
|
+ continue;
|
|
+ }
|
|
+
|
|
final ProtocolHandler.Init init = method.getAnnotation(ProtocolHandler.Init.class);
|
|
if (init != null) {
|
|
try {
|
|
@@ -140,12 +154,6 @@ public class LeavesProtocolManager {
|
|
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)) {
|
|
@@ -174,6 +182,17 @@ public class LeavesProtocolManager {
|
|
ALL_KNOWN_ID = ImmutableSet.copyOf(ALL_KNOWN_ID);
|
|
}
|
|
|
|
+ private static void cleanProtocols() {
|
|
+ KNOWN_TYPES.clear();
|
|
+ KNOW_RECEIVERS.clear();
|
|
+ //ALL_KNOWN_ID.clear(); // No need
|
|
+ TICKERS.clear();
|
|
+ PLAYER_JOIN.clear();
|
|
+ PLAYER_LEAVE.clear();
|
|
+ //RELOAD_SERVER.clear(); // No need
|
|
+ MINECRAFT_REGISTER.clear();
|
|
+ }
|
|
+
|
|
public static LeavesCustomPayload<?> decode(ResourceLocation id, FriendlyByteBuf buf) {
|
|
for (LeavesProtocol protocol : KNOWN_TYPES.keySet()) {
|
|
if (!ArrayUtils.contains(protocol.namespace(), id.getNamespace())) {
|
|
@@ -297,81 +316,6 @@ public class LeavesProtocolManager {
|
|
}
|
|
}
|
|
|
|
- public static Set<Class<?>> getClasses(String pack) {
|
|
- Set<Class<?>> classes = new LinkedHashSet<>();
|
|
- 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 exception) {
|
|
- LOGGER.warning("Failed to load jar file, " + exception.getCause() + ": " + exception.getMessage());
|
|
- }
|
|
- }
|
|
- }
|
|
- } catch (IOException exception) {
|
|
- LOGGER.warning("Failed to load classes, " + exception.getCause() + ": " + exception.getMessage());
|
|
- }
|
|
- 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 exception) {
|
|
- LOGGER.warning("Failed to load class " + className + ", " + exception.getCause() + ": " + exception.getMessage());
|
|
- }
|
|
- }
|
|
- }
|
|
- }
|
|
- }
|
|
-
|
|
- 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 exception) {
|
|
- LOGGER.warning("Failed to load class " + className + ", " + exception.getCause() + ": " + exception.getMessage());
|
|
- }
|
|
- }
|
|
- }
|
|
- }
|
|
- }
|
|
-
|
|
public record ErrorPayload(ResourceLocation id, String[] protocolID, String[] packetID) implements LeavesCustomPayload<ErrorPayload> {
|
|
@Override
|
|
public void write(@NotNull FriendlyByteBuf buf) {
|
|
diff --git a/src/main/java/org/leavesmc/leaves/protocol/jade/JadeProtocol.java b/src/main/java/org/leavesmc/leaves/protocol/jade/JadeProtocol.java
|
|
index cc9f71e2bc8261f07fdb4d296cc8d13cfa2753ad..a687c38577919bbb20a0f1b15255aec3c07fa3be 100644
|
|
--- a/src/main/java/org/leavesmc/leaves/protocol/jade/JadeProtocol.java
|
|
+++ b/src/main/java/org/leavesmc/leaves/protocol/jade/JadeProtocol.java
|
|
@@ -103,6 +103,10 @@ public class JadeProtocol {
|
|
|
|
public static final WrappedHierarchyLookup<IServerExtensionProvider<ItemStack>> itemStorageProviders = new WrappedHierarchyLookup<>();
|
|
|
|
+ public static boolean shouldEnable() {
|
|
+ return org.dreeam.leaf.config.modules.network.ProtocolSupport.jadeProtocol;
|
|
+ }
|
|
+
|
|
@Contract("_ -> new")
|
|
public static @NotNull ResourceLocation id(String path) {
|
|
return new ResourceLocation(PROTOCOL_ID, path);
|
|
@@ -126,9 +130,9 @@ public class JadeProtocol {
|
|
priorities = new PriorityStore<>(IJadeProvider::getDefaultPriority, IJadeProvider::getUid);
|
|
priorities.setSortingFunction((store, allKeys) -> {
|
|
List<ResourceLocation> keys = allKeys.stream()
|
|
- .filter(JadeProtocol::isPrimaryKey)
|
|
- .sorted(Comparator.comparingInt(store::byKey))
|
|
- .collect(Collectors.toCollection(ArrayList::new));
|
|
+ .filter(JadeProtocol::isPrimaryKey)
|
|
+ .sorted(Comparator.comparingInt(store::byKey))
|
|
+ .collect(Collectors.toCollection(ArrayList::new));
|
|
allKeys.stream().filter(Predicate.not(JadeProtocol::isPrimaryKey)).forEach($ -> {
|
|
int index = keys.indexOf(JadeProtocol.getPrimaryKey($));
|
|
keys.add(index + 1, $);
|
|
@@ -187,17 +191,11 @@ public class JadeProtocol {
|
|
|
|
@ProtocolHandler.PlayerJoin
|
|
public static void onPlayerJoin(ServerPlayer player) {
|
|
- if (org.dreeam.leaf.config.modules.network.ProtocolSupport.jadeProtocol) {
|
|
- ProtocolUtils.sendPayloadPacket(player, new ServerPingPayload("", shearableBlocks));
|
|
- }
|
|
+ ProtocolUtils.sendPayloadPacket(player, new ServerPingPayload("", shearableBlocks));
|
|
}
|
|
|
|
@ProtocolHandler.PayloadReceiver(payload = RequestEntityPayload.class, payloadId = "request_entity")
|
|
public static void requestEntityData(ServerPlayer player, RequestEntityPayload payload) {
|
|
- if (!org.dreeam.leaf.config.modules.network.ProtocolSupport.jadeProtocol) {
|
|
- return;
|
|
- }
|
|
-
|
|
MinecraftServer server = MinecraftServer.getServer();
|
|
server.execute(() -> {
|
|
Level world = player.level();
|
|
@@ -238,10 +236,6 @@ public class JadeProtocol {
|
|
|
|
@ProtocolHandler.PayloadReceiver(payload = RequestBlockPayload.class, payloadId = "request_block")
|
|
public static void requestBlockData(ServerPlayer player, RequestBlockPayload payload) {
|
|
- if (!org.dreeam.leaf.config.modules.network.ProtocolSupport.jadeProtocol) {
|
|
- return;
|
|
- }
|
|
-
|
|
MinecraftServer server = MinecraftServer.getServer();
|
|
server.execute(() -> {
|
|
Level world = player.level();
|
|
@@ -303,7 +297,8 @@ public class JadeProtocol {
|
|
}
|
|
}
|
|
|
|
- public record RequestEntityPayload(boolean showDetails, int entityId, int partIndex, Vec3 hitVec) implements LeavesCustomPayload<RequestEntityPayload> {
|
|
+ public record RequestEntityPayload(boolean showDetails, int entityId, int partIndex,
|
|
+ Vec3 hitVec) implements LeavesCustomPayload<RequestEntityPayload> {
|
|
|
|
private static final ResourceLocation PACKET_REQUEST_ENTITY = JadeProtocol.id("request_entity");
|
|
|
|
@@ -327,7 +322,8 @@ public class JadeProtocol {
|
|
}
|
|
}
|
|
|
|
- public record RequestBlockPayload(boolean showDetails, BlockHitResult hitResult, BlockState blockState, ItemStack fakeBlock) implements LeavesCustomPayload<RequestBlockPayload> {
|
|
+ public record RequestBlockPayload(boolean showDetails, BlockHitResult hitResult, BlockState blockState,
|
|
+ ItemStack fakeBlock) implements LeavesCustomPayload<RequestBlockPayload> {
|
|
|
|
private static final ResourceLocation PACKET_REQUEST_BLOCK = JadeProtocol.id("request_block");
|
|
private static final StreamCodec<RegistryFriendlyByteBuf, ItemStack> ITEM_STACK_CODEC = ItemStack.OPTIONAL_STREAM_CODEC;
|
|
@@ -353,7 +349,8 @@ public class JadeProtocol {
|
|
}
|
|
}
|
|
|
|
- public record ServerPingPayload(String serverConfig, List<Block> shearableBlocks) implements LeavesCustomPayload<ServerPingPayload> {
|
|
+ public record ServerPingPayload(String serverConfig,
|
|
+ List<Block> shearableBlocks) implements LeavesCustomPayload<ServerPingPayload> {
|
|
|
|
private static final ResourceLocation PACKET_SERVER_PING = JadeProtocol.id("server_ping_v1");
|
|
private static final StreamCodec<RegistryFriendlyByteBuf, List<Block>> SHEARABLE_BLOCKS_CODEC = ByteBufCodecs.registry(Registries.BLOCK).apply(ByteBufCodecs.list());
|
|
diff --git a/src/main/java/org/leavesmc/leaves/protocol/syncmatica/CommunicationManager.java b/src/main/java/org/leavesmc/leaves/protocol/syncmatica/CommunicationManager.java
|
|
index 0704ac7825c69e69097b3e7c77763044f9fa9e1e..c039765237d56def91a1e630a0510062305fd585 100644
|
|
--- a/src/main/java/org/leavesmc/leaves/protocol/syncmatica/CommunicationManager.java
|
|
+++ b/src/main/java/org/leavesmc/leaves/protocol/syncmatica/CommunicationManager.java
|
|
@@ -48,6 +48,10 @@ public class CommunicationManager {
|
|
public CommunicationManager() {
|
|
}
|
|
|
|
+ public static boolean shouldEnable() {
|
|
+ return org.dreeam.leaf.config.modules.network.ProtocolSupport.syncmaticaProtocol;
|
|
+ }
|
|
+
|
|
public static GameProfile getGameProfile(final ExchangeTarget exchangeTarget) {
|
|
return playerMap.get(exchangeTarget).getGameProfile();
|
|
}
|
|
@@ -66,9 +70,6 @@ public class CommunicationManager {
|
|
|
|
@ProtocolHandler.PlayerJoin
|
|
public static void onPlayerJoin(ServerPlayer player) {
|
|
- if (!org.dreeam.leaf.config.modules.network.ProtocolSupport.syncmaticaProtocol) {
|
|
- return;
|
|
- }
|
|
final ExchangeTarget newPlayer = player.connection.exchangeTarget;
|
|
final VersionHandshakeServer hi = new VersionHandshakeServer(newPlayer);
|
|
playerMap.put(newPlayer, player);
|
|
@@ -79,9 +80,6 @@ public class CommunicationManager {
|
|
|
|
@ProtocolHandler.PlayerLeave
|
|
public static void onPlayerLeave(ServerPlayer player) {
|
|
- if (!org.dreeam.leaf.config.modules.network.ProtocolSupport.syncmaticaProtocol) {
|
|
- return;
|
|
- }
|
|
final ExchangeTarget oldPlayer = player.connection.exchangeTarget;
|
|
final Collection<Exchange> potentialMessageTarget = oldPlayer.getExchanges();
|
|
if (potentialMessageTarget != null) {
|
|
@@ -96,9 +94,6 @@ public class CommunicationManager {
|
|
|
|
@ProtocolHandler.PayloadReceiver(payload = SyncmaticaPayload.class, payloadId = "main")
|
|
public static void onPacketGet(ServerPlayer player, SyncmaticaPayload payload) {
|
|
- if (!org.dreeam.leaf.config.modules.network.ProtocolSupport.syncmaticaProtocol) {
|
|
- return;
|
|
- }
|
|
onPacket(player.connection.exchangeTarget, payload.packetType(), payload.data());
|
|
}
|
|
|