Skip to content

Commit

Permalink
fix: Run item drop event on a temporary item entity since dropped ite…
Browse files Browse the repository at this point in the history
…m has already left inventory

fix: IIOB for GearyPlayerInventory.find which returns a nullable
  • Loading branch information
0ffz committed Jun 5, 2024
1 parent 11f77ed commit 499f8fb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.mineinabyss.geary.papermc.bridge.events.items

import com.mineinabyss.geary.helpers.addParent
import com.mineinabyss.geary.papermc.tracking.entities.toGeary
import com.mineinabyss.geary.papermc.tracking.items.inventory.toGeary
import com.mineinabyss.geary.papermc.tracking.items.itemEntityContext
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import org.bukkit.event.EventHandler
Expand Down Expand Up @@ -36,7 +39,10 @@ class ItemRemovedBridge : Listener {

@EventHandler(ignoreCancelled = true)
fun PlayerDropItemEvent.emitOnItemDrop() {
val droppedItem = player.inventory.toGeary()?.find(itemDrop.itemStack) ?: return
droppedItem.emit<OnItemDrop>()
itemEntityContext {
val droppedItem = itemDrop.itemStack.toGearyOrNull() ?: return
droppedItem.addParent(player.toGeary())
droppedItem.emit<OnItemDrop>()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ class GearyItemContext : Closeable {
val cached = mutableMapOf<ItemStack, GearyEntity>()
fun ItemStack.toGeary(): GearyEntity {
return cached.getOrPut(this) {
gearyItems.itemProvider.deserializeItemStackToEntity(this.fastPDC) ?: entity()
toGearyOrNull() ?: entity()
}
}

fun ItemStack.toGearyOrNull(): GearyEntity? {
return cached.getOrPut(this) {
gearyItems.itemProvider.deserializeItemStackToEntity(this.fastPDC)?.apply {
set<ItemStack>(this@toGearyOrNull)
} ?: return null
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ class GearyPlayerInventory(
}

fun find(itemStack: ItemStack): GearyEntity? {
return get(inventory.indexOf(itemStack))
val index = inventory.indexOf(itemStack)
if (index == -1) return null
return get(index)
}

// We use custom cursor slot so can't just call get
Expand Down

0 comments on commit 499f8fb

Please sign in to comment.