Jump to content

[1.7.10] Item Disappearing When Clicked On


loawkise

Recommended Posts

I created my custom inventory and gui for the player, but whenever I put an item in my custom lot, if I try to click on it the item disappears. It feel like it has something to do with my inventory class, but am not sure what.

 

Inventory Class

 

package com.backpack.inventories;

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;

public class InventoryPlayerSurvival implements IInventory
{
private final String name = "Player Survival Inventory";
private final String tagName = "PSI";

public static final int INV_SIZE = 5;

private ItemStack[] inventory = new ItemStack[iNV_SIZE];

public InventoryPlayerSurvival() 
{

}

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

@Override
public ItemStack getStackInSlot(int slot) 
{
	return inventory[slot];
}

@Override
public ItemStack decrStackSize(int slot, int amount) 
{
	ItemStack stack = getStackInSlot(slot);

	if(stack != null)
	{
		if(stack.stackSize > amount)
		{
			stack = stack.splitStack(amount);
		}
		else
		{
			setInventorySlotContents(slot, null);
		}
	}

	return null;
}

@Override
public ItemStack getStackInSlotOnClosing(int slot) 
{
	ItemStack stack = getStackInSlot(slot);
	setInventorySlotContents(slot, null);

	return null;
}

@Override
public void setInventorySlotContents(int slot, ItemStack itemstack) 
{
	this.inventory[slot] = itemstack;

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

@Override
public String getInventoryName() 
{
	return name;
}

@Override
public boolean hasCustomInventoryName() 
{
	return name.length() > 0;
}

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

@Override
public void markDirty() 
{

}

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

@Override
public void openInventory() 
{

}

@Override
public void closeInventory() 
{

}

@Override
public boolean isItemValidForSlot(int p_94041_1_, ItemStack p_94041_2_) 
{
	return true;
}

public void writeToNBT(NBTTagCompound compound)
{
	NBTTagList items = new NBTTagList();

	for (int i = 0; i < getSizeInventory(); ++i)
	{
		if (getStackInSlot(i) != null)
		{
			NBTTagCompound item = new NBTTagCompound();
			item.setByte("Slot", (byte) i);
			getStackInSlot(i).writeToNBT(item);
			items.appendTag(item);
		}
	}

	compound.setTag(tagName, items);
}

public void readFromNBT(NBTTagCompound compound)
{
	NBTTagList items = compound.getTagList(tagName, 0);

	for (int i = 0; i < items.tagCount(); ++i)
	{
		NBTTagCompound item = (NBTTagCompound) items.getCompoundTagAt(i);
		byte slot = item.getByte("Slot");

		if (slot >= 0 && slot < getSizeInventory()) inventory[slot] = ItemStack.loadItemStackFromNBT(item);
	}
}
}

 

 

Container Class

 

package com.backpack.gui;

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.Item;
import net.minecraft.item.ItemArmor;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.util.IIcon;

import com.backpack.inventories.InventoryPlayerSurvival;
import com.backpack.items.ItemBackpack;
import com.backpack.slots.SlotBackpack;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class ContainerPlayerSurvival extends Container
{
public InventoryCrafting craftMatrix = new InventoryCrafting(this, 2, 2);
    public IInventory craftResult = new InventoryCraftResult();
    private final EntityPlayer thePlayer;

public ContainerPlayerSurvival(EntityPlayer player, InventoryPlayer inventoryPlayer, InventoryPlayerSurvival inventoryCustom)
{
	thePlayer = player;

	addSlotToContainer(new SlotBackpack(inventoryCustom, 0, -10, 26));

        int i;
        int j;

        addSlotToContainer(new SlotCrafting(inventoryPlayer.player, craftMatrix, craftResult, 0, 144, 36));
        
        for (i = 0; i < 2; ++i)
        {
            for (j = 0; j < 2; ++j)
            {
                addSlotToContainer(new Slot(craftMatrix, j + i * 2, 88 + j * 18, 26 + i * 18));
            }
        }

        for (i = 0; i < 4; ++i)
        {
            final int k = i;
            addSlotToContainer(new Slot(inventoryPlayer, inventoryPlayer.getSizeInventory() - 1 - i, 8, 8 + i * 18)
            {
                public int getSlotStackLimit()
                {
                    return 1;
                }

                public boolean isItemValid(ItemStack itemstack)
                {
                    if (itemstack == null) return false;
                    return itemstack.getItem().isValidArmor(itemstack, k, thePlayer);
                }

                @SideOnly(Side.CLIENT)
                public IIcon getBackgroundIconIndex()
                {
                    return ItemArmor.func_94602_b(k);
                }
            });
        }

        for (i = 0; i < 3; ++i)
        {
            for (j = 0; j < 9; ++j)
            {
                addSlotToContainer(new Slot(inventoryPlayer, j + (i + 1) * 9, 8 + j * 18, 84 + i * 18));
            }
        }

        for (i = 0; i < 9; ++i)
        {
            addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 142));
        }

        onCraftMatrixChanged(craftMatrix);
}

public void onCraftMatrixChanged(IInventory p_75130_1_)
    {
        craftResult.setInventorySlotContents(0, CraftingManager.getInstance().findMatchingRecipe(craftMatrix, thePlayer.worldObj));
    }

    public void onContainerClosed(EntityPlayer p_75134_1_)
    {
        super.onContainerClosed(p_75134_1_);

        for (int i = 0; i < 4; ++i)
        {
            ItemStack itemstack = craftMatrix.getStackInSlotOnClosing(i);

            if (itemstack != null)
            {
                p_75134_1_.dropPlayerItemWithRandomChoice(itemstack, false);
            }
        }

        craftResult.setInventorySlotContents(0, (ItemStack)null);
    }

@Override
public boolean canInteractWith(EntityPlayer player)
{
	return true;
}

@Override
public ItemStack transferStackInSlot(EntityPlayer player, int slt)
{
	ItemStack itemstack = null;
	Slot slot = (Slot) inventorySlots.get(slt);

	if(slot != null && slot.getHasStack())
	{
		ItemStack itemstack2 = slot.getStack();
		itemstack = itemstack2.copy();

			if(itemstack2 != null)
			{
				Item item = itemstack2.getItem();

				if(slt >= 10 && item instanceof ItemBackpack)
				{
					if (!mergeItemStack(itemstack2, 0, 1, true))
					{
						return null;
					}

					slot.onSlotChange(itemstack2, itemstack);
				}
		}

		if (itemstack2.stackSize == 0) 
		{
			slot.putStack((ItemStack) null);
		} 
		else 
		{
			slot.onSlotChanged();
		}

		if (itemstack2.stackSize == itemstack.stackSize) 
		{
			return null;
		}

		slot.onPickupFromSlot(player, itemstack2);
	}

	return itemstack;
}
}

 

Link to comment
Share on other sites

These are always tricky to debug, but the first place I'd start is your read/write to NBT methods in your IInventory class; specifically, in the read you have:

compound.getTagList(tagName, 0);

The 2nd argument is supposed to be the type of data stored in the list, in this case NBTTagCompounds, the type of which is represented by the constant Constants.NBT.TAG_COMPOUND, or you can use the magic number 10. I prefer the constant.

 

Now, I don't know if that is the sole cause of your issue, but it's a place to begin.

 

Also, your use of @Override is sporadic - some methods have it, but others that should, don't. Is this simply unintentional, or did you remove it from those other methods because it 'gave you an error' ? I didn't check the method signatures very carefully, but if your answer is the second, you will want to put it back and fix the method signatures.

Link to comment
Share on other sites

The use of @Override is was unintentional I have kept redoing parts of code and just forgotten to add the @Override back on. With regards to the NBT methods, are these supposed to be overrided because currently mine aren't and I am not sure why, and I am not talking about just adding the @Override annotation if you're wondering.

Link to comment
Share on other sites

Well, it looks like you are trying to replace the player inventory, so have you implemented an IExtendedEntityProperties class that saves and loads the extra slots?

 

The IInventory specification does not have any save or load methods, so any that you have would either be inherited from the parent class (which you don't have, and that's fine), or you need to make sure you call the methods yourself at an appropriate time (such as from IEEP when it saves and loads).

Link to comment
Share on other sites

I was already calling the methods in my ExtendedPlayer class... However, I have sort of made progress. I copiedthe decrStackSize method from the InventoryBasic class and now instead of just disappearing the item is dumped onto the floor whenever I try to click on it in my custom slot.

 

Updated Inventory Class

 

package com.backpack.inventories;

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.minecraftforge.common.util.Constants;

public class InventoryPlayerSurvival implements IInventory
{
private final String name = "Player Survival Inventory";
private final String tagName = "PSI";

public static final int INV_SIZE = 1;

private ItemStack[] inventory = new ItemStack[iNV_SIZE];

public InventoryPlayerSurvival() 
{

}

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

@Override
public ItemStack getStackInSlot(int slot)
    {
        return slot >= 0 && slot < this.inventory.length ? this.inventory[slot] : null;
    }

@Override	
public ItemStack decrStackSize(int slot, int amount)
    {
        if (this.inventory[slot] != null)
        {
            ItemStack itemstack;

            if (this.inventory[slot].stackSize <= amount)
            {
                itemstack = this.inventory[slot];
                this.inventory[slot] = null;
                this.markDirty();
                return itemstack;
            }
            else
            {
                itemstack = this.inventory[slot].splitStack(amount);

                if (this.inventory[slot].stackSize == 0)
                {
                    this.inventory[slot] = null;
                }

                this.markDirty();
                return itemstack;
            }
        }
        else
        {
            return null;
        }
    }

@Override
public ItemStack getStackInSlotOnClosing(int slot) 
{
	ItemStack stack = getStackInSlot(slot);
	setInventorySlotContents(slot, null);

	return null;
}

@Override
public void setInventorySlotContents(int slot, ItemStack itemstack) 
{
	inventory[slot] = itemstack;

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

@Override
public String getInventoryName() 
{
	return name;
}

@Override
public boolean hasCustomInventoryName() 
{
	return name.length() > 0;
}

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

@Override
public void markDirty() 
{

}

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

@Override
public void openInventory() 
{

}

@Override
public void closeInventory() 
{

}

@Override
public boolean isItemValidForSlot(int p_94041_1_, ItemStack p_94041_2_) 
{
	return true;
}

public void writeToNBT(NBTTagCompound compound)
{
	NBTTagList items = new NBTTagList();

	for (int i = 0; i < getSizeInventory(); ++i)
	{
		if (getStackInSlot(i) != null)
		{
			NBTTagCompound item = new NBTTagCompound();
			item.setByte("Slot", (byte) i);
			getStackInSlot(i).writeToNBT(item);
			items.appendTag(item);
		}
	}

	compound.setTag(tagName, items);
}

public void readFromNBT(NBTTagCompound compound)
{
	NBTTagList items = compound.getTagList(tagName, Constants.NBT.TAG_COMPOUND);

	for (int i = 0; i < items.tagCount(); ++i)
	{
		NBTTagCompound item = (NBTTagCompound) items.getCompoundTagAt(i);
		byte slot = item.getByte("Slot");

		if (slot >= 0 && slot < getSizeInventory()) inventory[slot] = ItemStack.loadItemStackFromNBT(item);
	}
}
}

 

Link to comment
Share on other sites

Okay scrap all of that. It now turns out that I can pick up the item if I click on the right hand side of my slot, but if I click on the left hand side it then pops out. Could this be to do with the GUI size being smaller than the actual image or something?

 

Container

 

package com.backpack.gui;

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.Item;
import net.minecraft.item.ItemArmor;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.util.IIcon;

import com.backpack.inventories.InventoryPlayerSurvival;
import com.backpack.items.ItemBackpack;
import com.backpack.slots.SlotBackpack;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class ContainerPlayerSurvival extends Container
{
public InventoryCrafting craftMatrix = new InventoryCrafting(this, 2, 2);
    public IInventory craftResult = new InventoryCraftResult();
    private final EntityPlayer thePlayer;

public ContainerPlayerSurvival(EntityPlayer player, InventoryPlayer inventoryPlayer, InventoryPlayerSurvival inventoryCustom)
{
	thePlayer = player;

	addSlotToContainer(new SlotBackpack(inventoryCustom, 0, -10, 26));

        int i;
        int j;

        addSlotToContainer(new SlotCrafting(inventoryPlayer.player, craftMatrix, craftResult, 0, 144, 36));
        
        for (i = 0; i < 2; ++i)
        {
            for (j = 0; j < 2; ++j)
            {
                addSlotToContainer(new Slot(craftMatrix, j + i * 2, 88 + j * 18, 26 + i * 18));
            }
        }

        for (i = 0; i < 4; ++i)
        {
            final int k = i;
            addSlotToContainer(new Slot(inventoryPlayer, inventoryPlayer.getSizeInventory() - 1 - i, 8, 8 + i * 18)
            {
                public int getSlotStackLimit()
                {
                    return 1;
                }

                public boolean isItemValid(ItemStack itemstack)
                {
                    if (itemstack == null) return false;
                    return itemstack.getItem().isValidArmor(itemstack, k, thePlayer);
                }

                @SideOnly(Side.CLIENT)
                public IIcon getBackgroundIconIndex()
                {
                    return ItemArmor.func_94602_b(k);
                }
            });
        }

        for (i = 0; i < 3; ++i)
        {
            for (j = 0; j < 9; ++j)
            {
                addSlotToContainer(new Slot(inventoryPlayer, j + (i + 1) * 9, 8 + j * 18, 84 + i * 18));
            }
        }

        for (i = 0; i < 9; ++i)
        {
            addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 142));
        }

        onCraftMatrixChanged(craftMatrix);
}

public void onCraftMatrixChanged(IInventory p_75130_1_)
    {
        craftResult.setInventorySlotContents(0, CraftingManager.getInstance().findMatchingRecipe(craftMatrix, thePlayer.worldObj));
    }

    public void onContainerClosed(EntityPlayer p_75134_1_)
    {
        super.onContainerClosed(p_75134_1_);

        for (int i = 0; i < 4; ++i)
        {
            ItemStack itemstack = craftMatrix.getStackInSlotOnClosing(i);

            if (itemstack != null)
            {
                p_75134_1_.dropPlayerItemWithRandomChoice(itemstack, false);
            }
        }

        craftResult.setInventorySlotContents(0, (ItemStack)null);
    }

@Override
public boolean canInteractWith(EntityPlayer player)
{
	return true;
}

@Override
public ItemStack transferStackInSlot(EntityPlayer player, int slt)
{
	ItemStack itemstack = null;
	Slot slot = (Slot) inventorySlots.get(slt);

	if(slot != null && slot.getHasStack())
	{
		ItemStack itemstack2 = slot.getStack();
		itemstack = itemstack2.copy();

			if(itemstack2 != null)
			{
				Item item = itemstack2.getItem();

				if(slt >= 10 && item instanceof ItemBackpack)
				{
					if (!mergeItemStack(itemstack2, 0, 1, true))
					{
						return null;
					}

					slot.onSlotChange(itemstack2, itemstack);
				}
		}

		if (itemstack2.stackSize == 0) 
		{
			slot.putStack((ItemStack) null);
		} 
		else 
		{
			slot.onSlotChanged();
		}

		if (itemstack2.stackSize == itemstack.stackSize) 
		{
			return null;
		}

		slot.onPickupFromSlot(player, itemstack2);
	}

	return itemstack;
}
}

 

 

GUI

 

package com.backpack.gui;

import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.util.ResourceLocation;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;

import com.backpack.inventories.InventoryPlayerSurvival;
import com.backpack.references.References;

public class GuiPlayerSurvival extends GuiContainer
{
private float xSize_lo;

private float ySize_lo;

private static final ResourceLocation iconLocation = new ResourceLocation(References.MODID + ":textures/gui/inventory.png");

private final InventoryPlayerSurvival inventory;

public GuiPlayerSurvival(EntityPlayer player, InventoryPlayer inventoryPlayer, InventoryPlayerSurvival inventoryCustom)
{
	super(new ContainerPlayerSurvival(player, inventoryPlayer, inventoryCustom));
	this.inventory = inventoryCustom;

	xSize += 18;
}

public void drawScreen(int mouseX, int mouseY, float f)
{
	super.drawScreen(mouseX, mouseY, f);
	xSize_lo = mouseX;
	ySize_lo = mouseY;
}

protected void drawGuiContainerBackgroundLayer(float f, int mouseX, int mouseY)
{
	GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
	mc.getTextureManager().bindTexture(iconLocation);
	drawTexturedModalRect(guiLeft - 18, guiTop, 0, 0, xSize, ySize);
	drawPlayerModel(guiLeft + 51, guiTop + 75, 30, guiLeft + 51 - xSize_lo, guiTop + 25 - ySize_lo, mc.thePlayer);
}

public static void drawPlayerModel(int x, int y, int scale, float yaw, float pitch, EntityLivingBase entity)
{
	GL11.glEnable(GL11.GL_COLOR_MATERIAL);
	GL11.glPushMatrix();
	GL11.glTranslatef(x, y, 50.0F);
	GL11.glScalef(-scale, scale, scale);
	GL11.glRotatef(180.0F, 0.0F, 0.0F, 1.0F);
	float f2 = entity.renderYawOffset;
	float f3 = entity.rotationYaw;
	float f4 = entity.rotationPitch;
	float f5 = entity.prevRotationYawHead;
	float f6 = entity.rotationYawHead;
	GL11.glRotatef(135.0F, 0.0F, 1.0F, 0.0F);
	RenderHelper.enableStandardItemLighting();
	GL11.glRotatef(-135.0F, 0.0F, 1.0F, 0.0F);
	GL11.glRotatef(-((float) Math.atan(pitch / 40.0F)) * 20.0F, 1.0F, 0.0F, 0.0F);
	entity.renderYawOffset = (float) Math.atan(yaw / 40.0F) * 20.0F;
	entity.rotationYaw = (float) Math.atan(yaw / 40.0F) * 40.0F;
	entity.rotationPitch = -((float) Math.atan(pitch / 40.0F)) * 20.0F;
	entity.rotationYawHead = entity.rotationYaw;
	entity.prevRotationYawHead = entity.rotationYaw;
	GL11.glTranslatef(0.0F, entity.yOffset, 0.0F);
	RenderManager.instance.playerViewY = 180.0F;
	RenderManager.instance.renderEntityWithPosYaw(entity, 0.0D, 0.0D, 0.0D, 0.0F, 1.0F);
	entity.renderYawOffset = f2;
	entity.rotationYaw = f3;
	entity.rotationPitch = f4;
	entity.prevRotationYawHead = f5;
	entity.rotationYawHead = f6;
	GL11.glPopMatrix();
	RenderHelper.disableStandardItemLighting();
	GL11.glDisable(GL12.GL_RESCALE_NORMAL);
	OpenGlHelper.setActiveTexture(OpenGlHelper.lightmapTexUnit);
	GL11.glDisable(GL11.GL_TEXTURE_2D);
	OpenGlHelper.setActiveTexture(OpenGlHelper.defaultTexUnit);
}
}

 

Link to comment
Share on other sites

Okay scrap all of that. It now turns out that I can pick up the item if I click on the right hand side of my slot, but if I click on the left hand side it then pops out. Could this be to do with the GUI size being smaller than the actual image or something?

 

Yes, that's why the gui class has properties like xSize and ySize.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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.