From 9fa104a533dda7255c1e6497350388ace7263c0a Mon Sep 17 00:00:00 2001 From: creeper123123321 <7974274+creeper123123321@users.noreply.github.com> Date: Wed, 31 Mar 2021 20:41:59 -0300 Subject: [PATCH] i think i fixed 1.7 chunks? --- .../github/creeper123123321/viaaas/Util.kt | 5 ++- .../viaaas/packet/play/PluginMessage.kt | 2 +- .../id47toid5/chunks/Chunk1_8to1_7_6_10.kt | 28 +++++++++-------- .../chunks/ChunkPacketTransformer.kt | 31 +++++++------------ .../id47toid5/chunks/ExtendedBlockStorage.kt | 4 +-- .../id47toid5/packets/EntityPackets.kt | 2 +- .../id47toid5/packets/WorldPackets.kt | 4 +-- 7 files changed, 35 insertions(+), 41 deletions(-) diff --git a/src/main/kotlin/com/github/creeper123123321/viaaas/Util.kt b/src/main/kotlin/com/github/creeper123123321/viaaas/Util.kt index 084e146..e9e154d 100644 --- a/src/main/kotlin/com/github/creeper123123321/viaaas/Util.kt +++ b/src/main/kotlin/com/github/creeper123123321/viaaas/Util.kt @@ -46,8 +46,7 @@ fun resolveSrv(address: String, port: Int): Pair { val record = (attr.get(0) as String).split(" ") return record[3] to record[2].toInt() } - } catch (ignored: NameNotFoundException) { - } catch (ignored: ServiceUnavailableException) { // DuckDNS workaround + } catch (ignored: Exception) { // DuckDNS workaround } } return address to port @@ -171,4 +170,4 @@ fun generateServerId() = ByteArray(13).let { // https://developer.mozilla.org/en-US/docs/Glossary/Base64 133% of original } -fun Int.parseProtocol() = ProtocolVersion.getProtocol(this) \ No newline at end of file +fun Int.parseProtocol() = ProtocolVersion.getProtocol(this) diff --git a/src/main/kotlin/com/github/creeper123123321/viaaas/packet/play/PluginMessage.kt b/src/main/kotlin/com/github/creeper123123321/viaaas/packet/play/PluginMessage.kt index 6e3d84f..6e5d3e5 100644 --- a/src/main/kotlin/com/github/creeper123123321/viaaas/packet/play/PluginMessage.kt +++ b/src/main/kotlin/com/github/creeper123123321/viaaas/packet/play/PluginMessage.kt @@ -41,7 +41,7 @@ class PluginMessage : Packet { fun writeExtendedForgeShort(buf: ByteBuf, toWrite: Int) { var low = toWrite and 0x7FFF - val high = toWrite and 0x7F8000 shr 15 + val high = toWrite.and(0x7F8000).shr(15) if (high != 0) { low = low or 0x8000 } diff --git a/src/main/kotlin/com/github/creeper123123321/viaaas/protocol/id47toid5/chunks/Chunk1_8to1_7_6_10.kt b/src/main/kotlin/com/github/creeper123123321/viaaas/protocol/id47toid5/chunks/Chunk1_8to1_7_6_10.kt index 1303df5..90c6e7a 100644 --- a/src/main/kotlin/com/github/creeper123123321/viaaas/protocol/id47toid5/chunks/Chunk1_8to1_7_6_10.kt +++ b/src/main/kotlin/com/github/creeper123123321/viaaas/protocol/id47toid5/chunks/Chunk1_8to1_7_6_10.kt @@ -16,7 +16,7 @@ class Chunk1_8to1_7_6_10( var blockBiomeArray = ByteArray(256) fun filterChunk(storageArray: ExtendedBlockStorage?, i: Int) = - storageArray != null && primaryBitMask and 1 shl i != 0 + storageArray != null && (primaryBitMask.and(1 shl i) != 0) && (!groundUp || storageArray.isEmpty) fun get1_8Data(): ByteArray { @@ -30,14 +30,14 @@ class Chunk1_8to1_7_6_10( for (ind in blockIds.indices) { val id = blockIds[ind].toInt() and 255 val px = ind and 15 - val py = ind shr 8 and 15 - val pz = ind shr 4 and 15 + val py = ind.shr(8).and(15) + val pz = ind.shr(4).and(15) val data = nibblearray[px, py, pz].toInt() //data = SpigotDebreakifier.getCorrectedData(id, data); val `val` = (id shl 4 or data).toChar() buf.writeByte(`val`.toInt() and 255) - buf.writeByte(`val`.toInt() shr 8 and 255) + buf.writeByte(`val`.toInt().shr(8).and(255)) } } filteredChunks.forEach { @@ -60,38 +60,40 @@ class Chunk1_8to1_7_6_10( init { val input = Unpooled.wrappedBuffer(data) for (i in storageSections.indices) { - if (primaryBitMask and 1 shl i != 0) { - val storageSection = storageSections.getOrElse(i) { - ExtendedBlockStorage(i shl 4, skyLight).also { storageSections[i] = it } - }!! + if ((primaryBitMask and (1 shl i)) != 0) { + var storageSection = storageSections[i] + if (storageSection == null){ + storageSection = ExtendedBlockStorage(i shl 4, skyLight) + storageSections[i] = storageSection + } storageSection.blockLSBArray = input.readByteArray(4096) } else if (storageSections[i] != null && groundUp) { storageSections[i] = null } } for (i in storageSections.indices) { - if (primaryBitMask and 1 shl i != 0 && storageSections[i] != null) { + if (primaryBitMask.and(1 shl i) != 0 && storageSections[i] != null) { storageSections[i]!!.metadataArray.handle = input.readByteArray(4096 / 2) } } for (i in storageSections.indices) { - if (primaryBitMask and 1 shl i != 0 && storageSections[i] != null) { + if (primaryBitMask.and(1 shl i) != 0 && storageSections[i] != null) { storageSections[i]!!.blocklightArray.handle = input.readByteArray(4096 / 2) } } if (skyLight) { for (i in storageSections.indices) { - if (primaryBitMask and 1 shl i != 0 && storageSections[i] != null) { + if (primaryBitMask.and(1 shl i) != 0 && storageSections[i] != null) { storageSections[i]!!.skylightArray!!.handle = input.readByteArray(4096 / 2) } } } for (i in storageSections.indices) { - if (addBitMask and 1 shl i != 0) { + if (addBitMask.and(1 shl i) != 0) { if (storageSections[i] == null) { input.skipBytes(2048) } else { - var msbArray = storageSections[i]!!.blockMSBArray ?: storageSections[i]!!.createBlockMSBArray() + val msbArray = storageSections[i]!!.blockMSBArray ?: storageSections[i]!!.createBlockMSBArray() msbArray!!.handle = input.readByteArray(4096 / 2) } } else if (groundUp && storageSections[i] != null && storageSections[i]!!.blockMSBArray != null) { diff --git a/src/main/kotlin/com/github/creeper123123321/viaaas/protocol/id47toid5/chunks/ChunkPacketTransformer.kt b/src/main/kotlin/com/github/creeper123123321/viaaas/protocol/id47toid5/chunks/ChunkPacketTransformer.kt index 6af0ab4..ea2ba2c 100644 --- a/src/main/kotlin/com/github/creeper123123321/viaaas/protocol/id47toid5/chunks/ChunkPacketTransformer.kt +++ b/src/main/kotlin/com/github/creeper123123321/viaaas/protocol/id47toid5/chunks/ChunkPacketTransformer.kt @@ -6,7 +6,6 @@ import us.myles.ViaVersion.api.minecraft.BlockChangeRecord1_8 import us.myles.ViaVersion.api.type.Type import us.myles.ViaVersion.api.type.types.CustomByteType import java.io.IOException -import java.util.* import java.util.stream.IntStream import java.util.zip.DataFormatException import java.util.zip.Inflater @@ -23,14 +22,14 @@ object ChunkPacketTransformer { val compressedSize = packetWrapper.read(Type.INT) val customByteType = CustomByteType(compressedSize) val data = packetWrapper.read(customByteType) - var k = 0 - var l = 0 + var countOfPrimary = 0 + var countOfAdditional = 0 for (j in 0..15) { - k += primaryBitMask shr j and 1 - l += addBitMask shr j and 1 + countOfPrimary += primaryBitMask.shr(j).and(1) + countOfAdditional += addBitMask.shr(j).and(1) } - var uncompressedSize = 12288 * k - uncompressedSize += 2048 * l + var uncompressedSize = 12288 * countOfPrimary + uncompressedSize += 2048 * countOfAdditional if (groundUp) { uncompressedSize += 256 } @@ -39,29 +38,23 @@ object ChunkPacketTransformer { inflater.setInput(data, 0, compressedSize) try { inflater.inflate(uncompressedData) - } catch (ex: DataFormatException) { - throw IOException("Bad compressed data format") } finally { inflater.end() } val chunk = Chunk1_8to1_7_6_10(uncompressedData, primaryBitMask, addBitMask, true, groundUp) - var field = PacketWrapper::class.java.getDeclaredField("packetValues") - field.isAccessible = true - (field[packetWrapper] as MutableList<*>).clear() - field = PacketWrapper::class.java.getDeclaredField("readableObjects") - field.isAccessible = true - (field[packetWrapper] as LinkedList<*>).clear() - field = PacketWrapper::class.java.getDeclaredField("inputBuffer") + + packetWrapper.clearPacket() + val field = PacketWrapper::class.java.getDeclaredField("inputBuffer") field.isAccessible = true val buffer = field[packetWrapper] as ByteBuf + buffer.clear() buffer.writeInt(chunkX) buffer.writeInt(chunkZ) buffer.writeBoolean(groundUp) buffer.writeShort(primaryBitMask) - val finaldata = chunk.get1_8Data() - Type.VAR_INT.writePrimitive(buffer, finaldata.size) - buffer.writeBytes(finaldata) + val finalData = chunk.get1_8Data() + Type.BYTE_ARRAY_PRIMITIVE.write(buffer, finalData) } @Throws(Exception::class) diff --git a/src/main/kotlin/com/github/creeper123123321/viaaas/protocol/id47toid5/chunks/ExtendedBlockStorage.kt b/src/main/kotlin/com/github/creeper123123321/viaaas/protocol/id47toid5/chunks/ExtendedBlockStorage.kt index de71e71..61774c4 100644 --- a/src/main/kotlin/com/github/creeper123123321/viaaas/protocol/id47toid5/chunks/ExtendedBlockStorage.kt +++ b/src/main/kotlin/com/github/creeper123123321/viaaas/protocol/id47toid5/chunks/ExtendedBlockStorage.kt @@ -2,7 +2,7 @@ package com.github.creeper123123321.viaaas.protocol.id47toid5.chunks import us.myles.ViaVersion.api.minecraft.chunks.NibbleArray -class ExtendedBlockStorage(val yLocation: Int, paramBoolean: Boolean) { +class ExtendedBlockStorage(val yLocation: Int, hasSkyLight: Boolean) { var blockLSBArray: ByteArray = ByteArray(4096) var blockMSBArray: NibbleArray? = null var metadataArray: NibbleArray @@ -52,7 +52,7 @@ class ExtendedBlockStorage(val yLocation: Int, paramBoolean: Boolean) { init { metadataArray = NibbleArray(blockLSBArray.size) blocklightArray = NibbleArray(blockLSBArray.size) - if (paramBoolean) { + if (hasSkyLight) { skylightArray = NibbleArray(blockLSBArray.size) } } diff --git a/src/main/kotlin/com/github/creeper123123321/viaaas/protocol/id47toid5/packets/EntityPackets.kt b/src/main/kotlin/com/github/creeper123123321/viaaas/protocol/id47toid5/packets/EntityPackets.kt index 598f7a8..1f30fa2 100644 --- a/src/main/kotlin/com/github/creeper123123321/viaaas/protocol/id47toid5/packets/EntityPackets.kt +++ b/src/main/kotlin/com/github/creeper123123321/viaaas/protocol/id47toid5/packets/EntityPackets.kt @@ -96,7 +96,7 @@ fun Protocol1_8To1_7_6.registerEntityPackets() { if (type.toInt() == 70) { val id = data val metadata = data shr 16 - data = id or metadata shl 12 + data = id.or(metadata shl 12) } if (type.toInt() == 50 || type.toInt() == 70 || type.toInt() == 74) y -= 16 packetWrapper.set(Type.INT, 0, x) diff --git a/src/main/kotlin/com/github/creeper123123321/viaaas/protocol/id47toid5/packets/WorldPackets.kt b/src/main/kotlin/com/github/creeper123123321/viaaas/protocol/id47toid5/packets/WorldPackets.kt index 56f3007..6d8fc8f 100644 --- a/src/main/kotlin/com/github/creeper123123321/viaaas/protocol/id47toid5/packets/WorldPackets.kt +++ b/src/main/kotlin/com/github/creeper123123321/viaaas/protocol/id47toid5/packets/WorldPackets.kt @@ -40,7 +40,7 @@ fun Protocol1_8To1_7_6.registerWorldPackets() { handler { packetWrapper -> val blockId: Int = packetWrapper.read(Type.VAR_INT) val meta = packetWrapper.read(Type.UNSIGNED_BYTE).toInt() - packetWrapper.write(Type.VAR_INT, blockId shl 4 or (meta and 15)) + packetWrapper.write(Type.VAR_INT, blockId.shl(4).or(meta and 15)) } //Block Data } }) @@ -168,7 +168,7 @@ fun Protocol1_8To1_7_6.registerWorldPackets() { packetWrapper.write(Type.BYTE, mapData!!.scale) packetWrapper.write(Type.VAR_INT, mapData!!.mapIcons.size) for (mapIcon in mapData!!.mapIcons) { - packetWrapper.write(Type.BYTE, (mapIcon.direction.toInt() shl 4 or mapIcon.type.toInt() and 0xF).toByte()) + packetWrapper.write(Type.BYTE, (mapIcon.direction.toInt().shl(4).or(mapIcon.type.toInt() and 0xF)).toByte()) packetWrapper.write(Type.BYTE, mapIcon.x) packetWrapper.write(Type.BYTE, mapIcon.z) }