more work

This commit is contained in:
Yannick Lamprecht 2024-02-27 17:22:16 +01:00
parent 4548778122
commit 1e1b21830e
2 changed files with 51 additions and 26 deletions

View File

@ -6,7 +6,7 @@ Subject: [PATCH] add biome change per player
diff --git a/src/main/java/io/papermc/paper/event/packet/PlayerBiomesLoadEvent.java b/src/main/java/io/papermc/paper/event/packet/PlayerBiomesLoadEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..8f19181f54c3e82e9c51101b8f56255fc6668d00
index 0000000000000000000000000000000000000000..7774a3e3b19bfcaae319ea9ee03e85091be03a8b
--- /dev/null
+++ b/src/main/java/io/papermc/paper/event/packet/PlayerBiomesLoadEvent.java
@@ -0,0 +1,82 @@
@ -19,6 +19,7 @@ index 0000000000000000000000000000000000000000..8f19181f54c3e82e9c51101b8f56255f
+import org.bukkit.World;
+import org.bukkit.entity.Player;
+import org.bukkit.event.HandlerList;
+import org.bukkit.event.world.ChunkEvent;
+import org.bukkit.event.world.WorldEvent;
+import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.NotNull;
@ -32,16 +33,16 @@ index 0000000000000000000000000000000000000000..8f19181f54c3e82e9c51101b8f56255f
+ * Should only be used for packet/clientside related stuff.
+ * Not intended for modifying server side state.
+ */
+public class PlayerBiomesLoadEvent extends WorldEvent {
+public class PlayerBiomesLoadEvent extends ChunkEvent {
+
+ private static final HandlerList HANDLER_LIST = new HandlerList();
+
+ private final Player player;
+ private final Map<Chunk, BiomesSnapshot> chunkBiomesSnapshots = new HashMap<>();
+ private BiomesSnapshot biomesSnapshot;
+
+ @ApiStatus.Internal
+ public PlayerBiomesLoadEvent(@NotNull final Player player, World world) {
+ super(world);
+ public PlayerBiomesLoadEvent(@NotNull final Player player, Chunk chunk) {
+ super(chunk);
+ this.player = player;
+ }
+
@ -67,20 +68,19 @@ index 0000000000000000000000000000000000000000..8f19181f54c3e82e9c51101b8f56255f
+ /**
+ * Returns a biomes snapshot for the given chunk, by default it won't be set.
+ *
+ * @param chunk the given chunk
+ * @return biome snapshot if one is set
+ */
+ public @Nullable BiomesSnapshot getBiomeSnapshotAt(@NotNull Chunk chunk) {
+ return chunkBiomesSnapshots.get(chunk);
+ public @Nullable BiomesSnapshot getBiomeSnapshot() {
+ return biomesSnapshot;
+ }
+
+ /**
+ * Sets the biome snapshot for the given chunk that will be send as an override to the player
+ * Sets the biome snapshot for the given chunk that will be sent as an override to the player
+ *
+ * @param biomesSnapshot the biome override
+ */
+ public void setBiomeSnapshot(@NotNull Chunk chunk, @Nullable final BiomesSnapshot biomesSnapshot) {
+ this.chunkBiomesSnapshots.put(chunk, biomesSnapshot);
+ public void setBiomeSnapshot(@Nullable final BiomesSnapshot biomesSnapshot) {
+ this.biomesSnapshot = biomesSnapshot;
+ }
+
+ /**
@ -89,9 +89,31 @@ index 0000000000000000000000000000000000000000..8f19181f54c3e82e9c51101b8f56255f
+ * @return true if override was made, else false
+ */
+ public boolean hasOverrides() {
+ return !this.chunkBiomesSnapshots.isEmpty();
+ return biomesSnapshot != null;
+ }
+}
diff --git a/src/main/java/io/papermc/paper/event/packet/PlayerChunkLoadEvent.java b/src/main/java/io/papermc/paper/event/packet/PlayerChunkLoadEvent.java
index 3ddbc099a13df939b3912f30b54e7635840ba5a4..2933de0e9455720040619e52b7d4df66b5dbb717 100644
--- a/src/main/java/io/papermc/paper/event/packet/PlayerChunkLoadEvent.java
+++ b/src/main/java/io/papermc/paper/event/packet/PlayerChunkLoadEvent.java
@@ -15,7 +15,7 @@ import org.jetbrains.annotations.NotNull;
* Should only be used for packet/clientside related stuff.
* Not intended for modifying server side state.
*/
-public class PlayerChunkLoadEvent extends ChunkEvent {
+public class PlayerChunkLoadEvent extends PlayerBiomesLoadEvent {
private static final HandlerList HANDLER_LIST = new HandlerList();
@@ -23,7 +23,7 @@ public class PlayerChunkLoadEvent extends ChunkEvent {
@ApiStatus.Internal
public PlayerChunkLoadEvent(@NotNull Chunk chunk, @NotNull Player player) {
- super(chunk);
+ super(player, chunk);
this.player = player;
}
diff --git a/src/main/java/org/bukkit/BiomesSnapshot.java b/src/main/java/org/bukkit/BiomesSnapshot.java
new file mode 100644
index 0000000000000000000000000000000000000000..bb8683127eca2b86b23efa81bff9c54a357e04d4

View File

@ -5,15 +5,15 @@ Subject: [PATCH] add biome change per player
diff --git a/src/main/java/net/minecraft/network/protocol/game/ClientboundChunksBiomesPacket.java b/src/main/java/net/minecraft/network/protocol/game/ClientboundChunksBiomesPacket.java
index 51f647de153255c919b1440338cf1b3e2d6b5dbf..f525c51a1c888f76fa56528f204e4fac52416641 100644
index 51f647de153255c919b1440338cf1b3e2d6b5dbf..f6ec892c9e717a3a4b22e42c738ba74de1a510e5 100644
--- a/src/main/java/net/minecraft/network/protocol/game/ClientboundChunksBiomesPacket.java
+++ b/src/main/java/net/minecraft/network/protocol/game/ClientboundChunksBiomesPacket.java
@@ -2,12 +2,22 @@ package net.minecraft.network.protocol.game;
@@ -2,12 +2,21 @@ package net.minecraft.network.protocol.game;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
+import io.papermc.paper.event.packet.PlayerBiomesLoadEvent;
+import java.util.Arrays;
+import java.util.ArrayList;
import java.util.List;
+import net.minecraft.core.Holder;
import net.minecraft.network.FriendlyByteBuf;
@ -25,13 +25,12 @@ index 51f647de153255c919b1440338cf1b3e2d6b5dbf..f525c51a1c888f76fa56528f204e4fac
import net.minecraft.world.level.chunk.LevelChunkSection;
+import net.minecraft.world.level.chunk.PalettedContainer;
+import org.bukkit.BiomesSnapshot;
+import org.bukkit.Chunk;
+import org.bukkit.craftbukkit.CraftBiomesSnapshot;
+import org.bukkit.craftbukkit.CraftChunk;
public record ClientboundChunksBiomesPacket(List<ClientboundChunksBiomesPacket.ChunkBiomeData> chunkBiomeData) implements Packet<ClientGamePacketListener> {
private static final int TWO_MEGABYTES = 2097152;
@@ -16,8 +26,23 @@ public record ClientboundChunksBiomesPacket(List<ClientboundChunksBiomesPacket.C
@@ -16,8 +25,27 @@ public record ClientboundChunksBiomesPacket(List<ClientboundChunksBiomesPacket.C
this(buf.readList(ClientboundChunksBiomesPacket.ChunkBiomeData::new));
}
@ -39,17 +38,21 @@ index 51f647de153255c919b1440338cf1b3e2d6b5dbf..f525c51a1c888f76fa56528f204e4fac
- return new ClientboundChunksBiomesPacket(chunks.stream().map(ClientboundChunksBiomesPacket.ChunkBiomeData::new).toList());
+ public static ClientboundChunksBiomesPacket forChunks(Player player, List<LevelChunk> chunks) {
+ org.bukkit.entity.Player bukkitPlayer = (org.bukkit.entity.Player) player.getBukkitEntity();
+ final PlayerBiomesLoadEvent biomesLoadEvent = new PlayerBiomesLoadEvent(bukkitPlayer, bukkitPlayer.getWorld());
+ biomesLoadEvent.callEvent();
+ if(biomesLoadEvent.hasOverrides()) {
+ return new ClientboundChunksBiomesPacket(chunks.stream().map(levelChunk -> create(levelChunk, biomesLoadEvent)).toList());
+ } else {
+ return new ClientboundChunksBiomesPacket(chunks.stream().map(ClientboundChunksBiomesPacket.ChunkBiomeData::new).toList());
+ List<ChunkBiomeData> biomeData = new ArrayList<>();
+ for (LevelChunk chunk : chunks) {
+ final PlayerBiomesLoadEvent biomesLoadEvent = new PlayerBiomesLoadEvent(bukkitPlayer, new CraftChunk(chunk));
+ biomesLoadEvent.callEvent();
+ if (biomesLoadEvent.hasOverrides()) {
+ biomeData.add(create(chunk, biomesLoadEvent));
+ } else {
+ biomeData.add(new ChunkBiomeData(chunk));
+ }
+ }
+ return new ClientboundChunksBiomesPacket(biomeData);
+ }
+
+ private static ClientboundChunksBiomesPacket.ChunkBiomeData create(LevelChunk levelChunk, PlayerBiomesLoadEvent biomesLoadEvent) {
+ final BiomesSnapshot snapshotAt = biomesLoadEvent.getBiomeSnapshotAt(new CraftChunk(levelChunk));
+ final BiomesSnapshot snapshotAt = biomesLoadEvent.getBiomeSnapshot();
+ if(snapshotAt != null){
+ return new ClientboundChunksBiomesPacket.ChunkBiomeData(snapshotAt);
+ }
@ -57,7 +60,7 @@ index 51f647de153255c919b1440338cf1b3e2d6b5dbf..f525c51a1c888f76fa56528f204e4fac
}
@Override
@@ -38,6 +63,11 @@ public record ClientboundChunksBiomesPacket(List<ClientboundChunksBiomesPacket.C
@@ -38,6 +66,11 @@ public record ClientboundChunksBiomesPacket(List<ClientboundChunksBiomesPacket.C
extractChunkData(new FriendlyByteBuf(this.getWriteBuffer()), chunk);
}
@ -69,7 +72,7 @@ index 51f647de153255c919b1440338cf1b3e2d6b5dbf..f525c51a1c888f76fa56528f204e4fac
public ChunkBiomeData(FriendlyByteBuf buf) {
this(buf.readChunkPos(), buf.readByteArray(2097152));
}
@@ -71,6 +101,25 @@ public record ClientboundChunksBiomesPacket(List<ClientboundChunksBiomesPacket.C
@@ -71,6 +104,25 @@ public record ClientboundChunksBiomesPacket(List<ClientboundChunksBiomesPacket.C
}