Skip to content

Commit

Permalink
Progress #11 Added input item support for Brew Kettle Recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
Alatyami committed Nov 5, 2023
1 parent 3ba2e58 commit 037628c
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ public void tick(Level level, BlockPos blockPos, BlockState blockState, BrewKett
} else {
this.resetTickClock();
}
} else {
this.resetTickClock();
}
} else {
// Do nothing on the client side.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;

public class BrewKettleRecipeCategory implements IRecipeCategory<BrewKettleRecipe> {

Expand Down Expand Up @@ -85,9 +86,11 @@ public void setRecipe(IRecipeLayoutBuilder builder, BrewKettleRecipe recipe, IFo
.addItemStack(new ItemStack(GrowthcraftCellarItems.BREW_KETTLE_LID.get()));
}

// Input Item
builder.addSlot(RecipeIngredientRole.INPUT, 70, 25)
.addItemStack(recipe.getInputItemStack());
// Input Item with Tag Support
for (Ingredient ingredient : recipe.getIngredients()) {
builder.addSlot(RecipeIngredientRole.INPUT, 70, 25)
.addIngredients(ingredient);
}

// TODO: Input Fluid
builder.addSlot(RecipeIngredientRole.INPUT, 36, 7)
Expand Down
53 changes: 40 additions & 13 deletions src/main/java/growthcraft/cellar/recipe/BrewKettleRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import growthcraft.cellar.GrowthcraftCellar;
import growthcraft.cellar.shared.Reference;
import growthcraft.lib.utils.CraftingUtils;
import net.minecraft.core.NonNullList;
import net.minecraft.core.RegistryAccess;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.item.crafting.RecipeType;
Expand All @@ -19,23 +21,25 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;

public class BrewKettleRecipe implements Recipe<SimpleContainer> {

private final ResourceLocation recipeId;
private final FluidStack inputFluidStack;
private final ItemStack inputItemStack;
private final NonNullList<Ingredient> inputIngredients;

private final FluidStack outputFluidStack;
private final ItemStack byProduct;
private final boolean requiresLid;
private final boolean requiresHeat;
private final int byProductChance;
private final int processingTime;


public BrewKettleRecipe(ResourceLocation recipeId, FluidStack inputFluidStack, ItemStack inputItem, FluidStack outputFluidStack, ItemStack byProduct, int byProductChance, int processingTime, boolean requiresHeat, boolean requiresLid) {
public BrewKettleRecipe(ResourceLocation recipeId, FluidStack inputFluidStack, NonNullList<Ingredient> inputIngredients, FluidStack outputFluidStack, ItemStack byProduct, int byProductChance, int processingTime, boolean requiresHeat, boolean requiresLid) {
this.recipeId = recipeId;
this.inputFluidStack = inputFluidStack;
this.inputItemStack = inputItem;
this.inputIngredients = inputIngredients;
this.outputFluidStack = outputFluidStack;
this.byProduct = byProduct;
this.processingTime = processingTime;
Expand All @@ -51,14 +55,18 @@ public boolean matches(SimpleContainer container, Level level) {

public boolean matches(ItemStack itemStack, FluidStack fluidStack, boolean needsLid, boolean needsHeat) {

boolean inputItemTypeMatches = this.inputItemStack.getItem() == itemStack.getItem();
boolean inputItemCountLessThan = this.inputItemStack.getCount() <= itemStack.getCount();
boolean inputItemTypeMatches = Arrays.stream(
this.inputIngredients.get(0).getItems()).anyMatch(
ingredientItem -> ingredientItem.is(itemStack.getItem()) && ingredientItem.getCount() <= itemStack.getCount()
);

//boolean inputItemCountLessThan = this.inputIngredients.get(0).getItems()[0].getCount() <= itemStack.getCount();
boolean inputFluidTypeMatches = this.inputFluidStack.getFluid() == fluidStack.getFluid();
boolean inputFluidAmountLessThan = this.inputFluidStack.getAmount() <= fluidStack.getAmount();
boolean hasRequiredLid = this.requiresLid == needsLid && this.requiresHeat == needsHeat;

return inputItemTypeMatches
&& inputItemCountLessThan
//&& inputItemCountLessThan
&& inputFluidTypeMatches
&& inputFluidAmountLessThan
&& hasRequiredLid;
Expand Down Expand Up @@ -93,7 +101,12 @@ public FluidStack getOutputFluidStack() {
}

public ItemStack getInputItemStack() {
return inputItemStack;
return inputIngredients.get(0).getItems()[0];
}

@Override
public @NotNull NonNullList<Ingredient> getIngredients() {
return this.inputIngredients;
}

public ItemStack getByProduct() {
Expand Down Expand Up @@ -144,7 +157,8 @@ public static class Serializer implements RecipeSerializer<BrewKettleRecipe> {
@Override
public @NotNull BrewKettleRecipe fromJson(ResourceLocation recipeId, JsonObject json) {

ItemStack inputItemStack = CraftingHelper.getItemStack(GsonHelper.getAsJsonObject(json, "input_item"), false);
NonNullList<Ingredient> inputIngredient = CraftingUtils.readIngredient(GsonHelper.getAsJsonObject(json, "input_item"));

ItemStack byProductItemStack = CraftingHelper.getItemStack(GsonHelper.getAsJsonObject(json, "by_product"), false);

FluidStack inputFluid = CraftingUtils.getFluidStack(GsonHelper.getAsJsonObject(json, "input_fluid"));
Expand All @@ -156,15 +170,23 @@ public static class Serializer implements RecipeSerializer<BrewKettleRecipe> {
int processingTime = GsonHelper.getAsInt(json, "processing_time", 600);
int byProductChance = GsonHelper.getAsInt(json, "by_product_chance", 10);

return new BrewKettleRecipe(recipeId, inputFluid, inputItemStack,
return new BrewKettleRecipe(recipeId, inputFluid, inputIngredient,
outputFluid, byProductItemStack, byProductChance, processingTime,
requiresHeat, requiresLid);
}

@Override
public @Nullable BrewKettleRecipe fromNetwork(ResourceLocation recipeId, FriendlyByteBuf buffer) {
try {
ItemStack inputItemStack = buffer.readItem();

int i = buffer.readVarInt();
NonNullList<Ingredient> inputIngredients = NonNullList.withSize(i, Ingredient.EMPTY);

for (int j = 0; j < inputIngredients.size(); ++j) {
inputIngredients.set(j, Ingredient.fromNetwork(buffer));

}

ItemStack byProductItemStack = buffer.readItem();
FluidStack inputFluidStack = buffer.readFluidStack();
FluidStack outputFluidStack = buffer.readFluidStack();
Expand All @@ -173,7 +195,7 @@ public static class Serializer implements RecipeSerializer<BrewKettleRecipe> {
int processingTime = buffer.readVarInt();
int byProductChance = buffer.readVarInt();

return new BrewKettleRecipe(recipeId, inputFluidStack, inputItemStack,
return new BrewKettleRecipe(recipeId, inputFluidStack, inputIngredients,
outputFluidStack, byProductItemStack, byProductChance, processingTime,
requiresHeat, requiresLid);
} catch (Exception ex) {
Expand All @@ -185,7 +207,12 @@ public static class Serializer implements RecipeSerializer<BrewKettleRecipe> {

@Override
public void toNetwork(FriendlyByteBuf buffer, BrewKettleRecipe recipe) {
buffer.writeItemStack(recipe.getInputItemStack(), false);
buffer.writeVarInt(recipe.inputIngredients.size());

for (Ingredient ingredient : recipe.getIngredients()) {
ingredient.toNetwork(buffer);
}

buffer.writeItemStack(recipe.getByProduct(), false);
buffer.writeFluidStack(recipe.getInputFluidStack());
buffer.writeFluidStack(recipe.getOutputFluidStack());
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/growthcraft/lib/utils/CraftingUtils.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package growthcraft.lib.utils;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import net.minecraft.core.NonNullList;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.level.material.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.registries.ForgeRegistries;
Expand All @@ -23,5 +26,24 @@ public static FluidStack getFluidStack(JsonObject json) {
return new FluidStack(fluid, fluidAmount);
}

public static NonNullList<Ingredient> readIngredient(JsonObject ingredientObject) {
NonNullList<Ingredient> nonnulllist = NonNullList.create();
nonnulllist.add(Ingredient.fromJson(ingredientObject));
return nonnulllist;
}

public static NonNullList<Ingredient> readIngredients(JsonArray ingredientArray) {
NonNullList<Ingredient> nonnulllist = NonNullList.create();

for (int i = 0; i < ingredientArray.size(); ++i) {
Ingredient ingredient = Ingredient.fromJson(ingredientArray.get(i));
if (!ingredient.isEmpty()) {
nonnulllist.add(ingredient);
}
}

return nonnulllist;
}

private CraftingUtils() { /* Prevent automatic public constructor */ }
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
{
"type": "growthcraft_cellar:brew_kettle_recipe",
"requires_heat": true,
"requires_lid": "false",
"requires_lid": "true",
"processing_time": 300,
"by_product_chance": 1,
"by_product_chance": 100,
"input_fluid": {
"fluid": "minecraft:water",
"amount": 125
},
"input_item": {
"item": "growthcraft_rice:rice",
"tag": "forge:grain/rice",
"count": 1
},
"output_fluid": {
"fluid": "growthcraft_rice:rice_water_fluid_source",
"amount": 125
},
"by_product": {
"item": "minecraft:bone_meal",
"item": "growthcraft_rice:rice_cooked",
"count": 1
}
}

0 comments on commit 037628c

Please sign in to comment.