Skip to content

Commit

Permalink
LookAtEntityInteractionBoundEntry
Browse files Browse the repository at this point in the history
  • Loading branch information
gabber235 committed Dec 16, 2024
1 parent c104b52 commit 8cec3e5
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.typewritermc.engine.paper.entry.entity

import com.typewritermc.core.entries.Ref
import com.typewritermc.core.utils.point.Position
import com.typewritermc.engine.paper.entry.entries.EntityDefinitionEntry
import com.typewritermc.engine.paper.entry.entries.EntityInstanceEntry
import org.bukkit.Location
import java.util.UUID

Expand All @@ -10,13 +12,20 @@ interface ActivityEntityDisplay {
val definition: EntityDefinitionEntry?
get() = creator as? EntityDefinitionEntry

val instanceEntryRef: Ref<out EntityInstanceEntry>

fun playerSeesEntity(playerId: UUID, entityId: Int): Boolean

/**
* The location of the entity for the player.
*/
fun position(playerId: UUID): Position?

/**
* The entity state for the player.
*/
fun entityState(playerId: UUID): EntityState

/**
* Whether the player can view the entity.
* This is regardless of whether the entity is spawned in for the player.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class EntityHandler : PacketListenerAbstract(), KoinComponent {
.firstOrNull { it.playerSeesEntity(event.user.uuid, entityId) } ?: return

val definition = display.definition ?: return
AsyncEntityDefinitionInteract(player, entityId, definition, packet.hand, packet.action).callEvent()
val instance = display.instanceEntryRef.get() ?: return
AsyncEntityDefinitionInteract(player, entityId, definition, instance, packet.hand, packet.action).callEvent()
}

fun shutdown() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import java.util.*
import java.util.concurrent.ConcurrentHashMap

class GroupActivityEntityDisplay(
private val ref: Ref<out EntityInstanceEntry>,
override val instanceEntryRef: Ref<out EntityInstanceEntry>,
override val creator: EntityCreator,
private val activityCreators: ActivityCreator,
private val suppliers: List<Pair<PropertySupplier<*>, Int>>,
private val spawnPosition: Position,
private val group: GroupEntry,
) : AudienceFilter(ref), TickableDisplay, ActivityEntityDisplay {
) : AudienceFilter(instanceEntryRef), TickableDisplay, ActivityEntityDisplay {
private val activityManagers = ConcurrentHashMap<GroupId, ActivityManager<in SharedActivityContext>>()
private val entities = ConcurrentHashMap<UUID, DisplayEntity>()

Expand All @@ -34,7 +34,7 @@ class GroupActivityEntityDisplay(
val groupId = group.groupId(player) ?: GroupId(player.uniqueId)
activityManagers.computeIfAbsent(groupId) {
val viewers = groupViewers(groupId)
val context = SharedActivityContext(ref, viewers)
val context = SharedActivityContext(instanceEntryRef, viewers)
val activity = activityCreators.create(context, spawnPosition.toProperty())
val activityManager = ActivityManager(activity)
activityManager.initialize(context)
Expand Down Expand Up @@ -66,7 +66,7 @@ class GroupActivityEntityDisplay(
val viewerId = viewers.firstOrNull()?.uniqueId
val entityState = if (viewerId != null) entities[viewerId]?.state ?: EntityState() else EntityState()

val context = SharedActivityContext(ref, viewers, entityState)
val context = SharedActivityContext(instanceEntryRef, viewers, entityState)
manager.tick(context)
}

Expand All @@ -83,7 +83,7 @@ class GroupActivityEntityDisplay(
val groupId = group.groupId(player) ?: GroupId(player.uniqueId)
// If no players are considered for this group, we can remove the activity manager
if (consideredPlayers.none { groupId == group.groupId(it) }) {
activityManagers.remove(groupId)?.dispose(SharedActivityContext(ref, emptyList()))
activityManagers.remove(groupId)?.dispose(SharedActivityContext(instanceEntryRef, emptyList()))
}
}

Expand All @@ -92,7 +92,7 @@ class GroupActivityEntityDisplay(
entities.values.forEach { it.dispose() }
entities.clear()
activityManagers.forEach { (groupId, manager) ->
manager.dispose(SharedActivityContext(ref, groupViewers(groupId)))
manager.dispose(SharedActivityContext(instanceEntryRef, groupViewers(groupId)))
}
activityManagers.clear()
}
Expand All @@ -106,6 +106,7 @@ class GroupActivityEntityDisplay(
val groupId = group.groupId(player) ?: GroupId(player.uniqueId)
return activityManagers[groupId]?.position
}
override fun entityState(playerId: UUID): EntityState = entities[playerId]?.state ?: EntityState()

override fun canView(playerId: UUID): Boolean = canConsider(playerId)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package com.typewritermc.engine.paper.entry.entity

import lirand.api.extensions.server.server
import com.typewritermc.core.entries.Ref
import com.typewritermc.core.utils.point.Position
import com.typewritermc.engine.paper.entry.entries.AudienceFilter
import com.typewritermc.engine.paper.entry.entries.EntityInstanceEntry
import com.typewritermc.engine.paper.entry.entries.PropertySupplier
import com.typewritermc.engine.paper.entry.entries.TickableDisplay
import lirand.api.extensions.server.server
import org.bukkit.entity.Player
import java.util.*
import java.util.concurrent.ConcurrentHashMap

class IndividualActivityEntityDisplay(
private val ref: Ref<out EntityInstanceEntry>,
override val instanceEntryRef: Ref<out EntityInstanceEntry>,
override val creator: EntityCreator,
private val activityCreator: ActivityCreator,
private val suppliers: List<Pair<PropertySupplier<*>, Int>>,
private val spawnPosition: Position,
) : AudienceFilter(ref), TickableDisplay, ActivityEntityDisplay {
) : AudienceFilter(instanceEntryRef), TickableDisplay, ActivityEntityDisplay {
private val activityManagers = ConcurrentHashMap<UUID, ActivityManager<in IndividualActivityContext>>()
private val entities = ConcurrentHashMap<UUID, DisplayEntity>()

Expand All @@ -30,7 +30,7 @@ class IndividualActivityEntityDisplay(

override fun onPlayerAdd(player: Player) {
activityManagers.computeIfAbsent(player.uniqueId) {
val context = IndividualActivityContext(ref, player)
val context = IndividualActivityContext(instanceEntryRef, player)
val activity = activityCreator.create(context, spawnPosition.toProperty())
val activityManager = ActivityManager(activity)
activityManager.initialize(context)
Expand All @@ -54,7 +54,7 @@ class IndividualActivityEntityDisplay(
val player = server.getPlayer(pid) ?: return@forEach
val isViewing = pid in this
val entityState = entities[pid]?.state ?: EntityState()
manager.tick(IndividualActivityContext(ref, player, isViewing, entityState))
manager.tick(IndividualActivityContext(instanceEntryRef, player, isViewing, entityState))
}
entities.values.forEach { it.tick() }
}
Expand All @@ -66,15 +66,20 @@ class IndividualActivityEntityDisplay(

override fun onPlayerRemove(player: Player) {
super.onPlayerRemove(player)
activityManagers.remove(player.uniqueId)?.dispose(IndividualActivityContext(ref, player))
activityManagers.remove(player.uniqueId)?.dispose(IndividualActivityContext(instanceEntryRef, player))
}

override fun dispose() {
super.dispose()
entities.values.forEach { it.dispose() }
entities.clear()
activityManagers.entries.forEach { (playerId, activityManager) ->
activityManager.dispose(IndividualActivityContext(ref, server.getPlayer(playerId) ?: return@forEach))
activityManager.dispose(
IndividualActivityContext(
instanceEntryRef,
server.getPlayer(playerId) ?: return@forEach
)
)
}
activityManagers.clear()
}
Expand All @@ -84,6 +89,7 @@ class IndividualActivityEntityDisplay(
}

override fun position(playerId: UUID): Position? = activityManagers[playerId]?.position
override fun entityState(playerId: UUID): EntityState = entities[playerId]?.state ?: EntityState()
override fun canView(playerId: UUID): Boolean = canConsider(playerId)

override fun entityId(playerId: UUID): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@ import com.typewritermc.engine.paper.entry.entries.EntityInstanceEntry
import com.typewritermc.engine.paper.entry.entries.PropertySupplier
import com.typewritermc.engine.paper.entry.entries.TickableDisplay
import com.typewritermc.engine.paper.utils.config
import org.bukkit.Location
import org.bukkit.entity.Player
import java.util.*
import java.util.concurrent.ConcurrentHashMap

val entityShowRange by config("entity.show-range", 50.0, "The range at which entities are shown")

class SharedActivityEntityDisplay(
private val ref: Ref<out EntityInstanceEntry>,
override val instanceEntryRef: Ref<out EntityInstanceEntry>,
override val creator: EntityCreator,
private val activityCreators: ActivityCreator,
private val suppliers: List<Pair<PropertySupplier<*>, Int>>,
private val spawnPosition: Position,
) : AudienceFilter(ref), TickableDisplay, ActivityEntityDisplay {
) : AudienceFilter(instanceEntryRef), TickableDisplay, ActivityEntityDisplay {
private var activityManager: ActivityManager<SharedActivityContext>? = null
private val entities = ConcurrentHashMap<UUID, DisplayEntity>()

Expand All @@ -32,7 +31,7 @@ class SharedActivityEntityDisplay(

override fun initialize() {
super.initialize()
val context = SharedActivityContext(ref, players)
val context = SharedActivityContext(instanceEntryRef, players)
activityManager =
ActivityManager(activityCreators.create(context, spawnPosition.toProperty()))
activityManager?.initialize(context)
Expand All @@ -54,7 +53,7 @@ class SharedActivityEntityDisplay(
// But there is no real solution to this.
// So we pick the first entity's state and use to try and keep the state consistent.
val entityState = entities.values.firstOrNull()?.state ?: EntityState()
activityManager?.tick(SharedActivityContext(ref, players, entityState))
activityManager?.tick(SharedActivityContext(instanceEntryRef, players, entityState))
entities.values.forEach { it.tick() }
}

Expand All @@ -67,7 +66,7 @@ class SharedActivityEntityDisplay(
super.dispose()
entities.values.forEach { it.dispose() }
entities.clear()
activityManager?.dispose(SharedActivityContext(ref, players))
activityManager?.dispose(SharedActivityContext(instanceEntryRef, players))
activityManager = null
}

Expand All @@ -76,7 +75,7 @@ class SharedActivityEntityDisplay(
}

override fun position(playerId: UUID): Position? = activityManager?.position

override fun entityState(playerId: UUID): EntityState = entities[playerId]?.state ?: EntityState()
override fun canView(playerId: UUID): Boolean = canConsider(playerId)

override fun entityId(playerId: UUID): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.typewritermc.engine.paper.events
import com.github.retrooper.packetevents.protocol.player.InteractionHand
import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientInteractEntity.InteractAction
import com.typewritermc.engine.paper.entry.entries.EntityDefinitionEntry
import com.typewritermc.engine.paper.entry.entries.EntityInstanceEntry
import org.bukkit.entity.Player
import org.bukkit.event.HandlerList
import org.bukkit.event.player.PlayerEvent
Expand All @@ -28,6 +29,7 @@ class AsyncEntityDefinitionInteract(
player: Player,
val entityId: Int,
val definition: EntityDefinitionEntry,
val instance: EntityInstanceEntry,
val hand: InteractionHand,
val action: InteractAction,
) : PlayerEvent(player, true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.typewritermc.engine.paper.entry.entity.*
import com.typewritermc.engine.paper.entry.entries.GenericEntityActivityEntry
import com.typewritermc.engine.paper.snippets.snippet
import com.typewritermc.engine.paper.utils.isLookable
import org.bukkit.GameMode
import org.bukkit.entity.Player
import kotlin.math.abs
import kotlin.math.atan2
Expand Down Expand Up @@ -144,6 +143,10 @@ fun updateLookDirection(
return yaw to pitch
}

fun normalizeYaw(yaw: Float): Float {
return (yaw + 180) % 360 - 180
}

class Velocity(var value: Float)

fun smoothDamp(
Expand Down
Loading

0 comments on commit 8cec3e5

Please sign in to comment.