diff --git a/src/main/kotlin/com/viaversion/aas/handler/state/LoginState.kt b/src/main/kotlin/com/viaversion/aas/handler/state/LoginState.kt index f69637d..562a71c 100644 --- a/src/main/kotlin/com/viaversion/aas/handler/state/LoginState.kt +++ b/src/main/kotlin/com/viaversion/aas/handler/state/LoginState.kt @@ -1,24 +1,22 @@ package com.viaversion.aas.handler.state +import com.google.gson.Gson import com.viaversion.aas.* import com.viaversion.aas.codec.CompressionCodec import com.viaversion.aas.codec.CryptoCodec import com.viaversion.aas.handler.MinecraftHandler import com.viaversion.aas.handler.forward -import com.viaversion.aas.packet.* +import com.viaversion.aas.packet.Packet import com.viaversion.aas.packet.login.* -import com.google.gson.Gson import com.viaversion.aas.util.StacklessException -import io.ktor.client.request.* +import io.netty.channel.Channel import io.netty.channel.ChannelHandlerContext import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import us.myles.ViaVersion.packets.State -import java.util.* import java.util.concurrent.CompletableFuture import javax.crypto.Cipher -import io.netty.channel.Channel class LoginState : MinecraftConnectionState { val callbackPlayerId = CompletableFuture() @@ -179,7 +177,7 @@ class LoginState : MinecraftConnectionState { callbackPlayerId.whenComplete { id, e -> if (e != null) { - disconnect(handler, "Profile error: $e") + handler.data.frontChannel.pipeline().fireExceptionCaught(StacklessException("Profile error: $e", e)) } else { mcLogger.info("Login: ${handler.endRemoteAddress} $frontName $id") if (frontOnline != null) { diff --git a/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/chunks/Chunk1_8to1_7_6_10.kt b/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/chunks/Chunk1_8to1_7_6_10.kt index e3dbc93..fda769a 100644 --- a/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/chunks/Chunk1_8to1_7_6_10.kt +++ b/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/chunks/Chunk1_8to1_7_6_10.kt @@ -6,9 +6,9 @@ import io.netty.buffer.ByteBufAllocator import io.netty.buffer.Unpooled class Chunk1_8to1_7_6_10( - data: ByteArray?, + data: ByteArray, private val primaryBitMask: Int, - addBitMask: Int, + additionalBitMask: Int, private val skyLight: Boolean, private val groundUp: Boolean ) { @@ -22,7 +22,6 @@ class Chunk1_8to1_7_6_10( fun get1_8Data(): ByteArray { val buf = ByteBufAllocator.DEFAULT.buffer() try { - var finalSize = 0 val filteredChunks = storageSections.filterIndexed { i, value -> filterChunk(value, i) }.filterNotNull() filteredChunks.forEach { val blockIds = it.blockLSBArray @@ -57,12 +56,14 @@ class Chunk1_8to1_7_6_10( } } + fun filterBitmask(bitmask: Int, i: Int) = (bitmask and (1 shl i)) != 0 + init { val input = Unpooled.wrappedBuffer(data) for (i in storageSections.indices) { - if ((primaryBitMask and (1 shl i)) != 0) { + if (filterBitmask(primaryBitMask, i)) { var storageSection = storageSections[i] - if (storageSection == null){ + if (storageSection == null) { storageSection = ExtendedBlockStorage(i shl 4, skyLight) storageSections[i] = storageSection } @@ -72,29 +73,29 @@ class Chunk1_8to1_7_6_10( } } for (i in storageSections.indices) { - if (primaryBitMask.and(1 shl i) != 0 && storageSections[i] != null) { + if (filterBitmask(primaryBitMask, i) && 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) { - storageSections[i]!!.blocklightArray.handle = input.readByteArray(4096 / 2) + if (filterBitmask(primaryBitMask, i) && 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 (filterBitmask(primaryBitMask, i) && storageSections[i] != null) { storageSections[i]!!.skylightArray!!.handle = input.readByteArray(4096 / 2) } } } for (i in storageSections.indices) { - if (addBitMask.and(1 shl i) != 0) { + if (filterBitmask(additionalBitMask, i)) { if (storageSections[i] == null) { input.skipBytes(2048) } else { val msbArray = storageSections[i]!!.blockMSBArray ?: storageSections[i]!!.createBlockMSBArray() - msbArray!!.handle = input.readByteArray(4096 / 2) + msbArray.handle = input.readByteArray(4096 / 2) } } else if (groundUp && storageSections[i] != null && storageSections[i]!!.blockMSBArray != null) { storageSections[i]!!.clearMSBArray() diff --git a/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/chunks/ChunkPacketTransformer.kt b/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/chunks/ChunkPacketTransformer.kt index b43552d..9b3f396 100644 --- a/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/chunks/ChunkPacketTransformer.kt +++ b/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/chunks/ChunkPacketTransformer.kt @@ -60,18 +60,18 @@ object ChunkPacketTransformer { @Throws(Exception::class) fun transformChunkBulk(packetWrapper: PacketWrapper) { val columnCount = packetWrapper.read(Type.SHORT).toInt() //short1 - val size = packetWrapper.read(Type.INT) //size + val compressedSize = packetWrapper.read(Type.INT) //size val skyLightSent = packetWrapper.read(Type.BOOLEAN) //h val chunkX = IntArray(columnCount) //a val chunkZ = IntArray(columnCount) //b val primaryBitMask = IntArray(columnCount) //c val addBitMask = IntArray(columnCount) //d - val inflatedBuffers = arrayOfNulls(columnCount.toInt()) //inflatedBuffers - var customByteType = CustomByteType(size) + val inflatedBuffers = arrayOfNulls(columnCount) //inflatedBuffers + var customByteType = CustomByteType(compressedSize) val buildBuffer = packetWrapper.read(customByteType) //buildBuffer var data = ByteArray(196864 * columnCount) //abyte val inflater = Inflater() - inflater.setInput(buildBuffer, 0, size) + inflater.setInput(buildBuffer, 0, compressedSize) try { inflater.inflate(data) } catch (ex: DataFormatException) { @@ -79,51 +79,42 @@ object ChunkPacketTransformer { } finally { inflater.end() } - var i = 0 - for (j in 0 until columnCount) { - chunkX[j] = packetWrapper.read(Type.INT) - chunkZ[j] = packetWrapper.read(Type.INT) - primaryBitMask[j] = packetWrapper.read(Type.SHORT).toInt() - addBitMask[j] = packetWrapper.read(Type.SHORT).toInt() - var k = 0 - var l = 0 - var i1: Int - i1 = 0 - while (i1 < 16) { - k += primaryBitMask[j] shr i1 and 1 - l += addBitMask[j] shr i1 and 1 - ++i1 + var cursor = 0 + for (column in 0 until columnCount) { + chunkX[column] = packetWrapper.read(Type.INT) + chunkZ[column] = packetWrapper.read(Type.INT) + primaryBitMask[column] = packetWrapper.read(Type.SHORT).toInt() + addBitMask[column] = packetWrapper.read(Type.SHORT).toInt() + var primaryCount = 0 + var secondaryCount = 0 + (0 until 16).forEach { + primaryCount += primaryBitMask[column] shr it and 1 + secondaryCount += addBitMask[column] shr it and 1 } - i1 = 8192 * k + 256 - i1 += 2048 * l + var copySize = 8192 * primaryCount + 256 + copySize += 2048 * secondaryCount if (skyLightSent) { - i1 += 2048 * k + copySize += 2048 * primaryCount } - inflatedBuffers[j] = ByteArray(i1) - System.arraycopy(data, i, inflatedBuffers[j], 0, i1) - i += i1 + inflatedBuffers[column] = ByteArray(copySize) + System.arraycopy(data, cursor, inflatedBuffers[column]!!, 0, copySize) + cursor += copySize } - val chunks = arrayOfNulls(columnCount.toInt()) - i = 0 - while (i < columnCount) { - chunks[i] = Chunk1_8to1_7_6_10(inflatedBuffers[i], primaryBitMask[i], addBitMask[i], skyLightSent, true) - i++ + val chunks = arrayOfNulls(columnCount) + (0 until columnCount).forEach { + chunks[it] = Chunk1_8to1_7_6_10(inflatedBuffers[it]!!, primaryBitMask[it], addBitMask[it], skyLightSent, true) } packetWrapper.write(Type.BOOLEAN, skyLightSent) packetWrapper.write(Type.VAR_INT, columnCount) - i = 0 - while (i < columnCount) { - packetWrapper.write(Type.INT, chunkX[i]) - packetWrapper.write(Type.INT, chunkZ[i]) - packetWrapper.write(Type.UNSIGNED_SHORT, primaryBitMask[i]) - ++i + (0 until columnCount).forEach { + packetWrapper.write(Type.INT, chunkX[it]) + packetWrapper.write(Type.INT, chunkZ[it]) + packetWrapper.write(Type.UNSIGNED_SHORT, primaryBitMask[it]) } - i = 0 - while (i < columnCount) { - data = chunks[i]!!.get1_8Data() + (0 until columnCount).forEach { + data = chunks[it]!!.get1_8Data() customByteType = CustomByteType(data.size) packetWrapper.write(customByteType, data) - ++i } } diff --git a/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/chunks/ExtendedBlockStorage.kt b/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/chunks/ExtendedBlockStorage.kt index ace8216..03a4165 100644 --- a/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/chunks/ExtendedBlockStorage.kt +++ b/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/chunks/ExtendedBlockStorage.kt @@ -44,9 +44,9 @@ class ExtendedBlockStorage(val yLocation: Int, hasSkyLight: Boolean) { metadataArray = paramNibbleArray } - fun createBlockMSBArray(): NibbleArray? { + fun createBlockMSBArray(): NibbleArray { blockMSBArray = NibbleArray(blockLSBArray.size) - return blockMSBArray + return blockMSBArray!! } init { diff --git a/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/packets/EntityPackets.kt b/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/packets/EntityPackets.kt index 1842b71..7eb0753 100644 --- a/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/packets/EntityPackets.kt +++ b/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/packets/EntityPackets.kt @@ -28,8 +28,8 @@ fun Protocol1_8To1_7_6.registerEntityPackets() { this.registerOutgoing(State.PLAY, 0x0B, 0x0B, object : PacketRemapper() { override fun registerMap() { handler { packetWrapper -> - val entityId: Int = packetWrapper.read(Type.VAR_INT) //Entity Id - val animation: Short = packetWrapper.read(Type.UNSIGNED_BYTE) //Animation + val entityId = packetWrapper.read(Type.VAR_INT) //Entity Id + val animation = packetWrapper.read(Type.UNSIGNED_BYTE) //Animation packetWrapper.clearInputBuffer() if (animation.toInt() == 104 || animation.toInt() == 105) { packetWrapper.id = 0x1C //Entity Metadata @@ -67,12 +67,12 @@ fun Protocol1_8To1_7_6.registerEntityPackets() { map(Type.BYTE) map(Type.INT) handler { packetWrapper -> - val type: Byte = packetWrapper.get(Type.BYTE, 0) - var x: Int = packetWrapper.get(Type.INT, 0) - var y: Int = packetWrapper.get(Type.INT, 1) - var z: Int = packetWrapper.get(Type.INT, 2) - var yaw: Byte = packetWrapper.get(Type.BYTE, 2) - var data: Int = packetWrapper.get(Type.INT, 3) + val type = packetWrapper.get(Type.BYTE, 0) + var x = packetWrapper.get(Type.INT, 0) + var y = packetWrapper.get(Type.INT, 1) + var z = packetWrapper.get(Type.INT, 2) + var yaw = packetWrapper.get(Type.BYTE, 2) + var data = packetWrapper.get(Type.INT, 3) if (type.toInt() == 71) { when (data) { 0 -> { @@ -392,7 +392,7 @@ fun Protocol1_8To1_7_6.registerEntityPackets() { map(Type.FLOAT) //Sideways map(Type.FLOAT) //Forwards handler { packetWrapper -> - val flags: Short = packetWrapper.read(Type.UNSIGNED_BYTE) + val flags = packetWrapper.read(Type.UNSIGNED_BYTE) packetWrapper.write(Type.BOOLEAN, flags and 1 == 1.toShort()) //Jump packetWrapper.write(Type.BOOLEAN, flags and 2 == 2.toShort()) //Unmount } diff --git a/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/packets/InventoryPackets.kt b/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/packets/InventoryPackets.kt index 1221e78..0f50152 100644 --- a/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/packets/InventoryPackets.kt +++ b/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/packets/InventoryPackets.kt @@ -16,22 +16,25 @@ fun Protocol1_8To1_7_6.registerInventoryPackets() { //Open Window this.registerOutgoing(State.PLAY, 0x2D, 0x2D, object : PacketRemapper() { override fun registerMap() { + map(Type.UNSIGNED_BYTE) handler { packetWrapper -> - val windowId = packetWrapper.read(Type.UNSIGNED_BYTE) - packetWrapper.write(Type.UNSIGNED_BYTE, windowId) + val windowId = packetWrapper.get(Type.UNSIGNED_BYTE, 0) val windowType = packetWrapper.read(Type.UNSIGNED_BYTE) packetWrapper.user().get(Windows::class.java)!!.types[windowId] = windowType packetWrapper.write(Type.STRING, getInventoryString(windowType.toInt())) //Inventory Type val title = packetWrapper.read(Type.STRING) //Title val slots = packetWrapper.read(Type.UNSIGNED_BYTE) - val useProvidedWindowTitle: Boolean = packetWrapper.read(Type.BOOLEAN) //Use provided window title - packetWrapper.write(Type.STRING, GsonComponentSerializer.gson().serialize(if (useProvidedWindowTitle) { - LegacyComponentSerializer.legacySection().deserialize(title) - } else { - Component.translatable(title) // todo - })) //Window title + val useProvidedWindowTitle = packetWrapper.read(Type.BOOLEAN) //Use provided window title + val convertedTitle = GsonComponentSerializer.gson().serialize( + if (useProvidedWindowTitle) { + LegacyComponentSerializer.legacySection().deserialize(title) + } else { + Component.translatable(title) // todo + } + ) + packetWrapper.write(Type.STRING, convertedTitle) //Window title packetWrapper.write(Type.UNSIGNED_BYTE, slots) - if (packetWrapper.get(Type.UNSIGNED_BYTE, 0) == 11.toShort()) packetWrapper.passthrough(Type.INT) //Entity Id + if (windowType == 11.toShort()) packetWrapper.passthrough(Type.INT) //Entity Id } } }) @@ -40,8 +43,8 @@ fun Protocol1_8To1_7_6.registerInventoryPackets() { this.registerOutgoing(State.PLAY, 0x2F, 0x2F, object : PacketRemapper() { override fun registerMap() { handler { packetWrapper -> - val windowId: Short = packetWrapper.read(Type.BYTE).toShort() //Window Id - val windowType: Short = packetWrapper.user().get(Windows::class.java)!!.get(windowId).toShort() + val windowId = packetWrapper.read(Type.BYTE).toShort() //Window Id + val windowType = packetWrapper.user().get(Windows::class.java)!!.get(windowId).toShort() packetWrapper.write(Type.BYTE, windowId.toByte()) var slot = packetWrapper.read(Type.SHORT).toInt() if (windowType.toInt() == 4 && slot >= 1) slot += 1 @@ -54,9 +57,10 @@ fun Protocol1_8To1_7_6.registerInventoryPackets() { //Window Items this.registerOutgoing(State.PLAY, 0x30, 0x30, object : PacketRemapper() { override fun registerMap() { + map(Type.UNSIGNED_BYTE) //Window Id handler { packetWrapper -> - val windowId: Short = packetWrapper.passthrough(Type.UNSIGNED_BYTE) //Window Id - val windowType: Short = packetWrapper.user().get(Windows::class.java)!![windowId] + val windowId = packetWrapper.get(Type.UNSIGNED_BYTE, 0) + val windowType = packetWrapper.user().get(Windows::class.java)!![windowId] var items = packetWrapper.read(Types1_7_6_10.COMPRESSED_NBT_ITEM_ARRAY) if (windowType.toInt() == 4) { val old = items @@ -75,10 +79,10 @@ fun Protocol1_8To1_7_6.registerInventoryPackets() { this.registerIncoming(State.PLAY, 0x0E, 0x0E, object : PacketRemapper() { override fun registerMap() { handler { packetWrapper -> - val windowId: Short = packetWrapper.read(Type.UNSIGNED_BYTE) //Window Id + val windowId = packetWrapper.read(Type.UNSIGNED_BYTE) //Window Id packetWrapper.write(Type.BYTE, windowId.toByte()) - val windowType: Short = packetWrapper.user().get(Windows::class.java)!![windowId] - var slot: Int = packetWrapper.read(Type.SHORT).toInt() + val windowType = packetWrapper.user().get(Windows::class.java)!![windowId] + var slot = packetWrapper.read(Type.SHORT).toInt() if (windowType.toInt() == 4) { if (slot == 1) { packetWrapper.cancel() diff --git a/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/packets/PlayerPackets.kt b/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/packets/PlayerPackets.kt index f46785b..30d91d8 100644 --- a/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/packets/PlayerPackets.kt +++ b/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/packets/PlayerPackets.kt @@ -1,5 +1,6 @@ package com.viaversion.aas.protocol.id47toid5.packets +import com.google.common.base.Charsets import com.viaversion.aas.generateOfflinePlayerUuid import com.viaversion.aas.protocol.id47toid5.Protocol1_8To1_7_6 import com.viaversion.aas.protocol.id47toid5.metadata.MetadataRewriter @@ -8,7 +9,7 @@ import com.viaversion.aas.protocol.id47toid5.storage.Scoreboard import com.viaversion.aas.protocol.id47toid5.storage.Tablist import com.viaversion.aas.protocol.xyzToPosition import com.viaversion.aas.protocol.xyzUBytePos -import com.google.common.base.Charsets +import com.viaversion.aas.readRemainingBytes import de.gerrygames.viarewind.protocol.protocol1_7_6_10to1_8.types.CustomStringType import de.gerrygames.viarewind.protocol.protocol1_7_6_10to1_8.types.Types1_7_6_10 import de.gerrygames.viarewind.utils.ChatUtil @@ -17,7 +18,6 @@ import us.myles.ViaVersion.api.PacketWrapper import us.myles.ViaVersion.api.Via import us.myles.ViaVersion.api.entities.Entity1_10Types import us.myles.ViaVersion.api.minecraft.item.Item -import us.myles.ViaVersion.api.minecraft.metadata.Metadata import us.myles.ViaVersion.api.remapper.PacketRemapper import us.myles.ViaVersion.api.remapper.TypeRemapper import us.myles.ViaVersion.api.type.Type @@ -25,7 +25,6 @@ import us.myles.ViaVersion.api.type.types.CustomByteType import us.myles.ViaVersion.api.type.types.version.Types1_8 import us.myles.ViaVersion.packets.State import us.myles.ViaVersion.util.ChatColorUtil -import us.myles.viaversion.libs.opennbt.tag.builtin.CompoundTag import us.myles.viaversion.libs.opennbt.tag.builtin.ListTag import us.myles.viaversion.libs.opennbt.tag.builtin.StringTag import java.nio.charset.StandardCharsets @@ -86,7 +85,7 @@ fun Protocol1_8To1_7_6.registerPlayerPackets() { override fun registerMap() { map(Type.DOUBLE) //x handler { packetWrapper -> - val y: Double = packetWrapper.read(Type.DOUBLE) + val y = packetWrapper.read(Type.DOUBLE) packetWrapper.write(Type.DOUBLE, y - 1.62) //y - fixed value } map(Type.DOUBLE) //z @@ -134,7 +133,8 @@ fun Protocol1_8To1_7_6.registerPlayerPackets() { packetWrapper.write(Types1_8.METADATA_LIST, metadata) val tablist = packetWrapper.user().get(Tablist::class.java)!! var entryByName = tablist.getTabListEntry(name) - if (entryByName == null && name.length > 14) entryByName = tablist.getTabListEntry(name.substring(0, 14)) + if (entryByName == null && name.length > 14) entryByName = + tablist.getTabListEntry(name.substring(0, 14)) val entryByUUID = tablist.getTabListEntry(uuid) if (entryByName == null || entryByUUID == null) { if (entryByName != null || entryByUUID != null) { @@ -148,7 +148,8 @@ fun Protocol1_8To1_7_6.registerPlayerPackets() { val packetPlayerListItem = PacketWrapper(0x38, null, packetWrapper.user()) val newentry = Tablist.TabListEntry(name, uuid) if (entryByName != null || entryByUUID != null) { - newentry.displayName = if (entryByUUID != null) entryByUUID.displayName else entryByName!!.displayName + newentry.displayName = + if (entryByUUID != null) entryByUUID.displayName else entryByName!!.displayName } newentry.properties = properties tablist.add(newentry) @@ -180,7 +181,7 @@ fun Protocol1_8To1_7_6.registerPlayerPackets() { delayedPacket.write(Type.BYTE, yaw) delayedPacket.write(Type.BYTE, pitch) delayedPacket.write(Type.SHORT, item) - delayedPacket.write>(Types1_8.METADATA_LIST, metadata) + delayedPacket.write(Types1_8.METADATA_LIST, metadata) Via.getPlatform().runSync({ try { delayedPacket.send(Protocol1_8To1_7_6::class.java) @@ -211,7 +212,6 @@ fun Protocol1_8To1_7_6.registerPlayerPackets() { }) - //Player List Item this.registerOutgoing(State.PLAY, 0x38, 0x38, object : PacketRemapper() { override fun registerMap() { @@ -280,10 +280,10 @@ fun Protocol1_8To1_7_6.registerPlayerPackets() { //Scoreboard Objective this.registerOutgoing(State.PLAY, 0x3B, 0x3B, object : PacketRemapper() { override fun registerMap() { + map(Type.STRING) // name handler { packetWrapper -> - val name: String = packetWrapper.passthrough(Type.STRING) - val value: String = packetWrapper.read(Type.STRING) - val mode: Byte = packetWrapper.read(Type.BYTE) + val value = packetWrapper.read(Type.STRING) + val mode = packetWrapper.read(Type.BYTE) packetWrapper.write(Type.BYTE, mode) if (mode.toInt() == 0 || mode.toInt() == 2) { packetWrapper.write(Type.STRING, value) @@ -297,14 +297,14 @@ fun Protocol1_8To1_7_6.registerPlayerPackets() { this.registerOutgoing(State.PLAY, 0x3C, 0x3C, object : PacketRemapper() { override fun registerMap() { handler { packetWrapper -> - val name: String = packetWrapper.passthrough(Type.STRING) - val mode: Byte = packetWrapper.passthrough(Type.BYTE) + val name = packetWrapper.passthrough(Type.STRING) + val mode = packetWrapper.passthrough(Type.BYTE) if (mode.toInt() != 1) { - val objective: String = packetWrapper.passthrough(Type.STRING) + val objective = packetWrapper.passthrough(Type.STRING) packetWrapper.user().get(Scoreboard::class.java)!!.put(name, objective) packetWrapper.write(Type.VAR_INT, packetWrapper.read(Type.INT)) } else { - val objective: String = packetWrapper.user().get(Scoreboard::class.java)!!.get(name) + val objective = packetWrapper.user().get(Scoreboard::class.java)!!.get(name) packetWrapper.write(Type.STRING, objective) } } @@ -329,8 +329,8 @@ fun Protocol1_8To1_7_6.registerPlayerPackets() { if (mode.toInt() == 0 || mode.toInt() == 3 || mode.toInt() == 4) { val count = packetWrapper.read(Type.SHORT).toInt() val type = CustomStringType(count) - val entries: Array = packetWrapper.read(type) - packetWrapper.write>(Type.STRING_ARRAY, entries) + val entries = packetWrapper.read(type) + packetWrapper.write(Type.STRING_ARRAY, entries) } } } @@ -396,14 +396,18 @@ fun Protocol1_8To1_7_6.registerPlayerPackets() { override fun registerMap() { map(Type.STRING) handler { packetWrapper -> - val channel: String = packetWrapper.get(Type.STRING, 0) + val channel = packetWrapper.get(Type.STRING, 0) if (channel.equals("MC|ItemName", ignoreCase = true)) { val name: ByteArray = packetWrapper.read(Type.STRING).toByteArray(Charsets.UTF_8) packetWrapper.write(Type.REMAINING_BYTES, name) - } else if (channel.equals("MC|BEdit", ignoreCase = true) || channel.equals("MC|BSign", ignoreCase = true)) { + } else if (channel.equals("MC|BEdit", ignoreCase = true) || channel.equals( + "MC|BSign", + ignoreCase = true + ) + ) { packetWrapper.read(Type.SHORT) //length val book: Item = packetWrapper.read(Types1_7_6_10.COMPRESSED_NBT_ITEM) - val tag: CompoundTag? = book.tag + val tag = book.tag if (tag != null && tag.contains("pages")) { val pages = tag.get("pages") if (pages != null) { @@ -421,9 +425,9 @@ fun Protocol1_8To1_7_6.registerPlayerPackets() { packetWrapper.id = -1 val newPacketBuf = Unpooled.buffer() packetWrapper.writeToBuffer(newPacketBuf) - val newWrapper = PacketWrapper(0x17, newPacketBuf, packetWrapper.user()) + val newWrapper = PacketWrapper(0x17, null, packetWrapper.user()) newWrapper.passthrough(Type.STRING) - newWrapper.write(Type.SHORT, newPacketBuf.readableBytes().toShort()) + newWrapper.write(Type.SHORT_BYTE_ARRAY, readRemainingBytes(newPacketBuf)) newWrapper.sendToServer(Protocol1_8To1_7_6::class.java, true, true) } } @@ -477,7 +481,7 @@ fun Protocol1_8To1_7_6.registerPlayerPackets() { map(Type.BOOLEAN) create { packetWrapper -> packetWrapper.write(Type.BYTE, 0.toByte()) } handler { packetWrapper -> - val flags: Short = packetWrapper.read(Type.UNSIGNED_BYTE) + val flags = packetWrapper.read(Type.UNSIGNED_BYTE) packetWrapper.write(Type.BOOLEAN, flags and 1 == 1.toShort()) } } @@ -487,7 +491,7 @@ fun Protocol1_8To1_7_6.registerPlayerPackets() { this.registerIncoming(State.PLAY, 0x14, 0x14, object : PacketRemapper() { override fun registerMap() { handler { packetWrapper -> - val text: String = packetWrapper.read(Type.STRING) + val text = packetWrapper.read(Type.STRING) packetWrapper.clearInputBuffer() packetWrapper.write(Type.STRING, text) } diff --git a/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/packets/WorldPackets.kt b/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/packets/WorldPackets.kt index ee8c9c9..11ca65e 100644 --- a/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/packets/WorldPackets.kt +++ b/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/packets/WorldPackets.kt @@ -6,11 +6,9 @@ import com.viaversion.aas.protocol.id47toid5.chunks.ChunkPacketTransformer import com.viaversion.aas.protocol.id47toid5.data.Particle1_8to1_7 import com.viaversion.aas.protocol.id47toid5.storage.MapStorage import de.gerrygames.viarewind.protocol.protocol1_7_6_10to1_8.types.Types1_7_6_10 -import us.myles.ViaVersion.api.minecraft.Position import us.myles.ViaVersion.api.remapper.PacketRemapper import us.myles.ViaVersion.api.remapper.TypeRemapper import us.myles.ViaVersion.api.type.Type -import us.myles.ViaVersion.api.type.types.CustomByteType import us.myles.ViaVersion.packets.State import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9To1_8 import us.myles.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer @@ -37,7 +35,7 @@ fun Protocol1_8To1_7_6.registerWorldPackets() { override fun registerMap() { map(xyzUBytePos, TypeRemapper(Type.POSITION)) //Position handler { packetWrapper -> - val blockId: Int = packetWrapper.read(Type.VAR_INT) + val blockId = 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)) } //Block Data @@ -89,7 +87,7 @@ fun Protocol1_8To1_7_6.registerWorldPackets() { this.registerOutgoing(State.PLAY, 0x2A, 0x2A, object : PacketRemapper() { override fun registerMap() { handler { packetWrapper -> - val parts: Array = packetWrapper.read(Type.STRING).split("_").toTypedArray() + val parts = packetWrapper.read(Type.STRING).split("_").toTypedArray() var particle = Particle1_8to1_7.find(parts[0]) if (particle == null) particle = Particle1_8to1_7.CRIT packetWrapper.write(Type.INT, particle.ordinal) @@ -152,8 +150,7 @@ fun Protocol1_8To1_7_6.registerWorldPackets() { map(Type.VAR_INT) handler { packetWrapper -> val id = packetWrapper.get(Type.VAR_INT, 0) - val length = packetWrapper.read(Type.SHORT).toInt() - val data = packetWrapper.read(CustomByteType(length)) + val data = packetWrapper.read(Type.SHORT_BYTE_ARRAY) val mapStorage = packetWrapper.user().get(MapStorage::class.java)!! var mapData = mapStorage.getMapData(id) if (mapData == null) mapStorage.putMapData(id, MapStorage.MapData().also { mapData = it }) @@ -224,9 +221,9 @@ fun Protocol1_8To1_7_6.registerWorldPackets() { map(Type.UNSIGNED_BYTE) map(Type.ITEM, Types1_7_6_10.COMPRESSED_NBT_ITEM) handler { packetWrapper -> - val x: Int = packetWrapper.get(Type.INT, 0) - val y: Short = packetWrapper.get(Type.UNSIGNED_BYTE, 0) - val z: Int = packetWrapper.get(Type.INT, 1) + val x = packetWrapper.get(Type.INT, 0) + val y = packetWrapper.get(Type.UNSIGNED_BYTE, 0) + val z = packetWrapper.get(Type.INT, 1) // https://github.com/ViaVersion/ViaVersion/pull/1379 val direction = packetWrapper.get(Type.UNSIGNED_BYTE, 0) //Direction val item = packetWrapper.get(Types1_7_6_10.COMPRESSED_NBT_ITEM, 0) @@ -241,7 +238,7 @@ fun Protocol1_8To1_7_6.registerWorldPackets() { if (packetWrapper.isReadable(Type.BYTE, 0)) { packetWrapper.passthrough(Type.BYTE) } else { - val cursor: Short = packetWrapper.read(Type.UNSIGNED_BYTE) + val cursor = packetWrapper.read(Type.UNSIGNED_BYTE) packetWrapper.write(Type.BYTE, cursor.toByte()) } } diff --git a/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/storage/EntityTracker.kt b/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/storage/EntityTracker.kt index e12d319..ce905e2 100644 --- a/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/storage/EntityTracker.kt +++ b/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/storage/EntityTracker.kt @@ -31,7 +31,7 @@ class EntityTracker(user: UserConnection) : StoredObject(user) { if (!metadataBuffer.containsKey(entityId)) return val wrapper = PacketWrapper(0x1C, null, this.user) wrapper.write(Type.VAR_INT, entityId) - wrapper.write>(Types1_8.METADATA_LIST, metadataBuffer[entityId]) + wrapper.write(Types1_8.METADATA_LIST, metadataBuffer[entityId]) MetadataRewriter.transform(clientEntityTypes[entityId], metadataBuffer[entityId]!!) if (metadataBuffer[entityId]!!.isNotEmpty()) { try { diff --git a/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/storage/MapStorage.kt b/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/storage/MapStorage.kt index eae1edf..a0b53fc 100644 --- a/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/storage/MapStorage.kt +++ b/src/main/kotlin/com/viaversion/aas/protocol/id47toid5/storage/MapStorage.kt @@ -16,7 +16,7 @@ class MapStorage(user: UserConnection) : StoredObject(user) { class MapData { var scale: Byte = 0 - var mapIcons = arrayOf() + var mapIcons = emptyArray() } class MapIcon(var direction: Byte, var type: Byte, var x: Byte, var z: Byte) } \ No newline at end of file diff --git a/src/main/kotlin/com/viaversion/aas/protocol/id5toid4/Protocol1_7_6to1_7_2.kt b/src/main/kotlin/com/viaversion/aas/protocol/id5toid4/Protocol1_7_6to1_7_2.kt index f06db6b..92e647f 100644 --- a/src/main/kotlin/com/viaversion/aas/protocol/id5toid4/Protocol1_7_6to1_7_2.kt +++ b/src/main/kotlin/com/viaversion/aas/protocol/id5toid4/Protocol1_7_6to1_7_2.kt @@ -7,7 +7,6 @@ import us.myles.ViaVersion.api.remapper.PacketRemapper import us.myles.ViaVersion.api.type.Type import us.myles.ViaVersion.packets.State - // Based on https://github.com/Gerrygames/ClientViaVersion object Protocol1_7_6to1_7_2 : SimpleProtocol() { override fun registerPackets() { diff --git a/src/main/kotlin/com/viaversion/aas/util/StacklessException.kt b/src/main/kotlin/com/viaversion/aas/util/StacklessException.kt index 1a49a46..4363594 100644 --- a/src/main/kotlin/com/viaversion/aas/util/StacklessException.kt +++ b/src/main/kotlin/com/viaversion/aas/util/StacklessException.kt @@ -13,4 +13,5 @@ class StacklessException : RuntimeException { ) override fun fillInStackTrace(): Throwable = this + override fun toString(): String = "StacklessException: $message" } \ No newline at end of file diff --git a/src/main/kotlin/com/viaversion/aas/web/WebDashboardServer.kt b/src/main/kotlin/com/viaversion/aas/web/WebDashboardServer.kt index aa2fcca..2ae7fdc 100644 --- a/src/main/kotlin/com/viaversion/aas/web/WebDashboardServer.kt +++ b/src/main/kotlin/com/viaversion/aas/web/WebDashboardServer.kt @@ -1,29 +1,30 @@ package com.viaversion.aas.web -import com.viaversion.aas.httpClient -import com.viaversion.aas.parseUndashedId -import com.viaversion.aas.webLogger import com.google.common.cache.CacheBuilder import com.google.common.cache.CacheLoader import com.google.common.collect.MultimapBuilder import com.google.common.collect.Multimaps import com.google.gson.JsonObject +import com.viaversion.aas.httpClient +import com.viaversion.aas.parseUndashedId import com.viaversion.aas.util.StacklessException +import com.viaversion.aas.webLogger import io.ipinfo.api.IPInfo import io.ktor.client.request.* import io.ktor.http.cio.websocket.* import io.ktor.websocket.* import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.time.delay import kotlinx.coroutines.withContext -import us.myles.ViaVersion.api.Via import java.net.InetSocketAddress import java.net.SocketAddress +import java.time.Duration import java.util.* import java.util.concurrent.CompletableFuture import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.TimeUnit -import java.util.concurrent.TimeoutException class WebDashboardServer { // I don't think i'll need more than 1k/day @@ -87,9 +88,10 @@ class WebDashboardServer { ) it.ws.flush() } - Via.getPlatform().runSync({ + GlobalScope.run { + delay(Duration.ofSeconds(20)) future.completeExceptionally(StacklessException("No response from browser")) - }, 20 * 20) + } } return future }