mirror of
https://github.com/LeavesMC/Leaves.git
synced 2025-12-19 14:59:32 +00:00
319 lines
16 KiB
Diff
319 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 745ab8b97e6bf3e9a1621558883711eab25f8725..8150ac9c98a30afff5fc04fdec8af6d340c4b9f5 100644
|
|
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
@@ -1602,6 +1602,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 b4d49eb6b01a660777afe2e5bcd259dfb02866f4..cc736da1897c082efcc58dc006c720b9b1ebb70a 100644
|
|
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
|
|
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
|
|
@@ -3620,6 +3620,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 afbe81cec75b06d17699e005acf715d61c56f68d..81088d403e3988f4004d6cf368aa15dfa3ac26aa 100644
|
|
--- a/src/main/java/net/minecraft/server/players/PlayerList.java
|
|
+++ b/src/main/java/net/minecraft/server/players/PlayerList.java
|
|
@@ -356,6 +356,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();
|
|
|
|
@@ -607,6 +608,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.serverLevel();
|
|
|
|
@@ -1599,7 +1601,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 cc224af0139a6e3adefd22cbfa0cd519735b7191..2c3ca05644bc97d505e8ca92e7a5f48670fd8b66 100644
|
|
--- a/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java
|
|
+++ b/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java
|
|
@@ -1029,6 +1029,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 85de3ff6eedf6b87189ba64de56b3b8b42915a35..569ffe34b169902aa9bc52b5238376ee04959c11 100644
|
|
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
|
|
@@ -1048,6 +1048,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/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;
|
|
+ }
|
|
+ }
|
|
+}
|