fix: incorrect usage of canTakeItem

This commit is contained in:
xtex 2023-07-30 21:05:38 +08:00
parent 40ed66d76e
commit f3be1aae48
Signed by: xtex
GPG Key ID: B918086ED8045B91

View File

@ -68,7 +68,7 @@ class ExchangeItem<E> : Behavior<E>(
private fun exchangeItems(level: ServerLevel, entity: E) { private fun exchangeItems(level: ServerLevel, entity: E) {
val container = level.getBlockEntity(target!!) as Container val container = level.getBlockEntity(target!!) as Container
val inventory = entity.inventory val inventory = entity.inventory
for (i in 1..6) { for (i in 1..10) {
val maxCount = 1 + level.random.nextInt(16) val maxCount = 1 + level.random.nextInt(16)
if (level.random.nextBoolean()) { if (level.random.nextBoolean()) {
// take // take
@ -77,7 +77,7 @@ class ExchangeItem<E> : Behavior<E>(
if (!item.isEmpty) { if (!item.isEmpty) {
val takeCount = min(item.count, maxCount) val takeCount = min(item.count, maxCount)
val takeItem = item.copyWithCount(takeCount) val takeItem = item.copyWithCount(takeCount)
if (inventory.canTakeItem(container, slot, takeItem) && entity.canHoldItem(takeItem)) { if (entity.canHoldItem(takeItem)) {
val remaining = inventory.addItem(/*entity.equipItemIfPossible(takeItem)*/ takeItem) val remaining = inventory.addItem(/*entity.equipItemIfPossible(takeItem)*/ takeItem)
val actualCount = takeCount - remaining.count val actualCount = takeCount - remaining.count
item.shrink(actualCount) item.shrink(actualCount)
@ -91,33 +91,31 @@ class ExchangeItem<E> : Behavior<E>(
if (!item.isEmpty) { if (!item.isEmpty) {
val takeCount = min(item.count, maxCount) val takeCount = min(item.count, maxCount)
val takeItem = item.copyWithCount(takeCount) val takeItem = item.copyWithCount(takeCount)
if (container.canTakeItem(inventory, slot, takeItem)) { for (target in 0 until container.containerSize) {
val targetItem = container.getItem(target)
if (ItemStack.isSameItemSameTags(targetItem, takeItem)) {
val resultCount = min(targetItem.count + takeItem.count, item.maxStackSize)
val putCount = resultCount - targetItem.count
if (putCount != 0) {
targetItem.grow(putCount)
container.setItem(target, targetItem)
takeItem.shrink(putCount)
if (takeItem.isEmpty) break
}
}
}
if (!takeItem.isEmpty) {
for (target in 0 until container.containerSize) { for (target in 0 until container.containerSize) {
val targetItem = container.getItem(target) val targetItem = container.getItem(target)
if (ItemStack.isSameItemSameTags(targetItem, takeItem)) { if (targetItem.isEmpty) {
val resultCount = min(targetItem.count + takeItem.count, item.maxStackSize) container.setItem(target, takeItem.copyAndClear())
val putCount = resultCount - targetItem.count break
if (putCount != 0) {
targetItem.grow(putCount)
container.setItem(target, targetItem)
takeItem.shrink(putCount)
if (takeItem.isEmpty) break
}
} }
} }
if (!takeItem.isEmpty) {
for (target in 0 until container.containerSize) {
val targetItem = container.getItem(target)
if (targetItem.isEmpty) {
container.setItem(target, takeItem.copyAndClear())
break
}
}
}
val putCount = takeCount - takeItem.count
item.shrink(putCount)
inventory.setItem(slot, item)
} }
val putCount = takeCount - takeItem.count
item.shrink(putCount)
inventory.setItem(slot, item)
} }
} }
} }