From 3fb9cc3298e89e4dc542ad54207de5b5d0660712 Mon Sep 17 00:00:00 2001 From: Moritz Ruth Date: Mon, 4 Jan 2021 19:13:31 +0100 Subject: [PATCH] Give each entity an UID and make entity classes open --- .../mdsp/generator/EntitiesGenerator.kt | 1 + gradle.properties | 8 -------- .../main/kotlin/space/uranos/entity/Entity.kt | 19 +++++++++++++++---- .../kotlin/space/uranos/entity/ItemEntity.kt | 4 ++-- .../main/kotlin/space/uranos/server/Server.kt | 7 +++++++ .../net/packet/play/ChunkDataPacketCodec.kt | 5 +---- .../play/SpawnLivingEntityPacketCodec.kt | 3 +-- .../play/SpawnObjectEntityPacketCodec.kt | 4 +--- .../packet/play/SpawnPaintingPacketCodec.kt | 4 +--- .../main/kotlin/space/uranos/UranosServer.kt | 16 ++++++++++------ .../kotlin/space/uranos/event/EventBusTest.kt | 5 ----- 11 files changed, 39 insertions(+), 37 deletions(-) diff --git a/buildSrc/src/main/kotlin/space/uranos/mdsp/generator/EntitiesGenerator.kt b/buildSrc/src/main/kotlin/space/uranos/mdsp/generator/EntitiesGenerator.kt index 6c3ac5d..7fc9a50 100644 --- a/buildSrc/src/main/kotlin/space/uranos/mdsp/generator/EntitiesGenerator.kt +++ b/buildSrc/src/main/kotlin/space/uranos/mdsp/generator/EntitiesGenerator.kt @@ -78,6 +78,7 @@ class EntitiesGenerator( val type = TypeSpec.classBuilder(name) .superclass(ENTITY_TYPE) + .addModifiers(KModifier.OPEN) .addProperty( PropertySpec.builder("type", ENTITY_TYPE_TYPE, KModifier.OVERRIDE, KModifier.FINAL) .initializer("Type") diff --git a/gradle.properties b/gradle.properties index 1e973c2..e89be11 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,10 +1,2 @@ # suppress inspection "UnusedProperty" for whole file kotlin.code.style=official -version.kotlin=1.4.20 -version.netty=4.1.54.Final -version.moshi=1.11.0 -version.kotlinx-coroutines=1.4.2 -version.slf4j=1.7.30 -version.junit=5.7.0 -version.strikt=0.28.0 -version.guava=30.0-jre diff --git a/uranos-api/src/main/kotlin/space/uranos/entity/Entity.kt b/uranos-api/src/main/kotlin/space/uranos/entity/Entity.kt index 88fbc6d..c67b95f 100644 --- a/uranos-api/src/main/kotlin/space/uranos/entity/Entity.kt +++ b/uranos-api/src/main/kotlin/space/uranos/entity/Entity.kt @@ -5,13 +5,24 @@ package space.uranos.entity -import space.uranos.Location +import space.uranos.Uranos import java.util.* -import kotlin.reflect.KClass abstract class Entity internal constructor() { - val uuid: UUID = UUID.randomUUID() + /** + * An integer unique to this entity which will not be persisted, for example when the entity is serialized. + */ + val uid: Int = Uranos.claimEntityID() + + /** + * The UUID of this entity. + * + * If the entity is a player and the server uses authentication, it is the player's UUID, if it is not, + * it is a UUIDv3 generated from a string consisting of "OfflinePlayer:" and the player's username. + * + * Otherwise, it is usually randomly generated. + */ + open val uuid: UUID = UUID.randomUUID() abstract val type: EntityType - val globallyUniqueNumericID: Int = 0 // TODO: Get a real value } diff --git a/uranos-api/src/main/kotlin/space/uranos/entity/ItemEntity.kt b/uranos-api/src/main/kotlin/space/uranos/entity/ItemEntity.kt index 379b06e..8dc4436 100644 --- a/uranos-api/src/main/kotlin/space/uranos/entity/ItemEntity.kt +++ b/uranos-api/src/main/kotlin/space/uranos/entity/ItemEntity.kt @@ -7,8 +7,8 @@ package space.uranos.entity import space.uranos.Position -class ItemEntity(override var position: Position) : ObjectEntity() { - override val type: EntityType = Type +open class ItemEntity(override var position: Position) : ObjectEntity() { + final override val type: EntityType = Type companion object Type : ItemEntityType() } diff --git a/uranos-api/src/main/kotlin/space/uranos/server/Server.kt b/uranos-api/src/main/kotlin/space/uranos/server/Server.kt index 2d156a0..1e0dc84 100644 --- a/uranos-api/src/main/kotlin/space/uranos/server/Server.kt +++ b/uranos-api/src/main/kotlin/space/uranos/server/Server.kt @@ -57,6 +57,13 @@ interface Server { val developmentMode: Boolean val minimumLogLevel: Logger.Level + /** + * Returns an unused entity ID. + * + * **Should not be used by plugins.** + */ + fun claimEntityID(): Int + /** * Initiates shutting down the server. */ diff --git a/uranos-packet-codecs/src/main/kotlin/space/uranos/net/packet/play/ChunkDataPacketCodec.kt b/uranos-packet-codecs/src/main/kotlin/space/uranos/net/packet/play/ChunkDataPacketCodec.kt index 442f0c0..ae574d2 100644 --- a/uranos-packet-codecs/src/main/kotlin/space/uranos/net/packet/play/ChunkDataPacketCodec.kt +++ b/uranos-packet-codecs/src/main/kotlin/space/uranos/net/packet/play/ChunkDataPacketCodec.kt @@ -67,10 +67,7 @@ object ChunkDataPacketCodec : OutgoingPacketCodec(0x20, ChunkDa // Blocks val dataBuf = Unpooled.buffer() // TODO: Set an initial capacity for (section in data.sections.filterNotNull()) { - val ids = section.map { - println(it) - it.material().codec.getStateID(it) - }.toIntArray() + val ids = section.map { it.material().codec.getStateID(it) }.toIntArray() dataBuf.writeShort(ids.count { !(it == AirBlock.codec.id || it == CaveAirBlock.codec.id) }) dataBuf.writeByte(numberOfBitsPerBlock) diff --git a/uranos-packet-codecs/src/main/kotlin/space/uranos/net/packet/play/SpawnLivingEntityPacketCodec.kt b/uranos-packet-codecs/src/main/kotlin/space/uranos/net/packet/play/SpawnLivingEntityPacketCodec.kt index 88231cb..742e82a 100644 --- a/uranos-packet-codecs/src/main/kotlin/space/uranos/net/packet/play/SpawnLivingEntityPacketCodec.kt +++ b/uranos-packet-codecs/src/main/kotlin/space/uranos/net/packet/play/SpawnLivingEntityPacketCodec.kt @@ -6,7 +6,6 @@ package space.uranos.net.packet.play import io.netty.buffer.ByteBuf -import space.uranos.Difficulty import space.uranos.entity.LivingEntity import space.uranos.net.MinecraftProtocolDataTypes.writeUUID import space.uranos.net.MinecraftProtocolDataTypes.writeVarInt @@ -30,7 +29,7 @@ object SpawnLivingEntityPacketCodec : OutgoingPacketCodec(0x00, SpawnObjectEntityPacket::class) { @Suppress("DuplicatedCode") @@ -32,7 +30,7 @@ object SpawnObjectEntityPacketCodec : OutgoingPacketCodec(0x03, } fun getPacketFromEntity(entity: PaintingEntity) = SpawnPaintingPacket( - entity.globallyUniqueNumericID, + entity.uid, entity.uuid, entity.motive, getCenterLocation(entity.topLeftLocation, entity.motive), diff --git a/uranos-server/src/main/kotlin/space/uranos/UranosServer.kt b/uranos-server/src/main/kotlin/space/uranos/UranosServer.kt index b44fb35..fcb436e 100644 --- a/uranos-server/src/main/kotlin/space/uranos/UranosServer.kt +++ b/uranos-server/src/main/kotlin/space/uranos/UranosServer.kt @@ -28,6 +28,7 @@ import space.uranos.world.Dimension import java.io.File import java.security.KeyPair import java.util.concurrent.Executors +import java.util.concurrent.atomic.AtomicInteger import kotlin.coroutines.CoroutineContext import kotlin.system.exitProcess @@ -89,6 +90,15 @@ class UranosServer internal constructor() : Server { override val eventBus = UranosEventBus(developmentMode) override val eventHandlerPositions = UranosEventHandlerPositionManager() + private val nextEntityID = AtomicInteger() + override fun claimEntityID() = nextEntityID.incrementAndGet() + + override fun shutdown() { + runBlocking { + scheduler.shutdown() + } + } + private fun failInitialization(t: Throwable): Nothing { logger.error("Server initialization failed:", t) exitProcess(1) @@ -107,12 +117,6 @@ class UranosServer internal constructor() : Server { scheduler.startTicking() } - override fun shutdown() { - runBlocking { - scheduler.shutdown() - } - } - companion object { val VERSION = UranosServer::class.java.`package`.implementationVersion ?: "development" val VERSION_WITH_V = if (VERSION == "development") VERSION else "v$VERSION" diff --git a/uranos-server/src/test/kotlin/space/uranos/event/EventBusTest.kt b/uranos-server/src/test/kotlin/space/uranos/event/EventBusTest.kt index c99aa82..f66a1f9 100644 --- a/uranos-server/src/test/kotlin/space/uranos/event/EventBusTest.kt +++ b/uranos-server/src/test/kotlin/space/uranos/event/EventBusTest.kt @@ -60,14 +60,9 @@ class EventBusTest { ) val actualOrder = (Uranos.eventHandlerPositions as UranosEventHandlerPositionManager).positions - println(actualOrder) - Uranos.eventHandlerPositions.insertAfter(EventHandlerPosition.FIRST, Position1, Position5) - println(actualOrder) Uranos.eventHandlerPositions.insertBefore(Position1, Position2, Position4) - println(actualOrder) Uranos.eventHandlerPositions.insert(Position3, Position6) - println(actualOrder) actualOrder.forEachIndexed { index, position -> expectThat(index).isEqualTo(expectedOrder.indexOf(position))