From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Cryptite Date: Mon, 7 Nov 2022 22:44:22 -0600 Subject: [PATCH] PlayerPreChunkLoadEvent diff --git a/src/main/java/io/papermc/paper/chunk/PlayerChunkLoader.java b/src/main/java/io/papermc/paper/chunk/PlayerChunkLoader.java index 12bcf96ec1ba4314c7ea2eab9f3d140559f1dc08..d8773c7c36314b4f5953b9e7abc310c74401c23c 100644 --- a/src/main/java/io/papermc/paper/chunk/PlayerChunkLoader.java +++ b/src/main/java/io/papermc/paper/chunk/PlayerChunkLoader.java @@ -3,6 +3,7 @@ package io.papermc.paper.chunk; import com.destroystokyo.paper.PaperConfig; import com.destroystokyo.paper.util.misc.PlayerAreaMap; import com.destroystokyo.paper.util.misc.PooledLinkedHashSets; +import io.papermc.paper.event.packet.PlayerPreChunkLoadEvent; import io.papermc.paper.util.CoordinateUtils; import io.papermc.paper.util.IntervalledCounter; import io.papermc.paper.util.TickThread; @@ -787,7 +788,7 @@ public final class PlayerChunkLoader { // warning: modifications of this field must be aware that the loadQueue inside PlayerChunkLoader uses this field // in a comparator! protected final ArrayDeque loadQueue = new ArrayDeque<>(); - protected final LongOpenHashSet sentChunks = new LongOpenHashSet(); + public final LongOpenHashSet sentChunks = new LongOpenHashSet(); // Slice - public protected final LongOpenHashSet chunksToBeSent = new LongOpenHashSet(); protected final TreeSet sendQueue = new TreeSet<>((final ChunkPriorityHolder p1, final ChunkPriorityHolder p2) -> { @@ -871,7 +872,14 @@ public final class PlayerChunkLoader { } public void sendChunk(final int chunkX, final int chunkZ, final Runnable onChunkSend) { - if (this.sentChunks.add(CoordinateUtils.getChunkKey(chunkX, chunkZ))) { + // Slice start + if (!new PlayerPreChunkLoadEvent(this.player.getBukkitEntity().getWorld(), chunkX, chunkZ, this.player.getBukkitEntity()).callEvent()) { + this.player.connection.connection.execute(onChunkSend); + return; + } + // Slice end + + if (this.sentChunks.add(CoordinateUtils.getChunkKey(chunkX, chunkZ))) { // Slice this.player.getLevel().getChunkSource().chunkMap.updateChunkTracking(this.player, new ChunkPos(chunkX, chunkZ), new MutableObject<>(), false, true); // unloaded, loaded this.player.connection.connection.execute(onChunkSend); diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java index 9d400d82eeab062322acea81b87f6fc572151700..ee35b5ac41d2016a775b320b1a84fceb6329c4be 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java @@ -528,6 +528,13 @@ public class CraftWorld extends CraftRegionAccessor implements World { return true; } + @Override + public it.unimi.dsi.fastutil.longs.LongOpenHashSet getSentChunks(Player p) { + CraftPlayer craftPlayer = (CraftPlayer) p; + io.papermc.paper.chunk.PlayerChunkLoader.PlayerLoaderData data = this.world.chunkSource.chunkMap.playerChunkManager.getData(craftPlayer.getHandle()); + return data != null ? data.sentChunks : null; + } + @Override public boolean isChunkInUse(int x, int z) { return this.isChunkLoaded(x, z); diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java index 2e7c1a99697c31f005a62b5d663708c9d4bc85df..b62a427b123f0573e15e6e38ff67029eadd69966 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java @@ -2214,13 +2214,13 @@ public class CraftPlayer extends CraftHumanEntity implements Player { } // Paper end } - + @Override public void sendHealthUpdate() { this.sendHealthUpdate(this.getScaledHealth(), this.getHandle().getFoodData().getFoodLevel(), this.getHandle().getFoodData().getSaturationLevel()); } // Paper end - + public void injectScaledMaxHealth(Collection collection, boolean force) { if (!this.scaledHealth && !force) { return;