mirror of
https://github.com/LeavesMC/Leaves.git
synced 2025-12-19 14:59:32 +00:00
335 lines
16 KiB
Diff
335 lines
16 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: violetc <58360096+s-yh-china@users.noreply.github.com>
|
|
Date: Tue, 29 Nov 2022 09:51:16 +0800
|
|
Subject: [PATCH] BBOR Protocol
|
|
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
index 3dc75d267340fdda50668ec0cc7270867dd96117..5a36cebb698ff296a057abb2745c60b41c243ad7 100644
|
|
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
@@ -1594,6 +1594,8 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
|
|
|
|
this.profiler.popPush("server gui refresh");
|
|
|
|
+ top.leavesmc.leaves.protocol.BBORProtocol.tick(); // Leaves - bbor
|
|
+
|
|
MinecraftTimings.tickablesTimer.startTiming(); // Spigot // Paper
|
|
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/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
|
|
index 3b158e65781758665baad82b049210c102679451..ec0568956dd2750e84512d0c1d1e85bb4159130f 100644
|
|
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
|
|
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
|
|
@@ -3571,6 +3571,9 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Tic
|
|
if (top.leavesmc.leaves.LeavesConfig.pcaSyncProtocol && ProtocolUtils.isNamespacePacket(packet, top.leavesmc.leaves.protocol.PcaSyncProtocol.PROTOCOL_ID)) {
|
|
top.leavesmc.leaves.protocol.PcaSyncProtocol.handlePacket(server, player, packet);
|
|
}
|
|
+ if (top.leavesmc.leaves.LeavesConfig.bborProtocol && packet.identifier.equals(top.leavesmc.leaves.protocol.BBORProtocol.SUBSCRIBE)) {
|
|
+ top.leavesmc.leaves.protocol.BBORProtocol.onPlayerSubscribed(player);
|
|
+ }
|
|
} catch (Exception ex) {
|
|
ServerGamePacketListenerImpl.LOGGER.error("Couldn\'t dispatch custom payload", ex);
|
|
this.disconnect("Invalid custom payload!", org.bukkit.event.player.PlayerKickEvent.Cause.INVALID_PAYLOAD);
|
|
diff --git a/src/main/java/net/minecraft/server/players/PlayerList.java b/src/main/java/net/minecraft/server/players/PlayerList.java
|
|
index f818dadc0a6719389f535ec142d73d34d45c8aac..3032ae9cbf02f11329a3a29442e8eff1ac9be5e8 100644
|
|
--- a/src/main/java/net/minecraft/server/players/PlayerList.java
|
|
+++ b/src/main/java/net/minecraft/server/players/PlayerList.java
|
|
@@ -341,6 +341,7 @@ public abstract class PlayerList {
|
|
}
|
|
// Leaves end - bot support
|
|
top.leavesmc.leaves.protocol.PcaSyncProtocol.onJoin(player); // Leaves - pca
|
|
+ top.leavesmc.leaves.protocol.BBORProtocol.onPlayerLoggedIn(player); // Leaves - bbor
|
|
|
|
final net.kyori.adventure.text.Component jm = playerJoinEvent.joinMessage();
|
|
|
|
@@ -584,6 +585,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() : PaperAdventure.asAdventure(entityplayer.getDisplayName())));
|
|
}
|
|
public net.kyori.adventure.text.Component remove(ServerPlayer entityplayer, net.kyori.adventure.text.Component leaveMessage) {
|
|
+ top.leavesmc.leaves.protocol.BBORProtocol.onPlayerLoggedOut(entityplayer); // Leaves - bbor
|
|
// Paper end
|
|
ServerLevel worldserver = entityplayer.getLevel();
|
|
|
|
@@ -1553,7 +1555,7 @@ public abstract class PlayerList {
|
|
entityplayer.connection.send(packetplayoutrecipeupdate);
|
|
entityplayer.getRecipeBook().sendInitialRecipeBook(entityplayer);
|
|
}
|
|
-
|
|
+ top.leavesmc.leaves.protocol.BBORProtocol.onDataPackReload(); // Leaves - bbor
|
|
}
|
|
|
|
public boolean isAllowCheatsForAllPlayers() {
|
|
diff --git a/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java b/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java
|
|
index 78dc041216f13bc9aaa9bef2aeba34601e7239da..228ca319cdf1dc0d853271cbfae510aa11203a1b 100644
|
|
--- a/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java
|
|
+++ b/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java
|
|
@@ -1062,6 +1062,11 @@ public class LevelChunk extends ChunkAccess {
|
|
|
|
public void setLoaded(boolean loadedToWorld) {
|
|
this.loaded = loadedToWorld;
|
|
+ // Leaves start - bbor
|
|
+ if (loaded) {
|
|
+ top.leavesmc.leaves.protocol.BBORProtocol.onChunkLoaded(this);
|
|
+ }
|
|
+ // Leaves end - bbor
|
|
}
|
|
|
|
public Level getLevel() {
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
|
|
index 4ffa2d267778181d853f22ccfb66eb4869866aaf..92a6206841e80488b83dad00960d8daa29adc14d 100644
|
|
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
|
|
@@ -965,6 +965,13 @@ public final class CraftServer implements Server {
|
|
top.leavesmc.leaves.protocol.PcaSyncProtocol.disablePcaSyncProtocolGlobal();
|
|
}
|
|
// Leaves end - pca
|
|
+ // Leaves start - bbor
|
|
+ if (top.leavesmc.leaves.LeavesConfig.bborProtocol) {
|
|
+ top.leavesmc.leaves.protocol.BBORProtocol.initAllPlayer();
|
|
+ } else {
|
|
+ top.leavesmc.leaves.protocol.BBORProtocol.loggedOutAllPlayer();
|
|
+ }
|
|
+ // Leaves end - bbor
|
|
for (ServerLevel world : this.console.getAllLevels()) {
|
|
// world.serverLevelData.setDifficulty(config.difficulty); // Paper - per level difficulty
|
|
world.setSpawnSettings(world.serverLevelData.getDifficulty() != Difficulty.PEACEFUL && config.spawnMonsters, config.spawnAnimals); // Paper - per level difficulty (from MinecraftServer#setDifficulty(ServerLevel, Difficulty, boolean))
|
|
diff --git a/src/main/java/top/leavesmc/leaves/LeavesConfig.java b/src/main/java/top/leavesmc/leaves/LeavesConfig.java
|
|
index a72995e1315fe492f4c2299a5d67b077ed094509..b770012135ed4e2423bdb7f44d06eedd077171ce 100644
|
|
--- a/src/main/java/top/leavesmc/leaves/LeavesConfig.java
|
|
+++ b/src/main/java/top/leavesmc/leaves/LeavesConfig.java
|
|
@@ -403,6 +403,11 @@ public final class LeavesConfig {
|
|
}
|
|
}
|
|
|
|
+ public static boolean bborProtocol = false;
|
|
+ private static void bborProtocol() {
|
|
+ bborProtocol = getBoolean("settings.protocol.bbor-protocol", bborProtocol);
|
|
+ }
|
|
+
|
|
public static final class WorldConfig {
|
|
|
|
public final String worldName;
|
|
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..95469919589b48e1c672b6b9dc884118d33d86de
|
|
--- /dev/null
|
|
+++ b/src/main/java/top/leavesmc/leaves/protocol/BBORProtocol.java
|
|
@@ -0,0 +1,216 @@
|
|
+package top.leavesmc.leaves.protocol;
|
|
+
|
|
+import io.netty.buffer.Unpooled;
|
|
+import net.minecraft.core.BlockPos;
|
|
+import net.minecraft.core.Registry;
|
|
+import net.minecraft.core.registries.Registries;
|
|
+import net.minecraft.network.FriendlyByteBuf;
|
|
+import net.minecraft.resources.ResourceLocation;
|
|
+import net.minecraft.server.MinecraftServer;
|
|
+import net.minecraft.server.level.ServerLevel;
|
|
+import net.minecraft.server.level.ServerPlayer;
|
|
+import net.minecraft.world.level.chunk.LevelChunk;
|
|
+import net.minecraft.world.level.levelgen.structure.BoundingBox;
|
|
+import net.minecraft.world.level.levelgen.structure.Structure;
|
|
+import net.minecraft.world.level.levelgen.structure.StructurePiece;
|
|
+import net.minecraft.world.level.levelgen.structure.StructureStart;
|
|
+import org.jetbrains.annotations.Contract;
|
|
+import org.jetbrains.annotations.NotNull;
|
|
+import top.leavesmc.leaves.LeavesConfig;
|
|
+import top.leavesmc.leaves.util.ProtocolUtils;
|
|
+
|
|
+import java.util.HashMap;
|
|
+import java.util.HashSet;
|
|
+import java.util.Map;
|
|
+import java.util.Set;
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
+import java.util.stream.Collectors;
|
|
+
|
|
+public class BBORProtocol {
|
|
+
|
|
+ public static final String PROTOCOL_ID = "bbor";
|
|
+
|
|
+ // send
|
|
+ private static final ResourceLocation INITIALIZE_CLIENT = id("initialize");
|
|
+ private static final ResourceLocation ADD_BOUNDING_BOX = id("add_bounding_box_v2");
|
|
+ private static final ResourceLocation STRUCTURE_LIST_SYNC = id("structure_list_sync_v1");
|
|
+ // call
|
|
+ public static final ResourceLocation SUBSCRIBE = id("subscribe");
|
|
+ private static final Map<Integer, ServerPlayer> players = new ConcurrentHashMap<>();
|
|
+ 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);
|
|
+ }
|
|
+
|
|
+ public static void onPlayerLoggedIn(@NotNull ServerPlayer player) {
|
|
+ if (LeavesConfig.bborProtocol) {
|
|
+ FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
|
|
+ buf.writeLong(OVERWORLD.getSeed());
|
|
+ buf.writeInt(OVERWORLD.levelData.getXSpawn());
|
|
+ buf.writeInt(OVERWORLD.levelData.getZSpawn());
|
|
+ ProtocolUtils.sendPayloadPacket(player, INITIALIZE_CLIENT, buf);
|
|
+
|
|
+ final Registry<Structure> structureRegistry = player.server.registryAccess().registryOrThrow(Registries.STRUCTURE);
|
|
+ final Set<String> structureIds = structureRegistry.entrySet().stream().map(e -> e.getKey().location().toString()).collect(Collectors.toSet());
|
|
+ FriendlyByteBuf buf1 = new FriendlyByteBuf(Unpooled.buffer());
|
|
+ buf1.writeVarInt(structureIds.size());
|
|
+ structureIds.forEach(buf1::writeUtf);
|
|
+ ProtocolUtils.sendPayloadPacket(player, STRUCTURE_LIST_SYNC, buf1);
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public static void onPlayerSubscribed(@NotNull ServerPlayer player) {
|
|
+ if (LeavesConfig.bborProtocol) {
|
|
+ players.put(player.getId(), player);
|
|
+ sendBoundingToPlayer(player.getId(), player);
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public static void onDataPackReload() {
|
|
+ if (LeavesConfig.bborProtocol) {
|
|
+ for (var playerEntry : players.entrySet()) {
|
|
+ final Registry<Structure> structureRegistry = MinecraftServer.getServer().registryAccess().registryOrThrow(Registries.STRUCTURE);
|
|
+ final Set<String> structureIds = structureRegistry.entrySet().stream().map(e -> e.getKey().location().toString()).collect(Collectors.toSet());
|
|
+ FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
|
|
+ buf.writeVarInt(structureIds.size());
|
|
+ structureIds.forEach(buf::writeUtf);
|
|
+ ProtocolUtils.sendPayloadPacket(playerEntry.getValue(), STRUCTURE_LIST_SYNC, buf);
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public static void onPlayerLoggedOut(@NotNull ServerPlayer player) {
|
|
+ if (LeavesConfig.bborProtocol) {
|
|
+ players.remove(player.getId());
|
|
+ playerBoundingBoxesCache.remove(player.getId());
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public static void onChunkLoaded(@NotNull LevelChunk chunk) {
|
|
+ if (LeavesConfig.bborProtocol) {
|
|
+ Map<String, StructureStart> structures = new HashMap<>();
|
|
+ final Registry<Structure> structureFeatureRegistry = chunk.getLevel().registryAccess().registryOrThrow(Registries.STRUCTURE);
|
|
+ for (var es : chunk.getAllStarts().entrySet()) {
|
|
+ final var optional = structureFeatureRegistry.getResourceKey(es.getKey());
|
|
+ optional.ifPresent(key -> structures.put(key.location().toString(), es.getValue()));
|
|
+ }
|
|
+ if (structures.size() > 0) {
|
|
+ onStructuresLoaded(chunk.getLevel().dimension().location(), structures);
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public static void onStructuresLoaded(@NotNull ResourceLocation dimensionID, @NotNull Map<String, StructureStart> structures) {
|
|
+ Map<BBoundingBox, Set<BBoundingBox>> cache = getOrCreateCache(dimensionID);
|
|
+ for (var entry : structures.entrySet()) {
|
|
+ StructureStart structureStart = entry.getValue();
|
|
+ if (structureStart == null) {
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ String type = "structure:" + entry.getKey();
|
|
+ BoundingBox bb = structureStart.getBoundingBox();
|
|
+ BBoundingBox boundingBox = buildStructure(bb, type);
|
|
+ if (cache.containsKey(boundingBox)) {
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ Set<BBoundingBox> structureBoundingBoxes = new HashSet<>();
|
|
+ for (StructurePiece structureComponent : structureStart.getPieces()) {
|
|
+ structureBoundingBoxes.add(buildStructure(structureComponent.getBoundingBox(), type));
|
|
+ }
|
|
+ cache.put(boundingBox, structureBoundingBoxes);
|
|
+ }
|
|
+ }
|
|
+
|
|
+ private static @NotNull BBoundingBox buildStructure(@NotNull BoundingBox bb, String type) {
|
|
+ BlockPos min = new BlockPos(bb.minX(), bb.minY(), bb.minZ());
|
|
+ BlockPos max = new BlockPos(bb.maxX(), bb.maxY(), bb.maxZ());
|
|
+ return new BBoundingBox(type, min, max);
|
|
+ }
|
|
+
|
|
+ private static void sendBoundingToPlayer(int id, ServerPlayer player) {
|
|
+ for (var entry : dimensionCache.entrySet()) {
|
|
+ if (entry.getValue() == null) {
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ Set<BBoundingBox> playerBoundingBoxes = playerBoundingBoxesCache.computeIfAbsent(id, k -> new HashSet<>());
|
|
+ Map<BBoundingBox, Set<BBoundingBox>> boundingBoxMap = entry.getValue();
|
|
+ for (BBoundingBox key : boundingBoxMap.keySet()) {
|
|
+ if (playerBoundingBoxes.contains(key)) {
|
|
+ continue;
|
|
+ }
|
|
+
|
|
+ Set<BBoundingBox> boundingBoxes = boundingBoxMap.get(key);
|
|
+
|
|
+ FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
|
|
+ buf.writeResourceLocation(entry.getKey());
|
|
+ key.serialize(buf);
|
|
+ if (boundingBoxes != null && boundingBoxes.size() > 1) {
|
|
+ for (BBoundingBox box : boundingBoxes) {
|
|
+ box.serialize(buf);
|
|
+ }
|
|
+ }
|
|
+ ProtocolUtils.sendPayloadPacket(player, ADD_BOUNDING_BOX, buf);
|
|
+ playerBoundingBoxes.add(key);
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public static void tick() {
|
|
+ if (LeavesConfig.bborProtocol) {
|
|
+ for (var playerEntry : players.entrySet()) {
|
|
+ sendBoundingToPlayer(playerEntry.getKey(), playerEntry.getValue());
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public static void initAllPlayer() {
|
|
+ for (ServerPlayer player : MinecraftServer.getServer().getPlayerList().getPlayers()) {
|
|
+ onPlayerLoggedIn(player);
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public static void loggedOutAllPlayer() {
|
|
+ players.clear();
|
|
+ playerBoundingBoxesCache.clear();
|
|
+ for (var cache : dimensionCache.values()) {
|
|
+ cache.clear();
|
|
+ }
|
|
+ dimensionCache.clear();
|
|
+ }
|
|
+
|
|
+ private static Map<BBoundingBox, Set<BBoundingBox>> getOrCreateCache(ResourceLocation dimensionId) {
|
|
+ return dimensionCache.computeIfAbsent(dimensionId, dt -> new ConcurrentHashMap<>());
|
|
+ }
|
|
+
|
|
+ private record BBoundingBox(String type, BlockPos min, BlockPos max) {
|
|
+
|
|
+ public void serialize(@NotNull FriendlyByteBuf buf) {
|
|
+ buf.writeChar('S');
|
|
+ buf.writeInt(type.hashCode());
|
|
+ buf.writeVarInt(min.getX()).writeVarInt(min.getY()).writeVarInt(min.getZ());
|
|
+ buf.writeVarInt(max.getX()).writeVarInt(max.getY()).writeVarInt(max.getZ());
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public int hashCode() {
|
|
+ return combineHashCodes(min.hashCode(), max.hashCode());
|
|
+ }
|
|
+
|
|
+ private static int combineHashCodes(int @NotNull ... hashCodes) {
|
|
+ final int prime = 31;
|
|
+ int result = 0;
|
|
+ for (int hashCode : hashCodes) {
|
|
+ result = prime * result + hashCode;
|
|
+ }
|
|
+ return result;
|
|
+ }
|
|
+ }
|
|
+}
|