| 1 | package net.minecraft.item.crafting;
|
| 2 |
|
| 3 | import net.minecraft.inventory.InventoryCrafting;
|
| 4 | import net.minecraft.item.ItemStack;
|
| 5 | import net.minecraft.nbt.NBTTagCompound;
|
| 6 | import net.minecraft.world.World;
|
| 7 |
|
| 8 | public class ShapedRecipes implements IRecipe
|
| 9 | {
|
| 10 | /** How many horizontal slots this recipe is wide. */
|
| 11 | private final int recipeWidth;
|
| 12 |
|
| 13 | /** How many vertical slots this recipe uses. */
|
| 14 | private final int recipeHeight;
|
| 15 |
|
| 16 | /** Is a array of ItemStack that composes the recipe. */
|
| 17 | private final ItemStack[] recipeItems;
|
| 18 |
|
| 19 | /** Is the ItemStack that you get when craft the recipe. */
|
| 20 | private final ItemStack recipeOutput;
|
| 21 | private boolean copyIngredientNBT;
|
| 22 |
|
| 23 | public ShapedRecipes(int width, int height, ItemStack[] p_i1917_3_, ItemStack output)
|
| 24 | {
|
| 25 | this.recipeWidth = width;
|
| 26 | this.recipeHeight = height;
|
| 27 | this.recipeItems = p_i1917_3_;
|
| 28 | this.recipeOutput = output;
|
| 29 | }
|
| 30 |
|
| 31 | public ItemStack getRecipeOutput()
|
| 32 | {
|
| 33 | return this.recipeOutput;
|
| 34 | }
|
| 35 |
|
| 36 | public ItemStack[] getRemainingItems(InventoryCrafting inv)
|
| 37 | {
|
| 38 | ItemStack[] aitemstack = new ItemStack[inv.getSizeInventory()];
|
| 39 |
|
| 40 | for (int i = 0; i < aitemstack.length; ++i)
|
| 41 | {
|
| 42 | ItemStack itemstack = inv.getStackInSlot(i);
|
| 43 |
|
| 44 | if (itemstack != null && itemstack.getItem().hasContainerItem())
|
| 45 | {
|
| 46 | aitemstack[i] = new ItemStack(itemstack.getItem().getContainerItem());
|
| 47 | }
|
| 48 | }
|
| 49 |
|
| 50 | return aitemstack;
|
| 51 | }
|
| 52 |
|
| 53 | /**
|
| 54 | * Used to check if a recipe matches current crafting inventory
|
| 55 | */
|
| 56 | public boolean matches(InventoryCrafting inv, World worldIn)
|
| 57 | {
|
| 58 | for (int i = 0; i <= 3 - this.recipeWidth; ++i)
|
| 59 | {
|
| 60 | for (int j = 0; j <= 3 - this.recipeHeight; ++j)
|
| 61 | {
|
| 62 | if (this.checkMatch(inv, i, j, true))
|
| 63 | {
|
| 64 | return true;
|
| 65 | }
|
| 66 |
|
| 67 | if (this.checkMatch(inv, i, j, false))
|
| 68 | {
|
| 69 | return true;
|
| 70 | }
|
| 71 | }
|
| 72 | }
|
| 73 |
|
| 74 | return false;
|
| 75 | }
|
| 76 |
|
| 77 | /**
|
| 78 | * Checks if the region of a crafting inventory is match for the recipe.
|
| 79 | */
|
| 80 | private boolean checkMatch(InventoryCrafting p_77573_1_, int p_77573_2_, int p_77573_3_, boolean p_77573_4_)
|
| 81 | {
|
| 82 | for (int i = 0; i < 3; ++i)
|
| 83 | {
|
| 84 | for (int j = 0; j < 3; ++j)
|
| 85 | {
|
| 86 | int k = i - p_77573_2_;
|
| 87 | int l = j - p_77573_3_;
|
| 88 | ItemStack itemstack = null;
|
| 89 |
|
| 90 | if (k >= 0 && l >= 0 && k < this.recipeWidth && l < this.recipeHeight)
|
| 91 | {
|
| 92 | if (p_77573_4_)
|
| 93 | {
|
| 94 | itemstack = this.recipeItems[this.recipeWidth - k - 1 + l * this.recipeWidth];
|
| 95 | }
|
| 96 | else
|
| 97 | {
|
| 98 | itemstack = this.recipeItems[k + l * this.recipeWidth];
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | ItemStack itemstack1 = p_77573_1_.getStackInRowAndColumn(i, j);
|
| 103 |
|
| 104 | if (itemstack1 != null || itemstack != null)
|
| 105 | {
|
| 106 | if (itemstack1 == null && itemstack != null || itemstack1 != null && itemstack == null)
|
| 107 | {
|
| 108 | return false;
|
| 109 | }
|
| 110 |
|
| 111 | if (itemstack.getItem() != itemstack1.getItem())
|
| 112 | {
|
| 113 | return false;
|
| 114 | }
|
| 115 |
|
| 116 | if (itemstack.getMetadata() != 32767 && itemstack.getMetadata() != itemstack1.getMetadata())
|
| 117 | {
|
| 118 | return false;
|
| 119 | }
|
| 120 | }
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | return true;
|
| 125 | }
|
| 126 |
|
| 127 | /**
|
| 128 | * Returns an Item that is the result of this recipe
|
| 129 | */
|
| 130 | public ItemStack getCraftingResult(InventoryCrafting inv)
|
| 131 | {
|
| 132 | ItemStack itemstack = this.getRecipeOutput().copy();
|
| 133 |
|
| 134 | if (this.copyIngredientNBT)
|
| 135 | {
|
| 136 | for (int i = 0; i < inv.getSizeInventory(); ++i)
|
| 137 | {
|
| 138 | ItemStack itemstack1 = inv.getStackInSlot(i);
|
| 139 |
|
| 140 | if (itemstack1 != null && itemstack1.hasTagCompound())
|
| 141 | {
|
| 142 | itemstack.setTagCompound((NBTTagCompound)itemstack1.getTagCompound().copy());
|
| 143 | }
|
| 144 | }
|
| 145 | }
|
| 146 |
|
| 147 | return itemstack;
|
| 148 | }
|
| 149 |
|
| 150 | /**
|
| 151 | * Returns the size of the recipe area
|
| 152 | */
|
| 153 | public int getRecipeSize()
|
| 154 | {
|
| 155 | return this.recipeWidth * this.recipeHeight;
|
| 156 | }
|
| 157 | }
|
| 158 |
|