From 9a8ea2f1d45b9214b727ec7f95af856b3f92c8de Mon Sep 17 00:00:00 2001 From: Lixfel Date: Mon, 7 Sep 2020 11:52:02 +0200 Subject: [PATCH] Prevent thread stacking with syncing up Sending mbc-Packet sync takes now around 0-2ms, so its okeyish Signed-off-by: Lixfel --- .../fightsystem/utils/TechHider_15.java | 40 ++++++++++--------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/FightSystem_15/src/de/steamwar/fightsystem/utils/TechHider_15.java b/FightSystem_15/src/de/steamwar/fightsystem/utils/TechHider_15.java index 04f0c1d..1e2d231 100644 --- a/FightSystem_15/src/de/steamwar/fightsystem/utils/TechHider_15.java +++ b/FightSystem_15/src/de/steamwar/fightsystem/utils/TechHider_15.java @@ -34,11 +34,13 @@ import de.steamwar.fightsystem.Config; import de.steamwar.fightsystem.IFightSystem; import io.netty.buffer.ByteBuf; import io.netty.buffer.UnpooledByteBufAllocator; +import net.minecraft.server.v1_15_R1.Block; +import net.minecraft.server.v1_15_R1.ChunkSection; import net.minecraft.server.v1_15_R1.PacketPlayOutMapChunk; import org.bukkit.Bukkit; -import org.bukkit.Location; import org.bukkit.World; import org.bukkit.craftbukkit.v1_15_R1.CraftChunk; +import org.bukkit.craftbukkit.v1_15_R1.CraftWorld; import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer; import org.bukkit.entity.Player; @@ -211,7 +213,7 @@ public class TechHider_15 { }*/ final int cY = chunkY; - Bukkit.getScheduler().runTaskAsynchronously(IFightSystem.getPlugin(), + Bukkit.getScheduler().runTask(IFightSystem.getPlugin(), () -> fullChunkHider(p, chunkX, cY, chunkZ)); } @@ -230,38 +232,38 @@ public class TechHider_15 { } private static void fullChunkHider(Player player, int chunkX, int chunkY, int chunkZ){ - ArrayList blockChangeList = new ArrayList<>(); + if(!player.isOnline()) + return; - Location location = new Location(WORLD, chunkX * 16f, chunkY * 16f, chunkZ * 16f); - int maxXp1 = (chunkX+1) * 16; - int maxYp1 = (chunkY+1) * 16; - int maxZp1 = (chunkZ+1) * 16; - while(location.getBlockX() < maxXp1){ - while(location.getBlockY() < maxYp1){ - while(location.getBlockZ() < maxZp1){ - if(Config.HiddenBlockTags.contains(WORLD.getBlockAt(location).getType().name())) - blockChangeList.add(new MultiBlockChangeInfo(location, WRAPPED_BLOCK_DATA)); - location.setZ(location.getZ() + 1); + ArrayList blockChangeList = new ArrayList<>(); + ChunkCoordIntPair chunkCoords = new ChunkCoordIntPair(chunkX, chunkZ); + ChunkSection chunk = ((CraftWorld) WORLD).getHandle().getChunkAt(chunkX, chunkZ).getSections()[chunkY]; //This takes ~70ms async. + if(ChunkSection.a(chunk)) + return; + + int minY = chunkY * 16; + for(int x = 0; x < 16; x++){ + for(int y = 0; y < 16; y++){ + for(int z = 0; z < 16; z++){ + if(Config.HiddenBlocks.contains(Block.REGISTRY_ID.getId(chunk.getType(x, y, z)))) + blockChangeList.add(new MultiBlockChangeInfo((short)(x << 12 | z << 8 | (y + minY)), WRAPPED_BLOCK_DATA, chunkCoords)); } - location.setY(location.getY() + 1); - location.setZ(chunkZ * 16f); } - location.setX(location.getX() + 1); - location.setY(chunkY * 16f); - location.setZ(chunkZ * 16f); } if(blockChangeList.isEmpty()) return; PacketContainer packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.MULTI_BLOCK_CHANGE); - packet.getChunkCoordIntPairs().write(0, new ChunkCoordIntPair(chunkX, chunkZ)); + packet.getChunkCoordIntPairs().write(0, chunkCoords); packet.getMultiBlockChangeInfoArrays().write(0, blockChangeList.toArray(new MultiBlockChangeInfo[0])); try { ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet, false); } catch (InvocationTargetException e) { throw new SecurityException("Something went wrong sending the multiblockchange packet", e); + } catch (NullPointerException e){ + //ignored, player offline } } } -- 2.39.2