fix: crash when trying to interact with destoryed container

This commit is contained in:
xtex 2023-08-10 11:41:33 +08:00
parent 2c95dfa660
commit 296fe1efc2
Signed by: xtex
GPG Key ID: B918086ED8045B91

View File

@ -41,7 +41,9 @@ class ExchangeItem<E> : Behavior<E>(
if (closeAt == null) { if (closeAt == null) {
if (owner.brain.getMemory(MemoryModuleType.WALK_TARGET).isEmpty) { if (owner.brain.getMemory(MemoryModuleType.WALK_TARGET).isEmpty) {
// reached // reached
val chest = level.getBlockEntity(target!!) as BaseContainerBlockEntity val chest = level.getBlockEntity(target!!) ?: return
if (chest !is BaseContainerBlockEntity)
return
if (chest is ChestBlockEntity) { if (chest is ChestBlockEntity) {
ChestBlockEntity.playSound(level, target!!, level.getBlockState(target!!), SoundEvents.CHEST_OPEN) ChestBlockEntity.playSound(level, target!!, level.getBlockState(target!!), SoundEvents.CHEST_OPEN)
} }
@ -68,7 +70,9 @@ 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!!) ?: return
if (container !is Container)
return
val inventory = entity.inventory val inventory = entity.inventory
for (i in 1..10) { for (i in 1..10) {
val maxCount = 1 + level.random.nextInt(16) val maxCount = 1 + level.random.nextInt(16)