Skip to content

Commit

Permalink
entity flags: fix class cast exception
Browse files Browse the repository at this point in the history
Flags are mostly in the protocol a byte, but we will just work with ints. Fine. But observers are registered on ints, but the actual type is a byte, hence it crashes.
  • Loading branch information
Bixilon committed Jun 9, 2024
1 parent 6d0db5f commit fa99812
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2022 Moritz Zwerger
* Copyright (C) 2020-2024 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
Expand All @@ -13,4 +13,7 @@

package de.bixilon.minosoft.data.entities.data

class EntityDataField(vararg val names: String)
class EntityDataField(vararg val names: String) {

override fun toString() = names.contentToString()
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import de.bixilon.kutil.bit.BitByte.isBitMask
import de.bixilon.kutil.cast.CastUtil.unsafeCast
import de.bixilon.kutil.cast.CastUtil.unsafeNull
import de.bixilon.kutil.primitive.BooleanUtil.toBoolean
import de.bixilon.kutil.primitive.IntUtil.toInt
import de.bixilon.kutil.reflection.ReflectionUtil.field
import de.bixilon.kutil.reflection.ReflectionUtil.getFieldOrNull
import de.bixilon.kutil.time.TimeUtil.millis
Expand Down Expand Up @@ -52,7 +53,7 @@ abstract class Entity(
private var initialPosition: Vec3d,
private var initialRotation: EntityRotation,
) : Initializable, EntityAttachable {
private var flags: Int by data(FLAGS_DATA, 0x00)
private var flags: Int by data(FLAGS_DATA, 0x00) { it.toInt() }
protected val random = Random()
val id: Int?
get() = connection.world.entities.getId(this)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
* Copyright (C) 2020-2024 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
Expand All @@ -17,6 +17,7 @@ import de.bixilon.kotlinglm.vec3.Vec3i
import de.bixilon.kutil.bit.BitByte.isBitMask
import de.bixilon.kutil.cast.CastUtil.nullCast
import de.bixilon.kutil.cast.CastUtil.unsafeCast
import de.bixilon.kutil.primitive.IntUtil.toInt
import de.bixilon.minosoft.data.container.equipment.EntityEquipment
import de.bixilon.minosoft.data.entities.EntityRotation
import de.bixilon.minosoft.data.entities.Poses
Expand Down Expand Up @@ -50,7 +51,7 @@ abstract class LivingEntity(connection: PlayConnection, entityType: EntityType,
override val canRaycast: Boolean get() = super.canRaycast && health > 0.0
override val name: ChatComponent? get() = super.name

private var flags by data(FLAGS_DATA, 0x00)
private var flags by data(FLAGS_DATA, 0x00) { it.toInt() }
private fun getLivingEntityFlag(bitMask: Int): Boolean {
return flags.isBitMask(bitMask)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
* Copyright (C) 2020-2024 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
Expand All @@ -14,14 +14,15 @@ package de.bixilon.minosoft.data.entities.entities

import de.bixilon.kotlinglm.vec3.Vec3d
import de.bixilon.kutil.bit.BitByte.isBitMask
import de.bixilon.kutil.primitive.IntUtil.toInt
import de.bixilon.minosoft.data.entities.EntityRotation
import de.bixilon.minosoft.data.entities.data.EntityData
import de.bixilon.minosoft.data.entities.data.EntityDataField
import de.bixilon.minosoft.data.registries.entities.EntityType
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection

abstract class Mob(connection: PlayConnection, entityType: EntityType, data: EntityData, position: Vec3d, rotation: EntityRotation) : LivingEntity(connection, entityType, data, position, rotation) {
private var flags by data(FLAGS_DATA, 0x00)
private var flags by data(FLAGS_DATA, 0x00) { it.toInt() }

private fun getMobFlags(bitMask: Int): Boolean {
return flags.isBitMask(bitMask)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
* Copyright (C) 2020-2024 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
Expand All @@ -17,6 +17,7 @@ import de.bixilon.kotlinglm.vec3.Vec3
import de.bixilon.kotlinglm.vec3.Vec3d
import de.bixilon.kutil.bit.BitByte.isBitMask
import de.bixilon.kutil.observer.DataObserver.Companion.observe
import de.bixilon.kutil.primitive.IntUtil.toInt
import de.bixilon.minosoft.data.entities.EntityRotation
import de.bixilon.minosoft.data.entities.data.EntityData
import de.bixilon.minosoft.data.entities.data.EntityDataField
Expand All @@ -31,7 +32,7 @@ import de.bixilon.minosoft.data.text.formatting.color.RGBColor
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection

class ArmorStand(connection: PlayConnection, entityType: EntityType, data: EntityData, position: Vec3d, rotation: EntityRotation) : LivingEntity(connection, entityType, data, position, rotation) {
private var flags by data(FLAGS_DATA, 0x00)
private var flags: Int by data(FLAGS_DATA, 0x00) { it.toInt() }

private fun updateFlags() {
this.dimensions = when {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
* Copyright (C) 2020-2024 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
Expand Down Expand Up @@ -110,7 +110,7 @@ abstract class PlayerEntity(
data.observe(SKIN_PARTS_DATA) { raw: Any? -> updateSkinParts(raw?.toInt() ?: 0xFF) }
}

private var _mainArm by data(MAIN_ARM_DATA, 0x01)
private var _mainArm by data(MAIN_ARM_DATA, 0x01) { it.toInt() }

@get:SynchronizedEntityData
open val mainArm: Arms
Expand Down

0 comments on commit fa99812

Please sign in to comment.