From 9ee960c421a84da839fcc1d744b4d840b52868b1 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Fri, 14 Jan 2022 12:37:58 +0700 Subject: [PATCH] Make chemical generator not eat buckets and other stuff from other mods --- .../entity/BlockEntityChemicalGenerator.kt | 54 +++++++++++++------ .../client/screen/ChemicalGeneratorScreen.kt | 9 ++-- .../mc/otm/container/MatteryContainer.kt | 14 +++-- .../mc/otm/menu/ChemicalGeneratorMenu.kt | 15 ++++-- 4 files changed, 63 insertions(+), 29 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/BlockEntityChemicalGenerator.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/BlockEntityChemicalGenerator.kt index eeb710ed8..9c6b5cdb5 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/BlockEntityChemicalGenerator.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/BlockEntityChemicalGenerator.kt @@ -10,6 +10,7 @@ import net.minecraft.server.level.ServerLevel import net.minecraft.world.entity.player.Inventory import net.minecraft.world.entity.player.Player import net.minecraft.world.inventory.AbstractContainerMenu +import net.minecraft.world.item.ItemStack import net.minecraft.world.level.Level import net.minecraft.world.level.block.Block import net.minecraft.world.level.block.state.BlockState @@ -86,14 +87,6 @@ class BlockEntityChemicalGenerator(pos: BlockPos, state: BlockState) : BlockEnti } } - companion object { - private val THROUGHPUT = Fraction(160) - private const val THROUGHPUT_INT = 160 - private val GENERATION_SPEED = Fraction(40) - private val MAX_ENERGY = Fraction(24_000) - private val NAME = TranslatableComponent("block.overdrive_that_matters.chemical_generator") - } - override fun saveAdditional(nbt: CompoundTag) { super.saveAdditional(nbt) @@ -156,15 +149,20 @@ class BlockEntityChemicalGenerator(pos: BlockPos, state: BlockState) : BlockEnti } } - val container = MatteryContainer(this::setChangedLight, 2) + val container = MatteryContainer(this::setChangedLight, 3) val itemHandler = container.handler(fun (slot, stack): Boolean { - if (slot == 0) + if (slot == SLOT_INPUT) return ForgeHooks.getBurnTime(stack, null) > 0 + if (slot == SLOT_RESIDUE) + return false + return stack.getCapability(CapabilityEnergy.ENERGY).isPresent }, fun (slot, _, stack): Boolean { - return slot == 1 && + if (slot == SLOT_RESIDUE) return true + + return slot == SLOT_BATTERY && (!stack.getCapability(CapabilityEnergy.ENERGY).isPresent || stack.getCapability(CapabilityEnergy.ENERGY).resolve().get().receiveEnergy(Int.MAX_VALUE, true) <= 0) }) @@ -222,13 +220,25 @@ class BlockEntityChemicalGenerator(pos: BlockPos, state: BlockState) : BlockEnti } if (workingTicks == 0 && !isBlockedByRedstone && check) { - if (!container.getItem(0).isEmpty) { - val ticks = ForgeHooks.getBurnTime(container.getItem(0), null) + if (!container[SLOT_INPUT].isEmpty) { + val ticks = ForgeHooks.getBurnTime(container[SLOT_INPUT], null) + val residue = container[SLOT_INPUT].item.getContainerItem(container[SLOT_INPUT].copy().also { it.count = 1 }) + val canPutResidue = residue.isEmpty || container[SLOT_RESIDUE].isEmpty || ItemStack.isSameItemSameTags(container[SLOT_RESIDUE], residue) && container[SLOT_RESIDUE].count < container[2].maxStackSize - if (ticks >= 4 && (energy.batteryLevel < Fraction.ONE || GENERATION_SPEED * (ticks / 4) + energy.batteryLevel <= energy.maxBatteryLevel)) { + if (canPutResidue && ticks >= 4 && (energy.batteryLevel < Fraction.ONE || GENERATION_SPEED * (ticks / 4) + energy.batteryLevel <= energy.maxBatteryLevel)) { workingTicksTotal = ticks / 4 workingTicks = ticks / 4 - container.getItem(0).shrink(1) + container[SLOT_INPUT].shrink(1) + + if (!residue.isEmpty) { + if (container[SLOT_RESIDUE].isEmpty) { + container[SLOT_RESIDUE] = residue + } else { + container[SLOT_RESIDUE].count++ + } + } + + container.setChanged(SLOT_INPUT) } } @@ -237,7 +247,7 @@ class BlockEntityChemicalGenerator(pos: BlockPos, state: BlockState) : BlockEnti if (energy.batteryLevel.isZero()) return - val item = container.getItem(1) + val item = container[SLOT_BATTERY] if (!item.isEmpty) { item.getCapability(CapabilityEnergy.ENERGY).ifPresent(this::workWithPower) @@ -248,4 +258,16 @@ class BlockEntityChemicalGenerator(pos: BlockPos, state: BlockState) : BlockEnti consumer.ifPresent(this::workWithPower) } } + + companion object { + private val THROUGHPUT = Fraction(160) + private const val THROUGHPUT_INT = 160 + private val GENERATION_SPEED = Fraction(40) + private val MAX_ENERGY = Fraction(24_000) + private val NAME = TranslatableComponent("block.overdrive_that_matters.chemical_generator") + + const val SLOT_INPUT = 0 + const val SLOT_BATTERY = 1 + const val SLOT_RESIDUE = 2 + } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/ChemicalGeneratorScreen.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/ChemicalGeneratorScreen.kt index aec2e96bb..676fcd11f 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/ChemicalGeneratorScreen.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/ChemicalGeneratorScreen.kt @@ -9,14 +9,14 @@ import ru.dbotthepony.mc.otm.client.screen.widget.ProgressGaugePanel import ru.dbotthepony.mc.otm.menu.ChemicalGeneratorMenu class ChemicalGeneratorScreen(menu: ChemicalGeneratorMenu, inventory: Inventory, title: Component) : MatteryScreen(menu, inventory, title) { - override fun makeMainFrame(): FramePanel? { - val frame = super.makeMainFrame() + override fun makeMainFrame(): FramePanel { + val frame = super.makeMainFrame()!! PowerGaugePanel(this, frame, menu.energy, LEFT_MARGIN, GAUGE_TOP_WITH_SLOT) SlotPanel(this, frame, menu.batterySlot, LEFT_MARGIN, SLOT_TOP_UNDER_GAUGE) val self = this - val progress = object : ProgressGaugePanel(self, frame, menu.progress, 63f, PROGRESS_ARROW_TOP) { + val progress = object : ProgressGaugePanel(self, frame, menu.progress, 78f, PROGRESS_ARROW_TOP) { override fun makeTooltip(): MutableList { val list = super.makeTooltip() @@ -27,7 +27,8 @@ class ChemicalGeneratorScreen(menu: ChemicalGeneratorMenu, inventory: Inventory, } progress.flop = true - SlotPanel(this, frame, menu.fuelSlot, 93f, PROGRESS_SLOT_TOP) + SlotPanel(this, frame, menu.residueSlot, 56f, PROGRESS_SLOT_TOP) + SlotPanel(this, frame, menu.fuelSlot, 104f, PROGRESS_SLOT_TOP) return frame } 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 9d6965aa0..1da0493a7 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/container/MatteryContainer.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/container/MatteryContainer.kt @@ -274,11 +274,15 @@ open class MatteryContainer(val watcher: Runnable, private val size: Int) : Cont final override fun setChanged() { for (slot in 0 until size) { - if (!ItemStack.isSameItemSameTags(slots[slot], trackedSlots[slot])) { - setChanged(slot, slots[slot], trackedSlots[slot]) - trackedSlots[slot] = slots[slot].copy() - // mojang соси))0)0))0)))))0) - } + setChanged(slot) + } + } + + fun setChanged(slot: Int) { + if (!ItemStack.isSameItemSameTags(slots[slot], trackedSlots[slot])) { + setChanged(slot, slots[slot], trackedSlots[slot]) + trackedSlots[slot] = slots[slot].copy() + // mojang соси))0)0))0)))))0) } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/ChemicalGeneratorMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/ChemicalGeneratorMenu.kt index 05852e243..bc7c819ca 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/ChemicalGeneratorMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/ChemicalGeneratorMenu.kt @@ -14,15 +14,21 @@ import ru.dbotthepony.mc.otm.menu.widget.ProgressGaugeWidget class ChemicalGeneratorMenu @JvmOverloads constructor(id: Int, inv: Inventory, tile: BlockEntityChemicalGenerator? = null) : MatteryMenu(Registry.Menus.CHEMICAL_GENERATOR, id, inv, tile) { - val container = tile?.container ?: SimpleContainer(2) + val container = tile?.container ?: SimpleContainer(3) - val fuelSlot = object : MatterySlot(container, 0) { + val fuelSlot = object : MatterySlot(container, BlockEntityChemicalGenerator.SLOT_INPUT) { override fun mayPlace(p_40231_: ItemStack): Boolean { return ForgeHooks.getBurnTime(p_40231_, null) > 0 } } - val batterySlot = object : MatterySlot(container, 1) { + val residueSlot = object : MatterySlot(container, BlockEntityChemicalGenerator.SLOT_RESIDUE) { + override fun mayPlace(p_40231_: ItemStack): Boolean { + return false + } + } + + val batterySlot = object : MatterySlot(container, BlockEntityChemicalGenerator.SLOT_BATTERY) { override fun mayPlace(p_40231_: ItemStack): Boolean { return p_40231_.getCapability(CapabilityEnergy.ENERGY).isPresent } @@ -35,6 +41,7 @@ class ChemicalGeneratorMenu @JvmOverloads constructor(id: Int, inv: Inventory, t init { addSlot(fuelSlot) addSlot(batterySlot) + addSlot(residueSlot) addDataSlots(burnTime) addInventorySlots() } @@ -50,6 +57,6 @@ class ChemicalGeneratorMenu @JvmOverloads constructor(id: Int, inv: Inventory, t } override fun getWorkingSlotEnd(): Int { - return 2 + return 3 } }