Jump to content

Changing contents of a slot in block container help


AomicTim

Recommended Posts

Hi,

 

I'm having an issue changing the contents of slots in a container.

I have a block, a tile entity, a container and a gui.

When you click a button on the gui, it tells the container to remove the itemstack from that slot, or decrease it by 1.

When you click another button, it creates a new itemstack (as specified by a switch statement)

Problem is, neither are working properly.

When the button is clicked to remove/decrement the itemstack in slot 0, it is removed or decremented, but if i leave the gui and enter it again, the itemstack appears again, and if I click the slot, the itemstack appears on my mouse, as if it was never removed, but just hidden temporarily.

Here is the code that handles this:

 

public static ItemStack breakdown() {

IInventory inventory = (IInventory) ContainerSciBench.scibench;

ItemStack itemstack = inventory.getStackInSlot(0);

if (itemstack != null) {

if (itemstack.stackSize > 1) {

itemstack.stackSize -= 1;

//inventory.onInventoryChanged();

} else {

inventory.setInventorySlotContents(0, null);

//itemstack = null;

//inventory.onInventoryChanged();

}

}

return itemstack;

}

 

There is some commented out stuff in there that I was trying, none of it worked. It also doesn't need to be type ItemStack, just did it again to see if it worked.

 

My second problem is with the creation of items/blocks. I can see the code is being run right up to and past the point where the new itemstack should be created via the console, however no item/block appears in the slot.

Here is the code that handles this:

 

public static ItemStack combine() {

IInventory inventory = (IInventory) ContainerSciBench.scibench;

if (inventory.getStackInSlot(1) == null) {

ItemStack itemstack = null;

createNewItem(GuiSciBench.elements)

inventory.setInventorySlotContents(1, itemstack);

inventory.onInventoryChanged();

System.out.println("Should have been created");

 

}

return null;

}

 

Some stuff i there is again me just trying different things...and again it doesn't need to be type ItemStack.

 

I wont post the entirety of the code for the relevant classes unless asked for as I can see tha the code is being run. I'm just stumped as to the proper method of removing/decrementing itemstacks from a slot, and adding new itemstacks to a slot.

 

Any help would be greatly appreciated.

 

Link to comment
Share on other sites

Hi

 

My guess is that you've got a client<-->server synchronisation problem, i.e. you are making changes on the client but not on the server, so that when you "refresh" by reopening the GUI or clicking the slot, the server overwrites what you did on the client.

 

I haven't modded GUIs yet so I can't give you specific advice.  It might help to look very closely at what the vanilla code does (for example for the player inventory or the forging table or similar).

 

-TGG

Link to comment
Share on other sites

ContainerSciBench.scibench;

createNewItem(GuiSciBench.elements)

 

There's not much code to go off, but...

 

What are you accessing here? are those static fields?

 

Your gui and container should be normal classes, there can and will be multiple instances of them at the same time. In SP the container will exist in 2 copies, one for the client side and one for the server, make sure you are working with the right one.

 

Also your Gui class only exists on the client side, never on the server, while anything that actually changes slots should happen on the server, this looks fishy.

 

Link to comment
Share on other sites

craeteNewItem() is literally just a massive switch statement that returns an itemstack at the end, for use in putting an itemstack in the output slot of the container.

 

I have a guihandler class that deals with client and server side, the GUI is only being run client side, and the container server side. (At least i hope so)

 

My main problem is getting it to recognize which side it is on, and sending an update to the other side if you get what i mean. Would I use !world.isRemote to detect server side? and any idea where?

also how do i send updates to one side from the other...I would assume its the packet handler etc, but I have no idea how to use 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.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.