Jump to content

Recommended Posts

Posted

I'm having trouble with my inventory, I am trying to make a tabbed inventory that allows you to access more inventory space if you buy at tab.  The inventory itself is saved through the player data and it appears that the NBT data I set doesn't save with everything else when I use the inventory (the inventory saves itself but not the tab data)  Is there any cause of this that I am unaware and can easily fix? if not here is the code:

Gui:

public class GuiUltBag extends GuiContainer{

private InventoryUltBag invBag;
private InventoryPlayer invPlayer;
private GuiButton submit;
private List<CoolTab> tabList = new ArrayList<CoolTab>();

public GuiUltBag(InventoryPlayer invPlayer, InventoryUltBag invBag) {
	super(new ContainerUltBag(invPlayer, invBag));
	this.invBag = invBag;
	this.invPlayer = invPlayer;
	invPlayer.player.getEntityData().setBoolean("ultbag_tab0", true);
	xSize = 320;
	ySize = 239;
}

@Override
public void initGui() {
	invPlayer.player.getEntityData().setBoolean("ultbag_tab0", true);
	super.initGui();
	CoolTab MainTab = new CoolTab(this, "Items #1", 0, 10,6);
	MainTab.setType(CoolTab.TabType.TAB_ACTIVATED);
	tabList.add(MainTab);
	tabList.add(new CoolTab(this, "Items #2", 1, 10,30));
	tabList.add(new CoolTab(this, "Items #3", 2, 10,54));
	tabList.add(new CoolTab(this, "Items #4", 3, 10,78));
	tabList.add(new CoolTab(this, "Items #5", 4, 10,102));
	submit = new GuiButton(0,guiLeft + 10,guiTop + 210,60,20,"Submit");
	this.buttonList.add(submit);
}

private static final ResourceLocation texture = new ResourceLocation(ModInfo.TEXTURE_LOCATION, "textures/Gui/UltimateBag.png");

@Override
protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3) {
	GL11.glColor4f(1,1,1,1);

	Minecraft.getMinecraft().getTextureManager().bindTexture(texture);
	GuiHelper.drawTexturedRect(guiLeft, guiTop, 0, 0, xSize, ySize, 512, 512);
	for(CoolTab t : this.tabList){
		switch(t.getType()){
		case TAB_ACTIVATED:
			t.draw(this, guiLeft, guiTop, 64, 239, 512, 512);
			break;
		case TAB_DECATIVATED:
			t.draw(this, guiLeft, guiTop, 0, 239, 512, 512);
			break;
		case TAB_MOUSED:
			t.draw(this, guiLeft, guiTop, 32, 239, 512, 512);
			break;
		default:
			break;
		}
	}
}

@Override
protected void drawGuiContainerForegroundLayer(int x, int y) {
	super.drawGuiContainerForegroundLayer(x, y);
	for(CoolTab t : this.tabList){
		if(t.inRect(x, y, guiLeft, guiTop)){
			if(t.getTabName() != null && t.getTabName() != ""){
				this.drawHoveringText(Arrays.asList(t.getTabName().split("\n")), x - guiLeft, y - guiTop, fontRendererObj);
			}
			if(t.getType() != CoolTab.TabType.TAB_ACTIVATED){
				t.setType(CoolTab.TabType.TAB_MOUSED);
			}
		}else{
			if(t.getType() != CoolTab.TabType.TAB_ACTIVATED){
				t.setType(CoolTab.TabType.TAB_DECATIVATED);
			}
		}
	}
}

@Override
protected void mouseClicked(int x, int y, int button) {
	super.mouseClicked(x, y, button);
}

@Override
protected void mouseMovedOrUp(int mx, int my, int type) {
	super.mouseMovedOrUp(mx, my, type);
	for(CoolTab t : this.tabList){
		if(t.inRect(mx, my, guiLeft, guiTop) && this.invPlayer.player.getEntityData().getBoolean("coolstuff_ultbag_tab"+t.getInventoryNumber())){
			if(t.getType() != CoolTab.TabType.TAB_ACTIVATED){
				for(CoolTab ct : this.tabList){
					if(ct != t && ct.getType() == CoolTab.TabType.TAB_ACTIVATED){
						ct.setType(TabType.TAB_DECATIVATED);
					}
				}
				t.setType(TabType.TAB_ACTIVATED);
				if(this.inventorySlots instanceof ContainerUltBag){
					((ContainerUltBag)this.inventorySlots).changeTab(t);
				}
				t.setType(TabType.TAB_ACTIVATED);
				break;
			}
		}
	}
}

@Override
protected void actionPerformed(GuiButton button) {
	super.actionPerformed(button);
	if(button == this.submit){
		IMessage eMessage = new CoolPacket.CoolMessage(invPlayer.player.getEntityId(), 208, invPlayer.player.worldObj.provider.dimensionId);
		PacketHandler.net.sendToServer(eMessage);
		PacketHandler.net.sendToAll(eMessage);

		if(invPlayer.player.openContainer instanceof ContainerUltBag){
   				if(invPlayer.player.openContainer.getSlot(0).inventory.getStackInSlot(208) != null){
   					if(invPlayer.player.openContainer.getSlot(208).inventory.getStackInSlot(208).getItem() == Items.PHOENIX_CHARGE){
   						invPlayer.player.getEntityData().setBoolean("coolstuff_ultbag_tab1", true);
   					}
   				}
		}
	}
}

}

Container:

@ChestContainer(rowSize = 13, isLargeChest = true)
public class ContainerUltBag extends Container{

private InventoryUltBag invBag;
private InventoryPlayer invPlayer;
private int CurTab;

public ContainerUltBag(InventoryPlayer invPlayer, InventoryUltBag invBag){
	this.invBag = invBag;
	this.invPlayer = invPlayer;
	this.CurTab = 0;
	if(this.invBag!=null){
		for(int y = 0; y < 8; y++){
			for(int x = 0; x < 13; x++){
				this.addSlotToContainer(new UltBagSlot(invBag, x + y*13, 44 + 18 * x, 8 + 18 * y, 0));
			}
		}
		this.initSecondInv();
		this.changeTab(1);
		this.changeTab(0);
	}
	this.initPlayerInv();
}

@ContainerSectionCallback()
private void initPlayerInv(){
	for(int x = 0; x<9; x++){
		addSlotToContainer(new Slot(invPlayer, x, 80 + 18 * x, 215));
	}
	for(int y = 0; y<3;y++){
		for(int x = 0; x<9; x++){
			addSlotToContainer(new Slot(invPlayer, x + y*9 + 9, 80 + 18 * x, 157 + y * 18));
		}
	}
}

@ContainerSectionCallback()
private void initSecondInv(){
	for(int y = 0; y < 8; y++){
		for(int x = 0; x < 13; x++){
			this.addSlotToContainer(new UltBagSlot(invBag, x + y*13, 44 + 18 * x, 8 + 18 * y, 1));
		}
	}
	this.addSlotToContainer(new UltBagBuySlot(invBag, 104, 31, 189, 1));
}

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

@Override
public void onContainerClosed(EntityPlayer player) {
	invBag.closeInventory();
}

@Override
public ItemStack transferStackInSlot(EntityPlayer player, int islot){
	boolean b1 = true;
	boolean b2;
	Slot slot = getSlot(islot);
	if(slot != null && slot.getHasStack()){
		ItemStack stack = slot.getStack();
		ItemStack result = stack.copy();
		if(islot < 208){
			if(!mergeItemStack(stack,208,244,false)){
				return null;
			}
		}else if(!mergeItemStack(stack,this.CurTab * 104,((this.CurTab+1)*104)-1,false)){
			return null;
		}
		if(stack.stackSize == 0){
			slot.putStack(null);
			slot.onSlotChanged();
		}else{
			slot.onSlotChanged();
		}
		slot.onPickupFromSlot(player, stack);
		return result;
	}
	return null;
}

public void changeTab(CoolTab tab){
	this.changeTab(tab.getInventoryNumber());
}

public void changeTab(int displayNumber){
	int oldTab = CurTab;
	this.CurTab = displayNumber;
	for(int ix = 0; ix < this.inventorySlots.size(); ix++){
		if(((Slot)this.inventorySlots.get(ix)).inventory instanceof InventoryUltBag){

			if(this.inventorySlots.get(ix) instanceof UltBagSlot){
				UltBagSlot slot = (UltBagSlot) this.inventorySlots.get(ix);
				if(slot.getTab() == displayNumber){
						slot.xDisplayPosition = slot.getRealX();
				}else{
					if(!(slot instanceof UltBagBuySlot)){
						slot.xDisplayPosition = Integer.MIN_VALUE;
					}
				}
			}

		}
	}
}

}

IInventory:

public class InventoryUltBag implements IInventory{

ItemStack[] items;
private ItemStack invItem;
private InventoryPlayer p;
public InventoryUltBag(ItemStack invItem, InventoryPlayer p){
	items = new ItemStack[209];
	this.invItem = invItem;
	this.p = p;
	UltimateBag cp = (UltimateBag) this.invItem.getItem();
	ItemStack[] updateStack = null;
	updateStack = cp.getData(p);
	if(updateStack != null){
		for(int i = 0; i < updateStack.length; i++){
			this.setInventorySlotContents(i, updateStack[i]);
		}
	}
}

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

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

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

@Override
public ItemStack getStackInSlotOnClosing(int slot) {
	return this.getStackInSlot(slot);
}

@Override
public void setInventorySlotContents(int slot, ItemStack stack) {
	items[slot] = stack;
}

@Override
public String getInventoryName() {
	return "Ultimate Bag";
}

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

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

@Override
public void markDirty() {
	for (int i = 0; i < this.getSizeInventory(); ++i){
		if (getStackInSlot(i) != null && getStackInSlot(i).stackSize == 0){
			items[i] = null;
		}
	}
}

public void markDirty(ItemStack[] items){
	for(int i = 0; i < items.length; i++){
		this.items[i] = items[i];
	}
	for (int i = 0; i < this.getSizeInventory(); ++i){
		if (getStackInSlot(i) != null && getStackInSlot(i).stackSize == 0){
			this.items[i] = null;
		}
	}
}

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

@Override
public void openInventory() {
}

@Override
public void closeInventory() {
	((UltimateBag)this.invItem.getItem()).setData(this.items,p);
}

@Override
public boolean isItemValidForSlot(int slot, ItemStack stack) {
	return true;
}
}

Packet:

public class CoolPacket implements IMessageHandler<cm.NeonBlocks.Packet.CoolPacket.CoolMessage, IMessage>
{
  @Override
public IMessage onMessage(CoolMessage message, MessageContext ctx)
{
  if (ctx.side.isServer())
  {
	  ListIterator itl;
	  World rWorld = null;
	  for(int i = 0; i<MinecraftServer.getServer().worldServers.length; i++) {
		  if(MinecraftServer.getServer().worldServers[i].provider.dimensionId == message.worldID)rWorld = MinecraftServer.getServer().worldServers[i];
	  }
	  int integer = message.eID;
	  int slot = message.slot;
	  if(rWorld == null) return null;
	  Entity e = EntityHelper.getEntityByID(integer, rWorld);
	  if(e instanceof EntityPlayer && !e.worldObj.isRemote){
		  EntityPlayer p = (EntityPlayer) e;
   			if(p.openContainer instanceof ContainerUltBag){
   				if(p.openContainer.getSlot(0).inventory.getStackInSlot(slot) != null){
   					if(p.openContainer.getSlot(slot).inventory.getStackInSlot(slot).getItem() == Items.PHOENIX_CHARGE){
   						p.getEntityData().setBoolean("coolstuff_ultbag_tab1", true);
   						EntityPlayerMP mpPlayer = (EntityPlayerMP) p;
   						mpPlayer.openContainer.putStackInSlot(slot, null);
   						EntityPlayer singleP = (EntityPlayer) EntityHelper.getEntityByID(integer, Minecraft.getMinecraft().theWorld);
   						singleP.getEntityData().setBoolean("coolstuff_ultbag_tab1", true);
   					}
   				}
   			}
	  }
  }else{
	  int integer = message.eID;
	  EntityPlayer singleP = (EntityPlayer) EntityHelper.getEntityByID(integer, Minecraft.getMinecraft().theWorld);
	  singleP.getEntityData().setBoolean("coolstuff_ultbag_tab1", true);
  }
  return null;
}
  
  public static class CoolMessage implements IMessage
  {
    private int eID;
    private int slot;
    private int worldID;
    
    public CoolMessage() {}
    
    public CoolMessage(int entityID, int slot, int worldID)
    {
      this.eID = entityID;
      this.slot = slot;
      this.worldID = worldID;
    }
    
    @Override
    public void fromBytes(ByteBuf buf)
    {
      this.eID = buf.readInt();
      this.slot = buf.readInt();
      this.worldID = buf.readInt();
    }
    
    @Override
    public void toBytes(ByteBuf buf)
    {
      buf.writeInt(eID);
      buf.writeInt(slot);
      buf.writeInt(worldID);
    }
  }
}

Extra bits (not sure if this will help)

Tab Code:

public class CoolTab extends CoolRectangle{

private int tabInvNumber;
private String name;
private TabType type;

public CoolTab(Gui gui, String name, int TabID, int x, int y) {
	super(gui, x, y, 32, 24);
	this.tabInvNumber = TabID;
	this.name = name;
	this.type = TabType.TAB_DECATIVATED;
}

public int getInventoryNumber(){
	return this.tabInvNumber;
}

public String getTabName(){
	return this.name;
}

public TabType getType(){
	return this.type;
}

public void setType(TabType type){
	this.type = type;
}

public enum TabType{
	TAB_DECATIVATED, TAB_MOUSED, TAB_ACTIVATED
}

}

Rectangle Code: (for the tabs above)

public class CoolRectangle {

private int x;
private int y;
private int w;
private int h;

public CoolRectangle(Gui gui, int x, int y, int w, int h){
	this.x = x;
	this.y = y;
	this.w = w;
	this.h = h;
}

public boolean inRect(int mx, int my, int left, int top){
	 mx -= left;
	 my -= top;

	 return x <= mx && mx <= x + w && y <= my && my <= y+h;
}

public void draw(Gui gui, int left , int top, int srcX, int srcY, int maxL, int maxH){
	GuiHelper.drawTexturedRect(left+x, top+y, srcX, srcY, w, h, maxL, maxH);
}

public void drawString(Gui gui, int mx, int my, int left, int top, String s){
	if(this.inRect(mx, my, left, top)){
		if(gui instanceof GuiContainer){
			GuiContainer c = (GuiContainer) gui;

		}
	}
}

}

Posted

I know it's a big messy I was planning on cleaning it earlier, but it should still work according to the information.  I don't know why it won't save to the player on world reload

Posted

Never mind I got it to work, I forgot about the extended properties, I got a new class and set that as my tab saving class, and I call it when I need to get the info thank you :D

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.