Search in sources :

Example 1 with IContentsListener

use of mekanism.api.IContentsListener in project Mekanism by mekanism.

the class MergedChemicalInventorySlot method fill.

public static MergedChemicalInventorySlot<MergedChemicalTank> fill(MergedChemicalTank chemicalTank, @Nullable IContentsListener listener, int x, int y) {
    Objects.requireNonNull(chemicalTank, "Merged chemical tank cannot be null");
    Predicate<@NonNull ItemStack> gasExtractPredicate = ChemicalInventorySlot.getFillExtractPredicate(chemicalTank.getGasTank(), GasInventorySlot::getCapability);
    Predicate<@NonNull ItemStack> infusionExtractPredicate = ChemicalInventorySlot.getFillExtractPredicate(chemicalTank.getInfusionTank(), InfusionInventorySlot::getCapability);
    Predicate<@NonNull ItemStack> pigmentExtractPredicate = ChemicalInventorySlot.getFillExtractPredicate(chemicalTank.getPigmentTank(), PigmentInventorySlot::getCapability);
    Predicate<@NonNull ItemStack> slurryExtractPredicate = ChemicalInventorySlot.getFillExtractPredicate(chemicalTank.getSlurryTank(), SlurryInventorySlot::getCapability);
    Predicate<@NonNull ItemStack> gasInsertPredicate = stack -> ChemicalInventorySlot.fillInsertCheck(chemicalTank.getGasTank(), GasInventorySlot.getCapability(stack));
    Predicate<@NonNull ItemStack> infusionInsertPredicate = stack -> ChemicalInventorySlot.fillInsertCheck(chemicalTank.getInfusionTank(), InfusionInventorySlot.getCapability(stack));
    Predicate<@NonNull ItemStack> pigmentInsertPredicate = stack -> ChemicalInventorySlot.fillInsertCheck(chemicalTank.getPigmentTank(), PigmentInventorySlot.getCapability(stack));
    Predicate<@NonNull ItemStack> slurryInsertPredicate = stack -> ChemicalInventorySlot.fillInsertCheck(chemicalTank.getSlurryTank(), SlurryInventorySlot.getCapability(stack));
    return new MergedChemicalInventorySlot<>(chemicalTank, (stack, automationType) -> {
        if (automationType == AutomationType.MANUAL) {
            // Always allow the player to manually extract
            return true;
        }
        Current current = chemicalTank.getCurrent();
        if (current == Current.GAS) {
            return gasExtractPredicate.test(stack);
        } else if (current == Current.INFUSION) {
            return infusionExtractPredicate.test(stack);
        } else if (current == Current.PIGMENT) {
            return pigmentExtractPredicate.test(stack);
        } else if (current == Current.SLURRY) {
            return slurryExtractPredicate.test(stack);
        }
        // Else the tank is empty, check all our extraction predicates
        return gasExtractPredicate.test(stack) && infusionExtractPredicate.test(stack) && pigmentExtractPredicate.test(stack) && slurryExtractPredicate.test(stack);
    }, (stack, automationType) -> {
        Current current = chemicalTank.getCurrent();
        if (current == Current.GAS) {
            return gasInsertPredicate.test(stack);
        } else if (current == Current.INFUSION) {
            return infusionInsertPredicate.test(stack);
        } else if (current == Current.PIGMENT) {
            return pigmentInsertPredicate.test(stack);
        } else if (current == Current.SLURRY) {
            return slurryInsertPredicate.test(stack);
        }
        // Else the tank is empty, only allow it if one of the chemical insert predicates matches
        return gasInsertPredicate.test(stack) || infusionInsertPredicate.test(stack) || pigmentInsertPredicate.test(stack) || slurryInsertPredicate.test(stack);
    }, MergedChemicalInventorySlot::hasCapability, listener, x, y);
}
Also used : CurrentType(mekanism.common.capabilities.merged.MergedTank.CurrentType) Predicate(java.util.function.Predicate) MergedChemicalTank(mekanism.api.chemical.merged.MergedChemicalTank) AutomationType(mekanism.api.inventory.AutomationType) BasicInventorySlot(mekanism.common.inventory.slot.BasicInventorySlot) Objects(java.util.Objects) IContentsListener(mekanism.api.IContentsListener) BiPredicate(java.util.function.BiPredicate) ItemStack(net.minecraft.item.ItemStack) ContainerSlotType(mekanism.common.inventory.container.slot.ContainerSlotType) Capabilities(mekanism.common.capabilities.Capabilities) Current(mekanism.api.chemical.merged.MergedChemicalTank.Current) Nonnull(javax.annotation.Nonnull) NonNull(mekanism.api.annotations.NonNull) Nullable(javax.annotation.Nullable) ItemStack(net.minecraft.item.ItemStack) Current(mekanism.api.chemical.merged.MergedChemicalTank.Current)

Example 2 with IContentsListener

use of mekanism.api.IContentsListener in project Mekanism by mekanism.

the class GasInventorySlot method fillOrConvert.

/**
 * Fills the tank from this item OR converts the given item to a gas
 */
public static GasInventorySlot fillOrConvert(IGasTank gasTank, Supplier<World> worldSupplier, @Nullable IContentsListener listener, int x, int y) {
    Objects.requireNonNull(gasTank, "Gas tank cannot be null");
    Objects.requireNonNull(worldSupplier, "World supplier cannot be null");
    Function<ItemStack, GasStack> potentialConversionSupplier = stack -> getPotentialConversion(worldSupplier.get(), stack);
    return new GasInventorySlot(gasTank, worldSupplier, getFillOrConvertExtractPredicate(gasTank, GasInventorySlot::getCapability, potentialConversionSupplier), getFillOrConvertInsertPredicate(gasTank, GasInventorySlot::getCapability, potentialConversionSupplier), stack -> {
        if (stack.getCapability(Capabilities.GAS_HANDLER_CAPABILITY).isPresent()) {
            // Note: we mark all gas items as valid and have a more restrictive insert check so that we allow full tanks when they are done being filled
            return true;
        }
        // Allow gas conversion of items that have a gas that is valid
        GasStack gasConversion = getPotentialConversion(worldSupplier.get(), stack);
        return !gasConversion.isEmpty() && gasTank.isValid(gasConversion);
    }, listener, x, y);
}
Also used : Gas(mekanism.api.chemical.gas.Gas) IGasHandler(mekanism.api.chemical.gas.IGasHandler) MethodsReturnNonnullByDefault(mcp.MethodsReturnNonnullByDefault) MekanismRecipeType(mekanism.common.recipe.MekanismRecipeType) Predicate(java.util.function.Predicate) IMekanismInventory(mekanism.api.inventory.IMekanismInventory) World(net.minecraft.world.World) IChemicalHandler(mekanism.api.chemical.IChemicalHandler) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ParametersAreNonnullByDefault(javax.annotation.ParametersAreNonnullByDefault) BooleanSupplier(java.util.function.BooleanSupplier) Objects(java.util.Objects) IContentsListener(mekanism.api.IContentsListener) ItemStack(net.minecraft.item.ItemStack) FieldsAreNonnullByDefault(mekanism.api.annotations.FieldsAreNonnullByDefault) IGasTank(mekanism.api.chemical.gas.IGasTank) Capabilities(mekanism.common.capabilities.Capabilities) GasStack(mekanism.api.chemical.gas.GasStack) NonNull(mekanism.api.annotations.NonNull) Nullable(javax.annotation.Nullable) ItemStackToGasRecipe(mekanism.api.recipes.ItemStackToGasRecipe) GasStack(mekanism.api.chemical.gas.GasStack) ItemStack(net.minecraft.item.ItemStack)

Example 3 with IContentsListener

use of mekanism.api.IContentsListener in project Mekanism by mekanism.

the class HybridInventorySlot method outputOrFill.

public static HybridInventorySlot outputOrFill(MergedTank mergedTank, @Nullable IContentsListener listener, int x, int y) {
    Objects.requireNonNull(mergedTank, "Merged tank cannot be null");
    Predicate<@NonNull ItemStack> gasExtractPredicate = ChemicalInventorySlot.getFillExtractPredicate(mergedTank.getGasTank(), GasInventorySlot::getCapability);
    Predicate<@NonNull ItemStack> infusionExtractPredicate = ChemicalInventorySlot.getFillExtractPredicate(mergedTank.getInfusionTank(), InfusionInventorySlot::getCapability);
    Predicate<@NonNull ItemStack> pigmentExtractPredicate = ChemicalInventorySlot.getFillExtractPredicate(mergedTank.getPigmentTank(), PigmentInventorySlot::getCapability);
    Predicate<@NonNull ItemStack> slurryExtractPredicate = ChemicalInventorySlot.getFillExtractPredicate(mergedTank.getSlurryTank(), SlurryInventorySlot::getCapability);
    Predicate<@NonNull ItemStack> gasInsertPredicate = stack -> ChemicalInventorySlot.fillInsertCheck(mergedTank.getGasTank(), GasInventorySlot.getCapability(stack));
    Predicate<@NonNull ItemStack> infusionInsertPredicate = stack -> ChemicalInventorySlot.fillInsertCheck(mergedTank.getInfusionTank(), InfusionInventorySlot.getCapability(stack));
    Predicate<@NonNull ItemStack> pigmentInsertPredicate = stack -> ChemicalInventorySlot.fillInsertCheck(mergedTank.getPigmentTank(), PigmentInventorySlot.getCapability(stack));
    Predicate<@NonNull ItemStack> slurryInsertPredicate = stack -> ChemicalInventorySlot.fillInsertCheck(mergedTank.getSlurryTank(), SlurryInventorySlot.getCapability(stack));
    return new HybridInventorySlot(mergedTank, (stack, automationType) -> {
        if (automationType == AutomationType.MANUAL) {
            // Always allow the player to manually extract
            return true;
        }
        CurrentType currentType = mergedTank.getCurrentType();
        if (currentType == CurrentType.FLUID) {
            // Always allow extracting from a "fluid output" slot
            return true;
        } else if (currentType == CurrentType.GAS) {
            return gasExtractPredicate.test(stack);
        } else if (currentType == CurrentType.INFUSION) {
            return infusionExtractPredicate.test(stack);
        } else if (currentType == CurrentType.PIGMENT) {
            return pigmentExtractPredicate.test(stack);
        } else if (currentType == CurrentType.SLURRY) {
            return slurryExtractPredicate.test(stack);
        }
        // Else the tank is empty, check all our extraction predicates
        return gasExtractPredicate.test(stack) && infusionExtractPredicate.test(stack) && pigmentExtractPredicate.test(stack) && slurryExtractPredicate.test(stack);
    }, (stack, automationType) -> {
        CurrentType currentType = mergedTank.getCurrentType();
        if (currentType == CurrentType.FLUID) {
            // Only allow inserting internally for "fluid output" slots
            return automationType == AutomationType.INTERNAL;
        } else if (currentType == CurrentType.GAS) {
            return gasInsertPredicate.test(stack);
        } else if (currentType == CurrentType.INFUSION) {
            return infusionInsertPredicate.test(stack);
        } else if (currentType == CurrentType.PIGMENT) {
            return pigmentInsertPredicate.test(stack);
        } else if (currentType == CurrentType.SLURRY) {
            return slurryInsertPredicate.test(stack);
        }
        // Else the tank is empty, if the item is a fluid handler, and it is an internal check allow it
        if (automationType == AutomationType.INTERNAL && FluidUtil.getFluidHandler(stack).isPresent()) {
            return true;
        }
        // otherwise, only allow it if one of the chemical insert predicates matches
        return gasInsertPredicate.test(stack) || infusionInsertPredicate.test(stack) || pigmentInsertPredicate.test(stack) || slurryInsertPredicate.test(stack);
    }, HybridInventorySlot::hasCapability, listener, x, y);
}
Also used : FluidUtil(net.minecraftforge.fluids.FluidUtil) MergedChemicalInventorySlot(mekanism.common.inventory.slot.chemical.MergedChemicalInventorySlot) CurrentType(mekanism.common.capabilities.merged.MergedTank.CurrentType) Predicate(java.util.function.Predicate) InfusionInventorySlot(mekanism.common.inventory.slot.chemical.InfusionInventorySlot) IExtendedFluidTank(mekanism.api.fluid.IExtendedFluidTank) AutomationType(mekanism.api.inventory.AutomationType) ChemicalInventorySlot(mekanism.common.inventory.slot.chemical.ChemicalInventorySlot) PigmentInventorySlot(mekanism.common.inventory.slot.chemical.PigmentInventorySlot) SlurryInventorySlot(mekanism.common.inventory.slot.chemical.SlurryInventorySlot) CompoundNBT(net.minecraft.nbt.CompoundNBT) MergedTank(mekanism.common.capabilities.merged.MergedTank) Objects(java.util.Objects) IContentsListener(mekanism.api.IContentsListener) BiPredicate(java.util.function.BiPredicate) ItemStack(net.minecraft.item.ItemStack) GasInventorySlot(mekanism.common.inventory.slot.chemical.GasInventorySlot) Capabilities(mekanism.common.capabilities.Capabilities) NBTConstants(mekanism.api.NBTConstants) Nonnull(javax.annotation.Nonnull) NonNull(mekanism.api.annotations.NonNull) Nullable(javax.annotation.Nullable) SlurryInventorySlot(mekanism.common.inventory.slot.chemical.SlurryInventorySlot) CurrentType(mekanism.common.capabilities.merged.MergedTank.CurrentType) GasInventorySlot(mekanism.common.inventory.slot.chemical.GasInventorySlot) ItemStack(net.minecraft.item.ItemStack) InfusionInventorySlot(mekanism.common.inventory.slot.chemical.InfusionInventorySlot) PigmentInventorySlot(mekanism.common.inventory.slot.chemical.PigmentInventorySlot)

Example 4 with IContentsListener

use of mekanism.api.IContentsListener in project Mekanism by mekanism.

the class InfusionInventorySlot method fillOrConvert.

/**
 * Fills the tank from this item OR converts the given item to an infusion type
 */
public static InfusionInventorySlot fillOrConvert(IInfusionTank infusionTank, Supplier<World> worldSupplier, @Nullable IContentsListener listener, int x, int y) {
    Objects.requireNonNull(infusionTank, "Infusion tank cannot be null");
    Objects.requireNonNull(worldSupplier, "World supplier cannot be null");
    Function<ItemStack, InfusionStack> potentialConversionSupplier = stack -> getPotentialConversion(worldSupplier.get(), stack);
    return new InfusionInventorySlot(infusionTank, worldSupplier, getFillOrConvertExtractPredicate(infusionTank, InfusionInventorySlot::getCapability, potentialConversionSupplier), getFillOrConvertInsertPredicate(infusionTank, InfusionInventorySlot::getCapability, potentialConversionSupplier), stack -> {
        if (stack.getCapability(Capabilities.INFUSION_HANDLER_CAPABILITY).isPresent()) {
            // Note: we mark all infusion items as valid and have a more restrictive insert check so that we allow full tanks when they are done being filled
            return true;
        }
        // Allow infusion conversion of items that have an infusion that is valid
        InfusionStack conversion = getPotentialConversion(worldSupplier.get(), stack);
        return !conversion.isEmpty() && infusionTank.isValid(conversion);
    }, listener, x, y);
}
Also used : ItemStackToInfuseTypeRecipe(mekanism.api.recipes.ItemStackToInfuseTypeRecipe) MethodsReturnNonnullByDefault(mcp.MethodsReturnNonnullByDefault) MekanismRecipeType(mekanism.common.recipe.MekanismRecipeType) Predicate(java.util.function.Predicate) World(net.minecraft.world.World) IChemicalHandler(mekanism.api.chemical.IChemicalHandler) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ParametersAreNonnullByDefault(javax.annotation.ParametersAreNonnullByDefault) IInfusionHandler(mekanism.api.chemical.infuse.IInfusionHandler) Objects(java.util.Objects) IContentsListener(mekanism.api.IContentsListener) ItemStack(net.minecraft.item.ItemStack) IInfusionTank(mekanism.api.chemical.infuse.IInfusionTank) InfusionStack(mekanism.api.chemical.infuse.InfusionStack) FieldsAreNonnullByDefault(mekanism.api.annotations.FieldsAreNonnullByDefault) Capabilities(mekanism.common.capabilities.Capabilities) InfuseType(mekanism.api.chemical.infuse.InfuseType) NonNull(mekanism.api.annotations.NonNull) Nullable(javax.annotation.Nullable) InfusionStack(mekanism.api.chemical.infuse.InfusionStack) ItemStack(net.minecraft.item.ItemStack)

Aggregations

Objects (java.util.Objects)4 Predicate (java.util.function.Predicate)4 Nullable (javax.annotation.Nullable)4 IContentsListener (mekanism.api.IContentsListener)4 NonNull (mekanism.api.annotations.NonNull)4 Capabilities (mekanism.common.capabilities.Capabilities)4 ItemStack (net.minecraft.item.ItemStack)4 BiPredicate (java.util.function.BiPredicate)2 Function (java.util.function.Function)2 Supplier (java.util.function.Supplier)2 Nonnull (javax.annotation.Nonnull)2 ParametersAreNonnullByDefault (javax.annotation.ParametersAreNonnullByDefault)2 MethodsReturnNonnullByDefault (mcp.MethodsReturnNonnullByDefault)2 FieldsAreNonnullByDefault (mekanism.api.annotations.FieldsAreNonnullByDefault)2 IChemicalHandler (mekanism.api.chemical.IChemicalHandler)2 AutomationType (mekanism.api.inventory.AutomationType)2 CurrentType (mekanism.common.capabilities.merged.MergedTank.CurrentType)2 MekanismRecipeType (mekanism.common.recipe.MekanismRecipeType)2 World (net.minecraft.world.World)2 BooleanSupplier (java.util.function.BooleanSupplier)1