Skip to content

Commit

Permalink
Fix item remove with custom items
Browse files Browse the repository at this point in the history
  • Loading branch information
gabber235 committed Jan 29, 2024
1 parent 3a08099 commit 52ffa60
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,32 @@ class RemoveItemActionEntry(
override fun execute(player: Player) {
super.execute(player)

player.inventory.removeItemAnySlot(item.build(player))
// Because item.build() can return a identical looking item but with different data,
// we need to compare the items

val itemWithoutAmount = item.copy(amount = Optional.empty())

val items = player.inventory.contents.withIndex().filter {
itemWithoutAmount.isSameAs(player, it.value)
}.iterator()

println(items)

var toRemove = item.amount.orElse(1)

while (toRemove > 0 && items.hasNext()) {
val (index, item) = items.next()
if (item == null) continue
val amount = item.amount
if (amount > toRemove) {
item.amount = amount - toRemove
player.inventory.setItem(index, item)
break
} else {
toRemove -= amount
player.inventory.setItem(index, null)
}
}
}
}

Expand Down
22 changes: 21 additions & 1 deletion plugin/src/main/kotlin/me/gabber235/typewriter/utils/Item.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ open class Item(
@InnerMin(Min(0))
@Icon(Icons.HASHTAG)
@Help("The amount of items.")
private val amount: Optional<Int> = Optional.empty(),
val amount: Optional<Int> = Optional.empty(),
@Placeholder
@Colored
@Icon(Icons.TAG)
Expand Down Expand Up @@ -103,6 +103,26 @@ open class Item(
if (nbt.isPresent && item.strippedTagNbtData.toString() != nbt.get()) return false
return true
}

fun copy(
material: Optional<Material>? = null,
amount: Optional<Int>? = null,
name: Optional<String>? = null,
lore: Optional<String>? = null,
// enchantments: Optional<Map<Enchantment, Int>>? = null,
flags: Optional<List<ItemFlag>>? = null,
nbt: Optional<String>? = null,
): Item {
return Item(
material = material ?: this.material,
amount = amount ?: this.amount,
name = name ?: this.name,
lore = lore ?: this.lore,
// enchantments = enchantments ?: this.enchantments,
flags = flags ?: this.flags,
nbt = nbt ?: this.nbt,
)
}
}

fun ItemStack.toItem(): Item {
Expand Down

0 comments on commit 52ffa60

Please sign in to comment.