Archived
1
0
Fork 0

Add custom name attribute

This commit is contained in:
Moritz Ruth 2021-04-07 15:49:16 +02:00
parent b51ed1c291
commit 71cd8bf486
No known key found for this signature in database
GPG key ID: AFD57E23E753841B
7 changed files with 28 additions and 14 deletions

View file

@ -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

View file

@ -106,9 +106,10 @@ class TestPlugin : Plugin("Test", "1.0.0") {
val entity = Uranos.create<CowEntity>()
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())
}
}
}

View file

@ -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
}

View file

@ -26,6 +26,7 @@ object EntityMetadataPacketCodec : OutgoingPacketCodec<EntityMetadataPacket>(0x4
}
}
is EntityMetadataPacket.Entry.Boolean -> dst.writeBoolean(entry.value)
else -> throw NotImplementedError()
}
}

View file

@ -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) {

View file

@ -14,6 +14,8 @@ class UranosCowEntity(server: UranosServer) : UranosHasMovableHeadLivingEntity(s
companion object {
val entityMetadataFieldsTable = EntityMetadataFieldsTable.DEFAULT.extend {
livingEntity()
required(15u, Ageable::isBaby)
}
}

View file

@ -60,8 +60,10 @@ class EntityMetadataFieldsTable private constructor(val fields: Map<UByte, Field
}
}
class Computed(dependingProperties: Array<KProperty1<out Entity, *>>, val getEntry: (index: UByte, entity: Entity) -> EntityMetadataPacket.Entry<*>) :
Field(dependingProperties) {
class Computed(
dependingProperties: Array<KProperty1<out Entity, *>>,
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<UByte, Field
EntityMetadataFieldsTable(fields.plus(buildMap { BuilderContext(this).init() }))
@BuilderMarker
@Suppress("INAPPLICABLE_JVM_NAME")
@Suppress("INAPPLICABLE_JVM_NAME") // Fails during build without this annotation. Maybe a bug
class BuilderContext(private val fields: MutableMap<UByte, Field>) {
@JvmName("requiredInt")
fun required(index: UByte, property: KProperty1<out Entity, Int>) {
@ -143,13 +145,6 @@ class EntityMetadataFieldsTable private constructor(val fields: Map<UByte, Field
+Entity::glowing
+false // flying with an elytra
}
static(1u, 0) // ticks until drowning
static(2u, false) // custom name
static(3u, false) // custom name visible
static(4u, false) // silent
static(5u, false) // ignores gravity
static(6u, 0) // pose
}
}
}