Jump to content

How to read the itemdescription from the chest?


HenryFoster

Recommended Posts

You can get the current GUI opened with Minecraft.currentScreen. Then if it's a chest that GUI will be an instance of GuiContainer.  You can then access the Container of that GUI with GuiContainer.inventorySlots. Finally you can iterate all ItemStacks of that container with Container.inventoryItemStacks. 

  • Like 1
Link to comment
Share on other sites

GuiScreen myScreen =  Minecraft.getMinecraft().currentScreen;
GuiContainer con = myScreen.
NonNullList<ItemStack> mycon = con.inventorySlots.inventoryItemStacks;

 

It seems I dont understand Java enough for this ?

I thought as GuiContainer inherits from GuiScreen I can pass the information about the actual gui to the guicontainer instance and then get the list.

 

 

Edited by HenryFoster
Link to comment
Share on other sites

@SubscribeEvent
    public static void pickup(RightClickBlock event) 
    {
        GuiScreen chestScreen =  Minecraft.getMinecraft().currentScreen;
        if (chestScreen instanceof GuiContainer) 
        {
            GuiContainer c = (GuiContainer) chestScreen; 
            Container container = c.inventorySlots;
            NonNullList<ItemStack> itemStackList = container.getInventory();
            for(int i = 0; i <= itemStackList.size(); i++)
            {
                ItemStack itemStack = itemStackList.get(i);
                System.out.println(itemStack.getItem().getUnlocalizedName());
            }
        }
    }

Got some help on reddit but it still dont work because the if condition is not true when I klick the Chest. I gues the event dont work with this?

 

Its working with the GuiContainerEvent. Thanks to : 

The only problem is that it runns the event over and over until I close the chest. and the event is not canclable.

Edited by HenryFoster
Link to comment
Share on other sites

Well obviously it won't be the case when the RightClickBlock event fires. This event happens before any vanilla logic runs and thus before the game even has a chance to open the GUI. You need another event. For example the GuiOpenEvent. Then you can use GuiOpenEvent.gui instead of Minecraft.currentScreen.

 

Edit: Now when I think about it that won't work either. The client is never aware of the items within the chest and by extension the Container when it is opened. The game sends the packet to the client after the GUI is opened that tells the client about the items. So you would have to hook into some sort of TickEvent and chack your container there. You would need to detect when the GUI is opened(probably via the GuiOpenEvent) set some boolean somewhere to true, in your TickEvent check whether the boolean is true and check the contents of the container. If they are not empty do whatever you want to do and set the boolean to false.

Edited by V0idWa1k3r
  • Like 1
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.

Announcements



×
×
  • Create New...

Important Information

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