Skip to content

Commit 92eaffd

Browse files
committed
feat: Way more entity system extension
1 parent 59a5e97 commit 92eaffd

5 files changed

Lines changed: 1294 additions & 90 deletions

File tree

mods/kotale/components.csv

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
qualifiedName,storeType,type
2+
com.hypixel.hytale.server.core.modules.entity.player.PlayerSkinComponent,Entity,Default,
3+
com.hypixel.hytale.server.core.modules.entity.tracker.NetworkId,Entity,Default,
4+
com.hypixel.hytale.server.core.modules.entity.DespawnComponent,Entity,Default,
5+
6+
com.hypixel.hytale.server.core.modules.entity.component.ActiveAnimationComponent,Entity,Default,
7+
com.hypixel.hytale.server.core.modules.entity.component.AudioComponent,Entity,Default,
8+
com.hypixel.hytale.server.core.modules.entity.component.BoundingBox,Entity,Default,
9+
com.hypixel.hytale.server.core.modules.entity.component.DisplayNameComponent,Entity,Default,
10+
com.hypixel.hytale.server.core.modules.entity.component.DynamicLight,Entity,Default,
11+
com.hypixel.hytale.server.core.modules.entity.component.EntityScaleComponent,Entity,Default,
12+
com.hypixel.hytale.server.core.modules.entity.component.FromPrefab,Entity,Flag,
13+
com.hypixel.hytale.server.core.modules.entity.component.FromWorldGen,Entity,Flag,
14+
com.hypixel.hytale.server.core.modules.entity.component.HeadRotation,Entity,Default,
15+
com.hypixel.hytale.server.core.modules.entity.component.HiddenFromAdventurePlayers,Entity,Flag,
16+
com.hypixel.hytale.server.core.modules.entity.component.Intangible,Entity,Flag,
17+
com.hypixel.hytale.server.core.modules.entity.component.Invulnerable,Entity,Flag,
18+
com.hypixel.hytale.server.core.modules.entity.component.ModelComponent,Entity,Default,
19+
com.hypixel.hytale.server.core.modules.entity.component.MovementAudioComponent,Entity,Default,
20+
com.hypixel.hytale.server.core.modules.entity.component.NewSpawnComponent,Entity,Default,
21+
com.hypixel.hytale.server.core.modules.entity.component.PersistentDynamicLight,Entity,Default,
22+
com.hypixel.hytale.server.core.modules.entity.component.PersistentModel,Entity,Default,
23+
com.hypixel.hytale.server.core.modules.entity.component.PositionDataComponent,Entity,Default,
24+
com.hypixel.hytale.server.core.modules.entity.component.PropComponent,Entity,Flag,
25+
com.hypixel.hytale.server.core.modules.entity.component.RespondToHit,Entity,Flag,
26+
com.hypixel.hytale.server.core.modules.entity.component.RotateObjectComponent,Entity,Default,
27+
com.hypixel.hytale.server.core.modules.entity.component.SnapshotBuffer,Entity,Default,
28+
com.hypixel.hytale.server.core.modules.entity.component.TransformComponent,Entity,Default,
29+
com.hypixel.hytale.server.core.modules.entity.component.WorldGenId,Entity,Default,
30+
31+
com.hypixel.hytale.server.core.entity.Frozen,Entity,Flag,
32+
com.hypixel.hytale.server.core.entity.UUIDComponent,Entity,Default,Uuid
33+
com.hypixel.hytale.server.core.entity.knockback.KnockbackComponent,Entity,Default,
34+
com.hypixel.hytale.server.core.entity.nameplate.Nameplate,Entity,Default,
35+
com.hypixel.hytale.server.core.entity.group.EntityGroup,Entity,Default,
36+
37+
com.hypixel.hytale.server.core.modules.physics.component.PhysicsValues,Entity,Default,
38+
com.hypixel.hytale.server.core.modules.physics.component.Velocity,Entity,Default,
39+
40+
com.hypixel.hytale.server.core.modules.entity.damage.DeathComponent,Entity,Default,
41+
42+
com.hypixel.hytale.server.core.modules.entity.item.ItemComponent,Entity,Default,
43+
com.hypixel.hytale.server.core.modules.entity.item.PickupItemComponent,Entity,Default,
44+
com.hypixel.hytale.server.core.modules.entity.item.PreventItemMerging,Entity,Flag,
45+
com.hypixel.hytale.server.core.modules.entity.item.PreventPickup,Entity,Flag,
46+
47+
com.hypixel.hytale.server.core.modules.entity.repulsion.Repulsion,Entity,Default,
48+
com.hypixel.hytale.server.core.modules.entity.teleport.Teleport,Entity,Default,
49+
com.hypixel.hytale.server.core.modules.entitystats.EntityStatMap,Entity,Default,EntityStats
50+
51+
52+
com.hypixel.hytale.server.core.modules.projectile.component.PredictedProjectile,Entity,Default,
53+
com.hypixel.hytale.server.core.entity.entities.ProjectileComponent,Entity,Default,
54+
com.hypixel.hytale.server.core.modules.projectile.component.Projectile,Entity,Flag,ProjectileMarker
55+
com.hypixel.hytale.server.core.modules.projectile.config.StandardPhysicsProvider,Entity,Default,StandardPhysics
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
@file:DependsOn("com.squareup:kotlinpoet-jvm:2.2.0")
2+
3+
import java.io.File
4+
5+
val targetFile = File("src/main/kotlin/dev/helight/kotale/ext/Generated.kt")
6+
7+
enum class StoreType(
8+
val cname: String, val accessors: List<Accessor>
9+
) {
10+
Chunk(
11+
"ChunkStore", listOf(
12+
Accessor.Collection(
13+
"ComponentAccessor<ChunkStore>", "Ref<ChunkStore>", "ref"
14+
)
15+
)
16+
),
17+
Entity(
18+
"EntityStore", listOf(
19+
Accessor.Collection("ComponentAccessor<EntityStore>", "Ref<EntityStore>", "ref", true),
20+
Accessor.Collection("ArchetypeChunk<EntityStore>", "Int", "index"),
21+
Accessor.Direct("Ref<EntityStore>", ".store", "this, "),
22+
Accessor.Direct("Holder<EntityStore>", "", "", true)
23+
)
24+
);
25+
26+
val ref: String get() = "Ref<$cname>"
27+
val accessor: String get() = "ComponentAccessor<$cname>"
28+
}
29+
30+
sealed class Accessor() {
31+
abstract val mutable: Boolean
32+
33+
class Collection(
34+
val base: String, val keyType: String, val keyName: String, override val mutable: Boolean = false
35+
) : Accessor()
36+
37+
class Direct(
38+
val base: String, val path: String, val args: String, override val mutable: Boolean = false
39+
) : Accessor()
40+
41+
}
42+
43+
enum class ExtType {
44+
Default, Flag
45+
}
46+
47+
var importList = mutableSetOf<Pair<String, String>>()
48+
importList.add("com.hypixel.hytale.component" to "*")
49+
importList.add("com.hypixel.hytale.server.core.universe.world.storage" to "EntityStore")
50+
importList.add("com.hypixel.hytale.server.core.universe.world.storage" to "ChunkStore")
51+
52+
var functions = mutableListOf<String>()
53+
54+
File("components.csv").readLines().drop(1).forEach { line ->
55+
if (line.isBlank()) return@forEach
56+
57+
val parts = line.split(",")
58+
val qualified = parts[0]
59+
val nameParts = qualified.split(".")
60+
val name = nameParts.last()
61+
importList.add(nameParts.take(nameParts.size - 1).joinToString(".") to name)
62+
63+
val storeType = StoreType.valueOf(parts[1])
64+
val extType = ExtType.valueOf(parts[2])
65+
66+
val simpleName = parts[3].takeIf { it.isNotBlank() } ?: name.removeSuffix("Component")
67+
val lowerName = simpleName.replaceFirstChar { it.lowercase() }
68+
functions.add("\n// $name")
69+
70+
storeType.accessors.forEach {
71+
if (it is Accessor.Direct) {
72+
if (extType == ExtType.Default) """
73+
val ${it.base}.${lowerName}OrNull: $name?
74+
get() = this${it.path}.getComponent(${it.args}$name.getComponentType())
75+
""".trimIndent().let { functions.add(it) }
76+
77+
if (it.mutable) {
78+
if (extType == ExtType.Default) """
79+
var ${it.base}.$lowerName: $name
80+
get() = this${it.path}.getComponent(${it.args}$name.getComponentType())!!
81+
set(value) = this${it.path}.putComponent(${it.args}$name.getComponentType(), value)
82+
""".trimIndent().let { functions.add(it) }
83+
84+
if (extType == ExtType.Flag) """
85+
var ${it.base}.is$simpleName: Boolean
86+
get() = this${it.path}.getComponent(${it.args}$name.getComponentType()) != null
87+
set(value) {
88+
if (value) this${it.path}.ensureComponent(${it.args}$name.getComponentType())
89+
else this${it.path}.tryRemoveComponent(${it.args}$name.getComponentType())
90+
}
91+
""".trimIndent().let { functions.add(it) }
92+
else {
93+
94+
""" fun ${it.base}.ensure${simpleName}(component: $name): $name = this${it.path}.ensureAndGetComponent(${it.args}$name.getComponentType())
95+
""".trimIndent().let { functions.add(it) }
96+
97+
"""
98+
fun ${it.base}.add${simpleName}(component: $name) = this${it.path}.addComponent(${it.args}$name.getComponentType(), component)
99+
""".trimIndent().let { functions.add(it) }
100+
101+
"""
102+
fun ${it.base}.remove${simpleName}() = this${it.path}.removeComponent(${it.args}$name.getComponentType())
103+
""".trimIndent().let { functions.add(it) }
104+
}
105+
106+
107+
} else {
108+
if (extType == ExtType.Default) """
109+
val ${it.base}.$lowerName: $name
110+
get() = this${it.path}.getComponent(${it.args}$name.getComponentType())!!
111+
""".trimIndent().let { functions.add(it) }
112+
else """
113+
val ${it.base}.is$simpleName: Boolean
114+
get() = this${it.path}.getComponent(${it.args}$name.getComponentType()) != null
115+
""".trimIndent().let { functions.add(it) }
116+
117+
118+
}
119+
} else if (it is Accessor.Collection) {
120+
121+
if (extType == ExtType.Default) {
122+
"""
123+
fun ${it.base}.$lowerName(${it.keyName}: ${it.keyType}): $name = this.getComponent(${it.keyName}, $name.getComponentType())!!
124+
""".trimIndent().let { functions.add(it) }
125+
126+
"""
127+
fun ${it.base}.${lowerName}OrNull(${it.keyName}: ${it.keyType}): $name? = this.getComponent(${it.keyName}, $name.getComponentType())
128+
""".trimIndent().let { functions.add(it) }
129+
} else {
130+
"""
131+
fun ${it.base}.${lowerName}(${it.keyName}: ${it.keyType}): Boolean = this.getComponent(${it.keyName}, $name.getComponentType()) != null
132+
""".trimIndent().let { functions.add(it) }
133+
}
134+
135+
136+
137+
if (it.mutable) {
138+
if (extType == ExtType.Default) {
139+
"""
140+
fun ${it.base}.ensure$simpleName(${it.keyName}: ${it.keyType}): $name = this.ensureAndGetComponent(${it.keyName}, $name.getComponentType())
141+
""".trimIndent().let { functions.add(it) }
142+
143+
"""
144+
fun ${it.base}.add$simpleName(${it.keyName}: ${it.keyType}, component: $name) = this.addComponent(${it.keyName}, $name.getComponentType(), component)
145+
""".trimIndent().let { functions.add(it) }
146+
147+
"""
148+
fun ${it.base}.put$simpleName(${it.keyName}: ${it.keyType}, component: $name) = this.putComponent(${it.keyName}, $name.getComponentType(), component)
149+
""".trimIndent().let { functions.add(it) }
150+
151+
"""
152+
fun ${it.base}.remove$simpleName(${it.keyName}: ${it.keyType}) = this.removeComponent(${it.keyName}, $name.getComponentType())
153+
""".trimIndent().let { functions.add(it) }
154+
} else {
155+
"""
156+
fun ${it.base}.set$simpleName(${it.keyName}: ${it.keyType}, value: Boolean) {
157+
if (value) this.ensureAndGetComponent(${it.keyName}, $name.getComponentType())
158+
else this.tryRemoveComponent(${it.keyName}, $name.getComponentType())
159+
}
160+
""".trimIndent().let { functions.add(it) }
161+
}
162+
163+
}
164+
}
165+
}
166+
}
167+
168+
val builder = StringBuilder()
169+
builder.appendLine("@file:Suppress(\"unused\")")
170+
builder.appendLine()
171+
builder.appendLine("package dev.helight.kotale.ext")
172+
builder.appendLine()
173+
importList.forEach {
174+
builder.appendLine("import ${it.first}.${it.second}")
175+
}
176+
builder.appendLine()
177+
178+
functions.forEach {
179+
builder.appendLine(it)
180+
}
181+
println(builder.toString())
182+
targetFile.writeText(builder.toString())

mods/kotale/src/main/kotlin/dev/helight/kotale/ext/EntityStoreExtensions.kt

Lines changed: 29 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -2,117 +2,56 @@ package dev.helight.kotale.ext
22

33
import com.hypixel.hytale.component.ComponentAccessor
44
import com.hypixel.hytale.component.Ref
5-
import com.hypixel.hytale.server.core.entity.Frozen
6-
import com.hypixel.hytale.server.core.entity.UUIDComponent
7-
import com.hypixel.hytale.server.core.entity.damage.DamageDataComponent
5+
import com.hypixel.hytale.component.Store
6+
import com.hypixel.hytale.math.vector.Transform
87
import com.hypixel.hytale.server.core.entity.entities.Player
9-
import com.hypixel.hytale.server.core.entity.nameplate.Nameplate
10-
import com.hypixel.hytale.server.core.modules.entity.component.*
11-
import com.hypixel.hytale.server.core.modules.entity.damage.DeathComponent
12-
import com.hypixel.hytale.server.core.modules.entity.item.ItemComponent
13-
import com.hypixel.hytale.server.core.modules.entity.repulsion.Repulsion
14-
import com.hypixel.hytale.server.core.modules.physics.component.Velocity
15-
import com.hypixel.hytale.server.core.modules.projectile.config.StandardPhysicsProvider
8+
import com.hypixel.hytale.server.core.modules.entity.teleport.Teleport
9+
import com.hypixel.hytale.server.core.modules.entitystats.EntityStatMap
10+
import com.hypixel.hytale.server.core.modules.entitystats.asset.DefaultEntityStatTypes
1611
import com.hypixel.hytale.server.core.universe.PlayerRef
12+
import com.hypixel.hytale.server.core.universe.world.World
1713
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore
18-
import java.util.*
1914

2015
val Ref<EntityStore>.player: Player get() = this.store.player(this)
2116
val Ref<EntityStore>.playerRef: PlayerRef get() = this.store.playerRef(this)
22-
val Ref<EntityStore>.playerRefOrNull: PlayerRef? get() = this.store.maybePlayerRef(this)
17+
val Ref<EntityStore>.playerRefOrNull: PlayerRef? get() = this.store.playerRefOrNull(this)
18+
val PlayerRef.playerOrNull: Player? get() = this.reference?.player
19+
20+
fun Store<EntityStore>.heal(ref: Ref<EntityStore>) {
21+
val comp = getComponent(ref, EntityStatMap.getComponentType())
22+
comp?.resetStatValue(DefaultEntityStatTypes.getHealth())
23+
}
24+
25+
fun Store<EntityStore>.teleport(ref: Ref<EntityStore>, world: World, transform: Transform) {
26+
this.addComponent(
27+
ref,
28+
Teleport.getComponentType(),
29+
Teleport(world, transform.position.clone(), transform.rotation.clone())
30+
)
31+
}
2332

2433
fun ComponentAccessor<EntityStore>.playerRef(ref: Ref<EntityStore>): PlayerRef {
2534
return this.getComponent(ref, PlayerRef.getComponentType()) as PlayerRef
2635
}
2736

37+
@Deprecated("Name change for consistency", replaceWith = ReplaceWith("playerRefOrNull"))
2838
fun ComponentAccessor<EntityStore>.maybePlayerRef(ref: Ref<EntityStore>): PlayerRef? {
2939
return this.getComponent(ref, PlayerRef.getComponentType())
3040
}
3141

42+
fun ComponentAccessor<EntityStore>.playerRefOrNull(ref: Ref<EntityStore>): PlayerRef? {
43+
return this.getComponent(ref, PlayerRef.getComponentType())
44+
}
45+
3246
fun ComponentAccessor<EntityStore>.player(ref: Ref<EntityStore>): Player {
3347
return this.getComponent(ref, Player.getComponentType()) as Player
3448
}
3549

50+
@Deprecated("Name change for consistency", replaceWith = ReplaceWith("playerOrNull"))
3651
fun ComponentAccessor<EntityStore>.maybePlayer(ref: Ref<EntityStore>): Player? {
3752
return this.getComponent(ref, Player.getComponentType())
3853
}
3954

40-
fun ComponentAccessor<EntityStore>.transform(ref: Ref<EntityStore>): TransformComponent {
41-
return this.getComponent(ref, TransformComponent.getComponentType()) as TransformComponent
42-
}
43-
44-
fun ComponentAccessor<EntityStore>.uuid(ref: Ref<EntityStore>): UUID {
45-
return (this.getComponent(ref, UUIDComponent.getComponentType()) as UUIDComponent).uuid
46-
}
47-
48-
fun ComponentAccessor<EntityStore>.scale(ref: Ref<EntityStore>): Float {
49-
return (this.getComponent(ref, EntityScaleComponent.getComponentType()) as EntityScaleComponent).scale
50-
}
51-
52-
fun ComponentAccessor<EntityStore>.velocity(ref: Ref<EntityStore>): Velocity {
53-
return this.getComponent(ref, Velocity.getComponentType()) as Velocity
54-
}
55-
56-
fun ComponentAccessor<EntityStore>.standardPhysics(ref: Ref<EntityStore>): StandardPhysicsProvider {
57-
return this.getComponent(ref, StandardPhysicsProvider.getComponentType()) as StandardPhysicsProvider
58-
}
59-
60-
fun ComponentAccessor<EntityStore>.boundingBox(ref: Ref<EntityStore>): BoundingBox {
61-
return this.getComponent(ref, BoundingBox.getComponentType()) as BoundingBox
62-
}
63-
64-
fun ComponentAccessor<EntityStore>.headRotation(ref: Ref<EntityStore>): HeadRotation {
65-
return this.getComponent(ref, HeadRotation.getComponentType()) as HeadRotation
66-
}
67-
68-
fun ComponentAccessor<EntityStore>.audio(ref: Ref<EntityStore>): AudioComponent {
69-
return this.getComponent(ref, AudioComponent.getComponentType()) as AudioComponent
70-
}
71-
72-
fun ComponentAccessor<EntityStore>.model(ref: Ref<EntityStore>): ModelComponent {
73-
return this.getComponent(ref, ModelComponent.getComponentType()) as ModelComponent
74-
}
75-
76-
fun ComponentAccessor<EntityStore>.persistentModel(ref: Ref<EntityStore>): PersistentModel {
77-
return this.getComponent(ref, PersistentModel.getComponentType()) as PersistentModel
78-
}
79-
80-
fun ComponentAccessor<EntityStore>.item(ref: Ref<EntityStore>): ItemComponent {
81-
return this.getComponent(ref, ItemComponent.getComponentType()) as ItemComponent
82-
}
83-
84-
fun ComponentAccessor<EntityStore>.nameplate(ref: Ref<EntityStore>): Nameplate {
85-
return this.getComponent(ref, Nameplate.getComponentType()) as Nameplate
86-
}
87-
88-
fun ComponentAccessor<EntityStore>.repulsion(ref: Ref<EntityStore>): Repulsion {
89-
return this.getComponent(ref, Repulsion.getComponentType()) as Repulsion
90-
}
91-
92-
fun ComponentAccessor<EntityStore>.damageData(ref: Ref<EntityStore>): DamageDataComponent {
93-
return this.getComponent(ref, DamageDataComponent.getComponentType()) as DamageDataComponent
94-
}
95-
96-
fun ComponentAccessor<EntityStore>.maybeDeath(ref: Ref<EntityStore>): DeathComponent? {
97-
return this.getComponent(ref, DeathComponent.getComponentType())
98-
}
99-
100-
fun ComponentAccessor<EntityStore>.intangible(ref: Ref<EntityStore>): Boolean {
101-
return this.getComponent(ref, Intangible.getComponentType()) != null
102-
}
103-
104-
fun ComponentAccessor<EntityStore>.invulnerable(ref: Ref<EntityStore>): Boolean {
105-
return this.getComponent(ref, Invulnerable.getComponentType()) != null
106-
}
107-
108-
fun ComponentAccessor<EntityStore>.interactable(ref: Ref<EntityStore>): Boolean {
109-
return this.getComponent(ref, Interactable.getComponentType()) != null
110-
}
111-
112-
fun ComponentAccessor<EntityStore>.frozen(ref: Ref<EntityStore>): Boolean {
113-
return this.getComponent(ref, Frozen.getComponentType()) != null
114-
}
115-
116-
fun ComponentAccessor<EntityStore>.prop(ref: Ref<EntityStore>): Boolean {
117-
return this.getComponent(ref, PropComponent.getComponentType()) != null
55+
fun ComponentAccessor<EntityStore>.playerOrNull(ref: Ref<EntityStore>): Player? {
56+
return this.getComponent(ref, Player.getComponentType())
11857
}

0 commit comments

Comments
 (0)