From e5994ae6edcebef58ea45401328f3cbe03b0d3da Mon Sep 17 00:00:00 2001 From: Danielle Voznyy Date: Sun, 3 Dec 2023 18:23:55 -0500 Subject: [PATCH] Add async catching options for read, write, or conversion --- .../geary/papermc/GearyPaperConfig.kt | 6 +++++ .../geary/papermc/plugin/PaperEngineModule.kt | 25 ++++++++++++++++--- .../tracking/entities/EntityTracking.kt | 2 +- .../items/inventory/GearyPlayerInventory.kt | 4 ++- 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/geary-papermc-core/src/main/kotlin/com/mineinabyss/geary/papermc/GearyPaperConfig.kt b/geary-papermc-core/src/main/kotlin/com/mineinabyss/geary/papermc/GearyPaperConfig.kt index 07388ee..20d937f 100644 --- a/geary-papermc-core/src/main/kotlin/com/mineinabyss/geary/papermc/GearyPaperConfig.kt +++ b/geary-papermc-core/src/main/kotlin/com/mineinabyss/geary/papermc/GearyPaperConfig.kt @@ -18,5 +18,11 @@ class GearyPaperConfig( val configLang: Boolean = true, @YamlComment("If an item has no prefabs encoded, try to find its prefab by matching custom model data.") val migrateItemCustomModelDataToPrefab: Boolean = true, + @YamlComment("Whether to throw an error when an entity read operation occurs outside of the server thread.") + val catchAsyncRead: Boolean = false, + @YamlComment("Whether to throw an error when an entity write operation occurs outside of the server thread.") + val catchAsyncWrite: Boolean = true, + @YamlComment("Whether to throw an error when converting bukkit concepts to geary entities outside of the server thread.") + val catchAsyncEntityConversion: Boolean = false, val logLevel: Severity = Severity.Warn, ) diff --git a/geary-papermc-plugin/src/main/kotlin/com/mineinabyss/geary/papermc/plugin/PaperEngineModule.kt b/geary-papermc-plugin/src/main/kotlin/com/mineinabyss/geary/papermc/plugin/PaperEngineModule.kt index c5bfe40..dfcafce 100644 --- a/geary-papermc-plugin/src/main/kotlin/com/mineinabyss/geary/papermc/plugin/PaperEngineModule.kt +++ b/geary-papermc-plugin/src/main/kotlin/com/mineinabyss/geary/papermc/plugin/PaperEngineModule.kt @@ -3,19 +3,38 @@ package com.mineinabyss.geary.papermc.plugin import co.touchlab.kermit.Logger import co.touchlab.kermit.StaticConfig import com.mineinabyss.geary.engine.archetypes.ArchetypeEngine +import com.mineinabyss.geary.engine.archetypes.operations.ArchetypeMutateOperations +import com.mineinabyss.geary.engine.archetypes.operations.ArchetypeReadOperations import com.mineinabyss.geary.modules.ArchetypeEngineModule import com.mineinabyss.geary.modules.GearyModuleProvider import com.mineinabyss.geary.papermc.GearyPlugin import com.mineinabyss.geary.papermc.gearyPaper import com.mineinabyss.idofront.di.DI import com.mineinabyss.idofront.time.ticks +import org.spigotmc.AsyncCatcher -class PaperEngineModule(val plugin: GearyPlugin) : +class PaperEngineModule( + val plugin: GearyPlugin +) : ArchetypeEngineModule(tickDuration = 1.ticks) { override val engine: ArchetypeEngine = PaperMCEngine() - override val logger = Logger(StaticConfig(logWriterList = listOf(PaperWriter(plugin)), minSeverity = gearyPaper.config.logLevel)) + override val logger = + Logger(StaticConfig(logWriterList = listOf(PaperWriter(plugin)), minSeverity = gearyPaper.config.logLevel)) - companion object: GearyModuleProvider { + override val read: ArchetypeReadOperations + get() { + if (gearyPaper.config.catchAsyncRead) + AsyncCatcher.catchOp("Async entity read!") + return super.read + } + override val write: ArchetypeMutateOperations + get() { + if (gearyPaper.config.catchAsyncWrite) + AsyncCatcher.catchOp("Async entity write!") + return super.write + } + + companion object : GearyModuleProvider { override fun start(module: PaperEngineModule) { DI.add(module) } diff --git a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/entities/EntityTracking.kt b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/entities/EntityTracking.kt index 46b1e62..d6408a4 100644 --- a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/entities/EntityTracking.kt +++ b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/entities/EntityTracking.kt @@ -22,7 +22,7 @@ interface EntityTracking { companion object : GearyAddonWithDefault { override fun default(): EntityTracking = object : EntityTracking { override val bukkitEntityComponent = componentId() - override val bukkit2Geary = BukkitEntity2Geary() + override val bukkit2Geary = BukkitEntity2Geary(gearyPaper.config.catchAsyncEntityConversion) override val prefabs = GearyMobPrefabQuery() } diff --git a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/inventory/GearyPlayerInventory.kt b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/inventory/GearyPlayerInventory.kt index 8cfca83..494a6f3 100644 --- a/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/inventory/GearyPlayerInventory.kt +++ b/geary-papermc-tracking/src/main/kotlin/com/mineinabyss/geary/papermc/tracking/items/inventory/GearyPlayerInventory.kt @@ -1,6 +1,7 @@ package com.mineinabyss.geary.papermc.tracking.items.inventory import com.mineinabyss.geary.datatypes.GearyEntity +import com.mineinabyss.geary.papermc.gearyPaper import com.mineinabyss.geary.papermc.tracking.entities.toGearyOrNull import com.mineinabyss.geary.papermc.tracking.items.cache.PlayerItemCache import com.mineinabyss.geary.papermc.tracking.items.gearyItems @@ -61,7 +62,8 @@ class GearyPlayerInventory( fun PlayerInventory.toGeary(): GearyPlayerInventory? { try { - AsyncCatcher.catchOp("Async geary inventory access for $holder") + if (gearyPaper.config.catchAsyncEntityConversion) + AsyncCatcher.catchOp("Async geary inventory access for $holder") } catch (_: NoClassDefFoundError) { // Allow running in tests }