Declare batteryLevel and matterLevel as vars instead of vals

This commit is contained in:
DBotThePony 2023-01-07 16:15:00 +07:00
parent be2dcc6bae
commit adad99a6bf
Signed by: DBot
GPG Key ID: DCC23B5715498507
8 changed files with 73 additions and 47 deletions

View File

@ -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()

View File

@ -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

View File

@ -145,20 +145,6 @@ open class BatteryItem : Item {
}
}
override fun fillItemCategory(p_41391_: CreativeModeTab, p_41392_: NonNullList<ItemStack>) {
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)
}

View File

@ -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<ItemStack>) {
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

View File

@ -109,20 +109,6 @@ class MatterCapacitorItem : Item {
_capacity = { Decimal.LONG_MAX_VALUE }
}
override fun fillItemCategory(p_41391_: CreativeModeTab, p_41392_: NonNullList<ItemStack>) {
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

View File

@ -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)) {

View File

@ -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) {

View File

@ -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<Item>) {
for (item in values) {
@ -58,6 +62,37 @@ private fun CreativeModeTab.Output.all(values: Map<DyeColor?, Item>) {
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<Item>) {
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<Item>) {
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)