From 580cb4a91ec576ce041068d4b4916d8aadc4ed6d Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Fri, 1 Jan 2021 18:04:30 +0000 Subject: [PATCH] add chunk tickets and don't force sync sending chunk packets --- .../mc1_15_2/BukkitAdapter_1_15_2.java | 70 +++++++------- .../mc1_15_2/BukkitGetBlocks_1_15_2.java | 1 + .../mc1_16_1/BukkitAdapter_1_16_1.java | 70 +++++++------- .../mc1_16_1/BukkitGetBlocks_1_16_1.java | 1 + .../mc1_16_2/BukkitAdapter_1_16_2.java | 91 ++++++++++-------- .../mc1_16_2/BukkitGetBlocks_1_16_2.java | 1 + .../mc1_16_4/BukkitAdapter_1_16_4.java | 95 +++++++++++-------- .../mc1_16_4/BukkitGetBlocks_1_16_4.java | 1 + 8 files changed, 181 insertions(+), 149 deletions(-) diff --git a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_15_2/BukkitAdapter_1_15_2.java b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_15_2/BukkitAdapter_1_15_2.java index 8098145a1..12a25e0fc 100644 --- a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_15_2/BukkitAdapter_1_15_2.java +++ b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_15_2/BukkitAdapter_1_15_2.java @@ -9,6 +9,7 @@ import com.boydti.fawe.object.collection.BitArray; import com.boydti.fawe.util.MathMan; import com.boydti.fawe.util.ReflectionUtils; import com.boydti.fawe.util.TaskManager; +import com.sk89q.worldedit.bukkit.WorldEditPlugin; import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.block.BlockTypesCache; @@ -154,25 +155,33 @@ public final class BukkitAdapter_1_15_2 extends NMSAdapter { } public static Chunk ensureLoaded(World nmsWorld, int chunkX, int chunkZ) { - Chunk nmsChunk = nmsWorld.getChunkIfLoaded(chunkX, chunkZ); + final Chunk nmsChunk = nmsWorld.getChunkProvider().getChunkAt(chunkX, chunkZ, false); if (nmsChunk != null) { + TaskManager.IMP.task(() -> nmsChunk.bukkitChunk.addPluginChunkTicket(WorldEditPlugin.getInstance())); return nmsChunk; } if (Fawe.isMainThread()) { - return nmsWorld.getChunkAt(chunkX, chunkZ); + final Chunk nmsChunkMain = nmsWorld.getChunkAt(chunkX, chunkZ); + TaskManager.IMP.task(() -> nmsChunkMain.bukkitChunk.addPluginChunkTicket(WorldEditPlugin.getInstance())); + return nmsChunkMain; } if (PaperLib.isPaper()) { CraftWorld craftWorld = nmsWorld.getWorld(); CompletableFuture future = craftWorld.getChunkAtAsync(chunkX, chunkZ, true); try { - CraftChunk chunk = (CraftChunk) future.get(); + final CraftChunk chunk = (CraftChunk) future.get(); + TaskManager.IMP.task(() -> chunk.addPluginChunkTicket(WorldEditPlugin.getInstance())); return chunk.getHandle(); } catch (Throwable e) { e.printStackTrace(); } } // TODO optimize - return TaskManager.IMP.sync(() -> nmsWorld.getChunkAt(chunkX, chunkZ)); + return TaskManager.IMP.sync(() -> { + Chunk chunk = nmsWorld.getChunkAt(chunkX, chunkZ); + chunk.bukkitChunk.addPluginChunkTicket(WorldEditPlugin.getInstance()); + return chunk; + }); } public static PlayerChunk getPlayerChunk(WorldServer nmsWorld, final int cx, final int cz) { @@ -190,37 +199,32 @@ public final class BukkitAdapter_1_15_2 extends NMSAdapter { return; } if (playerChunk.hasBeenLoaded()) { - TaskManager.IMP.sync(() -> { - try { - int dirtyBits = fieldDirtyBits.getInt(playerChunk); - if (dirtyBits == 0) { - nmsWorld.getChunkProvider().playerChunkMap.a(playerChunk); - } - if (mask == 0) { - dirtyBits = 65535; - } else { - dirtyBits |= mask; - } - - fieldDirtyBits.set(playerChunk, dirtyBits); - fieldDirtyCount.set(playerChunk, 64); - - if (lighting) { - ChunkCoordIntPair chunkCoordIntPair = new ChunkCoordIntPair(chunkX, chunkZ); - PacketPlayOutLightUpdate packet = new PacketPlayOutLightUpdate(chunkCoordIntPair, nmsWorld.getChunkProvider().getLightEngine()); - playerChunk.players.a(chunkCoordIntPair, false).forEach(p -> { - p.playerConnection.sendPacket(packet); - }); - } - - } catch (IllegalAccessException e) { - e.printStackTrace(); + try { + int dirtyBits = fieldDirtyBits.getInt(playerChunk); + if (dirtyBits == 0) { + nmsWorld.getChunkProvider().playerChunkMap.a(playerChunk); } - return null; - }); - return; + if (mask == 0) { + dirtyBits = 65535; + } else { + dirtyBits |= mask; + } + + fieldDirtyBits.set(playerChunk, dirtyBits); + fieldDirtyCount.set(playerChunk, 64); + + if (lighting) { + ChunkCoordIntPair chunkCoordIntPair = new ChunkCoordIntPair(chunkX, chunkZ); + PacketPlayOutLightUpdate packet = new PacketPlayOutLightUpdate(chunkCoordIntPair, nmsWorld.getChunkProvider().getLightEngine()); + playerChunk.players.a(chunkCoordIntPair, false).forEach(p -> { + p.playerConnection.sendPacket(packet); + }); + } + + } catch (IllegalAccessException e) { + e.printStackTrace(); + } } - return; } /* diff --git a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_15_2/BukkitGetBlocks_1_15_2.java b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_15_2/BukkitGetBlocks_1_15_2.java index b6b2380a2..daae13a16 100644 --- a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_15_2/BukkitGetBlocks_1_15_2.java +++ b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_15_2/BukkitGetBlocks_1_15_2.java @@ -607,6 +607,7 @@ public class BukkitGetBlocks_1_15_2 extends CharGetBlocks { task.run(); } } + nmsChunk.bukkitChunk.removePluginChunkTicket(WorldEditPlugin.getInstance()); if (callback == null) { if (finalizer != null) { finalizer.run(); diff --git a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_1/BukkitAdapter_1_16_1.java b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_1/BukkitAdapter_1_16_1.java index d148d8e81..559d1a6b1 100644 --- a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_1/BukkitAdapter_1_16_1.java +++ b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_1/BukkitAdapter_1_16_1.java @@ -9,6 +9,7 @@ import com.boydti.fawe.object.collection.BitArrayUnstretched; import com.boydti.fawe.util.MathMan; import com.boydti.fawe.util.ReflectionUtils; import com.boydti.fawe.util.TaskManager; +import com.sk89q.worldedit.bukkit.WorldEditPlugin; import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.block.BlockTypesCache; @@ -151,25 +152,33 @@ public final class BukkitAdapter_1_16_1 extends NMSAdapter { } public static Chunk ensureLoaded(World nmsWorld, int chunkX, int chunkZ) { - Chunk nmsChunk = nmsWorld.getChunkProvider().getChunkAt(chunkX, chunkZ, false); + final Chunk nmsChunk = nmsWorld.getChunkProvider().getChunkAt(chunkX, chunkZ, false); if (nmsChunk != null) { + TaskManager.IMP.task(() -> nmsChunk.bukkitChunk.addPluginChunkTicket(WorldEditPlugin.getInstance())); return nmsChunk; } if (Fawe.isMainThread()) { - return nmsWorld.getChunkAt(chunkX, chunkZ); + final Chunk nmsChunkMain = nmsWorld.getChunkAt(chunkX, chunkZ); + TaskManager.IMP.task(() -> nmsChunkMain.bukkitChunk.addPluginChunkTicket(WorldEditPlugin.getInstance())); + return nmsChunkMain; } if (PaperLib.isPaper()) { CraftWorld craftWorld = nmsWorld.getWorld(); CompletableFuture future = craftWorld.getChunkAtAsync(chunkX, chunkZ, true); try { - CraftChunk chunk = (CraftChunk) future.get(); + final CraftChunk chunk = (CraftChunk) future.get(); + TaskManager.IMP.task(() -> chunk.addPluginChunkTicket(WorldEditPlugin.getInstance())); return chunk.getHandle(); } catch (Throwable e) { e.printStackTrace(); } } // TODO optimize - return TaskManager.IMP.sync(() -> nmsWorld.getChunkAt(chunkX, chunkZ)); + return TaskManager.IMP.sync(() -> { + Chunk chunk = nmsWorld.getChunkAt(chunkX, chunkZ); + chunk.bukkitChunk.addPluginChunkTicket(WorldEditPlugin.getInstance()); + return chunk; + }); } public static PlayerChunk getPlayerChunk(WorldServer nmsWorld, final int cx, final int cz) { @@ -187,35 +196,32 @@ public final class BukkitAdapter_1_16_1 extends NMSAdapter { return; } if (playerChunk.hasBeenLoaded()) { - TaskManager.IMP.sync(() -> { - try { - int dirtyBits = fieldDirtyBits.getInt(playerChunk); - if (dirtyBits == 0) { - nmsWorld.getChunkProvider().playerChunkMap.a(playerChunk); - } - if (mask == 0) { - dirtyBits = 65535; - } else { - dirtyBits |= mask; - } - - fieldDirtyBits.set(playerChunk, dirtyBits); - fieldDirtyCount.set(playerChunk, 64); - - if (lighting) { - ChunkCoordIntPair chunkCoordIntPair = new ChunkCoordIntPair(chunkX, chunkZ); - boolean trustEdges = false; //Added in 1.16.1 Not sure what it does. - PacketPlayOutLightUpdate packet = new PacketPlayOutLightUpdate(chunkCoordIntPair, nmsWorld.getChunkProvider().getLightEngine(), trustEdges); - playerChunk.players.a(chunkCoordIntPair, false).forEach(p -> { - p.playerConnection.sendPacket(packet); - }); - } - - } catch (IllegalAccessException e) { - e.printStackTrace(); + try { + int dirtyBits = fieldDirtyBits.getInt(playerChunk); + if (dirtyBits == 0) { + nmsWorld.getChunkProvider().playerChunkMap.a(playerChunk); } - return null; - }); + if (mask == 0) { + dirtyBits = 65535; + } else { + dirtyBits |= mask; + } + + fieldDirtyBits.set(playerChunk, dirtyBits); + fieldDirtyCount.set(playerChunk, 64); + + if (lighting) { + ChunkCoordIntPair chunkCoordIntPair = new ChunkCoordIntPair(chunkX, chunkZ); + boolean trustEdges = false; //Added in 1.16.1 Not sure what it does. + PacketPlayOutLightUpdate packet = new PacketPlayOutLightUpdate(chunkCoordIntPair, nmsWorld.getChunkProvider().getLightEngine(), trustEdges); + playerChunk.players.a(chunkCoordIntPair, false).forEach(p -> { + p.playerConnection.sendPacket(packet); + }); + } + + } catch (IllegalAccessException e) { + e.printStackTrace(); + } } } diff --git a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_1/BukkitGetBlocks_1_16_1.java b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_1/BukkitGetBlocks_1_16_1.java index 3a5a68338..39f1e9681 100644 --- a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_1/BukkitGetBlocks_1_16_1.java +++ b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_1/BukkitGetBlocks_1_16_1.java @@ -609,6 +609,7 @@ public class BukkitGetBlocks_1_16_1 extends CharGetBlocks { task.run(); } } + nmsChunk.bukkitChunk.removePluginChunkTicket(WorldEditPlugin.getInstance()); if (callback == null) { if (finalizer != null) { finalizer.run(); diff --git a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_2/BukkitAdapter_1_16_2.java b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_2/BukkitAdapter_1_16_2.java index f75477317..76fdbdece 100644 --- a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_2/BukkitAdapter_1_16_2.java +++ b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_2/BukkitAdapter_1_16_2.java @@ -11,6 +11,7 @@ import com.boydti.fawe.util.ReflectionUtils; import com.boydti.fawe.util.TaskManager; import com.destroystokyo.paper.util.misc.PooledLinkedHashSets; import com.mojang.datafixers.util.Either; +import com.sk89q.worldedit.bukkit.WorldEditPlugin; import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.block.BlockTypesCache; @@ -163,25 +164,33 @@ public final class BukkitAdapter_1_16_2 extends NMSAdapter { } public static Chunk ensureLoaded(World nmsWorld, int chunkX, int chunkZ) { - Chunk nmsChunk = nmsWorld.getChunkProvider().getChunkAt(chunkX, chunkZ, false); + final Chunk nmsChunk = nmsWorld.getChunkProvider().getChunkAt(chunkX, chunkZ, false); if (nmsChunk != null) { + TaskManager.IMP.task(() -> nmsChunk.bukkitChunk.addPluginChunkTicket(WorldEditPlugin.getInstance())); return nmsChunk; } if (Fawe.isMainThread()) { - return nmsWorld.getChunkAt(chunkX, chunkZ); + final Chunk nmsChunkMain = nmsWorld.getChunkAt(chunkX, chunkZ); + TaskManager.IMP.task(() -> nmsChunkMain.bukkitChunk.addPluginChunkTicket(WorldEditPlugin.getInstance())); + return nmsChunkMain; } if (PaperLib.isPaper()) { CraftWorld craftWorld = nmsWorld.getWorld(); CompletableFuture future = craftWorld.getChunkAtAsync(chunkX, chunkZ, true); try { - CraftChunk chunk = (CraftChunk) future.get(); + final CraftChunk chunk = (CraftChunk) future.get(); + TaskManager.IMP.task(() -> chunk.addPluginChunkTicket(WorldEditPlugin.getInstance())); return chunk.getHandle(); } catch (Throwable e) { e.printStackTrace(); } } // TODO optimize - return TaskManager.IMP.sync(() -> nmsWorld.getChunkAt(chunkX, chunkZ)); + return TaskManager.IMP.sync(() -> { + Chunk chunk = nmsWorld.getChunkAt(chunkX, chunkZ); + chunk.bukkitChunk.addPluginChunkTicket(WorldEditPlugin.getInstance()); + return chunk; + }); } public static PlayerChunk getPlayerChunk(WorldServer nmsWorld, final int chunkX, final int chunkZ) { @@ -199,51 +208,49 @@ public final class BukkitAdapter_1_16_2 extends NMSAdapter { return; } if (playerChunk.hasBeenLoaded()) { - TaskManager.IMP.sync(() -> { - ChunkCoordIntPair chunkCoordIntPair = new ChunkCoordIntPair(chunkX, chunkZ); - Optional optional = ((Either) playerChunk.a().getNow(PlayerChunk.UNLOADED_CHUNK)).left(); - if (optional.isPresent()) { - PacketPlayOutMapChunk chunkpacket = new PacketPlayOutMapChunk(optional.get(), 65535); + ChunkCoordIntPair chunkCoordIntPair = new ChunkCoordIntPair(chunkX, chunkZ); + Optional optional = ((Either) playerChunk.a().getNow(PlayerChunk.UNLOADED_CHUNK)).left(); + if (optional.isPresent()) { + PacketPlayOutMapChunk chunkpacket = new PacketPlayOutMapChunk(optional.get(), 65535); + playerChunk.players.a(chunkCoordIntPair, false).forEach(p -> { + p.playerConnection.sendPacket(chunkpacket); + }); + + if (lighting) { + //This needs to be true otherwise Minecraft will update lighting from/at the chunk edges (bad) + boolean trustEdges = true; + PacketPlayOutLightUpdate packet = new PacketPlayOutLightUpdate(chunkCoordIntPair, nmsWorld.getChunkProvider().getLightEngine(), trustEdges); playerChunk.players.a(chunkCoordIntPair, false).forEach(p -> { - p.playerConnection.sendPacket(chunkpacket); + p.playerConnection.sendPacket(packet); }); + } + } else if (PaperLib.isPaper()) { + //Require generic here to work with multiple dependencies trying to take control. + PooledLinkedHashSets.PooledObjectLinkedOpenHashSet objects = + nmsWorld.getChunkProvider().playerChunkMap.playerViewDistanceNoTickMap.getObjectsInRange(chunkX, chunkZ); + if (objects == null) { + return; + } + for (Object obj : objects.getBackingSet()) { + if (obj == null) { + continue; + } + EntityPlayer p = (EntityPlayer) obj; + Chunk chunk = nmsWorld.getChunkProvider().getChunkAtIfLoadedImmediately(chunkX, chunkZ); + if (chunk != null) { + PacketPlayOutMapChunk chunkpacket = new PacketPlayOutMapChunk(chunk, 65535); + p.playerConnection.sendPacket(chunkpacket); - if (lighting) { - boolean trustEdges = true; //This needs to be true otherwise Minecraft will update lighting from/at the chunk edges (bad) - PacketPlayOutLightUpdate packet = new PacketPlayOutLightUpdate(chunkCoordIntPair, nmsWorld.getChunkProvider().getLightEngine(), trustEdges); - playerChunk.players.a(chunkCoordIntPair, false).forEach(p -> { + if (lighting) { + //This needs to be true otherwise Minecraft will update lighting from/at the chunk edges (bad) + boolean trustEdges = true; + PacketPlayOutLightUpdate packet = + new PacketPlayOutLightUpdate(chunkCoordIntPair, nmsWorld.getChunkProvider().getLightEngine(), trustEdges); p.playerConnection.sendPacket(packet); - }); - } - } else if (PaperLib.isPaper()) { - //Require generic here to work with multiple dependencies trying to take control. - PooledLinkedHashSets.PooledObjectLinkedOpenHashSet objects = - nmsWorld.getChunkProvider().playerChunkMap.playerViewDistanceNoTickMap.getObjectsInRange(chunkX, chunkZ); - if (objects == null) { - return null; - } - for (Object obj : objects.getBackingSet()) { - if (obj == null) { - continue; - } - EntityPlayer p = (EntityPlayer) obj; - Chunk chunk = nmsWorld.getChunkProvider().getChunkAtIfLoadedImmediately(chunkX, chunkZ); - if (chunk != null) { - PacketPlayOutMapChunk chunkpacket = new PacketPlayOutMapChunk(chunk, 65535); - p.playerConnection.sendPacket(chunkpacket); - - if (lighting) { - boolean trustEdges = - true; //This needs to be true otherwise Minecraft will update lighting from/at the chunk edges (bad) - PacketPlayOutLightUpdate packet = - new PacketPlayOutLightUpdate(chunkCoordIntPair, nmsWorld.getChunkProvider().getLightEngine(), trustEdges); - p.playerConnection.sendPacket(packet); - } } } } - return null; - }); + } } } diff --git a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_2/BukkitGetBlocks_1_16_2.java b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_2/BukkitGetBlocks_1_16_2.java index 5a8873114..cb5b84019 100644 --- a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_2/BukkitGetBlocks_1_16_2.java +++ b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_2/BukkitGetBlocks_1_16_2.java @@ -612,6 +612,7 @@ public class BukkitGetBlocks_1_16_2 extends CharGetBlocks { task.run(); } } + nmsChunk.bukkitChunk.removePluginChunkTicket(WorldEditPlugin.getInstance()); if (callback == null) { if (finalizer != null) { finalizer.run(); diff --git a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_4/BukkitAdapter_1_16_4.java b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_4/BukkitAdapter_1_16_4.java index 6e0ca22a1..28293c93a 100644 --- a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_4/BukkitAdapter_1_16_4.java +++ b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_4/BukkitAdapter_1_16_4.java @@ -5,12 +5,14 @@ import com.boydti.fawe.FaweCache; import com.boydti.fawe.bukkit.adapter.DelegateLock; import com.boydti.fawe.bukkit.adapter.NMSAdapter; import com.boydti.fawe.config.Settings; +import com.boydti.fawe.object.RunnableVal; import com.boydti.fawe.object.collection.BitArrayUnstretched; import com.boydti.fawe.util.MathMan; import com.boydti.fawe.util.ReflectionUtils; import com.boydti.fawe.util.TaskManager; import com.destroystokyo.paper.util.misc.PooledLinkedHashSets; import com.mojang.datafixers.util.Either; +import com.sk89q.worldedit.bukkit.WorldEditPlugin; import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.block.BlockTypesCache; @@ -163,25 +165,33 @@ public final class BukkitAdapter_1_16_4 extends NMSAdapter { } public static Chunk ensureLoaded(World nmsWorld, int chunkX, int chunkZ) { - Chunk nmsChunk = nmsWorld.getChunkProvider().getChunkAt(chunkX, chunkZ, false); + final Chunk nmsChunk = nmsWorld.getChunkProvider().getChunkAt(chunkX, chunkZ, false); if (nmsChunk != null) { + TaskManager.IMP.task(() -> nmsChunk.bukkitChunk.addPluginChunkTicket(WorldEditPlugin.getInstance())); return nmsChunk; } if (Fawe.isMainThread()) { - return nmsWorld.getChunkAt(chunkX, chunkZ); + final Chunk nmsChunkMain = nmsWorld.getChunkAt(chunkX, chunkZ); + TaskManager.IMP.task(() -> nmsChunkMain.bukkitChunk.addPluginChunkTicket(WorldEditPlugin.getInstance())); + return nmsChunkMain; } if (PaperLib.isPaper()) { CraftWorld craftWorld = nmsWorld.getWorld(); CompletableFuture future = craftWorld.getChunkAtAsync(chunkX, chunkZ, true); try { - CraftChunk chunk = (CraftChunk) future.get(); + final CraftChunk chunk = (CraftChunk) future.get(); + TaskManager.IMP.task(() -> chunk.addPluginChunkTicket(WorldEditPlugin.getInstance())); return chunk.getHandle(); } catch (Throwable e) { e.printStackTrace(); } } // TODO optimize - return TaskManager.IMP.sync(() -> nmsWorld.getChunkAt(chunkX, chunkZ)); + return TaskManager.IMP.sync(() -> { + Chunk chunk = nmsWorld.getChunkAt(chunkX, chunkZ); + chunk.bukkitChunk.addPluginChunkTicket(WorldEditPlugin.getInstance()); + return chunk; + }); } public static PlayerChunk getPlayerChunk(WorldServer nmsWorld, final int chunkX, final int chunkZ) { @@ -199,51 +209,52 @@ public final class BukkitAdapter_1_16_4 extends NMSAdapter { return; } if (playerChunk.hasBeenLoaded()) { - TaskManager.IMP.sync(() -> { - ChunkCoordIntPair chunkCoordIntPair = new ChunkCoordIntPair(chunkX, chunkZ); - Optional optional = ((Either) playerChunk.a().getNow(PlayerChunk.UNLOADED_CHUNK)).left(); - if (optional.isPresent()) { - PacketPlayOutMapChunk chunkpacket = new PacketPlayOutMapChunk(optional.get(), 65535); + ChunkCoordIntPair chunkCoordIntPair = new ChunkCoordIntPair(chunkX, chunkZ); + Optional optional = ((Either) playerChunk.a().getNow(PlayerChunk.UNLOADED_CHUNK)).left(); + if (optional.isPresent()) { + PacketPlayOutMapChunk chunkpacket = new PacketPlayOutMapChunk(optional.get(), 65535); + playerChunk.players.a(chunkCoordIntPair, false).forEach(p -> { + p.playerConnection.sendPacket(chunkpacket); + }); + + if (lighting) { + //This needs to be true otherwise Minecraft will update lighting from/at the chunk edges (bad) + boolean trustEdges = true; + PacketPlayOutLightUpdate packet = + new PacketPlayOutLightUpdate(chunkCoordIntPair, nmsWorld.getChunkProvider().getLightEngine(), + trustEdges); playerChunk.players.a(chunkCoordIntPair, false).forEach(p -> { - p.playerConnection.sendPacket(chunkpacket); + p.playerConnection.sendPacket(packet); }); + } + } else if (PaperLib.isPaper()) { + //Require generic here to work with multiple dependencies trying to take control. + PooledLinkedHashSets.PooledObjectLinkedOpenHashSet objects = + nmsWorld.getChunkProvider().playerChunkMap.playerViewDistanceNoTickMap + .getObjectsInRange(chunkX, chunkZ); + if (objects == null) { + return; + } + for (Object obj : objects.getBackingSet()) { + if (obj == null) { + continue; + } + EntityPlayer p = (EntityPlayer) obj; + Chunk chunk = nmsWorld.getChunkProvider().getChunkAtIfLoadedImmediately(chunkX, chunkZ); + if (chunk != null) { + PacketPlayOutMapChunk chunkpacket = new PacketPlayOutMapChunk(chunk, 65535); + p.playerConnection.sendPacket(chunkpacket); - if (lighting) { - boolean trustEdges = true; //This needs to be true otherwise Minecraft will update lighting from/at the chunk edges (bad) - PacketPlayOutLightUpdate packet = new PacketPlayOutLightUpdate(chunkCoordIntPair, nmsWorld.getChunkProvider().getLightEngine(), trustEdges); - playerChunk.players.a(chunkCoordIntPair, false).forEach(p -> { + if (lighting) { + //This needs to be true otherwise Minecraft will update lighting from/at the chunk edges (bad) + boolean trustEdges = true; + PacketPlayOutLightUpdate packet = new PacketPlayOutLightUpdate(chunkCoordIntPair, + nmsWorld.getChunkProvider().getLightEngine(), trustEdges); p.playerConnection.sendPacket(packet); - }); - } - } else if (PaperLib.isPaper()) { - //Require generic here to work with multiple dependencies trying to take control. - PooledLinkedHashSets.PooledObjectLinkedOpenHashSet objects = - nmsWorld.getChunkProvider().playerChunkMap.playerViewDistanceNoTickMap.getObjectsInRange(chunkX, chunkZ); - if (objects == null) { - return null; - } - for (Object obj : objects.getBackingSet()) { - if (obj == null) { - continue; - } - EntityPlayer p = (EntityPlayer) obj; - Chunk chunk = nmsWorld.getChunkProvider().getChunkAtIfLoadedImmediately(chunkX, chunkZ); - if (chunk != null) { - PacketPlayOutMapChunk chunkpacket = new PacketPlayOutMapChunk(chunk, 65535); - p.playerConnection.sendPacket(chunkpacket); - - if (lighting) { - boolean trustEdges = - true; //This needs to be true otherwise Minecraft will update lighting from/at the chunk edges (bad) - PacketPlayOutLightUpdate packet = - new PacketPlayOutLightUpdate(chunkCoordIntPair, nmsWorld.getChunkProvider().getLightEngine(), trustEdges); - p.playerConnection.sendPacket(packet); - } } } } - return null; - }); + } } } diff --git a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_4/BukkitGetBlocks_1_16_4.java b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_4/BukkitGetBlocks_1_16_4.java index 8fb195641..90807bca5 100644 --- a/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_4/BukkitGetBlocks_1_16_4.java +++ b/worldedit-bukkit/src/main/java/com/boydti/fawe/bukkit/adapter/mc1_16_4/BukkitGetBlocks_1_16_4.java @@ -612,6 +612,7 @@ public class BukkitGetBlocks_1_16_4 extends CharGetBlocks { task.run(); } } + nmsChunk.bukkitChunk.removePluginChunkTicket(WorldEditPlugin.getInstance()); if (callback == null) { if (finalizer != null) { finalizer.run();