Jump to content

Disappearing Container Items


CJLetsGame

Recommended Posts

I have a multipage chest container. When I shift-click items out of the chest, they flash briefly in my inventory and then disappear. Normal clicking and moving keeps the items until I reopen the container and then they disappear. Shift-clink only does this with full stacks it seems.

 

It should be noted the inventory of the pages is a shared inventory like the Ender Chest.

 

ChestPage

package CJTech.inventory;

import java.util.logging.Level;
import java.util.logging.Logger;

import CJTech.tileentity.TileEntityEnderNetTerminal;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;

public class EnderNetItemPage implements IInventory{
private TileEntityEnderNetTerminal terminal;
    private int page;
    public EnderNetItemPage(TileEntityEnderNetTerminal terminal, int page)
    {
        this.terminal = terminal;
        this.page = page;
    }
@Override
public int getSizeInventory() {return 117;}
@Override
public ItemStack getStackInSlot(int i) {
	return terminal.GetStackInSlot((page * 117) + i);
	}
@Override
public ItemStack decrStackSize(int i, int j) {
	return terminal.decrStack((page * 117) + i, j);
}
@Override
public ItemStack getStackInSlotOnClosing(int i) {return null;}
@Override
public void setInventorySlotContents(int i, ItemStack itemstack) {
	//Treat as AddStack
	//Logger.getLogger("CJTECH").log(Level.INFO, "Adding: " + itemstack.stackSize);
	if (itemstack != null)
		if(CJTech.CJTech.instance.isSimulating()) //server handled by actual EnderNet
		{
			terminal.AddStack(itemstack);
		}
		else
		{

		}
}
@Override
public String getInvName() {return "Ender Net";}
@Override
public boolean isInvNameLocalized() {return true;}
@Override
public int getInventoryStackLimit() {return 64;}
@Override
public void onInventoryChanged() {}
@Override
public boolean isUseableByPlayer(EntityPlayer entityplayer) {return true;}
@Override
public void openChest() {}
@Override
public void closeChest() {}
@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack) {return true;}

}

 

 

Chest Container

package CJTech.inventory;

import java.util.List;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ICrafting;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

public class ContainerEnderNetItemTerminal extends Container{
public EnderNetItemPage itemPages;

public ContainerEnderNetItemTerminal(IInventory invPlayer, EnderNetItemPage itemPages)
    {
        this.itemPages = itemPages;
        for(int row = 0; row < 9; row++)
        {
            for(int col = 0; col < 13; col++)
            {
                addSlotToContainer(new Slot(itemPages, col + row * 13, 12 + col * 18, 8 + row * 18));
            }

        }

        for(int invPlayerRow = 0; invPlayerRow < 3; invPlayerRow++)
        {
            for(int invPlayerCol = 0; invPlayerCol < 9; invPlayerCol++)
            {
            	addSlotToContainer(new Slot(invPlayer, (invPlayerCol + invPlayerRow * 9) + 9, 48 + invPlayerCol * 18, 174 + invPlayerRow * 18));
            }

        }

        for(int hotbatSlot = 0; hotbatSlot < 9; hotbatSlot++)
        {
        	addSlotToContainer(new Slot(invPlayer, hotbatSlot, 48 + hotbatSlot * 18, 232));
        }

//        ItemStorage = new IInventory();
//        addSlotToContainer(new Slot(ItemStorage, 0, 228, 232));
        //this.inventoryItemStacks.clear();
    }

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

public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2)
    {
	ItemStack itemstack = null;
        Slot slot = (Slot)this.inventorySlots.get(par2);

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

            if (par2 < 117)
            {
                if (!this.mergeItemStack(itemstack1, 117, this.inventorySlots.size(), true))
                {
                    return null;
                }
            }
            else if (!this.mergeItemStack(itemstack1, 0, 117, false))
            {
                return null;
            }

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

        return itemstack;
    }
}

 

 

Class used to sync inventory

package CJTech.endernet;

import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

import CJTech.power.IPowerSink;
import CJTech.util.CJTechFileIO;
import CJTech.util.DebugLogger;
import net.minecraft.client.Minecraft;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;

public class EnderNet implements IPowerSink{
String SaveDir;
public List<ItemStack> stacks = new ArrayList<ItemStack>();
public long Power = 0;
public final long MaxPower = Long.MAX_VALUE;
public boolean isLoaded = false;

@Override
public long RecievePower(long Power, boolean Sim) {
	this.Power += Power;
	return 0;
}

@Override
public boolean canRecievePower() {
	return true;
}

/**
 * Removes power from the EnderNet
 * @param Amount Amount of power to drain/remove
 */
public void RemovePower(long Amount)
{
	Power -= Amount;
	if (Power < 0)
		Power = 0;
}
/**
 * Checks if EnderNet has the ItemStack with ID, Meta, and at least a certain stack size
 * @param stack
 * @return
 */
protected boolean ContainsItemStack(ItemStack stack)
{
	for (int f = 0; f < stacks.size(); f++)
	{
		if (stacks.get(f).itemID == stack.itemID)
		{
			if (stacks.get(f).getItemDamage() == stack.getItemDamage())
			{
				if (stacks.get(f).stackSize >= stack.stackSize)
				{
					return true;
				}
			}
		}
	}

	return false;
}

protected int IndexOfStack(ItemStack stack)
{
	if (stack == null)
		return -1;

	for (int f = 0; f < stacks.size(); f++)
	{
		if (stacks.get(f).itemID == stack.itemID)
		{
			if (stacks.get(f).getItemDamage() == stack.getItemDamage())
			{
				return f;
			}
		}
	}
	return -1;
}

public ItemStack RetrieveStack(ItemStack stack)
{
	int index = IndexOfStack(stack);
	if (index == -1)
		return null;
	ItemStack R = stacks.get(index).splitStack(stack.stackSize);
	if (stacks.get(index).stackSize <= 0)
		stacks.remove(index);

	return R;
	//return null;
}
public ItemStack RetrieveStack(int Slot, int Amount)
{
	if (Slot >= stacks.size())
		return null;
	if (Slot == -1)
		return null;

	if (!CJTech.CJTech.instance.isSimulating()) //Client
		return new ItemStack(stacks.get(Slot).itemID, Amount, stacks.get(Slot).getItemDamage());
	else //Server
	{
		stacks.get(Slot).stackSize -= Amount;
		if (stacks.get(Slot).stackSize <= 0)
			stacks.remove(Slot);
		return null;
	}
	//return null;
}

public void AddStack(ItemStack stack)
{

	boolean Server = CJTech.CJTech.instance.isSimulating();
	DebugLogger.log("ENet Stack Size: " + stack.stackSize);
	if (stacks.size() == 0)
	{
		stacks.add(stack);
		DebugLogger.log("ENet Stack Added as new Stack, size was 0");
		return;
	}



	//Try Merging Stacks
	for (int f = 0; f < stacks.size(); f++)
	{
		if (stacks.get(f).itemID == stack.itemID & stacks.get(f).getItemDamage() == stack.getItemDamage())
		{
			if (stacks.get(f).stackSize + stack.stackSize <= stacks.get(f).getMaxStackSize())
			{
				DebugLogger.log("ENet Stack Can Merge...: " + stack.stackSize + "|" + stacks.get(f).stackSize);
				stacks.get(f).stackSize += stack.stackSize;
				return;
			}
			else
			{
				int ToAdd = stacks.get(f).getMaxStackSize() - stacks.get(f).stackSize;
				DebugLogger.log("ENet Adding: " + ToAdd + " to stack.");
				stacks.get(f).stackSize += ToAdd;
				stack.stackSize -= ToAdd;
				//DebugLogger.log("ENet Putting rest in new stack");
				//stacks.add(new ItemStack(stack.itemID, stack.stackSize - ToAdd, stack.getItemDamage()));
				//return;
			}
		}

		if (stack.stackSize <= 0)
			return;
	}

	//Add New Stack
	stacks.add(stack);
}

public ItemStack getStackInSlot(int i)
{
	if (i < stacks.size())
		return stacks.get(i);
	else
		return null;
}

public List getInventory()
{
	return stacks;
}

}

Link to comment
Share on other sites

Update:

Im almost 100% sure this is a sync issue between the server and client. The problem only occurs when re-opening the gui/container. I checked the inventory of the player when the client gui element is opened, this was the correct inventory. However, the container element didnt have the updated player inventory. How do I ensure the inventory gets synced? I assumed Packet5PlayerInventory would work, then I realized Mojang doesnt handle it.

Link to comment
Share on other sites

You don't need to implement or use any packets to synch inventories.

 

My guess is that somewhere along the line you've improperly set up your gui / container / tileentity classes, but I wouldn't know where to look.

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

The ItemPage isnt useless because you can only access one page of the chest at a time. The container doesnt have access to every ItemStack at once.

 

GuiHandler

public class GuiHandler  implements IGuiHandler{

@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
	TileEntity tile_entity = world.getBlockTileEntity(x, y, z);
	int BlockID = world.getBlockId(x, y, z);
	int Meta = world.getBlockMetadata(x, y, z);

	if (BlockID == Blocks.blockEnderNetTerminal.blockID)
	{
		if (Meta == 1)
		{
			return new ContainerEnderNetItemTerminal(player, new EnderNetItemPage((TileEntityEnderNetTerminal) tile_entity, 0));
		}
	}
	return null;

}

@Override
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
	TileEntity tile_entity = world.getBlockTileEntity(x, y, z);

	int BlockID = world.getBlockId(x, y, z);
	int Meta = world.getBlockMetadata(x, y, z);

	if (BlockID == Blocks.blockEnderNetTerminal.blockID)
	{
		if (Meta == 1)
			return new guiENetItemTerminal(player, (TileEntityEnderNetTerminal) tile_entity, 0);
	}
	return null;
}
}

Link to comment
Share on other sites

Fixed it myself. I guess in my sleep deprivation, I thought it was a good idea to store the player inventory in a separate field in the container. I have no idea why I did this as I didnt do it for any other container in my mod. Everything seems to work fine now that I fixed that.

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.