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