From 56039706549246209cc00c47add0f7b74024238b Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Thu, 8 Sep 2022 17:11:16 +0700 Subject: [PATCH] Give batteries some love --- .../datagen/recipes/CraftingTableRecipes.kt | 10 +-- .../mc/otm/capability/EnergyStorageImpl.kt | 64 +++++++++++++------ .../ru/dbotthepony/mc/otm/item/BatteryItem.kt | 11 ++-- .../mc/otm/item/SingleUseBatteryItem.kt | 10 +-- .../ru/dbotthepony/mc/otm/registry/MItems.kt | 12 ++-- 5 files changed, 61 insertions(+), 46 deletions(-) diff --git a/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/recipes/CraftingTableRecipes.kt b/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/recipes/CraftingTableRecipes.kt index 50653cdf0..467d64969 100644 --- a/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/recipes/CraftingTableRecipes.kt +++ b/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/recipes/CraftingTableRecipes.kt @@ -229,20 +229,20 @@ fun addCraftingTableRecipes(consumer: Consumer) { // простые батарейки MatteryRecipe(MItems.BATTERY_CRUDE) .rowB(Tags.Items.DUSTS_REDSTONE) - // .rowB(MItems.ELECTRIC_PARTS) - // .rowB(MItemTags.PLATE_IRON) + .rowB(Tags.Items.CROPS_POTATO) .rowB(Tags.Items.INGOTS_IRON) .build(consumer) MatteryRecipe(MItems.BATTERY_BASIC) - .rowB(Tags.Items.DUSTS_REDSTONE) + .rowAC(Tags.Items.DUSTS_REDSTONE, Tags.Items.DUSTS_REDSTONE) .rowB(MItems.ELECTRIC_PARTS) .rowB(MItemTags.PLATE_IRON) .build(consumer) MatteryRecipe(MItems.BATTERY_NORMAL) - .row(Tags.Items.DUSTS_REDSTONE, MItemTags.COPPER_WIRES, Tags.Items.DUSTS_REDSTONE) - .row(MItems.ELECTRIC_PARTS, MItemTags.PLATE_IRON, MItems.ELECTRIC_PARTS) + .rowB(MItems.ELECTRIC_PARTS) + .row(MItemTags.COPPER_WIRES, MItemTags.PLATE_IRON, MItemTags.COPPER_WIRES) + .row(Tags.Items.DUSTS_REDSTONE, Tags.Items.DUSTS_REDSTONE, Tags.Items.DUSTS_REDSTONE) .build(consumer) MatteryRecipe(MItems.BATTERY_DENSE) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/EnergyStorageImpl.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/EnergyStorageImpl.kt index 444d78bce..f7d19ca9c 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/EnergyStorageImpl.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/EnergyStorageImpl.kt @@ -15,7 +15,7 @@ import net.minecraftforge.common.util.LazyOptional import ru.dbotthepony.mc.otm.compat.mekanism.MatteryToMekanismEnergyWrapper import ru.dbotthepony.mc.otm.core.ImpreciseFraction import ru.dbotthepony.mc.otm.core.ifHas -import ru.dbotthepony.mc.otm.container.set +import ru.dbotthepony.mc.otm.core.map import ru.dbotthepony.mc.otm.core.set import ru.dbotthepony.mc.otm.core.tagNotNull @@ -24,12 +24,19 @@ private enum class EnergyFlow { } sealed class ItemEnergyStorageImpl( - private val type: EnergyFlow, + private val direction: EnergyFlow, protected val itemStack: ItemStack, maxBatteryLevel: ImpreciseFraction, - protected var maxInput: ImpreciseFraction?, - protected var maxOutput: ImpreciseFraction? + maxInput: ImpreciseFraction?, + maxOutput: ImpreciseFraction?, + val initialBatteryLevel: ImpreciseFraction = ImpreciseFraction.ZERO ) : IMatteryEnergyStorage, ICapabilityProvider { + var maxInput: ImpreciseFraction? = maxInput + protected set + + var maxOutput: ImpreciseFraction? = maxOutput + protected set + private val resolver = LazyOptional.of { this } private val resolverMekanism = if (isMekanismLoaded) LazyOptional.of { MatteryToMekanismEnergyWrapper(this) } else null @@ -47,20 +54,20 @@ sealed class ItemEnergyStorageImpl( protected set override var batteryLevel: ImpreciseFraction - get() = itemStack.tag?.get(NBT_KEY)?.let { ImpreciseFraction.deserializeNBT(it) } ?: ImpreciseFraction.ZERO + get() = itemStack.tag?.map(NBT_KEY, ImpreciseFraction::deserializeNBT) ?: initialBatteryLevel protected set(value) { - itemStack.tagNotNull.put(NBT_KEY, value.serializeNBT()) + itemStack.tagNotNull[NBT_KEY] = value.serializeNBT() } override fun extractEnergyOuter(howMuch: ImpreciseFraction, simulate: Boolean): ImpreciseFraction { - if (type == EnergyFlow.INPUT) + if (direction == EnergyFlow.INPUT) return ImpreciseFraction.ZERO return extractEnergyInner(howMuch, simulate) } override fun receiveEnergyOuter(howMuch: ImpreciseFraction, simulate: Boolean): ImpreciseFraction { - if (type == EnergyFlow.OUTPUT) + if (direction == EnergyFlow.OUTPUT) return ImpreciseFraction.ZERO return receiveEnergyInner(howMuch, simulate) @@ -121,11 +128,11 @@ sealed class ItemEnergyStorageImpl( } override fun canExtract(): Boolean { - return type != EnergyFlow.INPUT + return direction != EnergyFlow.INPUT } override fun canReceive(): Boolean { - return type != EnergyFlow.OUTPUT + return direction != EnergyFlow.OUTPUT } companion object { @@ -133,18 +140,33 @@ sealed class ItemEnergyStorageImpl( } } -open class EnergyConsumerItem(stack: ItemStack, maxBatteryLevel: ImpreciseFraction, maxInput: ImpreciseFraction? = null, maxOutput: ImpreciseFraction? = maxInput) - : ItemEnergyStorageImpl(EnergyFlow.INPUT, stack, maxBatteryLevel, maxInput, maxOutput) +open class EnergyConsumerItem( + stack: ItemStack, + maxBatteryLevel: ImpreciseFraction, + maxInput: ImpreciseFraction? = null, + maxOutput: ImpreciseFraction? = maxInput, + initialBatteryLevel: ImpreciseFraction = ImpreciseFraction.ZERO +) : ItemEnergyStorageImpl(EnergyFlow.INPUT, stack, maxBatteryLevel, maxInput, maxOutput, initialBatteryLevel) -open class EnergyProducerItem(stack: ItemStack, maxBatteryLevel: ImpreciseFraction, maxInput: ImpreciseFraction? = null, maxOutput: ImpreciseFraction? = maxInput) - : ItemEnergyStorageImpl(EnergyFlow.OUTPUT, stack, maxBatteryLevel, maxInput, maxOutput) +open class EnergyProducerItem( + stack: ItemStack, + maxBatteryLevel: ImpreciseFraction, + maxInput: ImpreciseFraction? = null, + maxOutput: ImpreciseFraction? = maxInput, + initialBatteryLevel: ImpreciseFraction = ImpreciseFraction.ZERO +) : ItemEnergyStorageImpl(EnergyFlow.OUTPUT, stack, maxBatteryLevel, maxInput, maxOutput, initialBatteryLevel) -open class EnergyCapacitorItem(stack: ItemStack, maxBatteryLevel: ImpreciseFraction, maxInput: ImpreciseFraction? = null, maxOutput: ImpreciseFraction? = maxInput) - : ItemEnergyStorageImpl(EnergyFlow.BI_DIRECTIONAL, stack, maxBatteryLevel, maxInput, maxOutput) +open class EnergyCapacitorItem( + stack: ItemStack, + maxBatteryLevel: ImpreciseFraction, + maxInput: ImpreciseFraction? = null, + maxOutput: ImpreciseFraction? = maxInput, + initialBatteryLevel: ImpreciseFraction = ImpreciseFraction.ZERO +) : ItemEnergyStorageImpl(EnergyFlow.BI_DIRECTIONAL, stack, maxBatteryLevel, maxInput, maxOutput, initialBatteryLevel) sealed class BlockEnergyStorageImpl constructor( protected val listener: () -> Unit, - private val type: EnergyFlow, + private val direction: EnergyFlow, maxBatteryLevel: ImpreciseFraction, protected var maxInput: ImpreciseFraction?, protected var maxOutput: ImpreciseFraction? @@ -156,14 +178,14 @@ sealed class BlockEnergyStorageImpl constructor( protected set override fun extractEnergyOuter(howMuch: ImpreciseFraction, simulate: Boolean): ImpreciseFraction { - if (type == EnergyFlow.INPUT) + if (direction == EnergyFlow.INPUT) return ImpreciseFraction.ZERO return extractEnergyInner(howMuch, simulate) } override fun receiveEnergyOuter(howMuch: ImpreciseFraction, simulate: Boolean): ImpreciseFraction { - if (type == EnergyFlow.OUTPUT) + if (direction == EnergyFlow.OUTPUT) return ImpreciseFraction.ZERO return receiveEnergyInner(howMuch, simulate) @@ -222,11 +244,11 @@ sealed class BlockEnergyStorageImpl constructor( } override fun canExtract(): Boolean { - return type != EnergyFlow.INPUT + return direction != EnergyFlow.INPUT } override fun canReceive(): Boolean { - return type != EnergyFlow.OUTPUT + return direction != EnergyFlow.OUTPUT } override fun serializeNBT(): CompoundTag { 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 d89dcbbee..ef5ae00aa 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/item/BatteryItem.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/item/BatteryItem.kt @@ -3,7 +3,6 @@ package ru.dbotthepony.mc.otm.item import net.minecraft.ChatFormatting import net.minecraft.nbt.CompoundTag import net.minecraft.network.chat.Component -import net.minecraft.util.Mth import net.minecraft.world.item.Item import net.minecraft.world.item.ItemStack import net.minecraft.world.item.Rarity @@ -18,8 +17,7 @@ import ru.dbotthepony.mc.otm.core.formatPower import ru.dbotthepony.mc.otm.core.ifPresentK class BatteryItem : Item { - private inner class BatteryMatteryCapability(stack: ItemStack) - : EnergyCapacitorItem(stack, this@BatteryItem.storage, this@BatteryItem.receive, this@BatteryItem.extract) { + private inner class Power(stack: ItemStack) : EnergyCapacitorItem(stack, this@BatteryItem.storage, this@BatteryItem.receive, this@BatteryItem.extract, initialBatteryLevel = this@BatteryItem.initialBatteryLevel) { override fun extractEnergyInner(howMuch: ImpreciseFraction, simulate: Boolean): ImpreciseFraction { if (isCreative) return howMuch return super.extractEnergyInner(howMuch, simulate) @@ -43,14 +41,16 @@ class BatteryItem : Item { val storage: ImpreciseFraction val receive: ImpreciseFraction val extract: ImpreciseFraction + val initialBatteryLevel: ImpreciseFraction - constructor(storage: ImpreciseFraction, receive: ImpreciseFraction, extract: ImpreciseFraction) : super( + constructor(storage: ImpreciseFraction, receive: ImpreciseFraction, extract: ImpreciseFraction = receive, initialBatteryLevel: ImpreciseFraction = ImpreciseFraction.ZERO) : super( Properties().stacksTo(1).tab(OverdriveThatMatters.INSTANCE.CREATIVE_TAB) ) { isCreative = false this.storage = storage this.receive = receive this.extract = extract + this.initialBatteryLevel = initialBatteryLevel } constructor() : super(Properties().stacksTo(1).rarity(Rarity.EPIC).tab(OverdriveThatMatters.INSTANCE.CREATIVE_TAB)) { @@ -58,6 +58,7 @@ class BatteryItem : Item { storage = ImpreciseFraction.LONG_MAX_VALUE receive = ImpreciseFraction.LONG_MAX_VALUE extract = ImpreciseFraction.LONG_MAX_VALUE + initialBatteryLevel = ImpreciseFraction.LONG_MAX_VALUE } override fun isBarVisible(p_150899_: ItemStack): Boolean { @@ -112,7 +113,7 @@ class BatteryItem : Item { } override fun initCapabilities(stack: ItemStack, nbt: CompoundTag?): ICapabilityProvider { - return BatteryMatteryCapability(stack) + return Power(stack) } companion object { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/item/SingleUseBatteryItem.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/item/SingleUseBatteryItem.kt index 373cc6d62..ea2f53b90 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/item/SingleUseBatteryItem.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/item/SingleUseBatteryItem.kt @@ -19,14 +19,6 @@ open class SingleUseBatteryItem( val throughput: ImpreciseFraction? = null, properties: Properties = Properties().stacksTo(1).tab(OverdriveThatMatters.INSTANCE.CREATIVE_TAB) ) : Item(properties) { - private inner class Power(itemStack: ItemStack) : EnergyProducerItem(itemStack, storage, throughput) { - init { - if (!itemStack.tagNotNull.contains(NBT_KEY)) { - batteryLevel = storage - } - } - } - private val throughputText = throughput?.let { TranslatableComponent("otm.item.power.output_only", it.formatPower()).withStyle(ChatFormatting.GRAY) } override fun appendHoverText( @@ -53,7 +45,7 @@ open class SingleUseBatteryItem( } override fun initCapabilities(stack: ItemStack, nbt: CompoundTag?): ICapabilityProvider { - return Power(stack) + return EnergyProducerItem(stack, storage, throughput, initialBatteryLevel = storage) } override fun isBarVisible(p_150899_: ItemStack): Boolean { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MItems.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MItems.kt index 9c9a01ec1..97e1ea2d0 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MItems.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MItems.kt @@ -209,14 +209,14 @@ object MItems { val PILL_OBLIVION: Item by registry.register(MNames.PILL_OBLIVION) { PillItem(PillType.OBLIVION) } val PILL_HEAL: Item by registry.register(MNames.PILL_HEAL) { HealPillItem() } - val BATTERY_CRUDE: Item by registry.register(MNames.BATTERY_CRUDE) { BatteryItem(ImpreciseFraction(30_000), ImpreciseFraction(150), ImpreciseFraction(150)) } - val BATTERY_BASIC: Item by registry.register(MNames.BATTERY_BASIC) { BatteryItem(ImpreciseFraction(60_000), ImpreciseFraction(300), ImpreciseFraction(300)) } - val BATTERY_NORMAL: Item by registry.register(MNames.BATTERY_NORMAL) { BatteryItem(ImpreciseFraction(250_000), ImpreciseFraction(1000), ImpreciseFraction(1000)) } - val BATTERY_DENSE: Item by registry.register(MNames.BATTERY_DENSE) { BatteryItem(ImpreciseFraction(1_000_000), ImpreciseFraction(2000), ImpreciseFraction(2000)) } - val BATTERY_CAPACITOR: Item by registry.register(MNames.BATTERY_CAPACITOR) { BatteryItem(ImpreciseFraction(150_000), ImpreciseFraction(15000), ImpreciseFraction(15000)) } + val BATTERY_CRUDE: Item by registry.register(MNames.BATTERY_CRUDE) { BatteryItem(ImpreciseFraction(100_000), extract = ImpreciseFraction(160), receive = ImpreciseFraction(40), initialBatteryLevel = ImpreciseFraction(80_000)) } + val BATTERY_BASIC: Item by registry.register(MNames.BATTERY_BASIC) { BatteryItem(ImpreciseFraction(400_000), ImpreciseFraction(600)) } + val BATTERY_NORMAL: Item by registry.register(MNames.BATTERY_NORMAL) { BatteryItem(ImpreciseFraction(2_000_000), ImpreciseFraction(1_000)) } + val BATTERY_DENSE: Item by registry.register(MNames.BATTERY_DENSE) { BatteryItem(ImpreciseFraction(10_000_000), ImpreciseFraction(2_000)) } + val BATTERY_CAPACITOR: Item by registry.register(MNames.BATTERY_CAPACITOR) { BatteryItem(ImpreciseFraction(500_000), ImpreciseFraction(50_000)) } val BATTERY_CREATIVE: Item by registry.register(MNames.BATTERY_CREATIVE) { BatteryItem() } - val QUANTUM_BATTERY: Item by registry.register(MNames.QUANTUM_BATTERY) { QuantumBatteryItem(MNames.QUANTUM_BATTERY, ImpreciseFraction(20_000_000), ImpreciseFraction(10_000)) } + val QUANTUM_BATTERY: Item by registry.register(MNames.QUANTUM_BATTERY) { QuantumBatteryItem(MNames.QUANTUM_BATTERY, ImpreciseFraction(40_000_000), ImpreciseFraction(10_000)) } val QUANTUM_CAPACITOR: Item by registry.register(MNames.QUANTUM_CAPACITOR) { QuantumBatteryItem(MNames.QUANTUM_CAPACITOR, ImpreciseFraction(1_000_000), ImpreciseFraction(200_000)) } val QUANTUM_BATTERY_CREATIVE: Item by registry.register(MNames.QUANTUM_BATTERY_CREATIVE) { QuantumBatteryItem(MNames.QUANTUM_BATTERY_CREATIVE) } val ZPM_BATTERY: Item by registry.register(MNames.ZPM_BATTERY) { ZPMItem() }