-
Notifications
You must be signed in to change notification settings - Fork 18
/
TextureRecipe.java
97 lines (78 loc) · 3.57 KB
/
TextureRecipe.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package knightminer.inspirations.library.recipe;
import com.google.gson.JsonObject;
import knightminer.inspirations.Inspirations;
import knightminer.inspirations.library.util.TagUtil;
import knightminer.inspirations.library.util.TextureBlockUtil;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.inventory.CraftingInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipeSerializer;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.item.crafting.ShapedRecipe;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.NonNullList;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.crafting.CraftingHelper;
import javax.annotation.Nonnull;
public class TextureRecipe extends ShapedRecipe {
public final Ingredient texture; // first one found of these determines the output block used
public TextureRecipe(ResourceLocation id, String group, int width, int height, Ingredient texture, NonNullList<Ingredient> inputs, ItemStack output) {
super(id, group, width, height, inputs, output);
this.texture = texture;
}
private TextureRecipe(ShapedRecipe orig, Ingredient texture) {
super(orig.getId(), orig.getGroup(), orig.getWidth(), orig.getHeight(), orig.getIngredients(), orig.getRecipeOutput());
this.texture = texture;
}
@Nonnull
@Override
public ItemStack getCraftingResult(CraftingInventory craftMatrix) {
ItemStack result = super.getCraftingResult(craftMatrix);
for(int i = 0; i < craftMatrix.getSizeInventory(); i++) {
for(ItemStack potential : texture.getMatchingStacks()) {
ItemStack stack = craftMatrix.getStackInSlot(i);
if(potential.isItemEqual(stack) && Block.getBlockFromItem(stack.getItem()) != Blocks.AIR) {
Block outBlock = Block.getBlockFromItem(result.getItem());
// Special case for Enlightened Bushes - grab their underlying texture instead of the bush.
Block recurBlock = TextureBlockUtil.getTextureBlock(stack);
if (recurBlock != null) {
stack = new ItemStack(recurBlock);
}
return TextureBlockUtil.createTexturedStack(outBlock, Block.getBlockFromItem(stack.getItem()));
}
}
}
return result;
}
@Nonnull
@Override
public ItemStack getRecipeOutput() {
ItemStack output = super.getRecipeOutput();
if(!(texture.getMatchingStacks().length == 0) && !output.isEmpty()) {
ItemStack stack = texture.getMatchingStacks()[0];
Block block = Block.getBlockFromItem(output.getItem());
return TextureBlockUtil.createTexturedStack(block, Block.getBlockFromItem(stack.getItem()));
}
return super.getRecipeOutput();
}
public static final IRecipeSerializer<?> SERIALIZER = new TextureRecipe.Serializer().setRegistryName(new ResourceLocation(Inspirations.modID, "texture_recipe"));
private static class Serializer extends net.minecraftforge.registries.ForgeRegistryEntry<IRecipeSerializer<?>> implements IRecipeSerializer<TextureRecipe> {
@Override
public TextureRecipe read(ResourceLocation recipeId, JsonObject json) {
ShapedRecipe recipe = CRAFTING_SHAPED.read(recipeId, json);
Ingredient texture = CraftingHelper.getIngredient(TagUtil.getElement(json, "texture"));
return new TextureRecipe(recipe, texture);
}
@Override
public TextureRecipe read(ResourceLocation recipeId, PacketBuffer buffer) {
ShapedRecipe recipe = CRAFTING_SHAPED.read(recipeId, buffer);
return new TextureRecipe(recipe, Ingredient.read(buffer));
}
@Override
public void write(PacketBuffer buffer, TextureRecipe recipe) {
CRAFTING_SHAPED.write(buffer, recipe);
recipe.texture.write(buffer);
}
}
}