This repository was archived by the owner on Nov 28, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathEntity.kt
More file actions
403 lines (332 loc) · 14.2 KB
/
Entity.kt
File metadata and controls
403 lines (332 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package io.github.dockyardmc.entity
import cz.lukynka.bindables.Bindable
import cz.lukynka.bindables.BindableList
import cz.lukynka.bindables.BindableMap
import cz.lukynka.bindables.BindablePool
import io.github.dockyardmc.config.ConfigManager
import io.github.dockyardmc.effects.AppliedPotionEffect
import io.github.dockyardmc.effects.AppliedPotionEffectSettings
import io.github.dockyardmc.entity.EntityManager.despawnEntity
import io.github.dockyardmc.entity.handlers.*
import io.github.dockyardmc.entity.metadata.EntityMetadata
import io.github.dockyardmc.entity.metadata.EntityMetadataType
import io.github.dockyardmc.events.*
import io.github.dockyardmc.extentions.sendPacket
import io.github.dockyardmc.item.ItemStack
import io.github.dockyardmc.location.Location
import io.github.dockyardmc.maths.vectors.Vector3d
import io.github.dockyardmc.maths.vectors.Vector3f
import io.github.dockyardmc.player.EntityPose
import io.github.dockyardmc.player.PersistentPlayer
import io.github.dockyardmc.player.Player
import io.github.dockyardmc.player.toPersistent
import io.github.dockyardmc.protocol.packets.ClientboundPacket
import io.github.dockyardmc.protocol.packets.play.clientbound.*
import io.github.dockyardmc.protocol.types.EquipmentSlot
import io.github.dockyardmc.registry.DamageTypes
import io.github.dockyardmc.registry.registries.DamageType
import io.github.dockyardmc.registry.registries.EntityType
import io.github.dockyardmc.registry.registries.PotionEffect
import io.github.dockyardmc.scheduler.runnables.inWholeMinecraftTicks
import io.github.dockyardmc.scheduler.runnables.ticks
import io.github.dockyardmc.sounds.Sound
import io.github.dockyardmc.sounds.playSound
import io.github.dockyardmc.team.Team
import io.github.dockyardmc.team.TeamManager
import io.github.dockyardmc.utils.Disposable
import io.github.dockyardmc.utils.mergeEntityMetadata
import io.github.dockyardmc.utils.viewable.Viewable
import io.github.dockyardmc.world.World
import io.github.dockyardmc.world.chunk.Chunk
import io.github.dockyardmc.world.chunk.ChunkPos
import java.util.*
import kotlin.math.cos
import kotlin.math.sin
import kotlin.time.Duration
abstract class Entity(open var location: Location, open var world: World) : Disposable, Viewable() {
val bindablePool = BindablePool()
abstract var type: EntityType
abstract val health: Bindable<Float>
abstract var inventorySize: Int
open var id: Int = EntityManager.entityIdCounter.incrementAndGet()
open var uuid: UUID = UUID.randomUUID()
open val velocity: Vector3d = Vector3d()
open var isInvulnerable: Boolean = false
open var isOnGround: Boolean = true
open var tickable: Boolean = true
val customName: Bindable<String?> = bindablePool.provideBindable(null)
val customNameVisible: Bindable<Boolean> = bindablePool.provideBindable(false)
val metadata: EntityMetadataHandler = EntityMetadataHandler(this)
val pose: Bindable<EntityPose> = bindablePool.provideBindable(EntityPose.STANDING)
val metadataLayers: BindableMap<PersistentPlayer, MutableMap<EntityMetadataType, EntityMetadata>> = bindablePool.provideBindableMap()
val isOnFire: Bindable<Boolean> = bindablePool.provideBindable(false)
val freezeTicks: Bindable<Int> = bindablePool.provideBindable(0)
val hasNoGravity: Bindable<Boolean> = bindablePool.provideBindable(true)
val isSilent: Bindable<Boolean> = bindablePool.provideBindable(false)
val stuckArrows: Bindable<Int> = bindablePool.provideBindable(0)
var gravityTickCount = 0
val maxHealth get() = health.defaultValue
val potionEffects: BindableMap<PotionEffect, AppliedPotionEffect> = bindablePool.provideBindableMap()
val isInvisible: Bindable<Boolean> = bindablePool.provideBindable(false)
val isGlowing: Bindable<Boolean> = bindablePool.provideBindable(false)
val team: Bindable<Team?> = bindablePool.provideBindable(null)
val equipment: BindableMap<EquipmentSlot, ItemStack> = bindablePool.provideBindableMap()
val equipmentLayers: BindableMap<PersistentPlayer, Map<EquipmentSlot, ItemStack>> = bindablePool.provideBindableMap()
var viewDistanceBlocks: Int = ConfigManager.config.implementationConfig.defaultEntityViewDistanceBlocks
val passengers: BindableList<Entity> = BindableList()
var vehicle: Entity? = null
val equipmentHandler = EntityEquipmentHandler(this)
val vehicleHandler = EntityVehicleHandler(this)
val potionEffectsHandler = EntityPotionEffectsHandler(this)
val itemPickupHandler = EntityItemPickupHandler(this)
var isDead: Boolean = false
override var autoViewable: Boolean = true
constructor(location: Location) : this(location, location.world)
init {
equipmentHandler.handle(equipment, equipmentLayers)
vehicleHandler.handle(passengers)
potionEffectsHandler.handle(potionEffects)
metadata.handleBindables(
hasNoGravity = hasNoGravity,
entityIsOnFire = isOnFire,
freezeTicks = freezeTicks,
metadataLayers = metadataLayers,
isGlowing = isGlowing,
isInvisible = isInvisible,
pose = pose,
isSilent = isSilent,
customName = customName,
customNameVisible = customNameVisible,
stuckArrows = stuckArrows,
)
team.valueChanged { event ->
require(event.newValue == null || TeamManager.teams.values.containsKey(event.newValue!!.name)) { "Team ${event.newValue!!.name} is not registered!" }
event.oldValue?.entities?.remove(this)
event.newValue?.entities?.add(this)
}
}
fun getCurrentChunk(): Chunk? {
return world.chunks[getCurrentChunkPos().pack()]
}
fun getCurrentChunkPos(): ChunkPos {
return ChunkPos.fromLocation(location)
}
fun updateEntity(player: Player, respawn: Boolean = false) {
sendMetadataPacketToViewers()
}
open fun tick() {
potionEffectsHandler.tick()
itemPickupHandler.tick()
}
open fun canPickupItem(dropEntity: ItemDropEntity, item: ItemStack): Boolean {
return false
}
override fun addViewer(player: Player): Boolean {
if (this.isDead) return false
val event = EntityViewerAddEvent(this, player)
Events.dispatch(event)
if (event.cancelled) return false
if (!super.addViewer(player)) return false
sendMetadataPacket(player)
val entitySpawnPacket = ClientboundSpawnEntityPacket(id, uuid, type.getProtocolId(), location, location.yaw, 0, velocity)
isOnGround = true
synchronized(player.entityViewSystem.visibleEntities) {
player.entityViewSystem.visibleEntities.add(this)
}
player.sendPacket(entitySpawnPacket)
sendMetadataPacket(player)
sendMetadataPacketToViewers()
return true
}
fun canSee(entity: Entity): Boolean {
return entity.viewers.contains(this)
}
override fun removeViewer(player: Player) {
val event = EntityViewerRemoveEvent(this, player)
Events.dispatch(event)
if (event.cancelled) return
super.removeViewer(player)
val entityDespawnPacket = ClientboundEntityRemovePacket(this)
player.sendPacket(entityDespawnPacket)
synchronized(player.entityViewSystem.visibleEntities) {
player.entityViewSystem.visibleEntities.remove(this)
}
}
open fun lookAt(target: Entity) {
val newLoc = this.location.setDirection(target.location.toVector3d() - (this.location).toVector3d())
teleport(newLoc)
}
open fun lookAt(targetLocation: Location) {
val newLoc = this.location.setDirection(targetLocation.toVector3d() - (this.location).toVector3d())
teleport(newLoc)
}
open fun lookAtClientside(target: Entity, player: Player) {
lookAtClientside(target, listOf(player))
}
open fun lookAtClientside(target: Entity, players: Collection<Player>) {
val clonedLoc = location.clone()
val newLoc = clonedLoc.setDirection(target.location.toVector3d() - (this.location).toVector3d())
teleportClientside(newLoc, players)
}
open fun sendMetadataPacketToViewers() {
viewers.toList().forEach(this::sendMetadataPacket)
}
open fun sendMetadataPacket(player: Player) {
val metadata = mergeEntityMetadata(this, metadataLayers[player.toPersistent()])
val packet = ClientboundSetEntityMetadataPacket(this, metadata)
player.sendPacket(packet)
}
open fun sendEquipmentPacket(player: Player) {
val equipment = getMergedEquipmentData(equipment.values, equipmentLayers[player.toPersistent()])
val packet = ClientboundSetEntityEquipmentPacket(this, equipment)
player.sendPacket(packet)
}
open fun calculateBoundingBox(): BoundingBox {
val width = type.dimensions.width
val height = type.dimensions.height
return BoundingBox(
location.x - width / 2,
location.x + width / 2,
location.y - height / 2,
location.y + height / 2,
location.z - width / 2,
location.z + width / 2
)
}
open fun teleport(location: Location) {
if (this.isDead) return
this.location = location
viewers.sendPacket(ClientboundEntityTeleportPacket(this, location))
viewers.sendPacket(ClientboundSetHeadYawPacket(this))
if (passengers.values.isNotEmpty()) {
viewers.sendPacket(ClientboundMoveVehiclePacket(this))
}
}
open fun teleportClientside(location: Location, player: Player) {
if (this.isDead) return
teleportClientside(location, listOf(player))
}
open fun teleportClientside(location: Location, players: Collection<Player>) {
players.sendPacket(ClientboundEntityTeleportPacket(this, location))
players.sendPacket(ClientboundSetHeadYawPacket(this, location))
}
open fun damage(damage: Float, damageType: DamageType, attacker: Entity? = null, projectile: Entity? = null) {
val event = EntityDamageEvent(this, damage, damageType, attacker, projectile)
Events.dispatch(event)
if (event.cancelled) return
if (isDead) return
// var location: Location?
// if (attacker != null) location = attacker.location
// if (projectile != null) location = projectile.location
if (event.damage > 0) {
playDamageAnimation(damageType)
if (!isInvulnerable) {
if (health.value - event.damage <= 0) kill() else health.value -= event.damage
}
}
}
fun playDeathAnimation() {
val packet = ClientboundEntityEventPacket(this, EntityEvent.LIVING_ENTITY_PLAY_DEATH_SOUND)
pose.value = EntityPose.DYING
viewers.sendPacket(packet)
passengers.clear(false)
}
fun playDamageAnimation(damageType: DamageType = DamageTypes.GENERIC) {
val packet = ClientboundDamageEventPacket(this, damageType, null, null, null)
viewers.sendPacket(packet)
}
fun playSoundToViewers(sound: Sound, location: Location? = this.location) {
viewers.playSound(sound, location)
}
open fun kill() {
if (isDead) return
val event = EntityDeathEvent(this)
Events.dispatch(event)
if (event.cancelled) {
health.value = 0.1f
return
}
isDead = true
health.value = 0f
playDeathAnimation()
world.scheduler.runLater(20.ticks) {
world.despawnEntity(this)
}
}
data class BoundingBox(
val minX: Double,
val maxX: Double,
val minY: Double,
val maxY: Double,
val minZ: Double,
val maxZ: Double,
)
fun sendSelfPacketIfPlayer(packet: ClientboundPacket) {
if (this is Player) this.sendPacket(packet)
}
fun sendSelfMetadataIfPlayer() {
if (this is Player) sendMetadataPacket(this)
}
fun addPotionEffect(
effect: PotionEffect,
duration: Duration,
amplifier: Int = 1,
showParticles: Boolean = false,
showBlueBorder: Boolean = false,
showIconOnHud: Boolean = false,
) {
val potionEffect = AppliedPotionEffect(effect, AppliedPotionEffectSettings(amplifier, duration, showBlueBorder, showParticles, showIconOnHud))
this.potionEffects[effect] = potionEffect
}
fun removePotionEffect(effect: PotionEffect) {
this.potionEffects.remove(effect)
}
fun removePotionEffect(effect: AppliedPotionEffect) {
this.potionEffects.remove(effect.effect)
}
fun clearPotionEffects() {
this.potionEffects.clear()
}
fun refreshPotionEffects() {
viewers.forEach(::sendPotionEffectsPacket)
if (this is Player) this.sendPotionEffectsPacket(this)
}
fun sendPotionEffectsPacket(player: Player) {
potionEffects.values.values.forEach { effect ->
val packet = ClientboundEntityEffectPacket(
this,
effect.effect,
effect.settings.amplifier,
effect.settings.duration.inWholeMinecraftTicks,
effect.settings.showParticles,
effect.settings.isAmbient,
effect.settings.showIcon
)
player.sendPacket(packet)
}
}
fun getFacingDirectionVector(): Vector3f {
val yawRadians = Math.toRadians(location.yaw.toDouble())
val pitchRadians = Math.toRadians(location.pitch.toDouble())
val x = -sin(yawRadians) * cos(pitchRadians)
val y = -sin(pitchRadians)
val z = cos(yawRadians) * cos(pitchRadians)
return Vector3f(x.toFloat(), y.toFloat(), z.toFloat())
}
fun dismountCurrentVehicle() {
if (vehicle != null && vehicle!!.passengers.contains(this)) {
vehicle!!.passengers.remove(this)
}
}
val eyeHeight get() = location.add(0.0, this.type.dimensions.eyeHeight.toDouble(), 0.0)
override fun dispose() {
this.autoViewable = false
team.value = null
equipmentLayers.clear()
viewers.toList().forEach { removeViewer(it) }
metadataLayers.clear()
passengers.values.forEach(passengers::removeIfPresent)
bindablePool.dispose()
despawnEntity(this)
}
}