From 23eb87910593da7e7032bc725c2cfdf489594d01 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Wed, 31 Aug 2022 11:21:55 +0700 Subject: [PATCH] Drop exosuit inventory on death --- .../otm/capability/MatteryPlayerCapability.kt | 15 +++++++++++ .../mc/otm/container/MatteryContainer.kt | 26 +++++++++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/MatteryPlayerCapability.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/MatteryPlayerCapability.kt index b0841e2d0..583bfbfaa 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/MatteryPlayerCapability.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/MatteryPlayerCapability.kt @@ -80,6 +80,17 @@ class MatteryPlayerCapability(val ply: Player) : ICapabilityProvider, IMatteryEn field = value } + fun dropExoSuitInventory() { + val iterator = exoSuitContainer.iterator() + + for (item in iterator) { + if (!item.isEmpty) { + ply.drop(item, true, false) + iterator.remove() + } + } + } + var isExoSuitCraftingUpgraded by synchronizer.bool(setter = setter@{ value, access, _ -> if (value != access.read()) { access.write(value) @@ -799,6 +810,10 @@ class MatteryPlayerCapability(val ply: Player) : ICapabilityProvider, IMatteryEn return } + if (mattery.hasExoSuit) { + mattery.dropExoSuitInventory() + } + if (!mattery.isAndroid) { return } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/container/MatteryContainer.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/container/MatteryContainer.kt index 9a5990470..83dbf75de 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/container/MatteryContainer.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/container/MatteryContainer.kt @@ -346,8 +346,30 @@ open class MatteryContainer(val watcher: Runnable, private val size: Int) : Cont Arrays.fill(trackedSlots, ItemStack.EMPTY) } - final override fun iterator(): Iterator { - return slots.iterator() + final override fun iterator(): MutableIterator { + return object : MutableIterator { + private var index = 0 + + override fun hasNext(): Boolean { + return index < size + } + + override fun next(): ItemStack { + if (index >= size) { + throw IllegalStateException("Already finished iterating") + } + + return slots[index++] + } + + override fun remove() { + if (index == 0) { + throw IllegalStateException("Never called next()") + } + + set(index - 1, ItemStack.EMPTY) + } + } } }