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;
}
Aggregations