Skip to content

Commit

Permalink
feat: Add helpers for getting geary world off Bukkit classes
Browse files Browse the repository at this point in the history
  • Loading branch information
0ffz committed Oct 26, 2024
1 parent 168a64b commit ad3928a
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 26 deletions.
1 change: 1 addition & 0 deletions geary-papermc-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ plugins {
dependencies {
api(libs.geary.core)
api(libs.geary.serialization)
api(libs.geary.prefabs)
compileOnly(libs.geary.actions)

// MineInAbyss platform
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.mineinabyss.geary.papermc

import com.mineinabyss.geary.modules.Geary
import com.mineinabyss.geary.prefabs.PrefabKey
import com.mineinabyss.geary.prefabs.entityOfOrNull
import com.mineinabyss.idofront.typealiases.BukkitEntity
import org.bukkit.block.Block

context(Geary)
fun PrefabKey.toEntityOrNull() = entityOfOrNull(this)

inline fun <T> BukkitEntity.withGeary(run: Geary.() -> T) = with(world.toGeary()) { run() }

inline fun <T> Block.withGeary(run: Geary.() -> T) = with(world.toGeary()) { run() }
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.mineinabyss.geary.papermc.features.items.food

import com.mineinabyss.geary.papermc.toGeary
import com.mineinabyss.geary.papermc.tracking.items.itemEntityContext
import com.mineinabyss.geary.papermc.withGeary
import com.mineinabyss.idofront.serialization.SerializableItemStack
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
Expand All @@ -19,7 +19,7 @@ value class ReplaceBurnedDrop(

class ReplaceBurnedDropListener : Listener {
@EventHandler
fun EntityDeathEvent.replaceBurnedDrops() = with(entity.world.toGeary()) {
fun EntityDeathEvent.replaceBurnedDrops() = entity.withGeary {
if (entity.fireTicks == 0) return

itemEntityContext {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@ package com.mineinabyss.geary.papermc.tracking.blocks.helpers

import com.mineinabyss.geary.modules.Geary
import com.mineinabyss.geary.papermc.tracking.blocks.BlockTracking
import com.mineinabyss.geary.papermc.withGeary
import com.mineinabyss.geary.prefabs.entityOfOrNull
import org.bukkit.block.Block
import org.bukkit.block.data.BlockData


context(Geary)
private val gearyBlocks get() = getAddon(BlockTracking)
private val Geary.gearyBlocks get() = getAddon(BlockTracking)

context(Geary)
val Block.prefabKey get() = gearyBlocks.block2Prefab[blockData]
val Block.prefabKey get() = withGeary { gearyBlocks.block2Prefab[blockData] }

context(Geary)
val BlockData.prefabKey get() = gearyBlocks.block2Prefab[this]
fun Block.toGearyOrNull() = withGeary { entityOfOrNull(gearyBlocks.block2Prefab[blockData]) }

context(Geary)
fun Block.toGearyOrNull() = entityOfOrNull(gearyBlocks.block2Prefab[blockData])
val BlockData.prefabKey
get() = gearyBlocks.block2Prefab[this]

context(Geary)
fun BlockData.toGearyOrNull() = entityOfOrNull(gearyBlocks.block2Prefab[this])
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package com.mineinabyss.geary.papermc.tracking.entities

import com.mineinabyss.geary.datatypes.GearyEntity
import com.mineinabyss.geary.modules.Geary
import com.mineinabyss.geary.papermc.toGeary
import com.mineinabyss.geary.papermc.withGeary
import com.mineinabyss.idofront.typealiases.BukkitEntity
import org.bukkit.entity.Entity

fun BukkitEntity.toGeary(): GearyEntity = with(world.toGeary()) {
fun BukkitEntity.toGeary(): GearyEntity = withGeary {
return toGearyOrNull() ?: error("Entity $this is not being tracked by Geary!")
}

fun BukkitEntity.toGearyOrNull(): GearyEntity? =
with(world.toGeary()) { getAddon(EntityTracking).bukkit2Geary[this@toGearyOrNull] }
withGeary { getAddon(EntityTracking).bukkit2Geary[this@toGearyOrNull] }

fun GearyEntity.toBukkit(): BukkitEntity? =
with(world) { get(getAddon(EntityTracking).bukkitEntityComponent) as? BukkitEntity }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,30 @@ import com.mineinabyss.geary.datatypes.GearyEntity
import com.mineinabyss.geary.helpers.entity
import com.mineinabyss.geary.modules.Geary
import com.mineinabyss.geary.papermc.datastore.loadComponentsFrom
import com.mineinabyss.geary.papermc.toGeary
import com.mineinabyss.geary.papermc.tracking.entities.EntityTracking
import com.mineinabyss.geary.papermc.tracking.entities.components.AttemptSpawn
import com.mineinabyss.geary.prefabs.PrefabKey
import com.mineinabyss.geary.prefabs.Prefabs
import com.mineinabyss.geary.prefabs.prefabs
import com.mineinabyss.idofront.typealiases.BukkitEntity
import org.bukkit.Location
import org.bukkit.persistence.PersistentDataContainer


context(Geary)
fun Location.spawnFromPrefab(prefab: PrefabKey, initEntityPreEvent: GearyEntity.() -> Unit = {}): Result<BukkitEntity> {
val entity = getAddon(Prefabs).manager[prefab] ?: return Result.failure(IllegalArgumentException("No prefab found"))
return spawnFromPrefab(entity, initEntityPreEvent = initEntityPreEvent)
}
inline fun <T> Location.withGeary(run: Geary.() -> T) = with(world.toGeary()) { run() }

fun Location.spawnFromPrefab(prefab: PrefabKey, initEntityPreEvent: GearyEntity.() -> Unit = {}): Result<BukkitEntity> =
withGeary {
val entity =
getAddon(Prefabs).manager[prefab] ?: return Result.failure(IllegalArgumentException("No prefab found"))
return spawnFromPrefab(entity, initEntityPreEvent = initEntityPreEvent)
}

context(Geary)
fun Location.spawnFromPrefab(
prefab: GearyEntity,
existingPDC: PersistentDataContainer? = null,
initEntityPreEvent: GearyEntity.() -> Unit = {}
): Result<BukkitEntity> {
initEntityPreEvent: GearyEntity.() -> Unit = {},
): Result<BukkitEntity> = withGeary {
return runCatching {
val entity = entity {
if (existingPDC != null) loadComponentsFrom(existingPDC)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.mineinabyss.geary.papermc.tracking.items.inventory

import com.mineinabyss.geary.datatypes.GearyEntity
import com.mineinabyss.geary.modules.Geary
import com.mineinabyss.geary.papermc.CatchType
import com.mineinabyss.geary.papermc.gearyPaper
import com.mineinabyss.geary.papermc.toGeary
import com.mineinabyss.geary.papermc.tracking.entities.toGearyOrNull
import com.mineinabyss.geary.papermc.tracking.items.ItemTracking
import com.mineinabyss.geary.papermc.tracking.items.cache.PlayerItemCache
import com.mineinabyss.geary.papermc.withGeary
import net.minecraft.world.entity.player.Inventory
import org.bukkit.inventory.EquipmentSlot
import org.bukkit.inventory.ItemStack
Expand All @@ -16,7 +15,7 @@ import org.spigotmc.AsyncCatcher

class GearyPlayerInventory(
val inventory: PlayerInventory,
val converter: InventoryCacheWrapper
val converter: InventoryCacheWrapper,
) {
/**
* Gets or loads a Geary entity associated with the item in slot [slot] of this player's inventory.
Expand Down Expand Up @@ -70,7 +69,6 @@ class GearyPlayerInventory(
val itemInBoots get() = get(inventory.size - 5)
}

//context(Geary)
fun PlayerInventory.toGeary(): GearyPlayerInventory? {
try {
if (gearyPaper.config.catch.asyncEntityConversion == CatchType.ERROR)
Expand All @@ -79,7 +77,7 @@ fun PlayerInventory.toGeary(): GearyPlayerInventory? {
// Allow running in tests
}
val player = holder ?: return null
with(player.world.toGeary()) {
player.withGeary {
val wrap = getAddon(ItemTracking).getCacheWrapper(player.toGearyOrNull() ?: return null) ?: return null
return GearyPlayerInventory(this@toGeary, wrap)
}
Expand Down

0 comments on commit ad3928a

Please sign in to comment.