Jump to content

Recommended Posts

Posted

Hi,

 

The title says it: I want to make a kind of backpack.

I managed to open an Inventory on rightclick and everything but for some reason I can't actually SAVE the ItemStacks :(

 

The problem lies somewhere between putting an Item in and saving the NBTTagCompund with the Stack

The strange thing is, that it randomly works and as soon as I close out of the world it breaks again with absolutely no code changes... (I wish it would crash, then I had a clue where my problem is)

 

I think it has something to do with Client/Server Synchronisation and the GUIs since I can store the amount of rightclicks just fine with NBT data

 

Can somebody please look over my code and point out any glaring "What an Idiot" Errors? (I'm a noob in Java)

 

Thanks in advance for any help!

 

Code (MCP-725):

 

 

GuiHandler:

// Server Side:
ItemStack packet = player.inventory.getCurrentItem();
			if (packet.itemID == Generic.itemLunchPacket.shiftedIndex) {
				return new ContainerLunchPacket(player.inventory, packet);
			}

// Client Side:
ItemStack packet = player.inventory.getCurrentItem();
			if (packet.itemID == Generic.itemLunchPacket.shiftedIndex) {
				return new GuiLunchPacket(player.inventory, packet);
			}

 

GuiLunchPacket Constructor:

public GuiLunchPacket(InventoryPlayer inventoryPlayer, ItemStack stack) {
	super(new ContainerLunchPacket(inventoryPlayer, stack));
	// TODO: Constructor
}

 

ItemLunchPacket:

package tutorial.generic;

import java.util.List;

import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;

public class ItemLunchPacket extends Item /*implements IInventory*/ {

//private ItemStack[] inv;

public ItemLunchPacket(int par1) {
	super(par1);
	this.setMaxStackSize(2);
	this.setCreativeTab(Generic.genericTab);
	this.setIconIndex(16);
	this.setItemName("genericItemLunchPaket");

	//this.inv = new ItemStack[9];
}

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

@Override
public String getTextureFile() {
	return CommonProxy.ITEMS_PNG;
}

/*@Override
public int getSizeInventory() {
	return this.inv.length;
}

@Override
public ItemStack getStackInSlot(int slot) {
	return this.inv[slot];
}

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

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

	return stack;
}

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

	return stack;
}

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

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

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

@Override
public void onInventoryChanged() {
	// TODO Auto-generated method stub
	FMLLog.info("onInvChanged");
}

@Override
public boolean isUseableByPlayer(EntityPlayer player) {
	// TODO Auto-generated method stub
	return true;
}

@Override
public void openChest() {}

@Override
public void closeChest() {}*/

@Override
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) {
	/*if (world.isRemote) {
		return stack;
	}*/
	player.openGui(Generic.instance, Generic.lunchPacketGui, player.worldObj, (int) player.posX, (int) player.posY, (int) player.posZ);
	FMLLog.info("Opened?");
	return stack;
}

@Override
public void addInformation(ItemStack stack, EntityPlayer player, List tooltipList, boolean par4) {
	if (stack.hasTagCompound()) {
		tooltipList.add(stack.stackTagCompound.toString());
		if (stack.stackTagCompound.hasKey("randomString")) {
			tooltipList.add(stack.stackTagCompound.getString("randomString"));
		}
	}
}
}

 

InventoryStackWrapper:

package tutorial.generic;

import cpw.mods.fml.common.FMLLog;
import net.minecraft.block.Block;
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.nbt.NBTTagString;

public class InventoryStackWrapper implements IInventory {

protected ItemStack itemStack;
protected ItemStack[] invStacks;

public InventoryStackWrapper(ItemStack stack) {
	super();
	if (!stack.hasTagCompound()) {
		stack.stackTagCompound = new NBTTagCompound();
	}

	FMLLog.info("Stack Info: " + stack);
	FMLLog.info("Constructor: " + stack + " Hash: " + stack.hashCode());
	stack.stackTagCompound.setString("randomString", "Test String");

	//setInventorySlotContents(1, new ItemStack(Block.dirt, 10));

	/*if (stack.stackTagCompound.hasKey("Inventory")) {

	}*/
	this.itemStack = stack;
	this.invStacks = new ItemStack[9];
	this.loadFromStack();
}

@Override
public int getSizeInventory() {
	return 9;
}

@Override
public ItemStack getStackInSlot(int slot) {
	return this.invStacks[slot];

	/*NBTTagList stackList = this.itemStack.stackTagCompound.getTagList("Inventory");
	for (int i = 0; i < stackList.tagCount(); i++) {
		NBTTagCompound tag = (NBTTagCompound) stackList.tagAt(i);
		byte currSlot = tag.getByte("Slot");
		if (currSlot == slot) {
			return ItemStack.loadItemStackFromNBT(tag);
		}
	}

	//FMLLog.info(this.itemStack.stackTagCompound.toString());

	return null;*/
}

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

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

	return stack;
}

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

	return stack;
}

@Override
public void setInventorySlotContents(int slot, ItemStack stack) {
	this.invStacks[slot] = stack;
	this.itemStack.stackTagCompound.setString("randomString", "Test String50");
	this.saveToStack();
	return;

	//NBTTagList stackList = this.itemStack.stackTagCompound.getTagList("Inventory");
	/*for (int i = 0; i < stackList.tagCount(); i++) {
		NBTTagCompound tag = (NBTTagCompound) stackList.tagAt(i);
		byte currSlot = tag.getByte("Slot");
		if (currSlot == slot) {
			stackList.removeTag(i);
		}
	}*/

	/*stack = new ItemStack(Block.cobblestone, 10);

	synchronized(this) {
		if (stack != null) {
			FMLLog.info("Cobblee!!!!!");
			NBTTagCompound tag = new NBTTagCompound();
			tag.setByte("Slot", (byte) slot);
			stack.writeToNBT(tag);
			stackList.appendTag(tag);
		}
	}*/
}

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

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

@Override
public void onInventoryChanged() {
	FMLLog.info("OnChange: " + this.itemStack + " Hash: " + this.itemStack.hashCode());
	this.itemStack.stackTagCompound.setString("randomString", "Test String61");
	//this.saveToStack();
}

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

@Override
public void openChest() {}

@Override
public void closeChest() {}

private void saveToStack() {
	synchronized(this) {
		FMLLog.info("SaveRandStr: " + this.itemStack.stackTagCompound.getString("randomString"));
		NBTTagList stackList = new NBTTagList();
		for (int slot = 0; slot < this.invStacks.length; slot++) {
			if (this.invStacks[slot] != null) {
				NBTTagCompound tag = new NBTTagCompound();
				tag.setByte("Slot", (byte) slot);
				this.invStacks[slot].writeToNBT(tag);
				stackList.appendTag(tag);
			}
		}
		this.itemStack.stackTagCompound.setTag("Inventory", stackList);
		this.itemStack.stackTagCompound.removeTag("randomString");
		this.itemStack.stackTagCompound.setString("randomString", "Test String2");
	}
}

private void loadFromStack() {
	synchronized(this) {
		FMLLog.info("RandStr: " + this.itemStack.stackTagCompound.getString("randomString"));
		NBTTagList stackList = this.itemStack.stackTagCompound.getTagList("Inventory");
		if (stackList != null) {
			for (int i = 0; i < stackList.tagCount(); i++) {
				NBTTagCompound tag = (NBTTagCompound) stackList.tagAt(i);
				byte slot = tag.getByte("Slot");
				if (slot >= 0 && slot < this.invStacks.length) {
					this.invStacks[slot] = ItemStack.loadItemStackFromNBT(tag);
				}
			}
		}
	}
}
}

 

ContainerLunchPacket:

package tutorial.generic;

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

public class ContainerLunchPacket extends Container {

protected ItemStack bagStack;
protected InventoryStackWrapper wrapper;

public ContainerLunchPacket(InventoryPlayer inventoryPlayer, ItemStack stack) {
	this.bagStack = stack;
	this.wrapper = new InventoryStackWrapper(stack);

	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			addSlotToContainer(new Slot(this.wrapper, j + i * 3, 62 + j * 18, 17 + i * 18));
		}
	}

	//commonly used vanilla code that adds the player's inventory
	bindPlayerInventory(inventoryPlayer);
}

@Override
public boolean canInteractWith(EntityPlayer player) {
	// TODO Auto-generated method stub
	return true;
}

protected void bindPlayerInventory(InventoryPlayer inventoryPlayer) {
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 9; j++) {
			addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
		}
	}

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

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

	// null checks and checks if the item can be stacked (maxStackSize > 1)
	if (slotObject != null && slotObject.getHasStack()) {
		ItemStack stackInSlot = slotObject.getStack();
		stack = stackInSlot.copy();

		// merges the item into player inventory since its in the tileEntity
		if (slot < 9) {
			if (!this.mergeItemStack(stackInSlot, 9, 45, true)) {
				return null;
			}
		} else if (!this.mergeItemStack(stackInSlot, 0, 9, false)) {
			// places it into the tileEntity is possible since its in the player inventory
			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;
}
}

 

 

Posted

which methods do you mean?

writeToNBT and loadFromNBT?

 

In InventoryStackWrapper is my save and load code:

 

 

private void saveToStack() {
	synchronized(this) {
		FMLLog.info("SaveRandStr: " + this.itemStack.stackTagCompound.getString("randomString"));
		NBTTagList stackList = new NBTTagList();
		for (int slot = 0; slot < this.invStacks.length; slot++) {
			if (this.invStacks[slot] != null) {
				NBTTagCompound tag = new NBTTagCompound();
				tag.setByte("Slot", (byte) slot);
				this.invStacks[slot].writeToNBT(tag);
				stackList.appendTag(tag);
			}
		}
		this.itemStack.stackTagCompound.setTag("Inventory", stackList);
		this.itemStack.stackTagCompound.removeTag("randomString");
		this.itemStack.stackTagCompound.setString("randomString", "Test String2");
	}
}

private void loadFromStack() {
	synchronized(this) {
		FMLLog.info("RandStr: " + this.itemStack.stackTagCompound.getString("randomString"));
		NBTTagList stackList = this.itemStack.stackTagCompound.getTagList("Inventory");
		if (stackList != null) {
			for (int i = 0; i < stackList.tagCount(); i++) {
				NBTTagCompound tag = (NBTTagCompound) stackList.tagAt(i);
				byte slot = tag.getByte("Slot");
				if (slot >= 0 && slot < this.invStacks.length) {
					this.invStacks[slot] = ItemStack.loadItemStackFromNBT(tag);
				}
			}
		}
	}
}

 

 

(I have no idea what synchronized(this) does, I kinda stole it from EnderStorage  ;D)

 

the strange thing is that when I go trough the code step by step and watch the ItemStack object, I can see, that the NBTTag is properly written and in game it works too.

But when I run the code without the debugger it doesn't work!

 

Another thing I noticed in debug mode was, that the stack object of InventoryStackWrapper(ItemStack stack) changes, if it's referenced outside the constructor scope so the initial load seems to work but not the save later on.

 

Since I have no idea how the memory management in java works, i have no idea what could be causing that :(

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.