Archived
1
0
Fork 0

Fix PlayerPositionAndLook packet codec

This commit is contained in:
Moritz Ruth 2020-12-17 23:04:05 +01:00
parent f991d781b2
commit 636dfe6148
No known key found for this signature in database
GPG key ID: AFD57E23E753841B
11 changed files with 51 additions and 43 deletions

View file

@ -1,4 +1,4 @@
package space.blokk.tags
package space.blokk.tag
import space.blokk.NamespacedID
import space.blokk.entity.EntityType
@ -10,7 +10,7 @@ import space.blokk.world.block.Material
class Tag(val name: String, val type: Type, val rawValues: List<String>) {
val values: List<NamespacedID> by lazy {
rawValues.flatMap {
if (it.startsWith("#")) TagRegistry.tags.getValue(it.removePrefix("#")).values
if (it.startsWith("#")) TagRegistry.tagsByName.getValue(it.removePrefix("#")).values
else listOf(NamespacedID(it))
}
}

View file

@ -0,0 +1,6 @@
package space.blokk.tag
object TagRegistry {
val tags: List<Tag> = MINECRAFT_INTERNAL_TAGS.toList()
val tagsByName: Map<String, Tag> = tags.map { it.name to it }.toMap()
}

View file

@ -1,5 +0,0 @@
package space.blokk.tags
object TagRegistry {
val tags: Map<String, Tag> = MINECRAFT_INTERNAL_TAGS
}

View file

@ -1,11 +1,18 @@
package space.blokk.util
import kotlin.experimental.and
import kotlin.experimental.inv
import kotlin.experimental.or
/**
* Returns true if the bit at [index] is 1.
*/
fun Byte.checkBit(index: Int): Boolean {
val flag = (0x1 shl index).toByte()
val flag = (0b00000001 shl index).toByte()
return (this and flag) == flag
}
fun Byte.setBit(index: Int, value: Boolean): Byte {
val mask = (0b00000001 shl index).toByte()
return if (value) this or mask else this and mask.inv()
}

View file

@ -1,7 +1,9 @@
package space.blokk.net.packet.play
import io.netty.buffer.ByteBuf
import space.blokk.net.MinecraftProtocolDataTypes.writeVarInt
import space.blokk.net.packet.OutgoingPacketCodec
import space.blokk.util.setBit
object PlayerPositionAndLookPacketCodec :
OutgoingPacketCodec<PlayerPositionAndLookPacket>(0x36, PlayerPositionAndLookPacket::class) {
@ -12,13 +14,16 @@ object PlayerPositionAndLookPacketCodec :
dst.writeFloat(locationWithRotation.yaw)
dst.writeFloat(locationWithRotation.pitch)
var flags = 0x00
if (relativeX) flags = flags and 0x01
if (relativeY) flags = flags and 0x02
if (relativeZ) flags = flags and 0x04
if (relativeYaw) flags = flags and 0x08
if (relativePitch) flags = flags and 0x10
dst.writeByte(
0b00000000.toByte()
.setBit(0, relativeX)
.setBit(1, relativeY)
.setBit(2, relativeZ)
.setBit(3, relativeYaw)
.setBit(4, relativePitch)
.toInt()
)
dst.writeByte(flags)
dst.writeVarInt(0) // Teleport ID, I am not sure why this is needed
}
}

View file

@ -4,7 +4,7 @@ import io.netty.buffer.ByteBuf
import space.blokk.net.MinecraftProtocolDataTypes.writeString
import space.blokk.net.MinecraftProtocolDataTypes.writeVarInt
import space.blokk.net.packet.OutgoingPacketCodec
import space.blokk.tags.Tag
import space.blokk.tag.Tag
import kotlin.time.Duration
import kotlin.time.toJavaDuration

View file

@ -1,7 +1,7 @@
package space.blokk.net.packet.play
import space.blokk.net.packet.OutgoingPacket
import space.blokk.tags.Tag
import space.blokk.tag.Tag
/**
* Sent by the server while the player is joining.

View file

@ -1,10 +1,6 @@
package space.blokk.net
import io.netty.buffer.Unpooled
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.ensureActive
import space.blokk.BlokkServer
import space.blokk.Difficulty
import space.blokk.DifficultySettings
@ -17,18 +13,17 @@ import space.blokk.net.packet.login.*
import space.blokk.net.packet.play.*
import space.blokk.player.BlokkPlayer
import space.blokk.player.GameMode
import space.blokk.tags.TagRegistry
import space.blokk.tag.TagRegistry
import space.blokk.util.*
import space.blokk.world.Chunk
import java.security.MessageDigest
import java.util.*
import javax.crypto.Cipher
import javax.crypto.spec.SecretKeySpec
import kotlin.coroutines.CoroutineContext
import kotlin.random.Random
class LoginAndJoinProcedure(val session: BlokkSession) {
private val tagsPacket by lazy { TagsPacket(TagRegistry.tags.values) }
private val tagsPacket by lazy { TagsPacket(TagRegistry.tags) }
suspend fun start(packet: LoginStartPacket) {
session.state.getOrFail<Session.State.WaitingForLoginStart>()
@ -182,12 +177,12 @@ class LoginAndJoinProcedure(val session: BlokkSession) {
session.send(DeclareRecipesPacket(session.server.recipes))
session.send(tagsPacket)
// // TODO: Send Entity Status packet with OP permission level
//
// session.send(PlayerPositionAndLookPacket(state.initialWorldAndLocation.location))
//
// // TODO: Send PlayerInfo packet
//
// session.send(UpdateViewPositionPacket(Chunk.Key.from(player.location.asVoxelLocation())))
// TODO: Send Entity Status packet with OP permission level
session.send(PlayerPositionAndLookPacket(state.initialWorldAndLocation.location))
// TODO: Send PlayerInfo packet
session.send(UpdateViewPositionPacket(Chunk.Key.from(player.location.asVoxelLocation())))
}
}

View file

@ -47,7 +47,7 @@ class MinecraftDataSourcesPlugin : Plugin<Project> {
val registries = JsonIterator.deserialize(workingDir.resolve("generated/reports/registries.json").readText())
BlocksAndMaterialGenerator(workingDir, outputDir, sourcesDir).generate()
TagsFileGenerator(workingDir, outputDir).generate()
TagsGenerator(workingDir, outputDir).generate()
BiomesGenerator(workingDir, outputDir).generate()
EntitiesGenerator(workingDir, outputDir, sourcesDir).generate()
FluidIDMapGenerator(workingDir, outputDir, registries).generate()

View file

@ -7,9 +7,13 @@ const val WORLD_PACKAGE = "$BASE_PACKAGE.world"
const val BLOCK_PACKAGE = "$WORLD_PACKAGE.block"
const val ENTITY_PACKAGE = "$BASE_PACKAGE.entity"
const val ITEM_PACKAGE = "$BASE_PACKAGE.item"
const val TAG_PACKAGE = "$BASE_PACKAGE.tag"
val MATERIAL_TYPE = ClassName(BLOCK_PACKAGE, "Material")
val NAMESPACED_ID_TYPE = ClassName(BASE_PACKAGE, "NamespacedID")
val BLOCK_TYPE = ClassName(BLOCK_PACKAGE, "Block")
val ENTITY_TYPE = ClassName(ENTITY_PACKAGE, "Entity")
val ENTITY_TYPE_TYPE = ClassName(ENTITY_PACKAGE, "EntityType")
val ITEM_TYPE_TYPE = ClassName(ITEM_PACKAGE, "ItemType")
val ENTITY_TYPE_TYPE = ClassName(ENTITY_PACKAGE, "EntityType")
val TAG_TYPE = ClassName(TAG_PACKAGE, "Tag")
val TAG_TYPE_TYPE = TAG_TYPE.nestedClass("Type")

View file

@ -5,18 +5,14 @@ import com.squareup.kotlinpoet.*
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import java.io.File
class TagsFileGenerator(private val workingDir: File, private val outputDir: File) {
class TagsGenerator(workingDir: File, private val outputDir: File) {
companion object {
const val TAGS_PACKAGE = "space.blokk.tags"
val TAG_TYPE = ClassName(TAGS_PACKAGE, "Tag")
private val TAG_TYPE_TYPE = TAG_TYPE.nestedClass("Type")
val TAG_TYPE_TYPES_BY_DIR_NAME = mapOf(
"blocks" to TAG_TYPE_TYPE.nestedClass("BLOCKS"),
"entity_types" to TAG_TYPE_TYPE.nestedClass("ENTITY_TYPES"),
"fluids" to TAG_TYPE_TYPE.nestedClass("FLUIDS"),
"items" to TAG_TYPE_TYPE.nestedClass("ITEMS")
)
val MAP_TYPE = ClassName("kotlin.collections", "Map")
}
private val tagsDir = workingDir.resolve("generated/data/minecraft/tags")
@ -30,21 +26,21 @@ class TagsFileGenerator(private val workingDir: File, private val outputDir: Fil
val values = json.get("values").asList().map { it.toString() }
Pair(
"%S to %T(%S, %T, listOf(\n${values.joinToString(",\n") { "%S".prependIndent(" ") }}\n))",
arrayOf(name, TAG_TYPE, name, tagTypeType, *values.toTypedArray())
"%T(%S, %T, listOf(\n${values.joinToString(",\n") { "%S".prependIndent(" ") }}\n))",
arrayOf(TAG_TYPE, name, tagTypeType, *values.toTypedArray())
)
}
}
val property = PropertySpec.builder("MINECRAFT_INTERNAL_TAGS", Map::class.asTypeName().parameterizedBy(String::class.asTypeName(), TAG_TYPE))
val property = PropertySpec.builder("MINECRAFT_INTERNAL_TAGS", List::class.asTypeName().parameterizedBy(TAG_TYPE))
.initializer(
"mapOf(\n${entries.joinToString(",\n") { it.first.prependIndent(" ") }}\n)",
"listOf(\n${entries.joinToString(",\n") { it.first.prependIndent(" ") }}\n)",
*entries.flatMap { it.second.toList() }.toTypedArray()
)
.addModifiers(KModifier.INTERNAL)
.build()
FileSpec.builder(TAGS_PACKAGE, "Tags")
FileSpec.builder(TAG_PACKAGE, "MinecraftInternalTags")
.addProperty(property)
.build()
.writeTo(outputDir)