net.minecraft.recipe.Ingredient Java Examples

The following examples show how to use net.minecraft.recipe.Ingredient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FluidRecipe.java    From the-hallow with MIT License 6 votes vote down vote up
private boolean hasRequiredIngredients(List<ItemStack> toCheck) {
	for (Ingredient ingredient : ingredients) {
		boolean hasIngredient = false;
		for (ItemStack potentialIngredient : toCheck) {
			if (ingredient.test(potentialIngredient)) {
				toCheck.remove(potentialIngredient);
				hasIngredient = true;
				break;
			}
		}

		if (!hasIngredient) {
			return false;
		}
	}

	return true;
}
 
Example #2
Source File: ShapedCompressingRecipe.java    From Galacticraft-Rewoven with MIT License 6 votes vote down vote up
private boolean matchesSmall(Inventory inv, int int_1, int int_2, boolean boolean_1) {
    int invWidth = 3;
    int invHeight = 3;

    for (int int_3 = 0; int_3 < invWidth; ++int_3) {
        for (int int_4 = 0; int_4 < invHeight; ++int_4) {
            int int_5 = int_3 - int_1;
            int int_6 = int_4 - int_2;
            Ingredient ingredient_1 = Ingredient.EMPTY;
            if (int_5 >= 0 && int_6 >= 0 && int_5 < this.width && int_6 < this.height) {
                if (boolean_1) {
                    ingredient_1 = this.ingredients.get(this.width - int_5 - 1 + int_6 * this.width);
                } else {
                    ingredient_1 = this.ingredients.get(int_5 + int_6 * this.width);
                }
            }

            if (!ingredient_1.test(inv.getStack(int_3 + int_4 * invWidth))) {
                return false;
            }
        }
    }

    return true;
}
 
Example #3
Source File: ShapedCompressingRecipe.java    From Galacticraft-Rewoven with MIT License 6 votes vote down vote up
static Map<String, Ingredient> getComponents(JsonObject jsonObject_1) {
    Map<String, Ingredient> map_1 = Maps.newHashMap();

    for (Map.Entry<String, JsonElement> entry : jsonObject_1.entrySet()) {
        if (entry.getKey().length() != 1) {
            throw new JsonSyntaxException("Invalid key entry: '" + entry.getKey() + "' is an invalid symbol (must be 1 character only).");
        }

        if (" ".equals(entry.getKey())) {
            throw new JsonSyntaxException("Invalid key entry: ' ' is a reserved symbol.");
        }

        map_1.put(entry.getKey(), Ingredient.fromJson(entry.getValue()));
    }

    map_1.put(" ", Ingredient.EMPTY);
    return map_1;
}
 
Example #4
Source File: ShapedCompressingRecipe.java    From Galacticraft-Rewoven with MIT License 6 votes vote down vote up
static DefaultedList<Ingredient> getIngredients(String[] strings_1, Map<String, Ingredient> map_1, int int_1, int int_2) {
    DefaultedList<Ingredient> defaultedList_1 = DefaultedList.ofSize(int_1 * int_2, Ingredient.EMPTY);
    Set<String> set_1 = Sets.newHashSet(map_1.keySet());
    set_1.remove(" ");

    for (int int_3 = 0; int_3 < strings_1.length; ++int_3) {
        for (int int_4 = 0; int_4 < strings_1[int_3].length(); ++int_4) {
            String string_1 = strings_1[int_3].substring(int_4, int_4 + 1);
            Ingredient ingredient_1 = map_1.get(string_1);
            if (ingredient_1 == null) {
                throw new JsonSyntaxException("Pattern references symbol '" + string_1 + "' but it's not defined in the key");
            }

            set_1.remove(string_1);
            defaultedList_1.set(int_4 + int_1 * int_3, ingredient_1);
        }
    }

    if (!set_1.isEmpty()) {
        throw new JsonSyntaxException("Key defines symbols that aren't used in pattern: " + set_1);
    } else {
        return defaultedList_1;
    }
}
 
Example #5
Source File: FluidRecipeSerializer.java    From the-hallow with MIT License 6 votes vote down vote up
@Override
public FluidRecipe read(Identifier id, JsonObject json) {
	Item result;
	int count;
	ArrayList<Ingredient> ingredients = getIngredientList(json);

	// get item result && count
	if(json.get(RESULT_KEY).isJsonObject()) {
		JsonObject resultObject = json.getAsJsonObject(RESULT_KEY);
		result = getItem(resultObject);
		count = getCount(resultObject);
	} else {
		throw new InvalidJsonException("Expected a JsonObject as \"" + RESULT_KEY + "\", got " + json.get(INPUT_KEY).getClass() + "\n" + prettyPrintJson(json));
	}

	verifyIngredientsList(ingredients, json);

	return new FluidRecipe(ingredients, new ItemStack(result, count), id, type, this);
}
 
Example #6
Source File: CraftingFluidBlock.java    From the-hallow with MIT License 5 votes vote down vote up
@Override
public void onEntityCollision(BlockState blockState, World world, BlockPos pos, Entity entity) {
	super.onEntityCollision(blockState, world, pos, entity);
	if(entity instanceof ItemEntity) {
		List<ItemEntity> entities = world.getEntities(ItemEntity.class, new Box(pos), e -> true);
		BasicInventory inventory = new BasicInventory(entities.size());
		
		entities.forEach(itemEntity -> { //required for multi-input recipes
			ItemStack stack = itemEntity.getStack();
			inventory.add(stack);
		});
		
		Optional<FluidRecipe> match = world.getRecipeManager()
			.getFirstMatch(recipeType, inventory, world);

		if (match.isPresent()) {
			spawnCraftingResult(world, pos, match.get().craft(inventory));

			for (Ingredient ingredient : match.get().getIngredients()) {
				for (ItemEntity testEntity : entities) {
					if (ingredient.test(testEntity.getStack())) {
						testEntity.getStack().decrement(1);
						break;
					}
				}
			}
		}
	}
}
 
Example #7
Source File: HallowTweaker.java    From the-hallow with MIT License 5 votes vote down vote up
public void addInfusion(Object target, Object input, Object output) {
	try {
		ItemStack stack = RecipeParser.processItemStack(output);
		Identifier id = tweaker.getRecipeId(stack);
		Ingredient targ = RecipeParser.processIngredient(target);
		Ingredient in = RecipeParser.processIngredient(input);
		tweaker.addRecipe(new InfusionRecipe(id, targ, in, stack));
	} catch (Exception e) {
		tweaker.getLogger().error("Error parsing hallowed infusion recipe - " + e.getMessage());
	}
}
 
Example #8
Source File: HallowTweaker.java    From the-hallow with MIT License 5 votes vote down vote up
public void addWitchWater(Object[] input, Object output) {
	try {
		ItemStack stack = RecipeParser.processItemStack(output);
		Identifier id = tweaker.getRecipeId(stack);
		List<Ingredient> ingredients = new ArrayList<>();
		for (Object obj : input) {
			ingredients.add(RecipeParser.processIngredient(obj));
		}
		tweaker.addRecipe(new FluidRecipe(ingredients, stack, id, FluidRecipe.Type.WITCH_WATER, FluidRecipeSerializer.WITCH_WATER));
	} catch (Exception e) {
		tweaker.getLogger().error("Error parsing witch water recipe - " + e.getMessage());
	}
}
 
Example #9
Source File: HallowTweaker.java    From the-hallow with MIT License 5 votes vote down vote up
public void addBlood(Object[] input, Object output) {
	try {
		ItemStack stack = RecipeParser.processItemStack(output);
		Identifier id = tweaker.getRecipeId(stack);
		List<Ingredient> ingredients = new ArrayList<>();
		for (Object obj : input) {
			ingredients.add(RecipeParser.processIngredient(obj));
		}
		tweaker.addRecipe(new FluidRecipe(ingredients, stack, id, FluidRecipe.Type.BLOOD, FluidRecipeSerializer.BLOOD));
	} catch (Exception e) {
		tweaker.getLogger().error("Error parsing blood recipe - " + e.getMessage());
	}
}
 
Example #10
Source File: FluidRecipe.java    From the-hallow with MIT License 5 votes vote down vote up
public FluidRecipe(List<Ingredient> ingredients, ItemStack result, Identifier recipeId, Type type, FluidRecipeSerializer serializer) {
	this.ingredients = ingredients;
	this.result = result;
	this.recipeId = recipeId;
	this.type = type;
	this.serializer = serializer;
}
 
Example #11
Source File: FluidRecipeSerializer.java    From the-hallow with MIT License 5 votes vote down vote up
/**
 * Retrieves a list of required {@link Ingredient}s from the given JsonObject.
 * If the JsonObject doesn't have a proper list, an exception is thrown.
 *
 * @param json JsonObject to take Ingredient list from
 * @return list of Ingredients required for the recipe
 */
private ArrayList<Ingredient> getIngredientList(JsonObject json) {
	ArrayList<Ingredient> ingredients = new ArrayList<>();
	if(json.get(INPUT_KEY).isJsonArray()) {
		JsonArray inputArray = json.get(INPUT_KEY).getAsJsonArray();
		inputArray.forEach(jsonElement -> ingredients.add(Ingredient.fromJson(jsonElement)));
	} else {
		throw new InvalidJsonException("Expected a JsonArray as \"input\", got " + json.get(INPUT_KEY).getClass() + "\n" + prettyPrintJson(json));
	}

	return ingredients;
}
 
Example #12
Source File: CrowEntity.java    From the-hallow with MIT License 5 votes vote down vote up
@Override
protected void initGoals() {
	this.goalSelector.add(0, new EscapeDangerGoal(this, 1.25D));
	this.goalSelector.add(0, new SwimGoal(this));
	this.goalSelector.add(1, new LookAtEntityGoal(this, PlayerEntity.class, 8.0F));
	this.goalSelector.add(2, new EatBreadcrumbsGoal(HallowedBlocks.BREAD_CRUMBS, this, 1.25D, 40, 80));
	this.goalSelector.add(2, new TemptBirdGoal(this, 1.25D, false, Ingredient.ofItems(HallowedBlocks.BREAD_CRUMBS)));
	this.goalSelector.add(3, new FollowMobGoal(this, 1.0D, 3.0F, 7.0F));
}
 
Example #13
Source File: ShapelessCompressingRecipeSerializer.java    From Galacticraft-Rewoven with MIT License 5 votes vote down vote up
@Override
    public T read(Identifier id, JsonObject json) {
//            String group = JsonHelper.getString(json, "group", "");
        DefaultedList<Ingredient> ingredients = getIngredients(JsonHelper.getArray(json, "ingredients"));
        if (ingredients.isEmpty()) {
            throw new JsonParseException("No ingredients for compressing recipe");
        } else if (ingredients.size() > 9) {
            throw new JsonParseException("Too many ingredients for compressing recipe");
        } else {
            ItemStack result = ShapelessCompressingRecipe.getStack(JsonHelper.getObject(json, "result"));
            return factory.create(id, /*group, */result, ingredients);
        }
    }
 
Example #14
Source File: PumpcownEntity.java    From the-hallow with MIT License 5 votes vote down vote up
@Override
protected void initGoals() {
	this.goalSelector.add(0, new SwimGoal(this));
	this.goalSelector.add(1, new EscapeDangerGoal(this, 2.0D));
	this.goalSelector.add(2, new AnimalMateGoal(this, 1.0D));
	this.goalSelector.add(3, new TemptGoal(this, 1.25D, Ingredient.ofItems(Items.PUMPKIN_PIE), false));
	this.goalSelector.add(4, new FollowParentGoal(this, 1.25D));
	this.goalSelector.add(5, new WanderAroundFarGoal(this, 1.0D));
	this.goalSelector.add(6, new LookAtEntityGoal(this, PlayerEntity.class, 6.0F));
	this.goalSelector.add(7, new LookAroundGoal(this));
}
 
Example #15
Source File: TemptBirdGoal.java    From the-hallow with MIT License 5 votes vote down vote up
public TemptBirdGoal(MobEntityWithAi mobEntityWithAi, double speed, boolean canBeScared, Ingredient food) {
	this.mob = mobEntityWithAi;
	this.speed = speed;
	this.food = food;
	this.canBeScared = canBeScared;
	this.setControls(EnumSet.of(Goal.Control.MOVE, Goal.Control.LOOK));
	if (!(mobEntityWithAi.getNavigation() instanceof BirdNavigation)) {
		throw new IllegalArgumentException("Unsupported mob type for TemptBirdGoal");
	}
}
 
Example #16
Source File: FluidRecipeSerializer.java    From the-hallow with MIT License 5 votes vote down vote up
@Override
public FluidRecipe read(Identifier id, PacketByteBuf buf) {
	int size = buf.readInt();

	ArrayList<Ingredient> ingredients = new ArrayList<>();
	for(int i = 0; i < size; i++) {
		ingredients.add(Ingredient.fromPacket(buf));
	}

	return new FluidRecipe(ingredients, buf.readItemStack(), id, type, this);
}
 
Example #17
Source File: FabricationRecipeSerializer.java    From Galacticraft-Rewoven with MIT License 5 votes vote down vote up
@Override
public T read(Identifier id, JsonObject json) {
    String group = JsonHelper.getString(json, "group", "");
    Ingredient inputIngredient;
    if (JsonHelper.hasArray(json, "ingredient")) {
        inputIngredient = Ingredient.fromJson(JsonHelper.getArray(json, "ingredient"));
    } else {
        inputIngredient = Ingredient.fromJson(JsonHelper.getObject(json, "ingredient"));
    }

    String result = JsonHelper.getString(json, "result");
    int count = JsonHelper.getInt(json, "count");
    ItemStack outputItem = new ItemStack(Registry.ITEM.get(new Identifier(result)), count);
    return this.recipeFactory.create(id, group, inputIngredient, outputItem);
}
 
Example #18
Source File: FabricationRecipeSerializer.java    From Galacticraft-Rewoven with MIT License 5 votes vote down vote up
@Override
public T read(Identifier id, PacketByteBuf packet) {
    String string_1 = packet.readString(32767);
    Ingredient ingredient_1 = Ingredient.fromPacket(packet);
    ItemStack itemStack_1 = packet.readItemStack();
    return this.recipeFactory.create(id, string_1, ingredient_1, itemStack_1);
}
 
Example #19
Source File: ShapelessCompressingRecipeSerializer.java    From Galacticraft-Rewoven with MIT License 5 votes vote down vote up
@Override
    public T read(Identifier id, PacketByteBuf packet) {
//            String group = packet.readString(32767);
        int ingredientCount = packet.readVarInt();
        DefaultedList<Ingredient> ingredients = DefaultedList.ofSize(ingredientCount, Ingredient.EMPTY);

        for (int index = 0; index < ingredients.size(); ++index) {
            ingredients.set(index, Ingredient.fromPacket(packet));
        }

        ItemStack result = packet.readItemStack();
        return factory.create(id, /*group, */result, ingredients);
    }
 
Example #20
Source File: ShapelessCompressingRecipeSerializer.java    From Galacticraft-Rewoven with MIT License 5 votes vote down vote up
@Override
    public void write(PacketByteBuf packet, ShapelessCompressingRecipe recipe) {
//            packet.writeString(recipe.group);
        packet.writeVarInt(recipe.getInput().size());

        for (Ingredient ingredient : recipe.getInput()) {
            ingredient.write(packet);
        }

        packet.writeItemStack(recipe.getOutput());
    }
 
Example #21
Source File: ShapelessCompressingRecipeSerializer.java    From Galacticraft-Rewoven with MIT License 5 votes vote down vote up
private static DefaultedList<Ingredient> getIngredients(JsonArray jsonArray_1) {
    DefaultedList<Ingredient> defaultedList_1 = DefaultedList.of();

    for (int int_1 = 0; int_1 < jsonArray_1.size(); ++int_1) {
        Ingredient ingredient_1 = Ingredient.fromJson(jsonArray_1.get(int_1));
        if (!ingredient_1.isEmpty()) {
            defaultedList_1.add(ingredient_1);
        }
    }

    return defaultedList_1;
}
 
Example #22
Source File: ShapedCompressingRecipe.java    From Galacticraft-Rewoven with MIT License 5 votes vote down vote up
public ShapedCompressingRecipe(Identifier id, String group, int width, int height, DefaultedList<Ingredient> ingredients, ItemStack output) {
    this.id = id;
    this.group = group;
    this.width = width;
    this.height = height;
    this.ingredients = ingredients;
    this.output = output;
}
 
Example #23
Source File: InfusionRecipeSerializer.java    From the-hallow with MIT License 5 votes vote down vote up
public static Ingredient fromJson(@Nullable JsonElement jsonElement) {
	List<ItemStack> arrayStacks = new ArrayList<ItemStack>();
	JsonArray jsonArray = jsonElement.getAsJsonArray();
	jsonArray.forEach((element) -> {
		JsonObject inputObject = element.getAsJsonObject();
		if (inputObject.size() == 1) {
			arrayStacks.add(new ItemStack(Registry.ITEM.getOrEmpty(new Identifier(inputObject.get("item").getAsString())).get()));
		} else {
			arrayStacks.add(new ItemStack(Registry.ITEM.getOrEmpty(new Identifier(inputObject.get("item").getAsString())).get(), inputObject.get("count").getAsInt()));
		}
	});
	return Ingredient.ofStacks(Iterables.toArray(arrayStacks, ItemStack.class));
}
 
Example #24
Source File: ShapedCompressingRecipeSerializer.java    From Galacticraft-Rewoven with MIT License 5 votes vote down vote up
@Override
public void write(PacketByteBuf packet, T recipe) {
    packet.writeVarInt(recipe.getWidth());
    packet.writeVarInt(recipe.getHeight());
    packet.writeString(recipe.group);

    for (Ingredient ingredient_1 : recipe.getIngredients()) {
        ingredient_1.write(packet);
    }

    packet.writeItemStack(recipe.getOutput());
}
 
Example #25
Source File: ShapedCompressingRecipeSerializer.java    From Galacticraft-Rewoven with MIT License 5 votes vote down vote up
@Override
public T read(Identifier identifier_1, PacketByteBuf packet) {
    int int_1 = packet.readVarInt();
    int int_2 = packet.readVarInt();
    String group = packet.readString(32767);
    DefaultedList<Ingredient> defaultedList_1 = DefaultedList.ofSize(int_1 * int_2, Ingredient.EMPTY);

    for (int int_3 = 0; int_3 < defaultedList_1.size(); ++int_3) {
        defaultedList_1.set(int_3, Ingredient.fromPacket(packet));
    }

    ItemStack itemStack_1 = packet.readItemStack();
    return factory.create(identifier_1, group, int_1, int_2, defaultedList_1, itemStack_1);
}
 
Example #26
Source File: ShapedCompressingRecipeSerializer.java    From Galacticraft-Rewoven with MIT License 5 votes vote down vote up
@Override
public T read(Identifier identifier_1, JsonObject jsonObject_1) {
    String string_1 = JsonHelper.getString(jsonObject_1, "group", "");
    Map<String, Ingredient> map_1 = ShapedCompressingRecipe.getComponents(JsonHelper.getObject(jsonObject_1, "key"));
    String[] strings_1 = ShapedCompressingRecipe.combinePattern(ShapedCompressingRecipe.getPattern(JsonHelper.getArray(jsonObject_1, "pattern")));
    int int_1 = strings_1[0].length();
    int int_2 = strings_1.length;
    DefaultedList<Ingredient> defaultedList_1 = ShapedCompressingRecipe.getIngredients(strings_1, map_1, int_1, int_2);
    ItemStack itemStack_1 = ShapedRecipe.getItemStack(JsonHelper.getObject(jsonObject_1, "result"));
    return factory.create(identifier_1, string_1, int_1, int_2, defaultedList_1, itemStack_1);
}
 
Example #27
Source File: InfusionRecipe.java    From the-hallow with MIT License 4 votes vote down vote up
public InfusionRecipe(Identifier id, Ingredient target, Ingredient input, ItemStack output) {
	this.id = id;
	this.target = target;
	this.input = input;
	this.output = output;
}
 
Example #28
Source File: Ingredient_scarpetMixin.java    From fabric-carpet with MIT License 4 votes vote down vote up
@Override
public List<Collection<ItemStack>> getRecipeStacks()
{
    List<Collection<ItemStack>> res = Arrays.stream(entries).map(Ingredient.Entry::getStacks).collect(Collectors.toList());
    return res;
}
 
Example #29
Source File: InfusionRecipe.java    From the-hallow with MIT License 4 votes vote down vote up
public Ingredient getTarget() {
	return target;
}
 
Example #30
Source File: InfusionRecipe.java    From the-hallow with MIT License 4 votes vote down vote up
public Ingredient getInput() {
	return input;
}