improve tag reading

This commit is contained in:
Bixilon 2021-05-21 19:25:50 +02:00
parent 5fbe97c1ee
commit 6bec745340
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
13 changed files with 38 additions and 26 deletions

View File

@ -190,7 +190,7 @@ object DefaultEntityFactories {
val tweakedFactory = ENTITY_FACTORY_MAP[tweakedResourceLocation] ?: throw UnknownEntityException("Can not find tweaked entity type: $tweakedResourceLocation for $factory")
val tweakedEntityType = connection.mapping.entityRegistry[tweakedResourceLocation] ?: throw UnknownEntityException("Can not find tweaked entity type data in ${connection.version}: $tweakedResourceLocation for $factory")
val tweakedEntityType = connection.mapping.entityTypeRegistry[tweakedResourceLocation] ?: throw UnknownEntityException("Can not find tweaked entity type data in ${connection.version}: $tweakedResourceLocation for $factory")
return tweakedFactory.build(connection, tweakedEntityType, position, rotation)
}

View File

@ -14,7 +14,7 @@ package de.bixilon.minosoft.data
import de.bixilon.minosoft.data.mappings.ResourceLocation
data class Tag(
data class Tag<T>(
val resourceLocation: ResourceLocation,
val ids: IntArray,
val ids: Set<T>,
)

View File

@ -37,7 +37,7 @@ open class Registry<T : RegistryItem>(
}
open operator fun get(id: Int): T {
return idValueMap[id] ?: parentRegistry?.get(id)!!
return idValueMap[id] ?: parentRegistry?.get(id) ?: throw NullPointerException("Can not find item with id $id")
}
open fun getId(value: T): Int {

View File

@ -91,7 +91,7 @@ class VersionMapping {
val blockStateIdMap: MutableMap<Int, BlockState> = mutableMapOf()
val entityMetaIndexMap: MutableMap<EntityMetaDataFields, Int> = mutableMapOf()
val entityRegistry: Registry<EntityType> = Registry()
val entityTypeRegistry: Registry<EntityType> = Registry()
val blockEntityTypeRegistry = BlockEntityTypeRegistry()
val blockEntityMetaDataTypeRegistry: Registry<BlockEntityMetaType> = Registry()
@ -190,7 +190,7 @@ class VersionMapping {
villagerProfessionRegistry.initialize(pixlyzerData["villager_professions"]?.asJsonObject, this, VillagerProfession)
entityRegistry.initialize(pixlyzerData["entities"]?.asJsonObject, this, EntityType)
entityTypeRegistry.initialize(pixlyzerData["entities"]?.asJsonObject, this, EntityType)
blockEntityMetaDataTypeRegistry.initialize(pixlyzerData["block_entity_meta_data_types"]?.asJsonObject, this, BlockEntityMetaType, alternative = DefaultRegistries.BLOCK_ENTITY_META_TYPE_REGISTRY.forVersion(version))

View File

@ -30,7 +30,7 @@ class Player(
val healthCondition = PlayerHealthCondition()
val experienceCondition = PlayerExperienceCondition()
var spawnPosition: Vec3i = VecUtil.EMPTY_VEC3I
val entity: PlayerEntity = PlayerEntity(connection, connection.mapping.entityRegistry[PlayerEntity.RESOURCE_LOCATION]!!, VecUtil.EMPTY_VEC3, EntityRotation(0.0, 0.0), account.username)
val entity: PlayerEntity = PlayerEntity(connection, connection.mapping.entityTypeRegistry[PlayerEntity.RESOURCE_LOCATION]!!, VecUtil.EMPTY_VEC3, EntityRotation(0.0, 0.0), account.username)
@Deprecated(message = "Will be replaced with some kind of teleport manager, ...")
var isSpawnConfirmed = false

View File

@ -61,9 +61,9 @@ class EntityObjectSpawnS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket() {
}
entity = if (buffer.versionId < ProtocolVersions.V_19W05A) {
val entityResourceLocation = ENTITY_OBJECT_REGISTRY[type].resourceLocation
buffer.connection.mapping.entityRegistry[entityResourceLocation]!!.build(buffer.connection, position, rotation, null, buffer.versionId)!! // ToDo: Entity meta data tweaking
buffer.connection.mapping.entityTypeRegistry[entityResourceLocation]!!.build(buffer.connection, position, rotation, null, buffer.versionId)!! // ToDo: Entity meta data tweaking
} else {
buffer.connection.mapping.entityRegistry[type].build(buffer.connection, position, rotation, null, buffer.versionId)!!
buffer.connection.mapping.entityTypeRegistry[type].build(buffer.connection, position, rotation, null, buffer.versionId)!!
}
}

View File

@ -27,7 +27,7 @@ class ExperienceOrbSpawnS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket() {
val entityId: Int = buffer.readEntityId()
val entity: ExperienceOrb = ExperienceOrb(
connection = buffer.connection,
entityType = buffer.connection.mapping.entityRegistry[ExperienceOrb.RESOURCE_LOCATION]!!,
entityType = buffer.connection.mapping.entityTypeRegistry[ExperienceOrb.RESOURCE_LOCATION]!!,
position = if (buffer.versionId < ProtocolVersions.V_16W06A) {
Vec3(buffer.readFixedPointNumberInt(), buffer.readFixedPointNumberInt(), buffer.readFixedPointNumberInt())
} else {

View File

@ -35,7 +35,7 @@ class GlobalEntitySpawnS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket() {
buffer.readPosition()
}
entity = LightningBolt(buffer.connection, buffer.connection.mapping.entityRegistry[LightningBolt.RESOURCE_LOCATION]!!, position)
entity = LightningBolt(buffer.connection, buffer.connection.mapping.entityTypeRegistry[LightningBolt.RESOURCE_LOCATION]!!, position)
}
override fun handle(connection: PlayConnection) {

View File

@ -55,7 +55,7 @@ class MobSpawnS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket() {
} else {
null
}
val entityType = buffer.connection.mapping.entityRegistry[typeId]
val entityType = buffer.connection.mapping.entityTypeRegistry[typeId]
entity = entityType.build(buffer.connection, position, rotation, metaData, buffer.versionId)!!
entity.velocity = velocity
metaData?.let {

View File

@ -52,7 +52,7 @@ class PaintingSpawnS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket() {
position = buffer.readBlockPosition()
direction = byId(buffer.readUnsignedByte())
}
entity = Painting(buffer.connection, buffer.connection.mapping.entityRegistry[Painting.RESOURCE_LOCATION]!!, position, direction, motive!!)
entity = Painting(buffer.connection, buffer.connection.mapping.entityTypeRegistry[Painting.RESOURCE_LOCATION]!!, position, direction, motive!!)
}
override fun handle(connection: PlayConnection) {

View File

@ -68,7 +68,7 @@ class PlayerEntitySpawnS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket() {
}
entity = PlayerEntity(
connection = buffer.connection,
entityType = buffer.connection.mapping.entityRegistry[PlayerEntity.RESOURCE_LOCATION]!!,
entityType = buffer.connection.mapping.entityTypeRegistry[PlayerEntity.RESOURCE_LOCATION]!!,
position = position,
rotation = EntityRotation(yaw.toFloat(), pitch.toFloat(), 0.0f),
name = name,

View File

@ -23,23 +23,29 @@ import de.bixilon.minosoft.util.logging.LogLevels
import de.bixilon.minosoft.util.logging.LogMessageType
class TagsS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket() {
val tags: Map<ResourceLocation, List<Tag>>
val tags: Map<ResourceLocation, List<Tag<Any>>>
init {
val tags: MutableMap<ResourceLocation, List<Tag>> = mutableMapOf()
val tags: MutableMap<ResourceLocation, List<Tag<Any>>> = mutableMapOf()
if (buffer.versionId < ProtocolVersions.V_20W51A) {
tags[BLOCK_TAG_RESOURCE_LOCATION] = buffer.readTagArray().toList()
tags[ITEM_TAG_RESOURCE_LOCATION] = buffer.readTagArray().toList()
tags[FLUID_TAG_RESOURCE_LOCATION] = buffer.readTagArray().toList() // ToDo: when was this added? Was not available in 18w01
tags[BLOCK_TAG_RESOURCE_LOCATION] = buffer.readTagArray { buffer.connection.mapping.getBlockState(it)!! }
tags[ITEM_TAG_RESOURCE_LOCATION] = buffer.readTagArray { buffer.connection.mapping.itemRegistry[it] }
tags[FLUID_TAG_RESOURCE_LOCATION] = buffer.readTagArray { buffer.connection.mapping.fluidRegistry[it] } // ToDo: when was this added? Was not available in 18w01
if (buffer.versionId >= ProtocolVersions.V_18W43A) {
tags[ENTITY_TYPE_TAG_RESOURCE_LOCATION] = buffer.readTagArray().toList()
tags[ENTITY_TYPE_TAG_RESOURCE_LOCATION] = buffer.readTagArray { buffer.connection.mapping.entityTypeRegistry[it] }
}
if (buffer.versionId >= ProtocolVersions.V_20W49A) {
tags[GAME_EVENT_TAG_RESOURCE_LOCATION] = buffer.readTagArray().toList()
tags[GAME_EVENT_TAG_RESOURCE_LOCATION] = buffer.readTagArray { it }
}
} else {
for (i in 0 until buffer.readVarInt()) {
tags[buffer.readResourceLocation()] = buffer.readTagArray().toList()
when (buffer.readResourceLocation()) {
BLOCK_TAG_RESOURCE_LOCATION -> buffer.readTagArray { buffer.connection.mapping.blockRegistry[it] }
ITEM_TAG_RESOURCE_LOCATION -> buffer.readTagArray { buffer.connection.mapping.itemRegistry[it] }
FLUID_TAG_RESOURCE_LOCATION -> buffer.readTagArray { buffer.connection.mapping.fluidRegistry[it] }
ENTITY_TYPE_TAG_RESOURCE_LOCATION -> buffer.readTagArray { buffer.connection.mapping.entityTypeRegistry[it] }
GAME_EVENT_TAG_RESOURCE_LOCATION -> buffer.readTagArray { it }
}
}
}
this.tags = tags.toMap()

View File

@ -409,12 +409,18 @@ open class InByteBuffer {
return String(Base64.getEncoder().encode(readRest()))
}
fun readTag(): Tag {
return Tag(readResourceLocation(), readVarIntArray())
fun <T> readTag(idResolver: (Int) -> T): Tag<T> {
val resourceLocation = readResourceLocation()
val ids = readVarIntArray()
val items: MutableSet<T> = mutableSetOf()
for (id in ids) {
items += idResolver(id)
}
return Tag(resourceLocation, items.toSet())
}
fun readTagArray(length: Int = readVarInt()): Array<Tag> {
return readArray(length) { readTag() }
fun <T> readTagArray(length: Int = readVarInt(), idResolver: (Int) -> T): List<Tag<T>> {
return (readArray(length) { readTag(idResolver) }).toList()
}
fun <T> readOptional(reader: () -> T): T? {