Posted August 20, 201411 yr Hi! I've made a custom crafting for my Machine and when I place the items It doesn't craft, I get no error, can you help me? This is my ContainerMachine: package com.mega.bir.client.interfaces.machine; import com.mega.bir.block.tileentity.TileEntityInterChest; import com.mega.bir.block.tileentity.TileEntityMachine; import com.mega.bir.helping.ItemHelper; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.*; import net.minecraft.item.ItemStack; import net.minecraft.world.World; /** * Created by Megabitus on 8/13/2014 and hour 19. */ public class ContainerMachine extends Container{ private TileEntityMachine machine; public InventoryCrafting craftMatrix = new InventoryCrafting(this, TileEntityMachine.INVENTORY_SIZE, 1); public IInventory craftResult = new InventoryCraftResult(); private World worldObj; public ContainerMachine(InventoryPlayer invPlayer, TileEntityMachine machine, World world){ this.worldObj = world; this.machine = machine; for(int x = 0; x < 9; x++){ addSlotToContainer(new Slot(invPlayer, x, 8 + 18 * x, 142)); } for(int y = 0; y < 3; y++){ for(int x = 0; x < 9; x++){ addSlotToContainer(new Slot(invPlayer, x + y * 9 + 9, 8 + 18 * x, 84 + y * 18)); } } this.addSlotToContainer(new SlotCrafting(invPlayer.player, this.craftMatrix, this.craftResult, 0, 79, 41)); this.addSlotToContainer(new Slot(craftMatrix, 1, 48, 59)); this.addSlotToContainer(new Slot(craftMatrix, 2, 79, 5)); this.addSlotToContainer(new Slot(craftMatrix, 3, 112, 59)); onCraftMatrixChanged(craftMatrix); } @Override public boolean canInteractWith(EntityPlayer entityplayer){ return machine.isUseableByPlayer(entityplayer); } @Override protected boolean mergeItemStack(ItemStack itemStack, int slotMin, int slotMax, boolean ascending) { boolean slotFound = false; int currentSlotIndex = ascending ? slotMax - 1 : slotMin; Slot slot; ItemStack stackInSlot; if (itemStack.isStackable()) { while (itemStack.stackSize > 0 && (!ascending && currentSlotIndex < slotMax || ascending && currentSlotIndex >= slotMin)) { slot = (Slot) this.inventorySlots.get(currentSlotIndex); stackInSlot = slot.getStack(); if (slot.isItemValid(itemStack) && ItemHelper.equalsIgnoreStackSize(itemStack, stackInSlot)) { int combinedStackSize = stackInSlot.stackSize + itemStack.stackSize; int slotStackSizeLimit = Math.min(stackInSlot.getMaxStackSize(), slot.getSlotStackLimit()); if (combinedStackSize <= slotStackSizeLimit) { itemStack.stackSize = 0; stackInSlot.stackSize = combinedStackSize; slot.onSlotChanged(); slotFound = true; } else if (stackInSlot.stackSize < slotStackSizeLimit) { itemStack.stackSize -= slotStackSizeLimit - stackInSlot.stackSize; stackInSlot.stackSize = slotStackSizeLimit; slot.onSlotChanged(); slotFound = true; } } currentSlotIndex += ascending ? -1 : 1; } } if (itemStack.stackSize > 0) { currentSlotIndex = ascending ? slotMax - 1 : slotMin; while (!ascending && currentSlotIndex < slotMax || ascending && currentSlotIndex >= slotMin) { slot = (Slot) this.inventorySlots.get(currentSlotIndex); stackInSlot = slot.getStack(); if (slot.isItemValid(itemStack) && stackInSlot == null) { slot.putStack(ItemHelper.cloneItemStack(itemStack, Math.min(itemStack.stackSize, slot.getSlotStackLimit()))); slot.onSlotChanged(); if (slot.getStack() != null) { itemStack.stackSize -= slot.getStack().stackSize; slotFound = true; } break; } currentSlotIndex += ascending ? -1 : 1; } } return slotFound; } @Override public ItemStack transferStackInSlot(EntityPlayer player, int slotIndex){ ItemStack itemStack = null; Slot slot = (Slot) inventorySlots.get(slotIndex); if (slot != null && slot.getHasStack()) { ItemStack slotItemStack = slot.getStack(); itemStack = slotItemStack.copy(); if (slotIndex < TileEntityInterChest.INVENTORY_SIZE - 1) { if (!this.mergeItemStack(slotItemStack, 1, inventorySlots.size(), true)) { return null; } } else { if (!this.mergeItemStack(slotItemStack, 0, TileEntityInterChest.INVENTORY_SIZE - 1, false)) { return null; } } if (slotItemStack.stackSize == 0) { slot.putStack(null); } else { slot.onSlotChanged(); } } return itemStack; } @Override public void onContainerClosed(EntityPlayer player) { super.onContainerClosed(player); if (!this.worldObj.isRemote) { for (int i = 0; i < TileEntityMachine.INVENTORY_SIZE; ++i) { ItemStack itemstack = this.craftMatrix.getStackInSlotOnClosing(i); if (itemstack != null) { player.dropPlayerItemWithRandomChoice(itemstack, false); } } } } @Override public void onCraftMatrixChanged(IInventory p_75130_1_) { this.craftResult.setInventorySlotContents(0, MachineCraftingManager.getInstance().findMatchingRecipe(this.craftMatrix, this.worldObj)); } } This is my GuiMachine: package com.mega.bir.client.interfaces.machine; import com.mega.bir.block.tileentity.TileEntityMachine; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.util.ResourceLocation; import net.minecraft.world.World; import org.lwjgl.opengl.GL11; /** * Created by Megabitus on 8/13/2014 and hour 19. */ @SideOnly(Side.CLIENT) public class GuiMachine extends GuiContainer{ private static final ResourceLocation texture = new ResourceLocation("bir", "textures/gui/machine.png"); public GuiMachine(InventoryPlayer invPlayer, TileEntityMachine machine, World world){ super(new ContainerMachine(invPlayer, machine, world)); xSize = 176; ySize = 166; } @Override protected void drawGuiContainerBackgroundLayer(float f, int x, int y){ GL11.glColor4f(1, 1, 1, 1); Minecraft.getMinecraft().getTextureManager().bindTexture(texture); drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize); } @Override protected void drawGuiContainerForegroundLayer(int x, int y) { fontRendererObj.drawString("Machine", 7, 6, 0x404040); fontRendererObj.drawString("Inventory", 7, 74, 0x404040); } } This is my MachineComparator: package com.mega.bir.client.interfaces.machine; import net.minecraft.item.crafting.IRecipe; import java.util.Comparator; /** * Created by Megabitus on 8/20/2014. */ public class MachineComparator implements Comparator { final MachineCraftingManager machineCraftingManager; public MachineComparator(MachineCraftingManager par1MachineCraftingManager) { this.machineCraftingManager = par1MachineCraftingManager; } public int compareRecipes(IRecipe par1IRecipe, IRecipe par2IRecipe) { return par1IRecipe instanceof MachineShapelessRecipes && par2IRecipe instanceof MachineShapedRecipes ? 1 : (par2IRecipe instanceof MachineShapelessRecipes && par1IRecipe instanceof MachineShapedRecipes ? -1 : (par2IRecipe.getRecipeSize() < par1IRecipe.getRecipeSize() ? -1 : (par2IRecipe.getRecipeSize() > par1IRecipe.getRecipeSize() ? 1 : 0))); } public int compare(Object par1Obj, Object par2Obj) { return this.compareRecipes((IRecipe)par1Obj, (IRecipe)par2Obj); } } This is my MachineCraftingManager: package com.mega.bir.client.interfaces.machine; import com.mega.bir.helping.LogHelper; import com.mega.bir.item.ItemsManager; import net.minecraft.block.Block; 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.world.World; import java.util.*; /** * Created by Megabitus on 8/20/2014. */ public class MachineCraftingManager { private static final MachineCraftingManager instance = new MachineCraftingManager(); private List recipes = new ArrayList(); public static final MachineCraftingManager getInstance() { return instance; } private MachineCraftingManager() { /** Recipes!!! */ this.addMachineShapelessRecipe(new ItemStack(Items.ender_pearl, 1), new ItemStack(ItemsManager.eye)); Collections.sort(this.recipes, new MachineComparator(this)); LogHelper.info(this.getRecipeList().size() + " recipes!"); } public MachineShapedRecipes func_92051_a(ItemStack par1ItemStack, Object ... par2ArrayOfObj) { String var3 = ""; int var4 = 0; int var5 = 0; int var6 = 0; if (par2ArrayOfObj[var4] instanceof String[]) { String[] var7 = (String[])((String[])par2ArrayOfObj[var4++]); for (int var8 = 0; var8 < var7.length; ++var8) { String var9 = var7[var8]; ++var6; var5 = var9.length(); var3 = var3 + var9; } } else { while (par2ArrayOfObj[var4] instanceof String) { String var11 = (String)par2ArrayOfObj[var4++]; ++var6; var5 = var11.length(); var3 = var3 + var11; } } HashMap var12; for (var12 = new HashMap(); var4 < par2ArrayOfObj.length; var4 += 2) { Character var13 = (Character)par2ArrayOfObj[var4]; ItemStack var14 = null; if (par2ArrayOfObj[var4 + 1] instanceof Item) { var14 = new ItemStack((Item)par2ArrayOfObj[var4 + 1]); } else if (par2ArrayOfObj[var4 + 1] instanceof Block) { var14 = new ItemStack((Block)par2ArrayOfObj[var4 + 1], 1, -1); } else if (par2ArrayOfObj[var4 + 1] instanceof ItemStack) { var14 = (ItemStack)par2ArrayOfObj[var4 + 1]; } var12.put(var13, var14); } ItemStack[] var15 = new ItemStack[var5 * var6]; for (int var16 = 0; var16 < var5 * var6; ++var16) { char var10 = var3.charAt(var16); if (var12.containsKey(Character.valueOf(var10))) { var15[var16] = ((ItemStack)var12.get(Character.valueOf(var10))).copy(); } else { var15[var16] = null; } } MachineShapedRecipes var17 = new MachineShapedRecipes(var5, var6, var15, par1ItemStack); this.recipes.add(var17); return var17; } public void addMachineShapelessRecipe(ItemStack par1ItemStack, Object ... par2ArrayOfObj) { ArrayList var3 = new ArrayList(); Object[] var4 = par2ArrayOfObj; int var5 = par2ArrayOfObj.length; for (int var6 = 0; var6 < var5; ++var6) { Object var7 = var4[var6]; if (var7 instanceof ItemStack) { var3.add(((ItemStack)var7).copy()); } else if (var7 instanceof Item) { var3.add(new ItemStack((Item)var7)); } else { if (!(var7 instanceof Block)) { throw new RuntimeException("Invalid shapeless recipy!"); } var3.add(new ItemStack((Block)var7)); } } this.recipes.add(new MachineShapelessRecipes(par1ItemStack, var3)); } public ItemStack findMatchingRecipe(InventoryCrafting p_82787_1_, World p_82787_2_) { int i = 0; ItemStack itemstack = null; ItemStack itemstack1 = null; int j; for (j = 0; j < p_82787_1_.getSizeInventory(); ++j) { ItemStack itemstack2 = p_82787_1_.getStackInSlot(j); if (itemstack2 != null) { if (i == 0) { itemstack = itemstack2; } if (i == 1) { itemstack1 = itemstack2; } ++i; } } if (i == 2 && itemstack.getItem() == itemstack1.getItem() && itemstack.stackSize == 1 && itemstack1.stackSize == 1 && itemstack.getItem().isRepairable()) { Item item = itemstack.getItem(); int j1 = item.getMaxDamage() - itemstack.getItemDamageForDisplay(); int k = item.getMaxDamage() - itemstack1.getItemDamageForDisplay(); int l = j1 + k + item.getMaxDamage() * 5 / 100; int i1 = item.getMaxDamage() - l; if (i1 < 0) { i1 = 0; } return new ItemStack(itemstack.getItem(), 1, i1); } else { for (j = 0; j < this.recipes.size(); ++j) { IRecipe irecipe = (IRecipe) this.recipes.get(j); if (irecipe.matches(p_82787_1_, p_82787_2_)) { return irecipe.getCraftingResult(p_82787_1_); } } return null; } } /** * returns the List<> of all recipes */ public List getRecipeList() { return this.recipes; } } This is my MachineShapedRecipes: package com.mega.bir.client.interfaces.machine; import com.mega.bir.block.tileentity.TileEntityMachine; import net.minecraft.inventory.InventoryCrafting; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.IRecipe; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.world.World; /** * Created by Nitu on 8/20/2014. */ public class MachineShapedRecipes implements IRecipe{ /** How many horizontal slots this recipe is wide. */ public final int recipeWidth; /** How many vertical slots this recipe uses. */ public final int recipeHeight; /** Is a array of ItemStack that composes the recipe. */ public final ItemStack[] recipeItems; /** Is the ItemStack that you get when craft the recipe. */ private ItemStack recipeOutput; /** Is the itemID of the output item that you get when craft the recipe. */ public final int recipeOutputItemID; private boolean field_92101_f = false; public MachineShapedRecipes(int par1, int par2, ItemStack[] par3ArrayOfItemStack, ItemStack par4ItemStack) { this.recipeOutputItemID = par4ItemStack.getItemDamage(); this.recipeWidth = par1; this.recipeHeight = par2; this.recipeItems = par3ArrayOfItemStack; this.recipeOutput = par4ItemStack; } public ItemStack getRecipeOutput() { return this.recipeOutput; } /** * Used to check if a recipe matches current crafting inventory */ public boolean matches(InventoryCrafting par1InventoryCrafting, World par2World) { for (int i = 0; i <= TileEntityMachine.INVENTORY_SIZE - this.recipeWidth; ++i) { for (int j = 0; j <= 1 - this.recipeHeight; ++j) { if (this.checkMatch(par1InventoryCrafting, i, j, true)) { return true; } if (this.checkMatch(par1InventoryCrafting, i, j, false)) { return true; } } } return false; } /** * Checks if the region of a crafting inventory is match for the recipe. */ private boolean checkMatch(InventoryCrafting par1InventoryCrafting, int par2, int par3, boolean par4) { for (int k = 0; k < TileEntityMachine.INVENTORY_SIZE; ++k) { for (int l = 0; l < 1; ++l) { int i1 = k - par2; int j1 = l - par3; ItemStack itemstack = null; if (i1 >= 0 && j1 >= 0 && i1 < this.recipeWidth && j1 < this.recipeHeight) { if (par4) { itemstack = this.recipeItems[this.recipeWidth - i1 - 1 + j1 * this.recipeWidth]; } else { itemstack = this.recipeItems[i1 + j1 * this.recipeWidth]; } } ItemStack itemstack1 = par1InventoryCrafting.getStackInRowAndColumn(k, l); if (itemstack1 != null || itemstack != null) { if (itemstack1 == null && itemstack != null || itemstack1 != null && itemstack == null) { return false; } if (itemstack1.getItem() != itemstack.getItem()) { return false; } if (itemstack.getItemDamage() != 32767 && itemstack.getItemDamage() != itemstack1.getItemDamage()) { return false; } } } } return true; } /** * Returns an Item that is the result of this recipe */ public ItemStack getCraftingResult(InventoryCrafting par1InventoryCrafting) { ItemStack itemstack = this.getRecipeOutput().copy(); if (this.field_92101_f) { for (int i = 0; i < par1InventoryCrafting.getSizeInventory(); ++i) { ItemStack itemstack1 = par1InventoryCrafting.getStackInSlot(i); if (itemstack1 != null && itemstack1.hasTagCompound()) { itemstack.setTagCompound((NBTTagCompound)itemstack1.stackTagCompound.copy()); } } } return itemstack; } /** * Returns the size of the recipe area */ public int getRecipeSize() { return this.recipeWidth * this.recipeHeight; } public MachineShapedRecipes func_92100_c() { this.field_92101_f = true; return this; } } This my MachineShapelessRecipes: package com.mega.bir.client.interfaces.machine; import com.mega.bir.block.tileentity.TileEntityMachine; import net.minecraft.inventory.InventoryCrafting; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.IRecipe; import net.minecraft.world.World; import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** * Created by Megabitus on 8/20/2014. */ public class MachineShapelessRecipes implements IRecipe{ private final ItemStack recipeOutput; public final List recipeItems; public MachineShapelessRecipes(ItemStack par1ItemStack, List par2List) { this.recipeOutput = par1ItemStack; this.recipeItems = par2List; } public ItemStack getRecipeOutput() { return this.recipeOutput; } public boolean matches(InventoryCrafting par1InventoryCrafting, World par2World) { ArrayList arraylist = new ArrayList(this.recipeItems); for (int i = 0; i < TileEntityMachine.INVENTORY_SIZE; ++i) { for (int j = 0; j < 1; ++j) { ItemStack itemstack = par1InventoryCrafting.getStackInRowAndColumn(j, i); if (itemstack != null) { boolean flag = false; Iterator iterator = arraylist.iterator(); while (iterator.hasNext()) { ItemStack itemstack1 = (ItemStack)iterator.next(); if (itemstack.getItem() == itemstack1.getItem() && (itemstack1.getItemDamage() == 32767 || itemstack.getItemDamage() == itemstack1.getItemDamage())) { flag = true; arraylist.remove(itemstack1); break; } } if (!flag) { return false; } } } } return arraylist.isEmpty(); } /** * Returns an Item that is the result of this recipe */ public ItemStack getCraftingResult(InventoryCrafting par1InventoryCrafting) { return this.recipeOutput.copy(); } /** * Returns the size of the recipe area */ public int getRecipeSize() { return this.recipeItems.size(); } } And here is my Github with the last commit: https://github.com/megabitus98/Blocks-Items-Revolution Woking at my first mod, Blocks & Items Revolution : https://github.com/megabitus98/Blocks-Items-Revolution
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.