Search in sources :

Example 11 with Ingredient

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

the class RecipeShapedFluid method getRemainingItems.

@Override
public NonNullList<ItemStack> getRemainingItems(InventoryCrafting inv) {
    NonNullList<ItemStack> remains = ForgeHooks.defaultRecipeGetRemainingItems(inv);
    for (int i = 0; i < height * width; i++) {
        ItemStack stack = inv.getStackInSlot(i);
        NonNullList<Ingredient> matchedIngredients = this.input;
        if (matchedIngredients.get(i) instanceof IngredientFluidStack) {
            if (!stack.isEmpty()) {
                ItemStack copy = stack.copy();
                copy.setCount(1);
                remains.set(i, copy);
            }
            IFluidHandlerItem handler = FluidUtil.getFluidHandler(remains.get(i));
            if (handler != null) {
                FluidStack fluid = ((IngredientFluidStack) matchedIngredients.get(i)).getFluid();
                handler.drain(fluid.amount, true);
                remains.set(i, handler.getContainer());
            }
        }
    }
    return remains;
}
Also used : Ingredient(net.minecraft.item.crafting.Ingredient) IFluidHandlerItem(net.minecraftforge.fluids.capability.IFluidHandlerItem) FluidStack(net.minecraftforge.fluids.FluidStack) ItemStack(net.minecraft.item.ItemStack)

Example 12 with Ingredient

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

the class RecipeShapelessFluid method matches.

@Override
public boolean matches(InventoryCrafting matrix, World world) {
    ArrayList<Ingredient> required = new ArrayList<>(getIngredients());
    for (int i = 0; i < matrix.getSizeInventory(); i++) {
        ItemStack slot = matrix.getStackInSlot(i);
        if (!slot.isEmpty()) {
            boolean inRecipe = false;
            Iterator<Ingredient> iterator = required.iterator();
            while (iterator.hasNext()) {
                Ingredient next = iterator.next();
                if (next.apply(slot)) {
                    inRecipe = true;
                    iterator.remove();
                    break;
                }
            }
            if (!inRecipe)
                return false;
        }
    }
    return required.isEmpty();
}
Also used : Ingredient(net.minecraft.item.crafting.Ingredient) ArrayList(java.util.ArrayList) ItemStack(net.minecraft.item.ItemStack)

Example 13 with Ingredient

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

the class RecipeShapelessFluidFactory method parse.

@Override
public IRecipe parse(JsonContext context, JsonObject json) {
    String group = JsonUtils.getString(json, "group", "");
    NonNullList<Ingredient> ingredients = NonNullList.create();
    for (JsonElement element : JsonUtils.getJsonArray(json, "ingredients")) ingredients.add(CraftingHelper.getIngredient(element, context));
    if (ingredients.isEmpty())
        throw new JsonParseException("No ingredients in shapeless recipe");
    ItemStack result = CraftingHelper.getItemStack(JsonUtils.getJsonObject(json, "result"), context);
    RecipeShapelessFluid recipe = new RecipeShapelessFluid(group.isEmpty() ? null : new ResourceLocation(group), result, ingredients);
    return recipe;
}
Also used : Ingredient(net.minecraft.item.crafting.Ingredient) JsonElement(com.google.gson.JsonElement) ResourceLocation(net.minecraft.util.ResourceLocation) JsonParseException(com.google.gson.JsonParseException) ItemStack(net.minecraft.item.ItemStack)

Example 14 with Ingredient

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

the class FireRecipeLoader method processRecipes.

public void processRecipes(Map<Ingredient, FireRecipe> recipes) {
    Wizardry.logger.info("<<========================================================================>>");
    Wizardry.logger.info("> Starting fire recipe loading.");
    JsonContext context = new JsonContext("minecraft");
    LinkedList<File> recipeFiles = new LinkedList<>();
    Stack<File> toProcess = new Stack<>();
    toProcess.push(directory);
    while (!toProcess.isEmpty()) {
        File file = toProcess.pop();
        if (file.isDirectory()) {
            File[] children = file.listFiles();
            if (children != null)
                for (File child : children) toProcess.push(child);
        } else if (file.isFile())
            if (file.getName().endsWith(".json"))
                recipeFiles.add(file);
    }
    for (File file : recipeFiles) {
        try {
            if (!file.exists()) {
                Wizardry.logger.error("  > SOMETHING WENT WRONG! " + file.getPath() + " can NOT be found. Ignoring file...");
                continue;
            }
            JsonElement element;
            try {
                element = new JsonParser().parse(new FileReader(file));
            } catch (FileNotFoundException e) {
                Wizardry.logger.error("  > SOMETHING WENT WRONG! " + file.getPath() + " can NOT be found. Ignoring file...");
                continue;
            }
            if (element == null) {
                Wizardry.logger.error("  > SOMETHING WENT WRONG! Could not parse " + file.getPath() + ". Ignoring file...");
                continue;
            }
            if (!element.isJsonObject()) {
                Wizardry.logger.error("  > WARNING! " + file.getPath() + " does NOT contain a JsonObject. Ignoring file...: " + element.toString());
                continue;
            }
            JsonObject fileObject = element.getAsJsonObject();
            int duration = 200;
            if (!fileObject.has("input")) {
                Wizardry.logger.error("  > WARNING! " + file.getPath() + " does NOT provide an initial input item. Ignoring file...: " + element.toString());
                continue;
            }
            JsonElement inputObject = fileObject.get("input");
            Ingredient input = CraftingHelper.getIngredient(inputObject, context);
            if (input == Ingredient.EMPTY) {
                Wizardry.logger.error("  > WARNING! " + file.getPath() + " does NOT provide a valid input. Ignoring file...: " + element.toString());
                continue;
            }
            if (!fileObject.has("output")) {
                Wizardry.logger.error("  > WARNING! " + file.getPath() + " does NOT specify a recipe output. Ignoring file...: " + element.toString());
                continue;
            }
            if (!fileObject.get("output").isJsonObject()) {
                Wizardry.logger.error("  > WARNING! " + file.getPath() + " does NOT provide a valid output. Ignoring file...: " + element.toString());
                continue;
            }
            JsonObject outputObject = fileObject.get("output").getAsJsonObject();
            ItemStack output = CraftingHelper.getItemStack(outputObject, context);
            if (output.isEmpty()) {
                Wizardry.logger.error("  > WARNING! " + file.getPath() + " does NOT provide a valid output. Ignoring file...: " + element.toString());
                continue;
            }
            if (fileObject.has("duration")) {
                if (!fileObject.get("duration").isJsonPrimitive() || !fileObject.getAsJsonPrimitive("duration").isNumber()) {
                    Wizardry.logger.error("  > WARNING! " + file.getPath() + " does NOT give duration as a number. Ignoring file...:" + element.toString());
                    continue;
                }
                duration = fileObject.get("duration").getAsInt();
            }
            recipes.put(input, new FireRecipe(output, duration));
        } catch (JsonParseException jsonException) {
            Wizardry.logger.error("  > WARNING! Skipping " + file.getPath() + " due to error: ", jsonException);
        }
    }
    Wizardry.logger.info("> Finished fire recipe loading.");
    Wizardry.logger.info("<<========================================================================>>");
}
Also used : FileNotFoundException(java.io.FileNotFoundException) JsonObject(com.google.gson.JsonObject) JsonParseException(com.google.gson.JsonParseException) LinkedList(java.util.LinkedList) Stack(java.util.Stack) ItemStack(net.minecraft.item.ItemStack) JsonContext(net.minecraftforge.common.crafting.JsonContext) Ingredient(net.minecraft.item.crafting.Ingredient) JsonElement(com.google.gson.JsonElement) FileReader(java.io.FileReader) ItemStack(net.minecraft.item.ItemStack) File(java.io.File) JsonParser(com.google.gson.JsonParser)

Example 15 with Ingredient

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

the class FluidRecipeLoader method buildFluidCrafter.

private FluidCrafter buildFluidCrafter(String identifier, ItemStack outputItem, Ingredient input, List<Ingredient> extraInputs, Fluid fluid, int duration, int required, boolean consume, boolean explode, boolean bubbling, boolean harp) {
    Ingredient outputIngredient = Ingredient.fromStacks(outputItem);
    List<Ingredient> inputs = Lists.newArrayList(extraInputs);
    return 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) && !outputIngredient.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()) && !outputIngredient.apply(entity.getItem())) {
                    entity.getItem().shrink(1);
                    if (entity.getItem().isEmpty())
                        entity.setDead();
                }
            }
        }
        EntityItem output = new EntityItem(world, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, outputItem.copy());
        output.motionX = 0;
        output.motionY = 0;
        output.motionZ = 0;
        output.forceSpawn = true;
        world.spawnEntity(output);
        if (explode) {
            PacketHandler.NETWORK.sendToAllAround(new PacketExplode(output.getPositionVector(), Color.CYAN, Color.BLUE, 0.9, 2, 500, 100, 50, true), new NetworkRegistry.TargetPoint(world.provider.getDimension(), output.posX, output.posY, output.posZ, 256));
            PosUtils.boom(world, output.getPositionVector(), output, 3, false);
        }
        if (harp)
            world.playSound(null, output.posX, output.posY, output.posZ, ModSounds.HARP1, SoundCategory.BLOCKS, 0.3F, 1.0F);
    }, identifier, duration).setInputs(input, inputs).setOutput(outputItem).setDoesConsume(consume).setRequired(required).setFluid(fluid);
}
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) 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)

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