Skip to content

Commit

Permalink
Merge pull request #334 from Neige7/patch-1
Browse files Browse the repository at this point in the history
修复某些情况下PlayerUtil.giveItem可能吞物品的问题
  • Loading branch information
Bkm016 authored Jul 24, 2023
2 parents 9db2ee0 + a27c899 commit 0f92333
Showing 1 changed file with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ fun HumanEntity.giveItem(itemStack: List<ItemStack>) {

fun HumanEntity.giveItem(itemStack: ItemStack?, repeat: Int = 1) {
if (itemStack.isNotAir()) {
repeat(repeat) { inventory.addItem(itemStack).values.forEach { world.dropItem(location, it) } }
// CraftInventory.addItem 的执行过程中, 实质上有可能修改ItemStack的amount, 如果不注意这一点, 则会吞物品而不自知
val preAmount = itemStack.amount
repeat(repeat) {
inventory.addItem(itemStack).values.forEach { world.dropItem(location, it) }
itemStack.amount = preAmount
}
}
}

Expand Down Expand Up @@ -48,4 +53,4 @@ fun HumanEntity.feed() {

fun HumanEntity.saturate() {
saturation = 20F
}
}

0 comments on commit 0f92333

Please sign in to comment.