Search in sources :

Example 1 with IStrictEnergyHandler

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

the class MekanismJEI method getEnergyComponent.

private static String getEnergyComponent(ItemStack stack) {
    Optional<IStrictEnergyHandler> capability = stack.getCapability(Capabilities.STRICT_ENERGY_CAPABILITY).resolve();
    if (capability.isPresent()) {
        IStrictEnergyHandler energyHandlerItem = capability.get();
        String component = "";
        int containers = energyHandlerItem.getEnergyContainerCount();
        for (int container = 0; container < containers; container++) {
            FloatingLong neededEnergy = energyHandlerItem.getNeededEnergy(container);
            if (neededEnergy.isZero()) {
                component = addInterpretation(component, "filled");
            } else if (containers > 1) {
                component = addInterpretation(component, "empty");
            }
        }
        return component;
    }
    return IIngredientSubtypeInterpreter.NONE;
}
Also used : IStrictEnergyHandler(mekanism.api.energy.IStrictEnergyHandler) FloatingLong(mekanism.api.math.FloatingLong)

Example 2 with IStrictEnergyHandler

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

the class EnergyInventorySlot method fillContainerFromItem.

/**
 * @implNote Does not pre-check if the current stack is empty or that the energy container needs any energy
 */
private boolean fillContainerFromItem() {
    // TODO: Do we need to/want to add any special handling for if the handler is stacked? For example with how buckets are for fluids
    IStrictEnergyHandler itemEnergyHandler = EnergyCompatUtils.getStrictEnergyHandler(current);
    if (itemEnergyHandler != null) {
        boolean didTransfer = false;
        for (int container = 0; container < itemEnergyHandler.getEnergyContainerCount(); container++) {
            FloatingLong energyInItem = itemEnergyHandler.getEnergy(container);
            if (!energyInItem.isZero()) {
                // Simulate inserting energy from each container in the item into our container
                FloatingLong simulatedRemainder = energyContainer.insert(energyInItem, Action.SIMULATE, AutomationType.INTERNAL);
                if (simulatedRemainder.smallerThan(energyInItem)) {
                    // If we were simulated that we could actually insert any, then
                    // extract up to as much energy as we were able to accept from the item
                    FloatingLong extractedEnergy = itemEnergyHandler.extractEnergy(container, energyInItem.subtract(simulatedRemainder), Action.EXECUTE);
                    if (!extractedEnergy.isZero()) {
                        // If we were able to actually extract it from the item, then insert it into our energy container
                        MekanismUtils.logExpectedZero(energyContainer.insert(extractedEnergy, Action.EXECUTE, AutomationType.INTERNAL));
                        // and mark that we were able to transfer at least some of it
                        didTransfer = true;
                        if (energyContainer.getNeeded().isZero()) {
                            // to check about filling the container from the item
                            break;
                        }
                    }
                }
            }
        }
        if (didTransfer) {
            onContentsChanged();
            return true;
        }
    }
    return false;
}
Also used : IStrictEnergyHandler(mekanism.api.energy.IStrictEnergyHandler) FloatingLong(mekanism.api.math.FloatingLong)

Example 3 with IStrictEnergyHandler

use of mekanism.api.energy.IStrictEnergyHandler 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)

Example 4 with IStrictEnergyHandler

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

the class StorageUtils method addStoredEnergy.

public static void addStoredEnergy(@Nonnull ItemStack stack, @Nonnull List<ITextComponent> tooltip, boolean showMissingCap, ILangEntry langEntry) {
    if (Capabilities.STRICT_ENERGY_CAPABILITY != null) {
        // Ensure the capability is not null, as the first call to addInformation happens before capability injection
        Optional<IStrictEnergyHandler> capability = stack.getCapability(Capabilities.STRICT_ENERGY_CAPABILITY).resolve();
        if (capability.isPresent()) {
            IStrictEnergyHandler energyHandlerItem = capability.get();
            int energyContainerCount = energyHandlerItem.getEnergyContainerCount();
            for (int container = 0; container < energyContainerCount; container++) {
                tooltip.add(langEntry.translateColored(EnumColor.BRIGHT_GREEN, EnumColor.GRAY, EnergyDisplay.of(energyHandlerItem.getEnergy(container), energyHandlerItem.getMaxEnergy(container))));
            }
        } else if (showMissingCap) {
            tooltip.add(langEntry.translateColored(EnumColor.BRIGHT_GREEN, EnumColor.GRAY, EnergyDisplay.ZERO));
        }
    }
}
Also used : IStrictEnergyHandler(mekanism.api.energy.IStrictEnergyHandler)

Example 5 with IStrictEnergyHandler

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

the class EnergyNetwork method tickEmit.

private FloatingLong tickEmit(FloatingLong energyToSend) {
    Collection<Map<Direction, LazyOptional<IStrictEnergyHandler>>> acceptorValues = acceptorCache.getAcceptorValues();
    EnergyAcceptorTarget target = new EnergyAcceptorTarget(acceptorValues.size() * 2);
    for (Map<Direction, LazyOptional<IStrictEnergyHandler>> acceptors : acceptorValues) {
        for (LazyOptional<IStrictEnergyHandler> lazyAcceptor : acceptors.values()) {
            lazyAcceptor.ifPresent(acceptor -> {
                if (acceptor.insertEnergy(energyToSend, Action.SIMULATE).smallerThan(energyToSend)) {
                    target.addHandler(acceptor);
                }
            });
        }
    }
    return EmitUtils.sendToAcceptors(target, energyToSend.copy());
}
Also used : IStrictEnergyHandler(mekanism.api.energy.IStrictEnergyHandler) EnergyAcceptorTarget(mekanism.common.content.network.distribution.EnergyAcceptorTarget) LazyOptional(net.minecraftforge.common.util.LazyOptional) Map(java.util.Map) Direction(net.minecraft.util.Direction)

Aggregations

IStrictEnergyHandler (mekanism.api.energy.IStrictEnergyHandler)10 FloatingLong (mekanism.api.math.FloatingLong)4 Direction (net.minecraft.util.Direction)3 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Nullable (javax.annotation.Nullable)1 IEnergyContainer (mekanism.api.energy.IEnergyContainer)1 IMekanismStrictEnergyHandler (mekanism.api.energy.IMekanismStrictEnergyHandler)1 EnergyAcceptorTarget (mekanism.common.content.network.distribution.EnergyAcceptorTarget)1 ItemRobit (mekanism.common.item.ItemRobit)1 MultiblockData (mekanism.common.lib.multiblock.MultiblockData)1 TileEntityMekanism (mekanism.common.tile.base.TileEntityMekanism)1 TileEntityUpdateable (mekanism.common.tile.base.TileEntityUpdateable)1 BlockItem (net.minecraft.item.BlockItem)1 Item (net.minecraft.item.Item)1 ItemStack (net.minecraft.item.ItemStack)1 LazyOptional (net.minecraftforge.common.util.LazyOptional)1 IFluidHandler (net.minecraftforge.fluids.capability.IFluidHandler)1