From b6d622b1702271b490bbe03b87f6d12101c7bf8f Mon Sep 17 00:00:00 2001 From: Lixfel Date: Wed, 25 Sep 2019 18:21:20 +0200 Subject: [PATCH] Techhider speed improvement --- .../steamwar/fightsystem/utils/TechHider.java | 48 +++++++++++-------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/src/de/steamwar/fightsystem/utils/TechHider.java b/src/de/steamwar/fightsystem/utils/TechHider.java index 09b096b..52be39a 100644 --- a/src/de/steamwar/fightsystem/utils/TechHider.java +++ b/src/de/steamwar/fightsystem/utils/TechHider.java @@ -16,6 +16,8 @@ import com.google.common.primitives.Bytes; import de.steamwar.fightsystem.FightSystem; import de.steamwar.fightsystem.fight.Fight; import de.steamwar.fightsystem.fight.FightTeam; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.UnpooledByteBufAllocator; import javafx.util.Pair; import net.minecraft.server.v1_12_R1.PacketPlayOutMapChunk; import org.bukkit.Bukkit; @@ -105,45 +107,49 @@ public class TechHider { changed = false; byte [] data = byteArray.read(0); - List newData = new ArrayList<>(Bytes.asList(data)); + ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.directBuffer(data.length); int i = 0; - while(i < newData.size()){ - byte bitsPerBlock = newData.get(i++); + while(i < data.length){ + byte bitsPerBlock = data[i++]; + buffer.writeByte(bitsPerBlock); if(bitsPerBlock < 4) bitsPerBlock = 4; else if(bitsPerBlock > 8){ bitsPerBlock = 13; i++; + buffer.writeByte(data[1]); } if(bitsPerBlock != 13){ - int paletteLength = readVarInt(Bytes.toArray(newData), i); - i += readVarIntLength(Bytes.toArray(newData), i); + int paletteLength = readVarInt(data, i); + int paletteLengthLength = readVarIntLength(data, i); + buffer.writeBytes(data, i, paletteLengthLength); + i += paletteLengthLength; for(int actPaletteId = 0; actPaletteId < paletteLength; actPaletteId++){ - int actPalette = readVarInt(Bytes.toArray(newData), i); - int actPaletteLength = readVarIntLength(Bytes.toArray(newData), i); + int actPalette = readVarInt(data, i); + int actPaletteLength = readVarIntLength(data, i); int blockId = actPalette >> 4; if(Config.HiddenBlocks.contains(blockId)){ byte[] a = writeVarInt(obfuscateShift4); - for(int j = 0; j < actPaletteLength; j++){ - newData.remove(i); - } - for(byte b : a){ - newData.add(i++, b); - } + buffer.writeBytes(a); changed = true; }else{ - i += actPaletteLength; + buffer.writeBytes(data, i, actPaletteLength); } + i += actPaletteLength; } - int dataArrayLength = readVarInt(Bytes.toArray(newData), i); - i += readVarIntLength(Bytes.toArray(newData), i); + int dataArrayLength = readVarInt(data, i); + int dataArrayLengthLength = readVarIntLength(data, i); + buffer.writeBytes(data, i, dataArrayLength * 8 + dataArrayLengthLength); + i += dataArrayLengthLength; i += dataArrayLength * 8; }else{ - int dataArrayLength = readVarInt(Bytes.toArray(newData), i); - i += readVarIntLength(Bytes.toArray(newData), i); + int dataArrayLength = readVarInt(data, i); + int dataArrayLengthLength = readVarIntLength(data, i); + buffer.writeBytes(data, i, dataArrayLength*8 + dataArrayLengthLength); + i += dataArrayLengthLength; /*int arrayEnd = dataArrayLength * 8 + i; int bitsOver = 8; //9 bits (id) + 4 bits (metadata) while(i < arrayEnd){ @@ -165,11 +171,13 @@ public class TechHider { i += dataArrayLength * 8; } + buffer.writeBytes(data, i, 4096); i += 4096; //Skylight (Not in Nether/End!!!) 2048 + Blocklight 2048 } if(changed){ - byteArray.write(0, Bytes.toArray(newData)); + buffer.readBytes(data); + byteArray.write(0, data); } } }).start(4 * threadMultiplier); @@ -349,7 +357,7 @@ public class TechHider { } private static byte[] writeVarInt(int value) { - ArrayList buffer = new ArrayList<>(); + List buffer = new ArrayList<>(5); do { byte temp = (byte)(value & 0b01111111); // Note: >>> means that the sign bit is shifted with the rest of the number rather than being left alone