Skip to content

Commit

Permalink
Add async catching options for read, write, or conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
0ffz committed Dec 3, 2023
1 parent 2edcde2 commit e5994ae
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Original file line number Diff line number Diff line change
Expand Up @@ -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<PaperEngineModule> {
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<PaperEngineModule> {
override fun start(module: PaperEngineModule) {
DI.add<PaperEngineModule>(module)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface EntityTracking {
companion object : GearyAddonWithDefault<EntityTracking> {
override fun default(): EntityTracking = object : EntityTracking {
override val bukkitEntityComponent = componentId<BukkitEntity>()
override val bukkit2Geary = BukkitEntity2Geary()
override val bukkit2Geary = BukkitEntity2Geary(gearyPaper.config.catchAsyncEntityConversion)
override val prefabs = GearyMobPrefabQuery()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit e5994ae

Please sign in to comment.