Skip to content

Commit

Permalink
Add async catcher for bukkit entity and inventory access
Browse files Browse the repository at this point in the history
  • Loading branch information
0ffz committed Dec 2, 2023
1 parent b4c8b7b commit be45a97
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import com.mineinabyss.geary.helpers.entity
import com.mineinabyss.geary.helpers.toGeary
import com.mineinabyss.idofront.typealiases.BukkitEntity
import it.unimi.dsi.fastutil.ints.Int2LongOpenHashMap
import org.spigotmc.AsyncCatcher
import kotlin.collections.set

class BukkitEntity2Geary {
class BukkitEntity2Geary(val forceMainThread: Boolean = true) {
private val entityMap = Int2LongOpenHashMap().apply { defaultReturnValue(-1) }

operator fun get(bukkit: BukkitEntity): GearyEntity? {
if (forceMainThread) AsyncCatcher.catchOp("Async geary entity access for $bukkit")
val id = entityMap.get(bukkit.entityId)
if (id == -1L) return null
return id.toGeary()
Expand All @@ -27,6 +29,9 @@ class BukkitEntity2Geary {
}

fun getOrCreate(bukkit: BukkitEntity): GearyEntity {
return get(bukkit) ?: entity { set(bukkit) }
return get(bukkit) ?: run {
if (forceMainThread) AsyncCatcher.catchOp("Async geary entity creation for $bukkit")
entity { set(bukkit) }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.mineinabyss.geary.papermc.tracking.items.gearyItems
import net.minecraft.world.entity.player.Inventory
import org.bukkit.inventory.EquipmentSlot
import org.bukkit.inventory.PlayerInventory
import org.spigotmc.AsyncCatcher


class GearyPlayerInventory(
Expand Down Expand Up @@ -59,6 +60,11 @@ class GearyPlayerInventory(
}

fun PlayerInventory.toGeary(): GearyPlayerInventory? {
try {
AsyncCatcher.catchOp("Async geary inventory access for $holder")
} catch (_: NoClassDefFoundError) {
// Allow running in tests
}
val player = holder ?: return null
val wrap = gearyItems.getCacheWrapper(player.toGearyOrNull() ?: return null) ?: return null
return GearyPlayerInventory(this, wrap)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import com.mineinabyss.geary.papermc.commons.events.configurable.components.Even
import com.mineinabyss.geary.papermc.configlang.ConfigLang
import com.mineinabyss.geary.papermc.helpers.MockedServerTest
import com.mineinabyss.geary.papermc.helpers.SomeData
import com.mineinabyss.geary.papermc.helpers.TestEntityTracking
import com.mineinabyss.geary.papermc.helpers.withTestSerializers
import com.mineinabyss.geary.papermc.tracking.entities.EntityTracking
import com.mineinabyss.geary.papermc.tracking.entities.toGeary
import com.mineinabyss.geary.papermc.tracking.items.BukkitBackedItemTracking
import com.mineinabyss.geary.papermc.tracking.items.ItemTracking
Expand All @@ -33,7 +33,7 @@ class EventTriggersTests : MockedServerTest() {

init {
geary(TestEngineModule) {
install(EntityTracking)
install(TestEntityTracking)
install(ItemTracking, BukkitBackedItemTracking())
install(ConfigLang)
install(PaperBridge)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.mineinabyss.geary.papermc.helpers
import com.mineinabyss.geary.modules.GearyConfiguration
import com.mineinabyss.geary.modules.geary
import com.mineinabyss.geary.papermc.datastore.withUUIDSerializer
import com.mineinabyss.geary.papermc.tracking.entities.EntityTracking
import com.mineinabyss.geary.papermc.tracking.items.BukkitBackedItemTracking
import com.mineinabyss.geary.papermc.tracking.items.ItemTracking
import com.mineinabyss.geary.prefabs.Prefabs
Expand All @@ -18,7 +17,7 @@ fun GearyConfiguration.withMockTracking(
withUUIDSerializer()
withTestSerializers()
}
if (entities) install(EntityTracking)
if (entities) install(TestEntityTracking)
if (items) install(ItemTracking, BukkitBackedItemTracking())
install(Prefabs)
geary.pipeline.runStartupTasks()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.mineinabyss.geary.papermc.helpers

import com.mineinabyss.geary.addons.dsl.GearyAddonWithDefault
import com.mineinabyss.geary.papermc.tracking.entities.BukkitEntity2Geary
import com.mineinabyss.geary.papermc.tracking.entities.EntityTracking

class TestEntityTracking {
companion object : GearyAddonWithDefault<EntityTracking> by EntityTracking {
override fun default() = object : EntityTracking by EntityTracking.default() {
override val bukkit2Geary = BukkitEntity2Geary(forceMainThread = false)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.mineinabyss.geary.modules.geary
import com.mineinabyss.geary.papermc.datastore.decode
import com.mineinabyss.geary.papermc.helpers.MockedServerTest
import com.mineinabyss.geary.papermc.helpers.SomeData
import com.mineinabyss.geary.papermc.helpers.TestEntityTracking
import com.mineinabyss.geary.papermc.helpers.withTestSerializers
import com.mineinabyss.geary.serialization.dsl.serialization
import com.mineinabyss.idofront.typealiases.BukkitEntity
Expand All @@ -26,7 +27,7 @@ class EntityTrackingTests: MockedServerTest() {
serialization {
withTestSerializers()
}
install(EntityTracking)
install(TestEntityTracking)
}
geary.pipeline.runStartupTasks()
}
Expand Down
2 changes: 2 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ include(
"geary-papermc-bridge",
"geary-tests"
)

includeBuild("../geary")

0 comments on commit be45a97

Please sign in to comment.