Search in sources :

Example 6 with NonNull

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);
        }
    };
}
Also used : Nonnull(javax.annotation.Nonnull) NonNull(mekanism.api.annotations.NonNull) ChanceOutput(mekanism.api.recipes.SawmillRecipe.ChanceOutput) ItemStack(net.minecraft.item.ItemStack)

Example 7 with NonNull

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;
}
Also used : IInventorySlot(mekanism.api.inventory.IInventorySlot) IItemHandler(net.minecraftforge.items.IItemHandler) EnergyCompatUtils(mekanism.common.integration.energy.EnergyCompatUtils) IMekanismInventory(mekanism.api.inventory.IMekanismInventory) Item(net.minecraft.item.Item) BasicInventorySlot(mekanism.common.inventory.slot.BasicInventorySlot) Direction(net.minecraft.util.Direction) ParametersAreNonnullByDefault(javax.annotation.ParametersAreNonnullByDefault) ArrayList(java.util.ArrayList) ItemStack(net.minecraft.item.ItemStack) ItemHandlerHelper(net.minecraftforge.items.ItemHandlerHelper) Nonnull(javax.annotation.Nonnull) NonNull(mekanism.api.annotations.NonNull) ItemBlockBin(mekanism.common.item.block.ItemBlockBin) Nullable(javax.annotation.Nullable) TileEntityMekanism(mekanism.common.tile.base.TileEntityMekanism) ISustainedInventory(mekanism.common.tile.interfaces.ISustainedInventory) ListNBT(net.minecraft.nbt.ListNBT) MekanismRecipeType(mekanism.common.recipe.MekanismRecipeType) ItemRobit(mekanism.common.item.ItemRobit) Predicate(java.util.function.Predicate) DataHandlerUtils(mekanism.api.DataHandlerUtils) List(java.util.List) FieldsAreNonnullByDefault(mekanism.api.annotations.FieldsAreNonnullByDefault) IInventorySlot(mekanism.api.inventory.IInventorySlot) BlockItem(net.minecraft.item.BlockItem) NBTConstants(mekanism.api.NBTConstants) CapabilityItemHandler(net.minecraftforge.items.CapabilityItemHandler) ItemStackToEnergyRecipe(mekanism.api.recipes.ItemStackToEnergyRecipe) Optional(java.util.Optional) TileEntityMekanism(mekanism.common.tile.base.TileEntityMekanism) ISustainedInventory(mekanism.common.tile.interfaces.ISustainedInventory) IItemHandler(net.minecraftforge.items.IItemHandler) ItemRobit(mekanism.common.item.ItemRobit) ArrayList(java.util.ArrayList) BlockItem(net.minecraft.item.BlockItem) Direction(net.minecraft.util.Direction) ItemBlockBin(mekanism.common.item.block.ItemBlockBin) Item(net.minecraft.item.Item) BlockItem(net.minecraft.item.BlockItem) ItemStackToEnergyRecipe(mekanism.api.recipes.ItemStackToEnergyRecipe) IMekanismInventory(mekanism.api.inventory.IMekanismInventory) Nullable(javax.annotation.Nullable)

Example 8 with NonNull

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();
}
Also used : TileEntityMekanism(mekanism.common.tile.base.TileEntityMekanism) SecurityMode(mekanism.common.lib.security.SecurityMode) AutomationType(mekanism.api.inventory.AutomationType) BasicInventorySlot(mekanism.common.inventory.slot.BasicInventorySlot) SecurityUtils(mekanism.common.util.SecurityUtils) SoundEvents(net.minecraft.util.SoundEvents) InventorySlotHelper(mekanism.common.capabilities.holder.slot.InventorySlotHelper) Dist(net.minecraftforge.api.distmarker.Dist) BiPredicate(java.util.function.BiPredicate) ItemStack(net.minecraft.item.ItemStack) IInventorySlotHolder(mekanism.common.capabilities.holder.slot.IInventorySlotHolder) Nonnull(javax.annotation.Nonnull) NonNull(mekanism.api.annotations.NonNull) MekanismBlocks(mekanism.common.registries.MekanismBlocks) SoundCategory(net.minecraft.util.SoundCategory) InventorySlotHelper(mekanism.common.capabilities.holder.slot.InventorySlotHelper) AutomationType(mekanism.api.inventory.AutomationType) ItemStack(net.minecraft.item.ItemStack) Nonnull(javax.annotation.Nonnull)

Example 9 with NonNull

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();
        }
    };
}
Also used : NonNull(mekanism.api.annotations.NonNull) List(java.util.List) CrTGasStack(mekanism.common.integration.crafttweaker.chemical.CrTChemicalStack.CrTGasStack) ICrTGasStack(mekanism.common.integration.crafttweaker.chemical.ICrTChemicalStack.ICrTGasStack) GasStack(mekanism.api.chemical.gas.GasStack) IItemStack(com.blamejared.crafttweaker.api.item.IItemStack) ItemStack(net.minecraft.item.ItemStack) CrTGasStack(mekanism.common.integration.crafttweaker.chemical.CrTChemicalStack.CrTGasStack) ICrTGasStack(mekanism.common.integration.crafttweaker.chemical.ICrTChemicalStack.ICrTGasStack)

Example 10 with NonNull

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");
    }
}
Also used : ChemicalStack(mekanism.api.chemical.ChemicalStack) IIngredients(mezz.jei.api.ingredients.IIngredients) GuiGasGauge(mekanism.client.gui.element.gauge.GuiGasGauge) ChemicalType(mekanism.api.chemical.ChemicalType) IGuiHelper(mezz.jei.api.helpers.IGuiHelper) SlurryStack(mekanism.api.chemical.slurry.SlurryStack) GuiGauge(mekanism.client.gui.element.gauge.GuiGauge) TileEntityChemicalDissolutionChamber(mekanism.common.tile.machine.TileEntityChemicalDissolutionChamber) GasStack(mekanism.api.chemical.gas.GasStack) NonNull(mekanism.api.annotations.NonNull) MekanismBlocks(mekanism.common.registries.MekanismBlocks) IRecipeLayout(mezz.jei.api.gui.IRecipeLayout) GuiHorizontalPowerBar(mekanism.client.gui.element.bar.GuiHorizontalPowerBar) ChemicalDissolutionRecipe(mekanism.api.recipes.ChemicalDissolutionRecipe) BaseRecipeCategory(mekanism.client.jei.BaseRecipeCategory) SlotOverlay(mekanism.common.inventory.container.slot.SlotOverlay) SlotType(mekanism.client.gui.element.slot.SlotType) DataType(mekanism.common.tile.component.config.DataType) Collectors(java.util.stream.Collectors) MekanismJEI(mekanism.client.jei.MekanismJEI) ProgressType(mekanism.client.gui.element.progress.ProgressType) List(java.util.List) InfusionStack(mekanism.api.chemical.infuse.InfusionStack) IIngredientType(mezz.jei.api.ingredients.IIngredientType) GaugeType(mekanism.client.gui.element.gauge.GaugeType) PigmentStack(mekanism.api.chemical.pigment.PigmentStack) IGuiIngredientGroup(mezz.jei.api.gui.ingredient.IGuiIngredientGroup) VanillaTypes(mezz.jei.api.constants.VanillaTypes) Collections(java.util.Collections) BoxedChemicalStack(mekanism.api.chemical.merged.BoxedChemicalStack) GuiSlot(mekanism.client.gui.element.slot.GuiSlot) ChemicalType(mekanism.api.chemical.ChemicalType) InfusionStack(mekanism.api.chemical.infuse.InfusionStack) GasStack(mekanism.api.chemical.gas.GasStack) SlurryStack(mekanism.api.chemical.slurry.SlurryStack) BoxedChemicalStack(mekanism.api.chemical.merged.BoxedChemicalStack)

Aggregations

NonNull (mekanism.api.annotations.NonNull)12 Nonnull (javax.annotation.Nonnull)8 ItemStack (net.minecraft.item.ItemStack)8 List (java.util.List)6 BiPredicate (java.util.function.BiPredicate)4 Predicate (java.util.function.Predicate)4 Nullable (javax.annotation.Nullable)4 GasStack (mekanism.api.chemical.gas.GasStack)4 AutomationType (mekanism.api.inventory.AutomationType)4 BasicInventorySlot (mekanism.common.inventory.slot.BasicInventorySlot)4 Collections (java.util.Collections)3 NBTConstants (mekanism.api.NBTConstants)3 MekanismBlocks (mekanism.common.registries.MekanismBlocks)3 Objects (java.util.Objects)2 Collectors (java.util.stream.Collectors)2 ChemicalStack (mekanism.api.chemical.ChemicalStack)2 ChemicalType (mekanism.api.chemical.ChemicalType)2 InfusionStack (mekanism.api.chemical.infuse.InfusionStack)2 BoxedChemicalStack (mekanism.api.chemical.merged.BoxedChemicalStack)2 PigmentStack (mekanism.api.chemical.pigment.PigmentStack)2