Search in sources :

Example 16 with Ingredient

use of net.minecraft.item.crafting.Ingredient in project Wizardry by TeamWizardry.

the class FluidRecipeLoader method buildFluidCrafter.

private FluidCrafter buildFluidCrafter(String identifier, IBlockState outputBlock, Ingredient input, List<Ingredient> extraInputs, Fluid fluid, int duration, int required, boolean consume, boolean explode, boolean bubbling, boolean harp) {
    List<Ingredient> inputs = Lists.newArrayList(extraInputs);
    FluidCrafter builder = new FluidCrafter((world, pos, items) -> {
        if (allLiquidInPool(world, pos, required, fluid).size() < required)
            return false;
        List<ItemStack> list = items.stream().map(entity -> entity.getItem().copy()).collect(Collectors.toList());
        List<Ingredient> inputList = new ArrayList<>(inputs);
        inputList.add(input);
        for (Ingredient itemIn : inputList) {
            boolean foundMatch = false;
            List<ItemStack> toRemove = new LinkedList<>();
            for (ItemStack item : list) {
                if (itemIn.apply(item)) {
                    foundMatch = true;
                    break;
                }
            }
            if (!foundMatch)
                return false;
            list.removeAll(toRemove);
            toRemove.clear();
        }
        return true;
    }, (world, pos, items, currentDuration) -> {
        EntityItem entityItem = items.stream().filter(entity -> input.apply(entity.getItem())).findFirst().orElse(null);
        if (entityItem != null) {
            if (world.isRemote)
                LibParticles.CRAFTING_ALTAR_IDLE(world, entityItem.getPositionVector());
            if (bubbling && currentDuration % 10 == 0)
                world.playSound(null, entityItem.posX, entityItem.posY, entityItem.posZ, ModSounds.BUBBLING, SoundCategory.BLOCKS, 0.7F, (RandUtil.nextFloat() * 0.4F) + 0.8F);
        }
    }, (world, pos, items, currentDuration) -> {
        if (consume) {
            Block block = fluid.getBlock();
            if (block != null) {
                IBlockState defaultState = block.getDefaultState();
                Iterator<IProperty<?>> properties = defaultState.getPropertyKeys().iterator();
                IBlockState drainState = defaultState;
                if (properties.hasNext())
                    drainState = drainState.cycleProperty(properties.next());
                for (BlockPos position : allLiquidInPool(world, pos, required, fluid)) world.setBlockState(position, drainState);
            }
        }
        List<Ingredient> inputList = new ArrayList<>(inputs);
        inputList.add(input);
        for (Ingredient itemIn : inputList) {
            for (EntityItem entity : items) {
                if (itemIn.apply(entity.getItem())) {
                    entity.getItem().shrink(1);
                    if (entity.getItem().isEmpty())
                        entity.setDead();
                }
            }
        }
        world.setBlockState(pos, outputBlock);
        Vec3d output = new Vec3d(pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5);
        if (explode) {
            PacketHandler.NETWORK.sendToAllAround(new PacketExplode(output, Color.CYAN, Color.BLUE, 0.9, 2, 500, 100, 50, true), new NetworkRegistry.TargetPoint(world.provider.getDimension(), output.x, output.y, output.z, 256));
            PosUtils.boom(world, output, null, 3, false);
        }
        if (harp)
            world.playSound(null, output.x, output.y, output.z, ModSounds.HARP1, SoundCategory.BLOCKS, 0.3F, 1.0F);
    }, identifier, duration).setInputs(input, inputs).setIsBlock(true).setDoesConsume(true).setRequired(required).setFluid(fluid);
    Fluid fluidOutput = FluidRegistry.lookupFluidForBlock(outputBlock.getBlock());
    if (fluidOutput != null)
        builder.setOutput(new FluidStack(fluidOutput, 1000));
    else
        builder.setOutput(new ItemStack(outputBlock.getBlock(), 1, outputBlock.getBlock().damageDropped(outputBlock)));
    return builder;
}
Also used : JsonObject(com.google.gson.JsonObject) Ingredient(net.minecraft.item.crafting.Ingredient) java.util(java.util) JsonContext(net.minecraftforge.common.crafting.JsonContext) ModFluids(com.teamwizardry.wizardry.common.block.fluid.ModFluids) PacketExplode(com.teamwizardry.wizardry.common.network.PacketExplode) FluidRegistry(net.minecraftforge.fluids.FluidRegistry) Multimap(com.google.common.collect.Multimap) JsonParser(com.google.gson.JsonParser) PosUtils(com.teamwizardry.wizardry.api.util.PosUtils) JsonElement(com.google.gson.JsonElement) ItemStack(net.minecraft.item.ItemStack) Lists(com.google.common.collect.Lists) IProperty(net.minecraft.block.properties.IProperty) Block(net.minecraft.block.Block) Vec3d(net.minecraft.util.math.Vec3d) ModSounds(com.teamwizardry.wizardry.init.ModSounds) Fluid(net.minecraftforge.fluids.Fluid) SoundCategory(net.minecraft.util.SoundCategory) EntityItem(net.minecraft.entity.item.EntityItem) PacketHandler(com.teamwizardry.librarianlib.features.network.PacketHandler) World(net.minecraft.world.World) Wizardry(com.teamwizardry.wizardry.Wizardry) EnumFacing(net.minecraft.util.EnumFacing) BlockPos(net.minecraft.util.math.BlockPos) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) java.awt(java.awt) IBlockState(net.minecraft.block.state.IBlockState) JsonArray(com.google.gson.JsonArray) List(java.util.List) NetworkRegistry(net.minecraftforge.fml.common.network.NetworkRegistry) LibParticles(com.teamwizardry.wizardry.client.fx.LibParticles) ForgeRegistries(net.minecraftforge.fml.common.registry.ForgeRegistries) ResourceLocation(net.minecraft.util.ResourceLocation) RandUtil(com.teamwizardry.wizardry.api.util.RandUtil) FluidStack(net.minecraftforge.fluids.FluidStack) FileReader(java.io.FileReader) CraftingHelper(net.minecraftforge.common.crafting.CraftingHelper) IBlockState(net.minecraft.block.state.IBlockState) PacketExplode(com.teamwizardry.wizardry.common.network.PacketExplode) FluidStack(net.minecraftforge.fluids.FluidStack) Fluid(net.minecraftforge.fluids.Fluid) Vec3d(net.minecraft.util.math.Vec3d) Ingredient(net.minecraft.item.crafting.Ingredient) Block(net.minecraft.block.Block) List(java.util.List) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) EntityItem(net.minecraft.entity.item.EntityItem)

Example 17 with Ingredient

use of net.minecraft.item.crafting.Ingredient in project BaseMetals by MinecraftModDevelopmentMods.

the class ConfigVariedOutput method parse.

@Override
public IRecipe parse(final JsonContext context, final JsonObject json) {
    String confKey = JsonUtils.getString(json, "config_key");
    int resAmount = 0;
    switch(confKey) {
        case "gear":
            resAmount = Options.gearQuantity();
            break;
        case "plate":
            resAmount = Options.plateQuantity();
            break;
        default:
            BaseMetals.logger.error("Unknown quantity config value {}, setting to 1", confKey);
            resAmount = 1;
    }
    // load the data here, map the ingredients, setup the primer and return the ShapedOreRecipe :)
    final Map<Character, Ingredient> ingMap = Maps.newHashMap();
    JsonUtils.getJsonObject(json, "key").entrySet().stream().filter(ent -> ent.getKey().length() == 1 && !ent.getKey().isEmpty()).forEach(ent -> ingMap.put(ent.getKey().toCharArray()[0], CraftingHelper.getIngredient(ent.getValue(), context)));
    ingMap.put(' ', Ingredient.EMPTY);
    JsonArray patternJ = JsonUtils.getJsonArray(json, "pattern");
    if (patternJ.size() == 0) {
        throw new JsonSyntaxException("Invalid pattern: empty pattern not allows");
    }
    final String[] pattern = new String[patternJ.size()];
    for (int x = 0; x < pattern.length; ++x) {
        final String line = JsonUtils.getString(patternJ.get(x), "pattern[" + x + "]");
        if (x > 0 && pattern[0].length() != line.length()) {
            throw new JsonSyntaxException("Invalid pattern: each row must  be the same width");
        }
        pattern[x] = line;
    }
    final ShapedPrimer primer = new ShapedPrimer();
    primer.width = pattern[0].length();
    primer.height = pattern.length;
    primer.mirrored = true;
    primer.input = NonNullList.withSize(primer.width * primer.height, Ingredient.EMPTY);
    final Set<Character> keys = Sets.newHashSet(ingMap.keySet());
    keys.remove(' ');
    int x = 0;
    for (final String line : pattern) {
        for (final char chr : line.toCharArray()) {
            final Ingredient ing = ingMap.get(chr);
            if (ing == null) {
                throw new JsonSyntaxException("Pattern references symbol '" + chr + "' but it's not defined in the key");
            }
            primer.input.set(x++, ing);
            keys.remove(chr);
        }
    }
    if (!keys.isEmpty()) {
        throw new JsonSyntaxException("Key defines symbols that aren't used in pattern: " + keys);
    }
    final String group = JsonUtils.getString(json, "group", "");
    final ItemStack result = CraftingHelper.getItemStack(JsonUtils.getJsonObject(json, "result"), context);
    result.setCount(resAmount);
    return new ShapedOreRecipe(group.isEmpty() ? null : new ResourceLocation(group), result, primer);
}
Also used : JsonObject(com.google.gson.JsonObject) IRecipe(net.minecraft.item.crafting.IRecipe) Ingredient(net.minecraft.item.crafting.Ingredient) IRecipeFactory(net.minecraftforge.common.crafting.IRecipeFactory) JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonContext(net.minecraftforge.common.crafting.JsonContext) Set(java.util.Set) ShapedOreRecipe(net.minecraftforge.oredict.ShapedOreRecipe) Maps(com.google.common.collect.Maps) Sets(com.google.common.collect.Sets) ItemStack(net.minecraft.item.ItemStack) ShapedPrimer(net.minecraftforge.common.crafting.CraftingHelper.ShapedPrimer) JsonArray(com.google.gson.JsonArray) BaseMetals(com.mcmoddev.basemetals.BaseMetals) Map(java.util.Map) JsonUtils(net.minecraft.util.JsonUtils) ResourceLocation(net.minecraft.util.ResourceLocation) NonNullList(net.minecraft.util.NonNullList) Options(com.mcmoddev.lib.util.ConfigBase.Options) CraftingHelper(net.minecraftforge.common.crafting.CraftingHelper) ShapedOreRecipe(net.minecraftforge.oredict.ShapedOreRecipe) JsonArray(com.google.gson.JsonArray) ShapedPrimer(net.minecraftforge.common.crafting.CraftingHelper.ShapedPrimer) JsonSyntaxException(com.google.gson.JsonSyntaxException) Ingredient(net.minecraft.item.crafting.Ingredient) ResourceLocation(net.minecraft.util.ResourceLocation) ItemStack(net.minecraft.item.ItemStack)

Example 18 with Ingredient

use of net.minecraft.item.crafting.Ingredient in project TechReborn by TechReborn.

the class RollingMachineRecipe method addShapelessRecipe.

public void addShapelessRecipe(ResourceLocation resourceLocation, ItemStack output, Object... components) {
    NonNullList<Ingredient> ingredients = NonNullList.create();
    for (int j = 0; j < components.length; j++) {
        ingredients.add(CraftingHelper.getIngredient(components[j]));
    }
    recipes.put(resourceLocation, new ShapelessRecipes("", output, ingredients));
}
Also used : Ingredient(net.minecraft.item.crafting.Ingredient) ShapelessRecipes(net.minecraft.item.crafting.ShapelessRecipes)

Example 19 with Ingredient

use of net.minecraft.item.crafting.Ingredient in project TechReborn by TechReborn.

the class TileAutoCraftingTable method findBestSlotForStack.

public int findBestSlotForStack(IRecipe recipe, ItemStack stack) {
    if (recipe == null) {
        return -1;
    }
    List<Integer> possibleSlots = new ArrayList<>();
    for (int i = 0; i < recipe.getIngredients().size(); i++) {
        ItemStack stackInSlot = inventory.getStackInSlot(i);
        Ingredient ingredient = recipe.getIngredients().get(i);
        if (ingredient != Ingredient.EMPTY && ingredient.apply(stack)) {
            if (stackInSlot.isEmpty()) {
                possibleSlots.add(i);
            } else if (stackInSlot.getItem() == stack.getItem() && stackInSlot.getItemDamage() == stack.getItemDamage()) {
                if (stackInSlot.getMaxStackSize() >= stackInSlot.getCount() + stack.getCount()) {
                    possibleSlots.add(i);
                }
            }
        }
    }
    // Slot, count
    Pair<Integer, Integer> smallestCount = null;
    for (Integer slot : possibleSlots) {
        ItemStack slotStack = inventory.getStackInSlot(slot);
        if (slotStack.isEmpty()) {
            return slot;
        }
        if (smallestCount == null) {
            smallestCount = Pair.of(slot, slotStack.getCount());
        } else if (smallestCount.getRight() >= slotStack.getCount()) {
            smallestCount = Pair.of(slot, slotStack.getCount());
        }
    }
    if (smallestCount != null) {
        return smallestCount.getLeft();
    }
    return -1;
}
Also used : Ingredient(net.minecraft.item.crafting.Ingredient) ArrayList(java.util.ArrayList) ItemStack(net.minecraft.item.ItemStack)

Example 20 with Ingredient

use of net.minecraft.item.crafting.Ingredient in project TechReborn by TechReborn.

the class TileAutoCraftingTable method canMake.

public boolean canMake(IRecipe recipe) {
    if (customRecipe) {
        recipe = getIRecipe();
    }
    if (recipe != null && recipe.canFit(3, 3)) {
        boolean missingOutput = false;
        int[] stacksInSlots = new int[9];
        for (int i = 0; i < 9; i++) {
            stacksInSlots[i] = inventory.getStackInSlot(i).getCount();
        }
        for (Ingredient ingredient : recipe.getIngredients()) {
            if (ingredient != Ingredient.EMPTY) {
                boolean foundIngredient = false;
                for (int i = 0; i < 9; i++) {
                    ItemStack stack = inventory.getStackInSlot(i);
                    int requiredSize = customRecipe ? 1 : 0;
                    if (stack.getMaxStackSize() == 1) {
                        requiredSize = 0;
                    }
                    if (stacksInSlots[i] > requiredSize) {
                        if (ingredient.apply(stack)) {
                            if (stack.getItem().getContainerItem() != null) {
                                if (!hasRoomForExtraItem(stack.getItem().getContainerItem(stack))) {
                                    continue;
                                }
                            }
                            foundIngredient = true;
                            stacksInSlots[i]--;
                            break;
                        }
                    }
                }
                if (!foundIngredient) {
                    missingOutput = true;
                }
            }
        }
        if (!missingOutput) {
            if (hasOutputSpace(recipe.getRecipeOutput(), 9)) {
                return true;
            }
        }
        return false;
    }
    return false;
}
Also used : Ingredient(net.minecraft.item.crafting.Ingredient) ItemStack(net.minecraft.item.ItemStack)

Aggregations

Ingredient (net.minecraft.item.crafting.Ingredient)57 ItemStack (net.minecraft.item.ItemStack)49 ResourceLocation (net.minecraft.util.ResourceLocation)12 ArrayList (java.util.ArrayList)10 JsonElement (com.google.gson.JsonElement)8 JsonObject (com.google.gson.JsonObject)7 List (java.util.List)7 IRecipe (net.minecraft.item.crafting.IRecipe)7 JsonArray (com.google.gson.JsonArray)6 Block (net.minecraft.block.Block)6 Item (net.minecraft.item.Item)6 HashMap (java.util.HashMap)5 IBlockState (net.minecraft.block.state.IBlockState)5 ShapelessRecipes (net.minecraft.item.crafting.ShapelessRecipes)5 FluidStack (net.minecraftforge.fluids.FluidStack)5 JsonParser (com.google.gson.JsonParser)4 File (java.io.File)4 FileNotFoundException (java.io.FileNotFoundException)4 FileReader (java.io.FileReader)4 EntityItem (net.minecraft.entity.item.EntityItem)4