Jump to content

[1.7.10] Container/GUI Problems (+need help with crafting manager)


Recommended Posts

Posted

Hi!

For a new mod project i am working on with a few friends i created a 5x5 crafting table(+1 furnace slot).

I created my own recipes, crafting manager, container, te, gui... it works...somehow.

 

I ran into the following problems:

 

1.: When i click an item in my hotbar it is sometimes dropped (right-click-->whole stack is dropped)

 

2.: When i change the size of my window every slot is moved 1 px upwards.

 

3.: I want to use "shapes" in my crafting recipes. This means that the user has to fill all empty slots with either sand or bricks. I had the idea to simply switch null with sand/bricks in my findMatchingRecipe() function in my custom crafting manager. However, this somehow created an infinite loop. Any other ideas how to do this?

 

Source Code:

 

Design:

TzaSryE.png

 

TE:

 

 

package net.alchemisticarts.mod.tileentity;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;

public class TileEntityCraftingFurnace extends TileEntity implements IInventory {

        private ItemStack[] inv;



        public TileEntityCraftingFurnace(){
                inv = new ItemStack[1];
                
        }
        
        @Override
        public int getSizeInventory() {
                return inv.length;
        }

        @Override
        public ItemStack getStackInSlot(int slot) {
                return inv[slot];
        }
        
        @Override
        public void setInventorySlotContents(int slot, ItemStack stack) {
                inv[slot] = stack;
                if (stack != null && stack.stackSize > getInventoryStackLimit()) {
                        stack.stackSize = getInventoryStackLimit();
                }               
        }

        @Override
        public ItemStack decrStackSize(int slot, int amt) {
                ItemStack stack = getStackInSlot(slot);
                if (stack != null) {
                        if (stack.stackSize <= amt) {
                                setInventorySlotContents(slot, null);
                        } else {
                                stack = stack.splitStack(amt);
                                if (stack.stackSize == 0) {
                                        setInventorySlotContents(slot, null);
                                }
                        }
                }
                return stack;
        }

        @Override
        public ItemStack getStackInSlotOnClosing(int slot) {
                ItemStack stack = getStackInSlot(slot);
                if (stack != null) {
                        setInventorySlotContents(slot, null);
                }
                return stack;
        }
        
        @Override
        public int getInventoryStackLimit() {
                return 64;
        }

        @Override
        public boolean isUseableByPlayer(EntityPlayer player) {
                return worldObj.getTileEntity(xCoord, yCoord, zCoord) == this &&
                player.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) < 64;
        }


        @Override
        public void readFromNBT(NBTTagCompound tagCompound) {
                super.readFromNBT(tagCompound);
                
                NBTTagList tagList = tagCompound.getTagList("Inventory",0);
                for (int i = 0; i < tagList.tagCount(); i++) {
                        NBTTagCompound tag = (NBTTagCompound) tagList.getCompoundTagAt(i);
                        byte slot = tag.getByte("Slot");
                        if (slot >= 0 && slot < inv.length) {
                                inv[slot] = ItemStack.loadItemStackFromNBT(tag);
                        }
                }
                
        }

        @Override
        public void writeToNBT(NBTTagCompound tagCompound) {
                super.writeToNBT(tagCompound);
                                
                NBTTagList itemList = new NBTTagList();
                for (int i = 0; i < inv.length; i++) {
                        ItemStack stack = inv[i];
                        if (stack != null) {
                                NBTTagCompound tag = new NBTTagCompound();
                                tag.setByte("Slot", (byte) i);
                                stack.writeToNBT(tag);
                                itemList.appendTag(tag);
                        }
                }
                tagCompound.setTag("Inventory", itemList);

        }
        





			@Override
			public String getInventoryName() {
				 return "tco.tileentitytiny";
			}

			@Override
			public boolean hasCustomInventoryName() {
				// TODO Auto-generated method stub
				return false;
			}

			@Override
			public void openInventory() {
				// TODO Auto-generated method stub

			}

			@Override
			public void closeInventory() {
				// TODO Auto-generated method stub

			}

			@Override
			public boolean isItemValidForSlot(int p_94041_1_,
					ItemStack p_94041_2_) {
				// TODO Auto-generated method stub
				return true;
			}
}

 

 

 

Container:

 

 

package net.alchemisticarts.mod.container;

import net.alchemisticarts.mod.AlchemisticArts;
import net.alchemisticarts.mod.crafting.craftingfurnace.CraftingManagerCraftingFurnace;
import net.alchemisticarts.mod.tileentity.TileEntityCraftingFurnace;
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.InventoryCrafting;
import net.minecraft.inventory.Slot;
import net.minecraft.inventory.SlotCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

public class ContainerCraftingFurnace extends Container
{

public InventoryCrafting craftMatrix;
public IInventory craftResult;
public TileEntityCraftingFurnace tileEntity;
private World worldObj;
private int posX;
private int posY;
private int posZ;

public ContainerCraftingFurnace(InventoryPlayer inventoryplayer, TileEntityCraftingFurnace tileEntity, World world, int i, int j, int k)
{
/*
TileEntity te = world.getTileEntity(posX, posY, posZ);
TileEntityCraftingFurnace tileEntity = null;
if (te!=null || (te instanceof TileEntityCraftingFurnace)){
	tileEntity=(TileEntityCraftingFurnace)te;
} else {
	world.setTileEntity(posX, posY, posZ, new TileEntityCraftingFurnace());
}
*/

 craftMatrix = new InventoryCrafting(this, 5, 5);
 craftResult = new InventoryCraftResult();
 worldObj = world;
 this.tileEntity=tileEntity;
 posX = i;
 posY = j;
 posZ = k;


 //Output
 this.addSlotToContainer(new SlotCrafting(inventoryplayer.player, craftMatrix, craftResult, 0, 146, 15));

 //Fuel
 this.addSlotToContainer(new Slot(tileEntity, 0, 112, 60));



 //Crafting input

 for(int l = 0; l < 5; l++)
 {
	 for(int k1 = 0; k1 < 5; k1++)
	 {
	 this.addSlotToContainer(new Slot(craftMatrix, k1 + l * 5, 11 + k1 * 18, -3 + l * 18));
	 }

 }


 //Inventory
 for(int i1 = 0; i1 < 3; i1++)
 {
	 for(int l1 = 0; l1 < 9; l1++)
	 {
	 this.addSlotToContainer(new Slot(inventoryplayer, l1 + i1 * 9 + 9, 8 + l1 * 18, 103 + i1 * 18));
	 }

 }

 //hotbar
 for(int j1 = 0; j1 < 9; j1++)
 {
 this.addSlotToContainer(new Slot(inventoryplayer, j1, 8 + j1 * 18, 161));
 }



 onCraftMatrixChanged(craftMatrix);
}


public void onCraftMatrixChanged(IInventory iinventory){

	craftResult.setInventorySlotContents(0, CraftingManagerCraftingFurnace.getInstance().findMatchingRecipe(craftMatrix, worldObj));

}

@Override	
public void onContainerClosed(EntityPlayer entityplayer)
{
 super.onContainerClosed(entityplayer);

 if(worldObj.isRemote)
 {
	 return;
 }
 for(int i = 0; i < 25; i++)
 {
	 ItemStack itemstack = craftMatrix.getStackInSlot(i);
	 if(itemstack != null)
	 {
		 entityplayer.dropItem(itemstack.getItem(), itemstack.stackSize);
		 //entityplayer.dropPlayerItem(itemstack);
	 }
 }

}


public boolean canInteractWith(EntityPlayer entityplayer)
{
 if(worldObj.getBlock(posX, posY, posZ) != AlchemisticArts.blockCraftingFurnace)
 {
	 return false;
 } else{
	 return entityplayer.getDistanceSq((double)posX + 0.5D, (double)posY + 0.5D, (double)posZ + 0.5D) <= 64D;
 }
}

public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int slotID)
{
 ItemStack itemstack = null;
 Slot slot = (Slot)inventorySlots.get(slotID);
 if(slot != null && slot.getHasStack())
 {
	 ItemStack itemstack1 = slot.getStack();
	 itemstack = itemstack1.copy();
	//Output
	 if(slotID == 0)
	 {
		 //full inventory
		 if(!mergeItemStack(itemstack1, 27, 63, true))
		 {
			 return null;
		 }
	 } else
	//main Inventory
	 if(slotID >= 27 && slotID < 54)
	 {
		 //hotbar
		 if(!mergeItemStack(itemstack1, 54, 63, false))
		 {
			 return null;
		 }
	 } else
	//hotbar
	 if(slotID >= 54 && slotID < 63)
	 {
		 //main Inventory
		 if(!mergeItemStack(itemstack1, 27, 53, false))
		 {
			 return null;
		 }
	//everything with main inventory
	 } else if(!mergeItemStack(itemstack1, 27, 63, false))
	 {
		 return null;
	 }


	 if(itemstack1.stackSize == 0)
	 {
		 slot.putStack(null);
	 } else
	 {
		 slot.onSlotChanged();
	 }
	 if(itemstack1.stackSize != itemstack.stackSize)
	 {
		 slot.onPickupFromSlot(par1EntityPlayer, itemstack1);
	 } else
	 {
		 return null;
	 }
 }
 return itemstack;
}
}

 

 

GUI:

 

 

package net.alchemisticarts.mod.client.gui;

import net.alchemisticarts.mod.AlchemisticArts;
import net.alchemisticarts.mod.container.ContainerCraftingFurnace;
import net.alchemisticarts.mod.tileentity.TileEntityCraftingFurnace;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;

import org.lwjgl.opengl.GL11;

public class GuiCraftingFurnace extends GuiContainer
{
private ResourceLocation resourceLocation = new ResourceLocation(AlchemisticArts.MODID, "textures/gui/crafting_furnace.png");
public GuiCraftingFurnace(InventoryPlayer inventoryplayer, TileEntityCraftingFurnace tileEntity, World world, int i, int j, int k)
{
 super(new ContainerCraftingFurnace(inventoryplayer, tileEntity,world, i, j, k));
}

public void onGuiClosed()
{
 super.onGuiClosed();
}

protected void drawGuiContainerForegroundLayer(int par1, int par2)
{
 //FONTS!
}

protected void drawGuiContainerBackgroundLayer(float f, int i, int j)
{
int x;
int y=205;


GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
mc.renderEngine.bindTexture(resourceLocation);
int l = (width - xSize) / 2;
int i1 = (height - y) / 2;
drawTexturedModalRect(l, i1, 0, 0, xSize, y);
}
}

 

 

 

CraftingManager

 

 

package net.alchemisticarts.mod.crafting.craftingfurnace;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import cpw.mods.fml.common.registry.GameRegistry;
import net.alchemisticarts.mod.container.ContainerCraftingFurnace;
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.world.World;

public class CraftingManagerCraftingFurnace {
public static final int WILDCARD_VALUE = Short.MAX_VALUE;
private static final CraftingManagerCraftingFurnace instance = new CraftingManagerCraftingFurnace();
private List recipes = new ArrayList();

public static final CraftingManagerCraftingFurnace getInstance()
{
	 return instance;
}

private CraftingManagerCraftingFurnace()
{

CraftingFurnace.addShapelessRecipe(new ItemStack(Blocks.glass_pane), new ItemStack(Blocks.glass));	


//recipes = CraftingFurnace.recipes;

	 //This is where your recipes will go






Collections.sort(CraftingFurnace.recipes, new RecipeSorterCraftingFurnace(this));
	 System.out.println(CraftingFurnace.recipes.size() + " recipes");
}
/*
public ShapedRecipesCraftingFurnace 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;
		 }
	 }

	 ShapedRecipesCraftingFurnace var17 = new ShapedRecipesCraftingFurnace(var5, var6, var15, par1ItemStack);
	 this.recipes.add(var17);
	 return var17;
}

public void addBuilderShapelessRecipe(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 ShapelessRecipesCraftingFurnace(par1ItemStack, var3));
}
*/


//USED MORE OFTEN!?!
public ItemStack findMatchingRecipe(InventoryCrafting par1InventoryCrafting, World par2World)
{
	 int var3 = 0;
	 ItemStack var4 = null;
	 ItemStack var5 = null;
	 int var6;





	 for (var6 = 0; var6 < par1InventoryCrafting.getSizeInventory(); ++var6)
	 {
		 ItemStack var7 = par1InventoryCrafting.getStackInSlot(var6);

		 if (var7 != null)
		 {
			 if (var3 == 0)
			 {
				 var4 = var7;
			 }

			 if (var3 == 1)
			 {
				 var5 = var7;
			 }

			 ++var3;
		 }
	 }


	 for (var6 = 0; var6 < CraftingFurnace.recipes.size(); ++var6){
			 IRecipe var12 = (IRecipe)CraftingFurnace.recipes.get(var6);

			 if (var12.matches(par1InventoryCrafting, par2World))
			 {
				 return var12.getCraftingResult(par1InventoryCrafting);
			 }
	 }

	 return null;

}

/**
	 * returns the List<> of all recipes
	 */
public List getRecipeList()
{
	 return CraftingFurnace.recipes;
}
}

 

 

 

And CraftingFurnace, this is where i keep all my recipes:

 

 

package net.alchemisticarts.mod.crafting.craftingfurnace;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.ShapedRecipes;

public class CraftingFurnace {

    public static List recipes = new ArrayList();

    public List getRecipes(){
    	return recipes;
    }
    
    
    public static void addShapelessRecipe(ItemStack outputItemStack, Object ... p_77596_2_)
    {
        ArrayList arraylist = new ArrayList();
        Object[] aobject = p_77596_2_;
        int i = p_77596_2_.length;

        for (int j = 0; j < i; ++j)
        {
            Object object1 = aobject[j];

            if (object1 instanceof ItemStack)
            {
                arraylist.add(((ItemStack)object1).copy());
            }
            else if (object1 instanceof Item)
            {
                arraylist.add(new ItemStack((Item)object1));
            }
            else
            {
                if (!(object1 instanceof Block))
                {
                    throw new RuntimeException("Invalid shapeless recipy!");
                }

                arraylist.add(new ItemStack((Block)object1));
            }
        }

        recipes.add(new ShapelessRecipesCraftingFurnace(outputItemStack, arraylist));
    }
    
    public static ShapedRecipesCraftingFurnace addRecipe(ItemStack p_92103_1_, Object ... p_92103_2_)
    {
        String s = "";
        int i = 0;
        int j = 0;
        int k = 0;

        if (p_92103_2_[i] instanceof String[])
        {
            String[] astring = (String[])((String[])p_92103_2_[i++]);

            for (int l = 0; l < astring.length; ++l)
            {
                String s1 = astring[l];
                ++k;
                j = s1.length();
                s = s + s1;
            }
        }
        else
        {
            while (p_92103_2_[i] instanceof String)
            {
                String s2 = (String)p_92103_2_[i++];
                ++k;
                j = s2.length();
                s = s + s2;
            }
        }

        HashMap hashmap;

        for (hashmap = new HashMap(); i < p_92103_2_.length; i += 2)
        {
            Character character = (Character)p_92103_2_[i];
            ItemStack itemstack1 = null;

            if (p_92103_2_[i + 1] instanceof Item)
            {
                itemstack1 = new ItemStack((Item)p_92103_2_[i + 1]);
            }
            else if (p_92103_2_[i + 1] instanceof Block)
            {
                itemstack1 = new ItemStack((Block)p_92103_2_[i + 1], 1, 32767);
            }
            else if (p_92103_2_[i + 1] instanceof ItemStack)
            {
                itemstack1 = (ItemStack)p_92103_2_[i + 1];
            }

            hashmap.put(character, itemstack1);
        }

        ItemStack[] aitemstack = new ItemStack[j * k];

        for (int i1 = 0; i1 < j * k; ++i1)
        {
            char c0 = s.charAt(i1);

            if (hashmap.containsKey(Character.valueOf(c0)))
            {
                aitemstack[i1] = ((ItemStack)hashmap.get(Character.valueOf(c0))).copy();
            }
            else
            {
                aitemstack[i1] = null;
            }
        }

        ShapedRecipesCraftingFurnace shapedrecipes = new ShapedRecipesCraftingFurnace(j, k, aitemstack, p_92103_1_);
        recipes.add(shapedrecipes);
        return shapedrecipes;
    }
    
    

    

}

 

 

 

Here could be your advertisement!

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.