| package fr.scarex.ascalonmod.recipe; |
| |
| import java.util.Random; |
| |
| import net.minecraft.block.Block; |
| import net.minecraft.init.Blocks; |
| import net.minecraft.init.Items; |
| import net.minecraft.inventory.InventoryCrafting; |
| import net.minecraft.item.Item; |
| import net.minecraft.item.ItemStack; |
| import net.minecraft.item.crafting.IRecipe; |
| import net.minecraft.nbt.NBTTagCompound; |
| import net.minecraft.world.World; |
| import fr.scarex.ascalonmod.block.AscalonModBlocks; |
| import fr.scarex.ascalonmod.item.AscalonModItems; |
| |
| |
| |
| |
| |
| public class KeyRecipe implements IRecipe |
| { |
| private static final Random RNG = new Random(); |
| protected ItemStack result; |
| protected ExtendedItem[][] matrix; |
| protected byte slotIndex; |
| |
| public KeyRecipe(ItemStack result, byte slotIndex, ExtendedItem[][] matrix) { |
| this.result = result; |
| this.matrix = matrix; |
| this.slotIndex = slotIndex; |
| } |
| |
| public KeyRecipe(CraftMatrix c) { |
| this.result = c.result; |
| this.slotIndex = c.slot; |
| this.matrix = c.matrix; |
| } |
| |
| @Override |
| public boolean matches(InventoryCrafting inv, World world) { |
| for (int i = 0; i < 3; i++) { |
| for (int j = 0; j < 3; j++) { |
| if (!ExtendedItem.corresponds(this.matrix*[j], inv.getStackInSlot(i * 3 + j))) return false; |
| } |
| } |
| return true; |
| } |
| |
| @Override |
| public ItemStack getCraftingResult(InventoryCrafting inv) { |
| NBTTagCompound comp = new NBTTagCompound(); |
| if (slotIndex > 0) |
| comp.setInteger("key", inv.getStackInSlot(slotIndex).getTagCompound().getInteger("key")); |
| else |
| comp.setInteger("key", RNG.nextInt()); |
| ItemStack stack = result.copy(); |
| stack.setTagCompound(comp); |
| return stack; |
| } |
| |
| @Override |
| public int getRecipeSize() { |
| return 9; |
| } |
| |
| @Override |
| public ItemStack getRecipeOutput() { |
| return result.copy(); |
| } |
| |
| public static enum CraftMatrix |
| { |
| KEY( |
| new ItemStack(AscalonModItems.ITEM_KEY), |
| -1, |
| new Item[][] { |
| new Item[] { |
| Items.iron_ingot, |
| Items.iron_ingot, |
| null }, |
| new Item[] { |
| null, |
| Items.iron_ingot, |
| null }, |
| new Item[] { |
| null, |
| Items.iron_ingot, |
| null } }), |
| COPY_KEY( |
| new ItemStack(AscalonModItems.ITEM_KEY), |
| 6, |
| new Item[][] { |
| new Item[] { |
| Items.iron_ingot, |
| Items.iron_ingot, |
| null }, |
| new Item[] { |
| null, |
| Items.iron_ingot, |
| null }, |
| new Item[] { |
| AscalonModItems.ITEM_KEY, |
| Items.iron_ingot, |
| null } }), |
| LOCKED_CHEST( |
| new ItemStack(AscalonModBlocks.BLOCK_LOCKED_CHEST), |
| 5, |
| new Item[][] { |
| new Item[] { |
| Items.iron_ingot, |
| Items.iron_ingot, |
| Items.iron_ingot }, |
| new Item[] { |
| AscalonModItems.ITEM_LOCK, |
| Item.getItemFromBlock(Blocks.chest), |
| AscalonModItems.ITEM_KEY }, |
| new Item[] { |
| Items.iron_ingot, |
| Items.iron_ingot, |
| Items.iron_ingot } }), |
| LOCKED_DOOR( |
| new ItemStack(AscalonModItems.ITEM_SPRUCE_DOOR), |
| 5, |
| new ExtendedItem[][] { |
| new ExtendedItem[] { |
| new ExtendedItem(Blocks.planks, 1), |
| new ExtendedItem(Blocks.planks, 1), |
| null }, |
| new ExtendedItem[] { |
| new ExtendedItem(Blocks.planks, 1), |
| new ExtendedItem(Blocks.planks, 1), |
| new ExtendedItem(AscalonModItems.ITEM_KEY) }, |
| new ExtendedItem[] { |
| new ExtendedItem(Blocks.planks, 1), |
| new ExtendedItem(Blocks.planks, 1), |
| null } }); |
| |
| public ItemStack result; |
| public byte slot; |
| public ExtendedItem[][] matrix; |
| |
| private CraftMatrix(ItemStack stack, int slotIndex, Item[][] matrix) { |
| this.result = stack; |
| this.slot = (byte) slotIndex; |
| this.matrix = ExtendedItem.convertMatrix(matrix); |
| } |
| |
| private CraftMatrix(ItemStack result, int slot, ExtendedItem[][] matrix) { |
| this.result = result; |
| this.slot = (byte) slot; |
| this.matrix = matrix; |
| } |
| } |
| |
| public static class ExtendedItem |
| { |
| private Item item; |
| private int metadata; |
| |
| public ExtendedItem(Item item, int meta) { |
| this.item = item; |
| this.metadata = meta; |
| } |
| |
| public ExtendedItem(Block block, int metadata) { |
| this.item = Item.getItemFromBlock(block); |
| this.metadata = metadata; |
| } |
| |
| public ExtendedItem(Item item) { |
| this.item = item; |
| this.metadata = 0; |
| } |
| |
| public ExtendedItem(Block block) { |
| this.item = Item.getItemFromBlock(block); |
| this.metadata = 0; |
| } |
| |
| |
| |
| |
| public Item getItem() { |
| return item; |
| } |
| |
| |
| |
| |
| public int getMetadata() { |
| return metadata; |
| } |
| |
| public static ExtendedItem[][] convertMatrix(Item[][] items) { |
| ExtendedItem[][] xItems = new ExtendedItem[3][3]; |
| for (int i = 0; i < xItems.length; i++) { |
| for (int j = 0; j < xItems*.length; j++) { |
| xItems*[j] = new ExtendedItem(items*[j]); |
| } |
| } |
| return xItems; |
| } |
| |
| public static boolean corresponds(ExtendedItem item, ItemStack stack) { |
| if (stack == null && item != null && item.getItem() != null) return false; |
| if (stack != null && (item == null || item.getItem() == null)) return false; |
| if (stack != null && item.getItem() != null && (stack.getItem() != item.getItem() || stack.getItemDamage() != item.getMetadata())) return false; |
| return true; |
| } |
| } |
| } |
| |