Skip to content

Commit

Permalink
Fix Leaves Protocol Core, BECAUSE SOMETHING JUST RUN ON DEV
Browse files Browse the repository at this point in the history
  • Loading branch information
s-yh-china committed Oct 25, 2023
1 parent ba2e042 commit 374a32c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 28 deletions.
80 changes: 59 additions & 21 deletions patches/server/0006-Leaves-Protocol-Core.patch
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ index 0000000000000000000000000000000000000000..64a1d25973b032e8cab64bbffa6824a1
+}
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..c233f9cc008481c7a3449d58eb5f5b0f4d093990
index 0000000000000000000000000000000000000000..055f044ce6cef4377f6f577efdbfad0ec9a2d57b
--- /dev/null
+++ b/src/main/java/top/leavesmc/leaves/protocol/core/LeavesProtocolManager.java
@@ -0,0 +1,302 @@
@@ -0,0 +1,340 @@
+package top.leavesmc.leaves.protocol.core;
+
+import net.minecraft.network.FriendlyByteBuf;
Expand All @@ -139,6 +139,7 @@ index 0000000000000000000000000000000000000000..c233f9cc008481c7a3449d58eb5f5b0f
+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;
Expand All @@ -147,9 +148,12 @@ index 0000000000000000000000000000000000000000..c233f9cc008481c7a3449d58eb5f5b0f
+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 {
+
Expand Down Expand Up @@ -362,42 +366,76 @@ index 0000000000000000000000000000000000000000..c233f9cc008481c7a3449d58eb5f5b0f
+ }
+ }
+
+ private static List<Class<?>> getClasses(String packageName) {
+ List<Class<?>> classes = new ArrayList<>();
+ String packageDirName = packageName.replace('.', '/');
+ 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();
+ findClassInPackageByFile(packageName, URLDecoder.decode(url.getFile(), StandardCharsets.UTF_8), classes);
+ 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) {
+ throw new RuntimeException(e);
+ e.printStackTrace();
+ }
+ return classes;
+ }
+
+ private static void findClassInPackageByFile(String packageName, String filePath, List<Class<?>> classes) {
+ File dir = new File(filePath);
+ 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.getName().endsWith("class") || file.isDirectory());
+ if (dirFiles != null) {
+ for (File file : dirFiles) {
+ File[] dirfiles = dir.listFiles((file) -> file.isDirectory() || file.getName().endsWith(".class"));
+ if (dirfiles != null) {
+ for (File file : dirfiles) {
+ if (file.isDirectory()) {
+ if (!file.getName().equals("core")) {
+ findClassInPackageByFile(packageName + "." + file.getName(), file.getPath(), classes);
+ 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();
+ }
+ continue;
+ }
+ String className = file.getName().substring(0, file.getName().length() - 6);
+ try {
+ classes.add(Thread.currentThread().getContextClassLoader().loadClass(packageName + "." + className));
+ } catch (ClassNotFoundException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ }
+
+ 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();
+ }
+ }
+ }
+ }
Expand Down
13 changes: 6 additions & 7 deletions patches/server/0042-BBOR-Protocol.patch
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ index 17696dfdb3ad1d8c63ff1d1fa0c5eb6bf2e2ae42..bb47170917ed8918a9781b71ae60fd74
public Level getLevel() {
diff --git a/src/main/java/top/leavesmc/leaves/protocol/BBORProtocol.java b/src/main/java/top/leavesmc/leaves/protocol/BBORProtocol.java
new file mode 100644
index 0000000000000000000000000000000000000000..9bc31425ffaedcf17313aa000ba4fe3c16929810
index 0000000000000000000000000000000000000000..4e1e099f6f480ae694405f0b1f98f429e2653d31
--- /dev/null
+++ b/src/main/java/top/leavesmc/leaves/protocol/BBORProtocol.java
@@ -0,0 +1,228 @@
@@ -0,0 +1,227 @@
+package top.leavesmc.leaves.protocol;
+
+import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -83,8 +83,6 @@ index 0000000000000000000000000000000000000000..9bc31425ffaedcf17313aa000ba4fe3c
+ private static final Map<Integer, Set<BBoundingBox>> playerBoundingBoxesCache = new HashMap<>();
+ private static final Map<ResourceLocation, Map<BBoundingBox, Set<BBoundingBox>>> dimensionCache = new ConcurrentHashMap<>();
+
+ private static final ServerLevel OVERWORLD = MinecraftServer.getServer().overworld();
+
+ @Contract("_ -> new")
+ public static @NotNull ResourceLocation id(String path) {
+ return new ResourceLocation(PROTOCOL_ID, path);
Expand All @@ -111,10 +109,11 @@ index 0000000000000000000000000000000000000000..9bc31425ffaedcf17313aa000ba4fe3c
+ @ProtocolHandler.PlayerJoin
+ public static void onPlayerLoggedIn(@NotNull ServerPlayer player) {
+ if (LeavesConfig.bborProtocol) {
+ ServerLevel overworld = MinecraftServer.getServer().overworld();
+ ProtocolUtils.sendPayloadPacket(player, INITIALIZE_CLIENT, buf -> {
+ buf.writeLong(OVERWORLD.getSeed());
+ buf.writeInt(OVERWORLD.levelData.getXSpawn());
+ buf.writeInt(OVERWORLD.levelData.getZSpawn());
+ buf.writeLong(overworld.getSeed());
+ buf.writeInt(overworld.levelData.getXSpawn());
+ buf.writeInt(overworld.levelData.getZSpawn());
+ });
+ sendStructureList(player);
+ }
Expand Down

0 comments on commit 374a32c

Please sign in to comment.