Skip to content

Commit

Permalink
Add energy slot for blocks now
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfieboy09 committed Nov 12, 2024
1 parent 27ddabb commit 813e973
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/main/java/dev/wolfieboy09/qstorage/api/slots/EnergySlot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.wolfieboy09.qstorage.api.slots;

import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.capabilities.Capabilities;
import net.neoforged.neoforge.energy.IEnergyStorage;
import net.neoforged.neoforge.items.IItemHandler;
import net.neoforged.neoforge.items.SlotItemHandler;
import org.jetbrains.annotations.NotNull;

public class EnergySlot extends SlotItemHandler {
public EnergySlot(IItemHandler itemHandler, int index, int xPosition, int yPosition) {
super(itemHandler, index, xPosition, yPosition);
}

@Override
public boolean mayPlace(@NotNull ItemStack stack) {
IEnergyStorage energyStorage = stack.getCapability(Capabilities.EnergyStorage.ITEM);
return energyStorage != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ protected void onRemove(@NotNull BlockState state, Level level, @NotNull BlockPo
if (be instanceof DiskAssemblerBlockEntity blockEntity) {
SimpleContainer inputInv = blockEntity.getInputContainer();
SimpleContainer outputInv = blockEntity.getOutputContainer();
SimpleContainer energySlot = blockEntity.getEnergySlot();
Containers.dropContents(level, pos, inputInv);
Containers.dropContents(level, pos, outputInv);
Containers.dropContents(level, pos, energySlot);
}
super.onRemove(state, level, pos, newState, movedByPiston);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import net.minecraft.world.item.crafting.RecipeHolder;
import net.minecraft.world.item.crafting.RecipeManager;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.neoforge.capabilities.Capabilities;
import net.neoforged.neoforge.energy.EnergyStorage;
import net.neoforged.neoforge.energy.IEnergyStorage;
import net.neoforged.neoforge.items.ItemStackHandler;
import net.neoforged.neoforge.items.wrapper.RecipeWrapper;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -67,7 +69,25 @@ public void setChanged() {
updateContainer();
}

private void doEnergySlot() {
ItemStack energySlotStack = this.inventory.getStackInSlot(8);
if (energySlotStack.isEmpty()) return;

IEnergyStorage itemEnergyStorage = energySlotStack.getCapability(Capabilities.EnergyStorage.ITEM);
if (itemEnergyStorage == null || !itemEnergyStorage.canExtract()) return;

int itemEnergyStored = itemEnergyStorage.getEnergyStored();
int transferRate = Math.min(itemEnergyStored, this.energyStorage.getMaxReceive());
int energyToExtract = Math.min(transferRate, this.energyStorage.getMaxEnergyStored() - this.energyStorage.getEnergyStored());

if (energyToExtract > 0) {
int extractedEnergy = itemEnergyStorage.extractEnergy(energyToExtract, false);
this.energyStorage.addEnergy(extractedEnergy);
}
}

public void tick() {
doEnergySlot();
if (this.level == null || getInputContainer().isEmpty()) return;

// Validate the recipe
Expand Down Expand Up @@ -168,7 +188,7 @@ public static class DiskAssemblerSlot {
public static final int OUTPUT_SLOT = 7;
}

private final ItemStackHandler inventory = new ItemStackHandler(8) {
private final ItemStackHandler inventory = new ItemStackHandler(9) {
@Override
protected void onContentsChanged(int slot) {
if (slot < 7) {
Expand Down Expand Up @@ -217,6 +237,12 @@ public SimpleContainer getOutputContainer() {
container.setItem(0, this.inventory.getStackInSlot(this.inventory.getSlots() - 1));
return container;
}

public SimpleContainer getEnergySlot() {
SimpleContainer container = new SimpleContainer(1);
container.setItem(0, this.inventory.getStackInSlot(8));
return container;
}

private void saveRecipeToNBT(CompoundTag modData, HolderLookup.Provider registries) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.wolfieboy09.qstorage.block.disk_assembler;

import dev.wolfieboy09.qstorage.QuantiumizedStorage;
import dev.wolfieboy09.qstorage.api.slots.EnergySlot;
import dev.wolfieboy09.qstorage.block.AbstractEnergyContainerMenu;
import dev.wolfieboy09.qstorage.block.ItemResultSlot;
import dev.wolfieboy09.qstorage.registries.QSBlocks;
Expand Down Expand Up @@ -78,6 +79,7 @@ public DiskAssemblerMenu(int id, BlockPos pos, Inventory playerInventory, @NotNu
addSlot(new SlotItemHandler(itemStackHandler, 6, 134, 45));

addSlot(new ItemResultSlot(itemStackHandler, 7, 80, 36));
addSlot(new EnergySlot(itemStackHandler, 8, 134, 6));
}

@Override
Expand Down

0 comments on commit 813e973

Please sign in to comment.