Skip to content

Commit

Permalink
Improve interation and packet handling
Browse files Browse the repository at this point in the history
  • Loading branch information
gabber235 committed Sep 11, 2024
1 parent 8cb2753 commit a2222f8
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 28 deletions.
4 changes: 2 additions & 2 deletions engine/engine-paper/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ dependencies {
api(project(":engine-loader"))

api("me.tofaa.entitylib:spigot:2.4.9-SNAPSHOT")
api("com.github.shynixn.mccoroutine:mccoroutine-bukkit-api:2.17.0")
api("com.github.shynixn.mccoroutine:mccoroutine-bukkit-core:2.17.0")
api("com.github.shynixn.mccoroutine:mccoroutine-bukkit-api:2.19.0")
api("com.github.shynixn.mccoroutine:mccoroutine-bukkit-core:2.19.0")
api("dev.jorel:commandapi-bukkit-shade:9.5.3")
api("dev.jorel:commandapi-bukkit-kotlin:9.5.3")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.typewritermc.engine.paper.plugin
import com.typewritermc.engine.paper.snippets.snippet
import com.typewritermc.engine.paper.utils.ThreadType.DISPATCHERS_ASYNC
import kotlinx.coroutines.runBlocking
import lirand.api.extensions.server.registerEvents
import lirand.api.extensions.server.registerSuspendingEvents
import lirand.api.extensions.server.server
import org.bukkit.entity.Player
Expand Down Expand Up @@ -97,7 +98,7 @@ class InteractionHandler : Listener, KoinComponent {
}

fun initialize() {
plugin.registerSuspendingEvents(this)
plugin.registerEvents(this)
}

// When a player joins the server, we need to create an interaction for them.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ class PacketInterceptor : PacketListenerAbstract() {
}
}

fun cancel(player: UUID, bundle: InterceptionBundle) {
blockers.compute(player) { _, blocker ->
val newBlocker = blocker ?: return@compute null
if (newBlocker.cancel(bundle)) null else newBlocker
}
}

fun shutdown() {
PacketEvents.getAPI().eventManager.unregisterListener(this)
blockers.clear()
Expand All @@ -75,6 +82,11 @@ private data class PlayerPacketInterceptor(
return interceptions.isEmpty()
}

fun cancel(bundle: InterceptionBundle): Boolean {
bundle.subscriptions.forEach { cancel(it) }
return interceptions.isEmpty()
}

fun trigger(event: ProtocolPacketEvent) {
interceptions.values
.asSequence()
Expand Down Expand Up @@ -127,7 +139,7 @@ fun Player.interceptPackets(block: InterceptionBundle.() -> Unit): InterceptionB
}

class InterceptionBundle(private val playerId: UUID) {
private val subscriptions = mutableListOf<PacketInterceptionSubscription>()
internal val subscriptions = mutableListOf<PacketInterceptionSubscription>()

private fun intercept(interception: PacketInterception) {
val subscription = get<PacketInterceptor>(PacketInterceptor::class.java).interceptPacket(playerId, interception)
Expand All @@ -150,7 +162,6 @@ class InterceptionBundle(private val playerId: UUID) {

fun cancel() {
val interceptor = get<PacketInterceptor>(PacketInterceptor::class.java)
subscriptions.forEach { interceptor.cancel(playerId, it) }
subscriptions.clear()
interceptor.cancel(playerId, this)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,6 @@ fun Audience.playSound(
) = playSound(Sound.sound(Key.key(sound), source, volume, pitch))


val Player.isHighUp: Boolean
get() = this.location.isHighUp
val Location.isHighUp: Boolean
get() = this.y >= highUpLocation.y

val Location.highUpLocation: Location
get() {
val location = toHighestLocation()
location.y += 200
return location
}

fun Location.distanceSqrt(other: Location): Double? {
if (world != other.world) return null
val dx = x - other.x
Expand All @@ -99,9 +87,6 @@ fun Location.lerp(other: Location, amount: Double): Location {
val Location.up: Location
get() = clone().apply { y += 1 }

val Location.down: Location
get() = clone().apply { y -= 1 }

val Location.firstWalkableLocationBelow: Location
get() = clone().apply {
while (block.isPassable) y--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,15 @@ class CameraCinematicAction(
private suspend fun Player.teardown() {
listener?.unregister()
listener = null
interceptor?.cancel()
interceptor = null
originalState?.let {
SYNC.switchContext {
SYNC.switchContext {
interceptor?.cancel()
interceptor = null

originalState?.let {
restore(it)
if (gameMode != GameMode.CREATIVE) {
restoreInventory()
}
}
if (gameMode != GameMode.CREATIVE) {
restoreInventory()
}
}
originalState = null
Expand Down

0 comments on commit a2222f8

Please sign in to comment.