Search in sources :

Example 1 with IMekanismStrictEnergyHandler

use of mekanism.api.energy.IMekanismStrictEnergyHandler in project Mekanism by mekanism.

the class EnergyRecipeData method applyToStack.

@Override
public boolean applyToStack(ItemStack stack) {
    if (energyContainers.isEmpty()) {
        return true;
    }
    Item item = stack.getItem();
    Optional<IStrictEnergyHandler> capability = stack.getCapability(Capabilities.STRICT_ENERGY_CAPABILITY).resolve();
    List<IEnergyContainer> energyContainers = new ArrayList<>();
    if (capability.isPresent()) {
        IStrictEnergyHandler energyHandler = capability.get();
        for (int container = 0; container < energyHandler.getEnergyContainerCount(); container++) {
            energyContainers.add(BasicEnergyContainer.create(energyHandler.getMaxEnergy(container), null));
        }
    } else if (item instanceof BlockItem) {
        TileEntityMekanism tile = getTileFromBlock(((BlockItem) item).getBlock());
        if (tile == null || !tile.handles(SubstanceType.ENERGY)) {
            // Something went wrong
            return false;
        }
        for (int container = 0; container < tile.getEnergyContainerCount(); container++) {
            energyContainers.add(BasicEnergyContainer.create(tile.getMaxEnergy(container), null));
        }
    } else {
        return false;
    }
    if (energyContainers.isEmpty()) {
        // We don't actually have any tanks in the output
        return true;
    }
    IMekanismStrictEnergyHandler outputHandler = new IMekanismStrictEnergyHandler() {

        @Nonnull
        @Override
        public List<IEnergyContainer> getEnergyContainers(@Nullable Direction side) {
            return energyContainers;
        }

        @Override
        public void onContentsChanged() {
        }
    };
    boolean hasData = false;
    for (IEnergyContainer energyContainer : this.energyContainers) {
        if (!energyContainer.isEmpty()) {
            hasData = true;
            if (!outputHandler.insertEnergy(energyContainer.getEnergy(), Action.EXECUTE).isZero()) {
                // If we have a remainder, stop trying to insert as our upgraded item's buffer is just full
                break;
            }
        }
    }
    if (hasData) {
        // We managed to transfer it all into valid slots, so save it to the stack
        ItemDataUtils.setList(stack, NBTConstants.ENERGY_CONTAINERS, DataHandlerUtils.writeContainers(energyContainers));
    }
    return true;
}
Also used : TileEntityMekanism(mekanism.common.tile.base.TileEntityMekanism) ArrayList(java.util.ArrayList) IMekanismStrictEnergyHandler(mekanism.api.energy.IMekanismStrictEnergyHandler) BlockItem(net.minecraft.item.BlockItem) Direction(net.minecraft.util.Direction) IStrictEnergyHandler(mekanism.api.energy.IStrictEnergyHandler) Item(net.minecraft.item.Item) BlockItem(net.minecraft.item.BlockItem) IEnergyContainer(mekanism.api.energy.IEnergyContainer) Nullable(javax.annotation.Nullable)

Aggregations

ArrayList (java.util.ArrayList)1 Nullable (javax.annotation.Nullable)1 IEnergyContainer (mekanism.api.energy.IEnergyContainer)1 IMekanismStrictEnergyHandler (mekanism.api.energy.IMekanismStrictEnergyHandler)1 IStrictEnergyHandler (mekanism.api.energy.IStrictEnergyHandler)1 TileEntityMekanism (mekanism.common.tile.base.TileEntityMekanism)1 BlockItem (net.minecraft.item.BlockItem)1 Item (net.minecraft.item.Item)1 Direction (net.minecraft.util.Direction)1