Jump to content

[1.3.2][SOLVED] Cannot Pick Up Items from Custom Inventories


_bau5

Recommended Posts

Title says it all, When I try to pick up an item from the inventory of the player, or hotbar of the player, it quickly places it back in the slot. When I try to pick up an item that is in the inventory of my custom TileEntity, it disappears, never to be found again.

 

I'm assuming this is a sync issue, but I'm not familiar with syncing the inventory on client with the server. Any help would be appreciated, especially in the form of a step by step walkthrough.

 

Roughly what I have now:

    - Register the proxy, which contains the rendering for the tile entity

    - Register the gui handler with the NetworkRegistry

    - Register the block

    - Register the custom TileEntity

    - When the block is activated, open the Gui through openGui method in player

    - When the Gui is opened it makes a custom container based with the player inventory and the TileEntity passed to it.

    - The container adds a handful of slots for itself, along with the player inventory and hotbar.

 

The last step is where the problems are. It draws all the slots right, no errors or anything, but when i click on any of the items that are in my inventory, it just places them back down.

 

Thanks!

 

Here's my code: (irrelevant code snipped.)

 

Main class:

@Mod(modid = "_bau5Alptraum", name = "Alptraum", version = "0.01") 
@NetworkMod(clientSideRequired=true, serverSideRequired=false,
clientPacketHandlerSpec =
@SidedPacketHandler(channels = {"_bau5Alptraum" }, packetHandler = ClientPacketHandler.class),
serverPacketHandlerSpec =
@SidedPacketHandler(channels = {"_bau5Alptraum" }, packetHandler = ServerPacketHandler.class))
public class Alptraum 
{
@SidedProxy(clientSide = "_bau5.alptraum.ClientProxy", serverSide = "_bau5.alptraum.CommonProxy")

public static CommonProxy proxy;
@Instance
private static Alptraum instance;
       @Init
public void initAlptraum(FMLInitializationEvent event)
{
	MinecraftForgeClient.preloadTexture(textureFile);
	proxy.registerRenderInformation();
	NetworkRegistry.instance().registerGuiHandler(instance, proxy);

	registerBlocks();
	registerItems();
	initAlpVars();
	initOthers();
	initSounds();
}
        private void registerBlocks() 
{
	nightmareStone = new AlpStone(BnmStoneID, 0);
	blockUtility = new BlockUtility(BalpUtilityID, 0);

	GameRegistry.registerBlock(nightmareStone, _bau5.alptraum.AlpStoneItem.class);	
	GameRegistry.registerBlock(blockUtility, _bau5.alptraum.utility.BlockUtilityItem.class);
	GameRegistry.registerTileEntity(TileEntityDiscoverer.class, "Alp Discoverer");
         }
}

 

BlockUtil

public class BlockUtility extends BlockContainer
{
public BlockUtility(int id, int meta)
{
	super(id, meta, Material.rock);
	this.setBlockName("alpBlockUtility");
	this.setHardness(0.6F);
	setCreativeTab(CreativeTabs.tabBlock);
}
@SideOnly(Side.CLIENT) 
public boolean onBlockActivated(World par1World, int i, int j, int k, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9)
    {
	par5EntityPlayer.openGui(Alptraum.instance(), 0, par1World, i, j, k);
        return true;
        
    }
public TileEntity createNewTileEntity(World var1) 
{
	return new TileEntityDiscoverer();
}

public boolean isOpaqueCube()
    {
        return false;	
    }

    public int getRenderType()
    {
        return -1;
    }
}

 

TileEntity:

package _bau5.alptraum.utility;

import cpw.mods.fml.common.Side;
import cpw.mods.fml.common.asm.SideOnly;
import _bau5.alptraum.Alptraum;
import net.minecraft.src.Block;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.IInventory;
import net.minecraft.src.ItemStack;
import net.minecraft.src.NBTTagCompound;
import net.minecraft.src.NBTTagList;
import net.minecraft.src.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.common.ISidedInventory;

public class TileEntityDiscoverer extends TileEntity implements IInventory, ISidedInventory
{
private ItemStack[] discovererStack = new ItemStack[12];

public ItemStack currentItem;
public float angle = 0.0F;
public final static float move_speed = 0.1f;

public TileEntityDiscoverer()
{
	discovererStack[0] = new ItemStack(Block.dirt);
	discovererStack[1] = new ItemStack(Alptraum.shiftingResidue);
}

public void updateEntity()
{
	angle += move_speed;
}

@Override
public int getSizeInventory() 
{
	return discovererStack.length;
}

@Override
@SideOnly (Side.CLIENT)	
public ItemStack getStackInSlot(int var1) 
{
	return discovererStack[var1];
}

@Override
public ItemStack decrStackSize(int par1, int par2)
    {
        if (this.discovererStack[par1] != null)
        {
            ItemStack var3;

            if (this.discovererStack[par1].stackSize <= par2)
            {
                var3 = this.discovererStack[par1];
                this.discovererStack[par1] = null;
                this.onInventoryChanged();
                return var3;
            }
            else
            {
                var3 = this.discovererStack[par1].splitStack(par2);

                if (this.discovererStack[par1].stackSize == 0)
                {
                    this.discovererStack[par1] = null;
                }

                this.onInventoryChanged();
                return var3;
            }
        }
        else
        {
            return null;
        }
    }

@Override
public ItemStack getStackInSlotOnClosing(int par1)
    {
        if (this.discovererStack[par1] != null)
        {
            ItemStack var2 = this.discovererStack[par1];
            this.discovererStack[par1] = null;
            return var2;
        }
        else
        {
            return null;
        }
    }


@Override
public void setInventorySlotContents(int var1, ItemStack stack) 
{
	this.discovererStack[var1] = stack;

        if (stack != null && stack.stackSize > this.getInventoryStackLimit())
        {
            stack.stackSize = this.getInventoryStackLimit();
        }
        onInventoryChanged();
}

@Override
public String getInvName() 
{
	return "discoveryInventory";
}

@Override
public int getInventoryStackLimit() 
{
	return 64;
}

@Override
public boolean isUseableByPlayer(EntityPlayer var1) 
{
	return true;
}

public void readFromNBT(NBTTagCompound par1NBTTagCompound)
    {
        super.readFromNBT(par1NBTTagCompound);
        NBTTagList var2 = par1NBTTagCompound.getTagList("Items");
        this.discovererStack = new ItemStack[this.getSizeInventory()];

        for (int var3 = 0; var3 < var2.tagCount(); ++var3)
        {
            NBTTagCompound var4 = (NBTTagCompound)var2.tagAt(var3);
            byte var5 = var4.getByte("Slot");
            
            if (var5 >= 0 && var5 < this.discovererStack.length)
            {
                this.discovererStack[var5] = ItemStack.loadItemStackFromNBT(var4);
            }
        }
    }

    /**
     * Writes a tile entity to NBT.
     */
    public void writeToNBT(NBTTagCompound par1NBTTagCompound)
    {
        super.writeToNBT(par1NBTTagCompound);
        NBTTagList var2 = new NBTTagList();

        for (int var3 = 0; var3 < this.discovererStack.length; ++var3)
        {
            if (this.discovererStack[var3] != null)
            {
                NBTTagCompound var4 = new NBTTagCompound();
                var4.setByte("Slot", (byte)var3);
                this.discovererStack[var3].writeToNBT(var4);
                var2.appendTag(var4);
            }
        }

        par1NBTTagCompound.setTag("Items", var2);
    }
@Override
public void openChest() 
{

}

@Override
public void closeChest() 
{

}

@Override
public int getStartInventorySide(ForgeDirection side)
{
	if (side == ForgeDirection.DOWN) return 1;
        if (side == ForgeDirection.UP) return 0; 
        return 2;
}

@Override
public int getSizeInventorySide(ForgeDirection side) 
{
	return 1;
}

}

 

Gui:

 

package _bau5.alptraum.utility;

import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;

import _bau5.alptraum.Alptraum;
import net.minecraft.src.Container;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.GuiContainer;
import net.minecraft.src.GuiScreen;
import net.minecraft.src.GuiScreenBook;
import net.minecraft.src.InventoryPlayer;
import net.minecraft.src.ItemStack;
import net.minecraft.src.ScaledResolution;
import net.minecraft.src.StatCollector;

public class GuiDiscoverer extends GuiContainer 
{	
private TileEntityDiscoverer discovererInventory;

public GuiDiscoverer(InventoryPlayer playerInv, TileEntityDiscoverer discoverer) 
{
	super(new ContainerDiscoverer(playerInv, discoverer));
	this.discovererInventory = discoverer;
}
public boolean doesGuiPauseGame()
{
	return false;
}
protected void drawGuiContainerForegroundLayer()
    {
        this.fontRenderer.drawString("\u00A7d" + "Discoverer", 22, 6, 4210752);
        this.fontRenderer.drawString(StatCollector.translateToLocal("container.inventory"), 8, this.ySize - 96 + 2, 4210752);
    }

    /**
     * Draw the background layer for the GuiContainer (everything behind the items)
     */
    protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3)
    {
    	int var4 = this.mc.renderEngine.getTexture("/_bau5/alptraum/Textures/GUI/discovery.png");
        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.mc.renderEngine.bindTexture(var4);
        int var5 = (this.width - this.xSize) / 2;
        int var6 = (this.height - this.ySize) / 2;
        this.drawTexturedModalRect(var5, var6, 0, 0, this.xSize, this.ySize);
    }
}

 

Container:

package _bau5.alptraum.utility;

import _bau5.alptraum.ClientPacketHandler;
import net.minecraft.client.Minecraft;
import net.minecraft.src.Container;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.InventoryPlayer;
import net.minecraft.src.ItemStack;
import net.minecraft.src.Slot;

public class ContainerDiscoverer extends Container
{
private TileEntityDiscoverer discoverer;

public ContainerDiscoverer(InventoryPlayer invPlayer, TileEntityDiscoverer discoverer)
{
	this.discoverer = discoverer;
	this.discoverer.openChest();
	this.addSlotToContainer(new Slot(discoverer, 0, 39, 23));
	this.addSlotToContainer(new Slot(discoverer, 1, 54, 23));
	this.layoutContainer(invPlayer, discoverer);

}
public ItemStack slotClick (int slot, int button, boolean flag, EntityPlayer player) 
{
	  ItemStack stack = super.slotClick(slot, button, flag, player);
	  return stack;
	 }
private void layoutContainer(InventoryPlayer invPlayer,	TileEntityDiscoverer discoverer2) 
{
	int var3;
        for (var3 = 0; var3 < 3; ++var3)
        {
            for (int var4 = 0; var4 < 9; ++var4)
            {
                this.addSlotToContainer(new Slot(invPlayer, var4 + var3 * 9 + 9, 8 + var4 * 18, 84 + var3 * 18));
            }
        }

        for (var3 = 0; var3 < 9; ++var3)
        {
            this.addSlotToContainer(new Slot(invPlayer, var3, 8 + var3 * 18, 142));
        }
}

public boolean canInteractWith(EntityPlayer par1EntityPlayer)
    {
        return true;
    }
public ItemStack transferStackInSlot(int par1)
    {
        return null;
    }

}

 

 

 

 

Link to comment
Share on other sites

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.