Posted November 13, 201311 yr Hi! I created a new crafting Table with new recipes which take Mana from the Player. Everything works fine except from one fact: If I perform a shift-klick to craft as much Items as possible, neither the Crafting result is placed in the Inventory of the player, nor the Mana is reduced by the specified amount. Only the Crafting-items are removed. Can anybody tell me, why? The Slot for the Crafting result: package magicum.minecraft.items.crafting; import magicum.minecraft.blocks.container.ContainerMagicCrafting; import magicum.minecraft.extendedproperties.ExtendedProperties; import net.minecraft.block.Block; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.inventory.Container; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.Slot; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.stats.AchievementList; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.entity.player.PlayerDestroyItemEvent; import cpw.mods.fml.common.registry.GameRegistry; public class ModSlotCrafting extends Slot { /** The craft matrix inventory linked to this result slot. */ private final IInventory craftMatrix; /** The player that is using the GUI where this slot resides. */ private EntityPlayer thePlayer; public boolean canTake; private Container owner; /** * The number of items that have been crafted so far. Gets passed to ItemStack.onCrafting before being reset. */ private int amountCrafted; public ModSlotCrafting(Container owner, EntityPlayer par1EntityPlayer, IInventory par2IInventory, IInventory par3IInventory, int par4, int par5, int par6) { super(par3IInventory, par4, par5, par6); this.thePlayer = par1EntityPlayer; this.craftMatrix = par2IInventory; this.owner = owner; } /** * Check if the stack is a valid item for this slot. Always true beside for the armor slots. */ @Override public boolean isItemValid(ItemStack par1ItemStack) { return false; } /** * Decrease the size of the stack in slot (first int arg) by the amount of the second int arg. Returns the new * stack. */ @Override public ItemStack decrStackSize(int par1) { if (this.getHasStack()) { this.amountCrafted += Math.min(par1, this.getStack().stackSize); } return super.decrStackSize(par1); } /** * the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood. Typically increases an * internal count then calls onCrafting(item). */ @Override protected void onCrafting(ItemStack par1ItemStack, int par2) { this.amountCrafted += par2; this.onCrafting(par1ItemStack); } /** * the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood. */ @Override protected void onCrafting(ItemStack par1ItemStack) { par1ItemStack.onCrafting(this.thePlayer.worldObj, this.thePlayer, this.amountCrafted); this.amountCrafted = 0; if (par1ItemStack.itemID == Block.workbench.blockID) { this.thePlayer.addStat(AchievementList.buildWorkBench, 1); } else if (par1ItemStack.itemID == Item.pickaxeWood.itemID) { this.thePlayer.addStat(AchievementList.buildPickaxe, 1); } else if (par1ItemStack.itemID == Block.furnaceIdle.blockID) { this.thePlayer.addStat(AchievementList.buildFurnace, 1); } else if (par1ItemStack.itemID == Item.hoeWood.itemID) { this.thePlayer.addStat(AchievementList.buildHoe, 1); } else if (par1ItemStack.itemID == Item.bread.itemID) { this.thePlayer.addStat(AchievementList.makeBread, 1); } else if (par1ItemStack.itemID == Item.cake.itemID) { this.thePlayer.addStat(AchievementList.bakeCake, 1); } else if (par1ItemStack.itemID == Item.pickaxeStone.itemID) { this.thePlayer.addStat(AchievementList.buildBetterPickaxe, 1); } else if (par1ItemStack.itemID == Item.swordWood.itemID) { this.thePlayer.addStat(AchievementList.buildSword, 1); } else if (par1ItemStack.itemID == Block.enchantmentTable.blockID) { this.thePlayer.addStat(AchievementList.enchantments, 1); } else if (par1ItemStack.itemID == Block.bookShelf.blockID) { this.thePlayer.addStat(AchievementList.bookcase, 1); } } @Override public void onPickupFromSlot(EntityPlayer par1EntityPlayer, ItemStack par2ItemStack) { GameRegistry.onItemCrafted(par1EntityPlayer, par2ItemStack, this.craftMatrix); this.onCrafting(par2ItemStack); if (par1EntityPlayer instanceof EntityPlayerMP) { ExtendedProperties.get((EntityPlayerMP) par1EntityPlayer).rem("Mana", ((ContainerMagicCrafting)this.owner).manaNeeded); } for (int i = 0; i < this.craftMatrix.getSizeInventory(); ++i) { ItemStack itemstack1 = this.craftMatrix.getStackInSlot(i); if (itemstack1 != null) { this.craftMatrix.decrStackSize(i, 1); if (itemstack1.getItem().hasContainerItem()) { ItemStack itemstack2 = itemstack1.getItem().getContainerItemStack(itemstack1); if (itemstack2.isItemStackDamageable() && itemstack2.getItemDamage() > itemstack2.getMaxDamage()) { MinecraftForge.EVENT_BUS.post(new PlayerDestroyItemEvent(this.thePlayer, itemstack2)); itemstack2 = null; } if (itemstack2 != null && (!itemstack1.getItem().doesContainerItemLeaveCraftingGrid(itemstack1) || !this.thePlayer.inventory.addItemStackToInventory(itemstack2))) { if (this.craftMatrix.getStackInSlot(i) == null) { this.craftMatrix.setInventorySlotContents(i, itemstack2); } else { this.thePlayer.dropPlayerItem(itemstack2); } } } } } this.owner.onCraftMatrixChanged(((ContainerMagicCrafting)this.owner).tileEntity); } @Override public boolean canTakeStack(EntityPlayer par1EntityPlayer) { return this.canTake; } } The Container: package magicum.minecraft.blocks.container; import java.util.ArrayList; import java.util.List; import magicum.Main; import magicum.minecraft.blocks.tileentity.TileEntityMagicCrafting; import magicum.minecraft.elements.ElementCounted; import magicum.minecraft.items.crafting.ModCraftingManager; import magicum.minecraft.items.crafting.ModSlotCraftMatrix; import magicum.minecraft.items.crafting.ModSlotCrafting; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.InventoryCraftResult; import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; import net.minecraft.world.World; public class ContainerMagicCrafting extends Container { public TileEntityMagicCrafting tileEntity; public IInventory craftResult = new InventoryCraftResult(); private World worldObj; public int manaNeeded; public List<ElementCounted> Elements = new ArrayList<ElementCounted>(); private ModSlotCrafting crafting; public ContainerMagicCrafting (InventoryPlayer inventoryPlayer, TileEntityMagicCrafting te){ this.tileEntity = te; this.worldObj = inventoryPlayer.player.worldObj; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { this.addSlotToContainer(new ModSlotCraftMatrix(this, this.tileEntity, j + i * 3, 44 + j * 18, 17 + i * 18)); } } for (int i = 0; i < 3; i++) this.addSlotToContainer(new Slot(this.tileEntity, 9 + i, 8, 17 + i * 18)); this.crafting = new ModSlotCrafting(this, inventoryPlayer.player, this.tileEntity, this.craftResult, 0, 138, 35); this.addSlotToContainer(this.crafting); this.addPlayerInventory(inventoryPlayer); this.onCraftMatrixChanged(this.tileEntity); } @Override public boolean canInteractWith(EntityPlayer player) { return this.tileEntity.isUseableByPlayer(player); } protected void addPlayerInventory(InventoryPlayer inventoryPlayer) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 9; j++) { this.addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9, 8 + j * 18, 95 + i * 18)); } } for (int i = 0; i < 9; i++) { this.addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 153)); } } @Override public ItemStack transferStackInSlot(EntityPlayer player, int slot) { ItemStack stack = null; Slot slotObject = (Slot) this.inventorySlots.get(slot); if (slotObject != null && slotObject.getHasStack()) { ItemStack stackInSlot = slotObject.getStack(); stack = stackInSlot.copy(); if (slot < 9) { if (!this.mergeItemStack(stackInSlot, 0, 35, true)) { return null; } } else if (!this.mergeItemStack(stackInSlot, 0, 9, false)) { return null; } if (stackInSlot.stackSize == 0) { slotObject.putStack(null); } else { slotObject.onSlotChanged(); } if (stackInSlot.stackSize == stack.stackSize) { return null; } slotObject.onPickupFromSlot(player, stackInSlot); } return stack; } /** * Callback for when the crafting matrix is changed. */ @Override public void onCraftMatrixChanged(IInventory par1IInventory) { this.manaNeeded = ModCraftingManager.getInstance().getManaRequired(this.tileEntity, this.worldObj); this.Elements = ModCraftingManager.getInstance().getElementsRequired(this.tileEntity, this.worldObj); this.crafting.canTake = Main.proxy.playerMana >= this.manaNeeded && this.elementsCorrect(); this.craftResult.setInventorySlotContents(0, ModCraftingManager.getInstance().findMatchingRecipe(this.tileEntity, this.worldObj)); } private boolean elementsCorrect() { return true; } } Write if you need more code! http://i.imgur.com/wNvtGZw.png[/img] MODS and MODDING TUTORIALS
November 14, 201311 yr Author I did some experiments and found out, that the crafted Items are placed in the craftMatrix, not in the Player's inventory. There the stacksize is reduced and the Item vanishes. How can I get the Items into the correct Inventory? I really don't know, which classes to change. http://i.imgur.com/wNvtGZw.png[/img] MODS and MODDING TUTORIALS
November 14, 201311 yr Author Anybody? http://i.imgur.com/wNvtGZw.png[/img] MODS and MODDING TUTORIALS
November 14, 201311 yr Author OK, I solved it now, but I really don't understand y´why: I just put addPlayerInventory(inventoryPlayer); to the Top. Now it works.. Such a little problem But now, I got another Problem: If I do a shift klick, I craft as much Items as possible from the Item Count. But if I got maybe the Mana for 2 Items and the Items for 4, it will craft 4. How can I add a Control for the Mana when performing a Shiftklick? http://i.imgur.com/wNvtGZw.png[/img] MODS and MODDING TUTORIALS
November 15, 201311 yr Author Its just me who's posting.. I now solved the problem. Final Code: ContainerMagicCrafting: package magicum.minecraft.blocks.container; import java.util.ArrayList; import java.util.List; import magicum.Main; import magicum.minecraft.blocks.tileentity.TileEntityMagicCrafting; import magicum.minecraft.elements.ElementCounted; import magicum.minecraft.items.crafting.ModCraftingManager; import magicum.minecraft.items.crafting.ModSlotCraftMatrix; import magicum.minecraft.items.crafting.ModSlotCrafting; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.InventoryCraftResult; import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; import net.minecraft.world.World; public class ContainerMagicCrafting extends Container { public TileEntityMagicCrafting tileEntity; public IInventory craftResult = new InventoryCraftResult(); private World worldObj; public int manaNeeded; public List<ElementCounted> Elements = new ArrayList<ElementCounted>(); private ModSlotCrafting crafting; public ContainerMagicCrafting (InventoryPlayer inventoryPlayer, TileEntityMagicCrafting te){ this.tileEntity = te; this.worldObj = inventoryPlayer.player.worldObj; this.addPlayerInventory(inventoryPlayer); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { this.addSlotToContainer(new ModSlotCraftMatrix(this, this.tileEntity, j + i * 3, 44 + j * 18, 17 + i * 18)); } } for (int i = 0; i < 3; i++) this.addSlotToContainer(new Slot(this.tileEntity, 9 + i, 8, 17 + i * 18)); this.crafting = new ModSlotCrafting(this, inventoryPlayer.player, this.tileEntity, this.craftResult, 0, 138, 35); this.addSlotToContainer(this.crafting); this.onCraftMatrixChanged(this.tileEntity); } @Override public boolean canInteractWith(EntityPlayer player) { return this.tileEntity.isUseableByPlayer(player); } protected void addPlayerInventory(InventoryPlayer inventoryPlayer) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 9; j++) { this.addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9, 8 + j * 18, 95 + i * 18)); } } for (int i = 0; i < 9; i++) { this.addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 153)); } } @Override public ItemStack transferStackInSlot(EntityPlayer player, int slot) { ItemStack stack = null; Slot slotObject = (Slot) this.inventorySlots.get(slot); if (slotObject != null && slotObject.getHasStack()) { ItemStack stackInSlot = slotObject.getStack(); stack = stackInSlot.copy(); if (slot < 9) { if (!this.mergeItemStack(stackInSlot, 0, 35, true)) { return null; } } else if (!this.mergeItemStack(stackInSlot, 0, 9, false)) { return null; } if (stackInSlot.stackSize == 0) { slotObject.putStack(null); } else { slotObject.onSlotChanged(); } if (stackInSlot.stackSize == stack.stackSize) { return null; } slotObject.onPickupFromSlot(player, stackInSlot); } return stack; } /** * Callback for when the crafting matrix is changed. */ @Override public void onCraftMatrixChanged(IInventory par1IInventory) { this.manaNeeded = ModCraftingManager.getInstance().getManaRequired(this.tileEntity, this.worldObj); this.Elements = ModCraftingManager.getInstance().getElementsRequired(this.tileEntity, this.worldObj); if (Main.proxy.playerMana >= this.manaNeeded && this.elementsCorrect()) this.craftResult.setInventorySlotContents(0, ModCraftingManager.getInstance().findMatchingRecipe(this.tileEntity, this.worldObj)); } private boolean elementsCorrect() { return true; } } ModSlotCrafting: package magicum.minecraft.items.crafting; import magicum.Main; import magicum.minecraft.blocks.container.ContainerMagicCrafting; import magicum.minecraft.extendedproperties.ExtendedProperties; import net.minecraft.block.Block; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.Slot; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.stats.AchievementList; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.entity.player.PlayerDestroyItemEvent; import cpw.mods.fml.common.registry.GameRegistry; public class ModSlotCrafting extends Slot { /** The craft matrix inventory linked to this result slot. */ private final IInventory craftMatrix; /** The player that is using the GUI where this slot resides. */ private EntityPlayer thePlayer; private ContainerMagicCrafting owner; /** * The number of items that have been crafted so far. Gets passed to ItemStack.onCrafting before being reset. */ private int amountCrafted; public ModSlotCrafting(ContainerMagicCrafting owner, EntityPlayer par1EntityPlayer, IInventory par2IInventory, IInventory par3IInventory, int par4, int par5, int par6) { super(par3IInventory, par4, par5, par6); this.thePlayer = par1EntityPlayer; this.craftMatrix = par2IInventory; this.owner = owner; } /** * Check if the stack is a valid item for this slot. Always true beside for the armor slots. */ @Override public boolean isItemValid(ItemStack par1ItemStack) { return false; } /** * Decrease the size of the stack in slot (first int arg) by the amount of the second int arg. Returns the new * stack. */ @Override public ItemStack decrStackSize(int par1) { if (this.getHasStack()) { this.amountCrafted += Math.min(par1, this.getStack().stackSize); } return super.decrStackSize(par1); } /** * the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood. Typically increases an * internal count then calls onCrafting(item). */ @Override protected void onCrafting(ItemStack par1ItemStack, int par2) { this.amountCrafted += par2; this.onPickupFromSlot(this.thePlayer, par1ItemStack); } /** * the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood. */ @Override protected void onCrafting(ItemStack par1ItemStack) { if (this.thePlayer instanceof EntityPlayerMP) ExtendedProperties.get((EntityPlayerMP) this.thePlayer).rem("Mana", this.owner.manaNeeded); Main.proxy.playerMana -= this.owner.manaNeeded; par1ItemStack.onCrafting(this.thePlayer.worldObj, this.thePlayer, this.amountCrafted); this.amountCrafted = 0; if (par1ItemStack.itemID == Block.workbench.blockID) { this.thePlayer.addStat(AchievementList.buildWorkBench, 1); } else if (par1ItemStack.itemID == Item.pickaxeWood.itemID) { this.thePlayer.addStat(AchievementList.buildPickaxe, 1); } else if (par1ItemStack.itemID == Block.furnaceIdle.blockID) { this.thePlayer.addStat(AchievementList.buildFurnace, 1); } else if (par1ItemStack.itemID == Item.hoeWood.itemID) { this.thePlayer.addStat(AchievementList.buildHoe, 1); } else if (par1ItemStack.itemID == Item.bread.itemID) { this.thePlayer.addStat(AchievementList.makeBread, 1); } else if (par1ItemStack.itemID == Item.cake.itemID) { this.thePlayer.addStat(AchievementList.bakeCake, 1); } else if (par1ItemStack.itemID == Item.pickaxeStone.itemID) { this.thePlayer.addStat(AchievementList.buildBetterPickaxe, 1); } else if (par1ItemStack.itemID == Item.swordWood.itemID) { this.thePlayer.addStat(AchievementList.buildSword, 1); } else if (par1ItemStack.itemID == Block.enchantmentTable.blockID) { this.thePlayer.addStat(AchievementList.enchantments, 1); } else if (par1ItemStack.itemID == Block.bookShelf.blockID) { this.thePlayer.addStat(AchievementList.bookcase, 1); } } @Override public void onPickupFromSlot(EntityPlayer par1EntityPlayer, ItemStack par2ItemStack) { GameRegistry.onItemCrafted(par1EntityPlayer, par2ItemStack, this.thePlayer.inventory); this.onCrafting(par2ItemStack); for (int i = 0; i < this.craftMatrix.getSizeInventory(); ++i) { ItemStack itemstack1 = this.craftMatrix.getStackInSlot(i); if (itemstack1 != null) { this.craftMatrix.decrStackSize(i, 1); if (itemstack1.getItem().hasContainerItem()) { ItemStack itemstack2 = itemstack1.getItem().getContainerItemStack(itemstack1); if (itemstack2.isItemStackDamageable() && itemstack2.getItemDamage() > itemstack2.getMaxDamage()) { MinecraftForge.EVENT_BUS.post(new PlayerDestroyItemEvent(this.thePlayer, itemstack2)); itemstack2 = null; } if (itemstack2 != null && (!itemstack1.getItem().doesContainerItemLeaveCraftingGrid(itemstack1) || !this.thePlayer.inventory.addItemStackToInventory(itemstack2))) { if (this.craftMatrix.getStackInSlot(i) == null) { this.craftMatrix.setInventorySlotContents(i, itemstack2); } else { this.thePlayer.dropPlayerItem(itemstack2); } } } } } this.inventory.setInventorySlotContents(0, null); this.owner.onCraftMatrixChanged(this.owner.tileEntity); } } http://i.imgur.com/wNvtGZw.png[/img] MODS and MODDING TUTORIALS
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.