Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[Semi-Solved] [1.13.2] Getting items stored in GuiContainer


Wealthyturtle
 Share

Recommended Posts

Foreword: Sorry if I make any silly mistakes here, I'm not too familiar with how Forge handles Inventories and such...

 

Essentially, I want to make a client-side Forge mod that will be able to extract the contents of any GUI and print it into the logs (for now). I do not wish to use the PlayerContainerEvent because it does not work properly (i.e. just returns a NonNullList<ItemStack> full of air) when getting GUIs from some servers' plugins (perhaps due to Bukkit/Spigot doing some funky stuff backend)

 

Listener Class:

public class Listeners {
	@SubscribeEvent
	public static void onContainerOpen(GuiOpenEvent event) {
		GuiScreen gui = event.getGui();
		if(gui instanceof GuiContainer) {
			GuiContainer gc = (GuiContainer)gui;
			NonNullList<ItemStack> items = gc.inventorySlots.getInventory();
			for(int i = 0; i < items.size(); i++) {
				project.LOGGER.debug("CONTAINER/" + i + ": " + items.get(i).toString());
			}
		}
	}
}

 

Currently, when using this class, even through there are items in the chest being right clicked, this occurs

[Client thread/DEBUG] [co.we.au.project/]: CONTAINER/0: 1xblock.minecraft.air
[Client thread/DEBUG] [co.we.au.project/]: CONTAINER/1: 1xblock.minecraft.air
[Client thread/DEBUG] [co.we.au.project/]: CONTAINER/2: 1xblock.minecraft.air
[Client thread/DEBUG] [co.we.au.project/]: CONTAINER/3: 1xblock.minecraft.air
[Client thread/DEBUG] [co.we.au.project/]: CONTAINER/4: 1xblock.minecraft.air
[Client thread/DEBUG] [co.we.au.project/]: CONTAINER/5: 1xblock.minecraft.air
[Client thread/DEBUG] [co.we.au.project/]: CONTAINER/6: 1xblock.minecraft.air
[Client thread/DEBUG] [co.we.au.project/]: CONTAINER/7: 1xblock.minecraft.air
[Client thread/DEBUG] [co.we.au.project/]: CONTAINER/8: 1xblock.minecraft.air
[Client thread/DEBUG] [co.we.au.project/]: CONTAINER/9: 1xblock.minecraft.air
[Client thread/DEBUG] [co.we.au.project/]: CONTAINER/10: 1xblock.minecraft.air
[Client thread/DEBUG] [co.we.au.project/]: CONTAINER/11: 1xblock.minecraft.air
[Client thread/DEBUG] [co.we.au.project/]: CONTAINER/12: 1xblock.minecraft.air
[Client thread/DEBUG] [co.we.au.project/]: CONTAINER/13: 1xblock.minecraft.air
[Client thread/DEBUG] [co.we.au.project/]: CONTAINER/14: 1xblock.minecraft.air
[Client thread/DEBUG] [co.we.au.project/]: CONTAINER/15: 1xblock.minecraft.air
[Client thread/DEBUG] [co.we.au.project/]: CONTAINER/16: 1xblock.minecraft.air
[Client thread/DEBUG] [co.we.au.project/]: CONTAINER/17: 1xblock.minecraft.air
[Client thread/DEBUG] [co.we.au.project/]: CONTAINER/18: 1xblock.minecraft.air
[Client thread/DEBUG] [co.we.au.project/]: CONTAINER/19: 1xblock.minecraft.air
[Client thread/DEBUG] [co.we.au.project/]: CONTAINER/20: 1xblock.minecraft.air
[Client thread/DEBUG] [co.we.au.project/]: CONTAINER/21: 1xblock.minecraft.air
[Client thread/DEBUG] [co.we.au.project/]: CONTAINER/22: 1xblock.minecraft.air
[Client thread/DEBUG] [co.we.au.project/]: CONTAINER/23: 1xblock.minecraft.air
[Client thread/DEBUG] [co.we.au.project/]: CONTAINER/24: 1xblock.minecraft.air
[Client thread/DEBUG] [co.we.au.project/]: CONTAINER/25: 1xblock.minecraft.air
[Client thread/DEBUG] [co.we.au.project/]: CONTAINER/26: 1xblock.minecraft.air
[Client thread/DEBUG] [co.we.au.project/]: CONTAINER/27: 64xblock.minecraft.bedrock

Where indices 0 to 26 are supposed to contain the chest's items (I put sand inside) but it returns air instead? Indices 27 and onwards contains the player's inventory.

 

I think the issue might be that the client is not properly receiving the items from the server, but I'm not sure if that is truly the case, and how to fix it if it is the case...

 

Any assistance would be greatly appreciated. Thanks!

Edited by Wealthyturtle
Link to comment
Share on other sites

  • 1 month later...
On 8/4/2019 at 5:38 PM, diesieben07 said:

I don't think there is an easy way to add a listener. You just have to wait until the inventory is populated. What are you trying to achieve?

Sorry for the late response, but originally I wanted to test if the GuiContainer contained a particular item, e.g. diamond, and if it's true, alert the player. The rest can be settled later, the main issue is the deriving of the container's items.

 

I've tried additional things like Thread.sleep, which just hangs the client and doesn't actually affect the result. I've also tried tried TickEvent.ClientTickEvent, however even when the container is opened for a few seconds, it doesn't register the items at all for some reason...

 

Any advice?

Link to comment
Share on other sites

1 hour ago, FullAuto said:

Don't use anything like Thread.sleep in Minecraft. You will break everything.

 

Maybe you can implement some hotkey, which will print the current GuiContainer to the logs.

Just tried implementing a KeyHandler, it works, but only when the user is not in a GUI Screen, otherwise the keystroke isn't registered

Link to comment
Share on other sites

15 minutes ago, Wealthyturtle said:

Just tried implementing a KeyHandler, it works, but only when the user is not in a GUI Screen, otherwise the keystroke isn't registered

Subscribe to KeyboardKeyEvent, which is only triggered when keys are pressed when a GUI is opened.

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Link to comment
Share on other sites

Thanks for the help, but now I seem to be stuck...

 

From what I can see, the only real solution is to add a custom PacketHandler. The problem is, all the examples I could find were with custom packet sending/recieving (e.g. https://github.com/sinkillerj/ProjectE/blob/c17ff6e1b7151b9ef12396af47a937bb599bf7bf/src/main/java/moze_intel/projecte/network/PacketHandler.java#L23-L52), I couldn't find any examples for vanilla packets, like at all. 

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
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.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.