Jump to content

Recommended Posts

Posted

I have hit a block and cannot get past it. (not literally)

I have a block with a GUI, a container and a tile entity. The container has 2 slots of my own. The gui has 2 button next to my own slots.

 

The idea is element manipulation. If you put an item in one of the slots and press the adjacent button, the idea is that it will remove 1 item from the itemstack, or the whole itemstack if its size is one. This part of the code works, but my problem comes with what I think is a client-server sync issue. Once the item has been removed from the slot, I can take it out of the slot like you could if it was still there. It also reappears when i re-open the GUI.

 

I am having the same sort of issue with the element combining. If you select some elements and press the combine button, the idea is it will check the element ids in an array and return an itemstack accordingly. This works and the itemstack is being returned fine. However when I try to add the new itemstack to the slot, it doesn't appear. I had a go with some packet handling, but I didn't really know what I was doing. The new itemstack appears, but when i try to take it out it disappears instantly.

 

 

Container class - just the item handling parts...there are probably some really random and wrong things in here, thats because i keep trying different things and dont totally know what im doing with packet handling or editing slots of the container.

 

public static void breakdown() {
	IInventory inventory = (IInventory) ContainerSciBench.scibench;			
		ItemStack itemstack = inventory.getStackInSlot(0);
		if (itemstack != null) {
			if (itemstack.stackSize > 1) {
				itemstack.stackSize -= 1;			
			} else {
				inventory.setInventorySlotContents(0, null);
				itemstack = null;
			}
		}

}

public static void combine() {
	IInventory inventory = (IInventory) ContainerSciBench.scibench;	
		if (inventory.getStackInSlot(1) == null) {
			ItemStack itemstack = null;
			if (createNewItem(GuiSciBench.elements) != null) {
				itemstack = createNewItem(GuiSciBench.elements);
				String id = Integer.toString(itemstack.itemID);
				String size = Integer.toString(itemstack.stackSize);
				String data = id + "#" + size;
				World world = Minecraft.getMinecraft().theWorld;
				System.out.println(data);

				if (world.isRemote){
					Util.sendDataToServer(data, "SciBenchCombine");
					combineFinal(data);
					inventory.onInventoryChanged();
				}					
			}		
		}

}

public static void combineFinal(String data) {
	IInventory inventory = (IInventory) ContainerSciBench.scibench;	
	String string = data;
	int gap = string.indexOf("#");
	System.out.println(gap);
	String id = string.substring(0, gap);
	int idnum = Integer.parseInt(id);
	System.out.println(idnum);

	ItemStack itemstack = new ItemStack(Items.oxygengasbottle, 1);
	inventory.setInventorySlotContents(1, itemstack);
	inventory.onInventoryChanged();
}

public static ItemStack createNewItem(@SuppressWarnings("rawtypes") ArrayList elements) {
	if (elements != null) {
		int e1 = 0, e2 = 0, e3 = 0;
		if (elements.size() > 0) {
			e1 = (int) elements.get(0);	
			if (elements.size() > 1) {
				e2 = (int) elements.get(1);
				if (elements.size() > 2) {
					e3 = (int) elements.get(2);
				}
			}				
		}

		System.out.println("e1 = " + e1 + ", e2 = " + e2 + ", e3 = " + e3);

		ItemStack itemstack = null;
		switch(e1) {
		case 1:
			if (e2 == 4) {
				itemstack = new ItemStack(Items.tarbucket, 1);
			}

		case 3:
			if (e2 == 12) {
				itemstack = new ItemStack(Items.spraylicl, 1);
			}

		case 4:
			if (e2 == 1) {
				itemstack = new ItemStack(Items.tarbucket, 1);
			}

		case 6:
			if (e2 == 20) {					
				if (e3 == 12) {
					itemstack = new ItemStack(Items.spraycuso4, 1);
				}
			} else if (e2 == 9){
				if (e3 == 12) {
					itemstack = new ItemStack(Items.spraymgso4, 1);
				}
			} else {
				itemstack = new ItemStack(Items.oxygengasbottle, 1);
				System.out.println(itemstack.itemID + ", " + itemstack.stackSize);
				return itemstack;
			}

		case 8:
			if (e2 == 12) {
				itemstack = new ItemStack(Items.spraynacl, 1);
			}

		case 9:
			if (e2 == 12) {
				if (e3 == 6) {
					itemstack = new ItemStack(Items.spraymgso4, 1);
				}
			} else if (e2 == 6) {
				if (e3 == 12) {
					itemstack = new ItemStack(Items.spraymgso4, 1);
				}
			}

		case 12:
			if (e2 == 14) {
				itemstack = new ItemStack(Items.spraykcl, 1);
			} else if (e2 == 20) {
				if (e3 == 6) {
					itemstack = new ItemStack(Items.spraycuso4, 1);	
				} else if (e3 == 9) {
					itemstack = new ItemStack(Items.spraymgso4, 1);
				} else {
					itemstack = new ItemStack(Items.spraycucl2, 1);
				}				
			} else if (e2 ==  {
				itemstack = new ItemStack(Items.spraynacl, 1);
			} else if (e2 == 15) {
				itemstack = new ItemStack(Items.spraycacl, 1);
			} else if (e2 == 9) {
				if (e3 == 6) {
					itemstack = new ItemStack(Items.spraymgso4, 1);
				}
			}

		case 15:
			if (e2 == 12) {
				itemstack = new ItemStack(Items.spraycacl, 1);
			} else {
				itemstack = new ItemStack(Block.cobblestone, 1);
			}	

		case 16:
			itemstack = new ItemStack(Items.titaniumingot, 1);	

		case 18:
			itemstack = new ItemStack(Item.ingotIron, 1);

		case 20:
			if (e2 == 6) {
				if (e3 == 12) {
					itemstack = new ItemStack(Items.spraycuso4, 1);
				}
			} else if (e2 == 12) {
				itemstack = new ItemStack(Items.spraycucl2, 1);
			}

		case 21:
			itemstack = new ItemStack(Items.nickelingot, 1);		

		case 24:
			itemstack = new ItemStack(Items.yttriumcrystal, 1);

		default:
			itemstack = null;
		}		
		return itemstack;
	} else {
		return null;
	}
}

 

 

PacketHandler Class:

 

@Override
public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player playerEntity) {
	if (packet.channel.equals("SciBenchBreak")) {
		handlePackets(packet, playerEntity);
	}
	if (packet.channel.equals("SciBenchCombine")) {
		handlePackets(packet, playerEntity);
	}
}

private void handlePackets(Packet250CustomPayload packet, Player playerEntity) {
	DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(packet.data));
	try {
		String string = inputStream.readUTF();
		int gap = string.indexOf("#");
		System.out.println(gap);
		String id = string.substring(0, gap);
		String size = string.substring(gap + 1);
		System.out.println(id + ", " + size);
		String output = id + "#" + size;
		ContainerSciBench.combineFinal(output);
	} catch (IOException e) {
		e.printStackTrace();
		return;
	}	
}

 

 

So basically I have no idea what i'm trying to achieve with this packet handling thing...I dont know what side im on where and which side i ned to send to where and when etc...if anyone can help i would be very grateful.

Posted

Hi

 

I think you are right, you are having synch issues.  Containers are tricky to get right (at least, tricky for me), so I would suggest you start with the code from a vanilla container (eg ContainerRepair, or ContainerWorkBench), copy it to a new class of your own, make sure it still works, then introduce one small change at a time until it has all the features you want.

 

-TGG

Posted

Thanks for your reply. I've looked around but can't really figure out what does what and where goes where if you get what I mean.

This is what I hate about Minecraft code...its feels all over the place and unnecessarily too, with no real evidence of how things work (maybe i'm not looking hard enough but it seems to me to be such an unnecessary hassle to update a slot)

Posted

Try not to use static methods.

You can get the container with EntityPlayer-openContainer and casting down.

 

You seem to have two channels, but your packet handler does the same thing for both.

 

We need the code which sends the packet too.

Posted

Yeah there are 2 channels because 1 will do one thing and one will do another, just haven't got there quite yet :)

 

 

public class Util {

public static void sendDataToServer(String data, String channel) {
	ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length());
	DataOutputStream outputStream = new DataOutputStream(bos);
	try {
		outputStream.writeUTF(data);
	} catch (Exception ex) {
		ex.printStackTrace();
	}
	Packet250CustomPayload packet = new Packet250CustomPayload();
	packet.channel = channel;
	packet.data = bos.toByteArray();
	packet.length = bos.size();
	PacketDispatcher.sendPacketToServer(packet);
	System.out.println("sent");
}
}
[code][spoiler]
This the code that sends the packet. This is called from the container class (A friend helped me up to this point, but didn't know how to manipulate inventories properly)

Posted

I just noticed you don't use anything from the packet data.  :-\

And you are using a string to store two integers.

And the handlePackets method in your packet handler is 99% useless...splitting a string in 3 and putting it back together.  :o

Posted

yup xD as i said im not raelly sure what im mean to be doing :(

so ive sent the data to the packethandler...is that from the client side or the server side? what do i need to do with it from there? do i need to set the inventory slot of the container using the data sent in the packet from the packethandler class? or am i mean to send it somewhere else?

(if i am meant to set the inv slot from the ph class, i have no idea how to...and i have tried numerous ways xD )

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.