Created
July 19, 2019 21:50
-
-
Save YanisBft/d9a7e087d721f84cd1c0f5212b5f7039 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.yanis48.minalchemy.recipe; | |
import com.yanis48.minalchemy.init.MinalchemyBlocks; | |
import com.yanis48.minalchemy.init.MinalchemyRecipes; | |
import net.fabricmc.api.EnvType; | |
import net.fabricmc.api.Environment; | |
import net.minecraft.inventory.Inventory; | |
import net.minecraft.item.ItemStack; | |
import net.minecraft.recipe.Ingredient; | |
import net.minecraft.recipe.Recipe; | |
import net.minecraft.recipe.RecipeFinder; | |
import net.minecraft.recipe.RecipeSerializer; | |
import net.minecraft.recipe.RecipeType; | |
import net.minecraft.util.DefaultedList; | |
import net.minecraft.util.Identifier; | |
import net.minecraft.world.World; | |
public class AlchemyRecipe implements Recipe<Inventory> { | |
private final Identifier id; | |
private final String group; | |
private final ItemStack output; | |
protected final DefaultedList<Ingredient> ingredients; | |
public AlchemyRecipe(Identifier id, String group, ItemStack output, DefaultedList<Ingredient> ingredients) { | |
this.id = id; | |
this.group = group; | |
this.output = output; | |
this.ingredients = ingredients; | |
} | |
@Override | |
public boolean matches(Inventory inv, World world) { | |
RecipeFinder finder = new RecipeFinder(); | |
return finder.findRecipe(this, null); | |
} | |
@Override | |
public ItemStack craft(Inventory inv) { | |
return this.output.copy(); | |
} | |
@Override | |
public boolean fits(int x, int y) { | |
return true; | |
} | |
@Override | |
public ItemStack getOutput() { | |
return this.output; | |
} | |
@Override | |
public Identifier getId() { | |
return this.id; | |
} | |
@Override | |
public DefaultedList<Ingredient> getPreviewInputs() { | |
return ingredients; | |
} | |
@Environment(EnvType.CLIENT) | |
public String getGroup() { | |
return this.group; | |
} | |
@Environment(EnvType.CLIENT) | |
public ItemStack getRecipeKindIcon() { | |
return new ItemStack(MinalchemyBlocks.ALCHEMY_TABLE); | |
} | |
@Override | |
public RecipeSerializer<?> getSerializer() { | |
return MinalchemyRecipes.ALCHEMY_SERIALIZER; | |
} | |
@Override | |
public RecipeType<?> getType() { | |
return MinalchemyRecipes.ALCHEMY; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.yanis48.minalchemy.recipe; | |
import com.google.gson.JsonArray; | |
import com.google.gson.JsonObject; | |
import net.minecraft.item.ItemStack; | |
import net.minecraft.recipe.Ingredient; | |
import net.minecraft.recipe.RecipeSerializer; | |
import net.minecraft.recipe.ShapedRecipe; | |
import net.minecraft.util.DefaultedList; | |
import net.minecraft.util.Identifier; | |
import net.minecraft.util.JsonHelper; | |
import net.minecraft.util.PacketByteBuf; | |
public class AlchemyRecipeSerializer implements RecipeSerializer<AlchemyRecipe> { | |
@Override | |
public AlchemyRecipe read(Identifier id, JsonObject json) { | |
String group = JsonHelper.getString(json, "group", ""); | |
DefaultedList<Ingredient> ingredients = getIngredients(JsonHelper.getArray(json, "ingredients")); | |
ItemStack result = ShapedRecipe.getItemStack(JsonHelper.getObject(json, "result")); | |
return new AlchemyRecipe(id, group, result, ingredients); | |
} | |
@Override | |
public AlchemyRecipe read(Identifier id, PacketByteBuf buf) { | |
String group = buf.readString(32767); | |
int ingredientSize = buf.readVarInt(); | |
DefaultedList<Ingredient> ingredients = DefaultedList.ofSize(ingredientSize, Ingredient.EMPTY); | |
for(int i = 0; i < ingredients.size(); i++) { | |
ingredients.set(i, Ingredient.fromPacket(buf)); | |
} | |
ItemStack result = buf.readItemStack(); | |
return new AlchemyRecipe(id, group, result, ingredients); | |
} | |
@Override | |
public void write(PacketByteBuf buf, AlchemyRecipe recipe) { | |
buf.writeString(recipe.getGroup()); | |
for (Ingredient ingredient : recipe.ingredients) { | |
ingredient.write(buf); | |
} | |
buf.writeItemStack(recipe.getOutput()); | |
} | |
private static DefaultedList<Ingredient> getIngredients(JsonArray json) { | |
DefaultedList<Ingredient> ingredients = DefaultedList.of(); | |
for (int i = 0; i < json.size(); i++) { | |
Ingredient ingredient = Ingredient.fromJson(json.get(i)); | |
if (!ingredient.isEmpty()) { | |
ingredients.add(ingredient); | |
} | |
} | |
return ingredients; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment