use of mekanism.api.annotations.NonNull in project Mekanism by mekanism.
the class OutputHelper method getOutputHandler.
/**
* Wraps two inventory slots, a "main" slot, and a "secondary" slot into an {@link IOutputHandler} for handling {@link ChanceOutput}s.
*
* @param mainSlot Main slot to wrap.
* @param secondarySlot Secondary slot to wrap.
*/
public static IOutputHandler<@NonNull ChanceOutput> getOutputHandler(IInventorySlot mainSlot, IInventorySlot secondarySlot) {
Objects.requireNonNull(mainSlot, "Main slot cannot be null.");
Objects.requireNonNull(secondarySlot, "Secondary/Extra slot cannot be null.");
return new IOutputHandler<@NonNull ChanceOutput>() {
@Override
public void handleOutput(@Nonnull ChanceOutput toOutput, int operations) {
OutputHelper.handleOutput(mainSlot, toOutput.getMainOutput(), operations);
// TODO: Batch this into a single addition call, by looping over and calculating things?
ItemStack secondaryOutput = toOutput.getSecondaryOutput();
for (int i = 0; i < operations; i++) {
OutputHelper.handleOutput(secondarySlot, secondaryOutput, operations);
if (i < operations - 1) {
secondaryOutput = toOutput.nextSecondaryOutput();
}
}
}
@Override
public int operationsRoomFor(@Nonnull ChanceOutput toOutput, int currentMax) {
currentMax = OutputHelper.operationsRoomFor(mainSlot, toOutput.getMainOutput(), currentMax);
return OutputHelper.operationsRoomFor(secondarySlot, toOutput.getMaxSecondaryOutput(), currentMax);
}
};
}
use of mekanism.api.annotations.NonNull in project Mekanism by mekanism.
the class ItemRecipeData method applyToStack.
@Override
public boolean applyToStack(ItemStack stack) {
if (slots.isEmpty()) {
return true;
}
Item item = stack.getItem();
boolean isBin = item instanceof ItemBlockBin;
Optional<IItemHandler> capability = stack.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).resolve();
List<IInventorySlot> slots = new ArrayList<>();
if (capability.isPresent()) {
IItemHandler itemHandler = capability.get();
for (int i = 0; i < itemHandler.getSlots(); i++) {
int slot = i;
slots.add(new DummyInventorySlot(itemHandler.getSlotLimit(slot), itemStack -> itemHandler.isItemValid(slot, itemStack), isBin));
}
} else if (item instanceof BlockItem) {
TileEntityMekanism tile = getTileFromBlock(((BlockItem) item).getBlock());
if (tile == null || !tile.persistInventory()) {
// Something went wrong
return false;
}
for (int i = 0; i < tile.getSlots(); i++) {
int slot = i;
slots.add(new DummyInventorySlot(tile.getSlotLimit(slot), itemStack -> tile.isItemValid(slot, itemStack), isBin));
}
} else if (item instanceof ItemRobit) {
// Inventory slots
for (int slotY = 0; slotY < 3; slotY++) {
for (int slotX = 0; slotX < 9; slotX++) {
slots.add(new DummyInventorySlot(BasicInventorySlot.DEFAULT_LIMIT, BasicInventorySlot.alwaysTrue, false));
}
}
// Energy slot
slots.add(new DummyInventorySlot(BasicInventorySlot.DEFAULT_LIMIT, itemStack -> {
if (EnergyCompatUtils.hasStrictEnergyHandler(itemStack)) {
return true;
}
ItemStackToEnergyRecipe foundRecipe = MekanismRecipeType.ENERGY_CONVERSION.getInputCache().findTypeBasedRecipe(null, itemStack);
return foundRecipe != null && !foundRecipe.getOutput(itemStack).isZero();
}, false));
// Smelting input slot
slots.add(new DummyInventorySlot(BasicInventorySlot.DEFAULT_LIMIT, itemStack -> MekanismRecipeType.SMELTING.getInputCache().containsInput(null, itemStack), false));
// Smelting output slot
slots.add(new DummyInventorySlot(BasicInventorySlot.DEFAULT_LIMIT, BasicInventorySlot.alwaysTrue, false));
} else if (item instanceof ISustainedInventory) {
// Fallback just save it all
for (IInventorySlot slot : this.slots) {
if (!slot.isEmpty()) {
// We have no information about what our item supports, but we have at least some stacks we want to transfer
((ISustainedInventory) stack.getItem()).setInventory(DataHandlerUtils.writeContainers(this.slots), stack);
return true;
}
}
return true;
} else {
return false;
}
if (slots.isEmpty()) {
// We don't actually have any tanks in the output
return true;
}
// TODO: Improve the logic so that it maybe tries multiple different slot combinations
IMekanismInventory outputHandler = new IMekanismInventory() {
@Nonnull
@Override
public List<IInventorySlot> getInventorySlots(@Nullable Direction side) {
return slots;
}
@Override
public void onContentsChanged() {
}
};
boolean hasData = false;
for (IInventorySlot slot : this.slots) {
if (!slot.isEmpty()) {
if (!ItemHandlerHelper.insertItemStacked(outputHandler, slot.getStack(), false).isEmpty()) {
// If we have a remainder something failed so bail
return false;
}
hasData = true;
}
}
if (hasData) {
// We managed to transfer it all into valid slots, so save it to the stack
((ISustainedInventory) stack.getItem()).setInventory(DataHandlerUtils.writeContainers(slots), stack);
}
return true;
}
use of mekanism.api.annotations.NonNull in project Mekanism by mekanism.
the class TileEntityPersonalChest method getInitialInventory.
@Nonnull
@Override
protected IInventorySlotHolder getInitialInventory() {
InventorySlotHelper builder = InventorySlotHelper.forSide(this::getDirection);
// Note: We always allow manual interaction (even for insertion), as if a player has the GUI open we treat that as they are allowed to interact with it
// and if the security mode changes we then boot any players who can't interact with it anymore out of the GUI
BiPredicate<@NonNull ItemStack, @NonNull AutomationType> canInteract = (stack, automationType) -> automationType == AutomationType.MANUAL || SecurityUtils.getSecurity(this, Dist.DEDICATED_SERVER) == SecurityMode.PUBLIC;
for (int slotY = 0; slotY < 6; slotY++) {
for (int slotX = 0; slotX < 9; slotX++) {
// Note: we allow access to the slots from all sides as long as it is public, unlike in 1.12 where we always denied the bottom face
// We did that to ensure that things like hoppers that could check IInventory did not bypass any restrictions
builder.addSlot(BasicInventorySlot.at(canInteract, canInteract, this, 8 + slotX * 18, 26 + slotY * 18));
}
}
return builder.build();
}
use of mekanism.api.annotations.NonNull in project Mekanism by mekanism.
the class PressurizedReactionRecipeManager method getAction.
@Override
protected ActionAddMekanismRecipe getAction(PressurizedReactionRecipe recipe) {
return new ActionAddMekanismRecipe(recipe) {
@Override
protected String describeOutputs() {
Pair<List<@NonNull ItemStack>, @NonNull GasStack> output = getRecipe().getOutputDefinition();
StringBuilder builder = new StringBuilder();
List<ItemStack> itemOutputs = output.getLeft();
if (!itemOutputs.isEmpty()) {
builder.append("item: ").append(CrTUtils.describeOutputs(itemOutputs, ItemStackHelper::getCommandString));
}
GasStack gasOutput = output.getRight();
if (!gasOutput.isEmpty()) {
if (!itemOutputs.isEmpty()) {
builder.append("; ");
}
builder.append("gas: ").append(new CrTGasStack(gasOutput));
}
return builder.toString();
}
};
}
use of mekanism.api.annotations.NonNull in project Mekanism by mekanism.
the class ChemicalDissolutionRecipeCategory method setIngredients.
@Override
public void setIngredients(ChemicalDissolutionRecipe recipe, IIngredients ingredients) {
ingredients.setInputLists(VanillaTypes.ITEM, Collections.singletonList(recipe.getItemInput().getRepresentations()));
List<@NonNull GasStack> gasInputs = recipe.getGasInput().getRepresentations();
List<GasStack> scaledGases = gasInputs.stream().map(gas -> new GasStack(gas, gas.getAmount() * TileEntityChemicalDissolutionChamber.BASE_TICKS_REQUIRED)).collect(Collectors.toList());
ingredients.setInputLists(MekanismJEI.TYPE_GAS, Collections.singletonList(scaledGases));
BoxedChemicalStack outputDefinition = recipe.getOutputDefinition();
ChemicalType chemicalType = outputDefinition.getChemicalType();
if (chemicalType == ChemicalType.GAS) {
ingredients.setOutput(MekanismJEI.TYPE_GAS, (GasStack) outputDefinition.getChemicalStack());
} else if (chemicalType == ChemicalType.INFUSION) {
ingredients.setOutput(MekanismJEI.TYPE_INFUSION, (InfusionStack) outputDefinition.getChemicalStack());
} else if (chemicalType == ChemicalType.PIGMENT) {
ingredients.setOutput(MekanismJEI.TYPE_PIGMENT, (PigmentStack) outputDefinition.getChemicalStack());
} else if (chemicalType == ChemicalType.SLURRY) {
ingredients.setOutput(MekanismJEI.TYPE_SLURRY, (SlurryStack) outputDefinition.getChemicalStack());
} else {
throw new IllegalStateException("Unknown chemical type");
}
}
Aggregations