From adad99a6bf83c533b090f6ee50cd58f493729253 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sat, 7 Jan 2023 16:15:00 +0700 Subject: [PATCH] Declare batteryLevel and matterLevel as vars instead of vals --- .../otm/capability/IMatteryEnergyStorage.kt | 16 ++++++- .../otm/capability/matter/IMatterHandler.kt | 16 ++++++- .../ru/dbotthepony/mc/otm/item/BatteryItem.kt | 14 ------ .../mc/otm/item/EnergySwordItem.kt | 10 ----- .../mc/otm/item/MatterCapacitorItem.kt | 14 ------ .../item/ProceduralExoPackSlotUpgradeItem.kt | 2 +- .../mc/otm/recipe/EnergyContainerRecipe.kt | 5 ++- .../mc/otm/registry/CreativeTabs.kt | 43 +++++++++++++++++-- 8 files changed, 73 insertions(+), 47 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/IMatteryEnergyStorage.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/IMatteryEnergyStorage.kt index f4c241e34..a38abf9ce 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/IMatteryEnergyStorage.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/IMatteryEnergyStorage.kt @@ -42,11 +42,25 @@ interface IMatteryEnergyStorage : IEnergyStorage { */ fun receiveEnergyInner(howMuch: Decimal, simulate: Boolean): Decimal - val batteryLevel: Decimal + var batteryLevel: Decimal val maxBatteryLevel: Decimal val missingPower: Decimal get() = (maxBatteryLevel - batteryLevel).moreThanZero() + /** + * Empties power of this energy storage + */ + fun emptyBattery() { + batteryLevel = Decimal.ZERO + } + + /** + * Fully fills power in this energy storage + */ + fun fillBattery() { + batteryLevel = maxBatteryLevel + } + override fun receiveEnergy(maxReceive: Int, simulate: Boolean): Int { val received = receiveEnergyOuter(maxReceive, true).toInt() diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/matter/IMatterHandler.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/matter/IMatterHandler.kt index a661ef67a..38f5dd893 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/matter/IMatterHandler.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/matter/IMatterHandler.kt @@ -8,9 +8,23 @@ import ru.dbotthepony.mc.otm.core.orNull import kotlin.math.roundToInt interface IMatterHandler { - val storedMatter: Decimal + var storedMatter: Decimal val maxStoredMatter: Decimal + /** + * Empties matter stored of this matter storage + */ + fun emptyMatter() { + storedMatter = Decimal.ZERO + } + + /** + * Fully fills matter stored in this matter storage + */ + fun fillMatter() { + storedMatter = maxStoredMatter + } + fun receiveMatterOuter(howMuch: Decimal, simulate: Boolean): Decimal fun receiveMatterInner(howMuch: Decimal, simulate: Boolean): Decimal fun extractMatterOuter(howMuch: Decimal, simulate: Boolean): Decimal diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/item/BatteryItem.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/item/BatteryItem.kt index 9aeea6fff..dbe491b6b 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/item/BatteryItem.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/item/BatteryItem.kt @@ -145,20 +145,6 @@ open class BatteryItem : Item { } } - override fun fillItemCategory(p_41391_: CreativeModeTab, p_41392_: NonNullList) { - super.fillItemCategory(p_41391_, p_41392_) - - if (!isCreative && allowedIn(p_41391_)) { - p_41392_.add(ItemStack(this).also { - it.matteryEnergy?.let { - if (it is Power) { - it.maxPower() - } - } - }) - } - } - override fun initCapabilities(stack: ItemStack, nbt: CompoundTag?): ICapabilityProvider { return Power(stack) } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/item/EnergySwordItem.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/item/EnergySwordItem.kt index d303d4852..2f7d19a66 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/item/EnergySwordItem.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/item/EnergySwordItem.kt @@ -193,16 +193,6 @@ class EnergySwordItem : Item(Properties().stacksTo(1).rarity(Rarity.RARE)), Vani return EnergyConsumerItem(stack, MAX_ENERGY) } - override fun fillItemCategory(p_41391_: CreativeModeTab, p_41392_: NonNullList) { - super.fillItemCategory(p_41391_, p_41392_) - - if (allowedIn(p_41391_)) { - p_41392_.add(ItemStack(this).also { - it.matteryEnergy?.receiveEnergyInner(MAX_ENERGY, false) - }) - } - } - override fun getAttributeModifiers( slot: EquipmentSlot, itemStack: ItemStack diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/item/MatterCapacitorItem.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/item/MatterCapacitorItem.kt index 9058ef64a..bef7f1022 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/item/MatterCapacitorItem.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/item/MatterCapacitorItem.kt @@ -109,20 +109,6 @@ class MatterCapacitorItem : Item { _capacity = { Decimal.LONG_MAX_VALUE } } - override fun fillItemCategory(p_41391_: CreativeModeTab, p_41392_: NonNullList) { - super.fillItemCategory(p_41391_, p_41392_) - - if (!isCreative && allowedIn(p_41391_)) { - p_41392_.add(ItemStack(this).also { - it.getCapability(MatteryCapability.MATTER).ifPresentK { - if (it is Matter) { - it.storedMatter = it.maxStoredMatter - } - } - }) - } - } - override fun isBarVisible(p_150899_: ItemStack): Boolean { if (isCreative) return false diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/item/ProceduralExoPackSlotUpgradeItem.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/item/ProceduralExoPackSlotUpgradeItem.kt index b7c645f20..9128da3b0 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/item/ProceduralExoPackSlotUpgradeItem.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/item/ProceduralExoPackSlotUpgradeItem.kt @@ -15,7 +15,7 @@ import ru.dbotthepony.mc.otm.data.loot.IRandomizableItem import java.util.* @Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS") // .tab(null) is a legal statement because tab field itself is nullable -class ProceduralExoPackSlotUpgradeItem : AbstractExoPackSlotUpgradeItem(defaultProperties().tab(null)), +class ProceduralExoPackSlotUpgradeItem : AbstractExoPackSlotUpgradeItem(defaultProperties()), IRandomizableItem { override fun getRarity(itemStack: ItemStack): Rarity { return when (slotCount(itemStack)) { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/EnergyContainerRecipe.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/EnergyContainerRecipe.kt index 53599a2e1..61d607024 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/EnergyContainerRecipe.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/EnergyContainerRecipe.kt @@ -17,6 +17,7 @@ import ru.dbotthepony.mc.otm.capability.ItemEnergyStorageImpl import ru.dbotthepony.mc.otm.capability.matteryEnergy import ru.dbotthepony.mc.otm.container.iterator import ru.dbotthepony.mc.otm.container.stream +import ru.dbotthepony.mc.otm.core.filterNotNull import ru.dbotthepony.mc.otm.core.set import ru.dbotthepony.mc.otm.core.tagNotNull @@ -37,11 +38,11 @@ class EnergyContainerRecipe( val battery = container.stream() .filter { !it.isEmpty } .map { it.matteryEnergy } - .filter { it != null } + .filterNotNull() .findAny().orElse(null) if (battery != null) { - itemStack.tagNotNull[ItemEnergyStorageImpl.ENERGY_KEY] = battery.batteryLevel.serializeNBT() + itemStack.matteryEnergy?.batteryLevel = battery.batteryLevel } if (itemStack.isEnchantable) { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/CreativeTabs.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/CreativeTabs.kt index e8851322b..59dae9636 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/CreativeTabs.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/CreativeTabs.kt @@ -3,6 +3,10 @@ package ru.dbotthepony.mc.otm.registry import net.minecraft.world.item.CreativeModeTab import net.minecraft.world.item.DyeColor import net.minecraft.world.item.Item +import net.minecraft.world.item.ItemStack +import ru.dbotthepony.mc.otm.capability.matter.matter +import ru.dbotthepony.mc.otm.capability.matteryEnergy +import ru.dbotthepony.mc.otm.core.registryName private fun CreativeModeTab.Output.accept(values: Collection) { for (item in values) { @@ -58,6 +62,37 @@ private fun CreativeModeTab.Output.all(values: Map) { colored(values) } +private fun CreativeModeTab.Output.energized(value: Item) { + accept(value) + + val stack = ItemStack(value, 1) + val energy = stack.matteryEnergy ?: throw IllegalArgumentException("${value.registryName} does not implement mattery energy capability") + energy.fillBattery() + accept(stack) +} + +private fun CreativeModeTab.Output.energized(values: Iterable) { + for (value in values) { + energized(value) + } +} + +private fun CreativeModeTab.Output.mattery(value: Item) { + accept(value) + + val stack = ItemStack(value, 1) + val matter = stack.matter ?: throw IllegalArgumentException("${value.registryName} does not implement matter capability") + + matter.fillMatter() + accept(stack) +} + +private fun CreativeModeTab.Output.mattery(values: Iterable) { + for (value in values) { + mattery(value) + } +} + internal fun addMainCreativeTabItems(consumer: CreativeModeTab.Output) { with(consumer) { accept(MItems.MACHINES) @@ -77,8 +112,8 @@ internal fun addMainCreativeTabItems(consumer: CreativeModeTab.Output) { accept(MItems.TRITANIUM_TOOLS) accept(MItems.TRITANIUM_ARMOR) - accept(MItems.ENERGY_SWORD) - accept(MItems.PLASMA_RIFLE) + energized(MItems.ENERGY_SWORD) + energized(MItems.PLASMA_RIFLE) accept(MItems.BLACK_HOLE_SCANNER) accept(MItems.GRAVITATION_FIELD_LIMITER) @@ -87,8 +122,8 @@ internal fun addMainCreativeTabItems(consumer: CreativeModeTab.Output) { accept(MItems.BLACK_HOLE) accept(MItems.GRAVITATIONAL_DISRUPTOR) - accept(MItems.ALL_BATTERIES) - accept(MItems.MATTER_CAPACITORS) + energized(MItems.ALL_BATTERIES) + mattery(MItems.MATTER_CAPACITORS) base(MItems.CARGO_CRATE_MINECARTS)