From 71cd8bf486694242fbe177b9b06cb1c490085c75 Mon Sep 17 00:00:00 2001 From: Moritz Ruth Date: Wed, 7 Apr 2021 15:49:16 +0200 Subject: [PATCH] Add custom name attribute --- README.md | 2 +- .../kotlin/space/uranos/testplugin/TestPlugin.kt | 5 +++-- .../kotlin/space/uranos/entity/LivingEntity.kt | 5 ++++- .../net/packet/play/EntityMetadataPacketCodec.kt | 1 + .../kotlin/space/uranos/entity/UranosEntity.kt | 12 ++++++++++++ .../space/uranos/entity/impl/UranosCowEntity.kt | 2 ++ .../entity/metadata/EntityMetadataFieldsTable.kt | 15 +++++---------- 7 files changed, 28 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 298f8cb..cf773c3 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Its goal is to be a modern alternative to Bukkit, Spigot and Paper. It is primar is possible with the existing alternatives, but it can also be used for something like a lobby/hub server. The most important thing for Uranos is -[developer experience (DX)](https://medium.com/swlh/what-is-dx-developer-experience-401a0e44a9d9). After that comes performance. +[developer experience (DX)](https://css-tricks.com/what-is-developer-experience-dx/). After that comes performance. ## Milestones diff --git a/test-plugin/src/main/kotlin/space/uranos/testplugin/TestPlugin.kt b/test-plugin/src/main/kotlin/space/uranos/testplugin/TestPlugin.kt index b97e8c1..bfb16a3 100644 --- a/test-plugin/src/main/kotlin/space/uranos/testplugin/TestPlugin.kt +++ b/test-plugin/src/main/kotlin/space/uranos/testplugin/TestPlugin.kt @@ -106,9 +106,10 @@ class TestPlugin : Plugin("Test", "1.0.0") { val entity = Uranos.create() entity.position = Position(0.0, 4.0, 0.0) entity.setWorld(world) + entity.alwaysShowName = true - Uranos.scheduler.executeRepeating(20) { - entity.isBaby = !entity.isBaby + Uranos.scheduler.executeRepeating(10) { +// entity.customName = TextComponent("Cow", color = ChatColor.NAMED_COLORS.values.random()) } } } diff --git a/uranos-api/src/main/kotlin/space/uranos/entity/LivingEntity.kt b/uranos-api/src/main/kotlin/space/uranos/entity/LivingEntity.kt index 31a09ce..6f4d3d8 100644 --- a/uranos-api/src/main/kotlin/space/uranos/entity/LivingEntity.kt +++ b/uranos-api/src/main/kotlin/space/uranos/entity/LivingEntity.kt @@ -1,5 +1,8 @@ package space.uranos.entity +import space.uranos.chat.ChatComponent + interface LivingEntity : Entity, Mobile { - // potion effects + var customName: ChatComponent? + var alwaysShowName: Boolean } diff --git a/uranos-packet-codecs/src/main/kotlin/space/uranos/net/packet/play/EntityMetadataPacketCodec.kt b/uranos-packet-codecs/src/main/kotlin/space/uranos/net/packet/play/EntityMetadataPacketCodec.kt index cb2c3b4..33a6131 100644 --- a/uranos-packet-codecs/src/main/kotlin/space/uranos/net/packet/play/EntityMetadataPacketCodec.kt +++ b/uranos-packet-codecs/src/main/kotlin/space/uranos/net/packet/play/EntityMetadataPacketCodec.kt @@ -26,6 +26,7 @@ object EntityMetadataPacketCodec : OutgoingPacketCodec(0x4 } } is EntityMetadataPacket.Entry.Boolean -> dst.writeBoolean(entry.value) + else -> throw NotImplementedError() } } diff --git a/uranos-server/src/main/kotlin/space/uranos/entity/UranosEntity.kt b/uranos-server/src/main/kotlin/space/uranos/entity/UranosEntity.kt index 8d4364b..3576bd8 100644 --- a/uranos-server/src/main/kotlin/space/uranos/entity/UranosEntity.kt +++ b/uranos-server/src/main/kotlin/space/uranos/entity/UranosEntity.kt @@ -4,8 +4,10 @@ import kotlinx.coroutines.launch import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock import space.uranos.* +import space.uranos.chat.ChatComponent import space.uranos.entity.event.ViewingChangedEvent import space.uranos.entity.impl.UranosPlayerEntity +import space.uranos.entity.metadata.EntityMetadataFieldsTable import space.uranos.net.packet.OutgoingPacket import space.uranos.net.packet.play.* import space.uranos.player.Player @@ -97,6 +99,16 @@ sealed class UranosLivingEntity(server: UranosServer) : UranosEntity(server), Li override var velocity: Vector = Vector.ZERO override var position: Position = Position.ZERO override val chunkKey: Chunk.Key by memoized({ position }) { Chunk.Key.from(position) } + + override var customName: ChatComponent? = null + override var alwaysShowName: Boolean = false + + companion object { + fun EntityMetadataFieldsTable.BuilderContext.livingEntity() { + optional(2u, LivingEntity::customName) + required(3u, LivingEntity::alwaysShowName) + } + } } abstract class UranosNotHasMovableHeadLivingEntity(server: UranosServer) : UranosLivingEntity(server) { diff --git a/uranos-server/src/main/kotlin/space/uranos/entity/impl/UranosCowEntity.kt b/uranos-server/src/main/kotlin/space/uranos/entity/impl/UranosCowEntity.kt index cdca305..b252ba7 100644 --- a/uranos-server/src/main/kotlin/space/uranos/entity/impl/UranosCowEntity.kt +++ b/uranos-server/src/main/kotlin/space/uranos/entity/impl/UranosCowEntity.kt @@ -14,6 +14,8 @@ class UranosCowEntity(server: UranosServer) : UranosHasMovableHeadLivingEntity(s companion object { val entityMetadataFieldsTable = EntityMetadataFieldsTable.DEFAULT.extend { + livingEntity() + required(15u, Ageable::isBaby) } } diff --git a/uranos-server/src/main/kotlin/space/uranos/entity/metadata/EntityMetadataFieldsTable.kt b/uranos-server/src/main/kotlin/space/uranos/entity/metadata/EntityMetadataFieldsTable.kt index d23b46b..0d264d9 100644 --- a/uranos-server/src/main/kotlin/space/uranos/entity/metadata/EntityMetadataFieldsTable.kt +++ b/uranos-server/src/main/kotlin/space/uranos/entity/metadata/EntityMetadataFieldsTable.kt @@ -60,8 +60,10 @@ class EntityMetadataFieldsTable private constructor(val fields: Map>, val getEntry: (index: UByte, entity: Entity) -> EntityMetadataPacket.Entry<*>) : - Field(dependingProperties) { + class Computed( + dependingProperties: Array>, + val getEntry: (index: UByte, entity: Entity) -> EntityMetadataPacket.Entry<*> + ) : Field(dependingProperties) { override fun createMetadataPacketEntry(index: UByte, entity: Entity) = getEntry(index, entity) } } @@ -70,7 +72,7 @@ class EntityMetadataFieldsTable private constructor(val fields: Map) { @JvmName("requiredInt") fun required(index: UByte, property: KProperty1) { @@ -143,13 +145,6 @@ class EntityMetadataFieldsTable private constructor(val fields: Map