Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

Hi,

 

I'm new to modding but experienced in server plugin development. I have no idea where to start. I'm trying to capture the InventoryOpenEvent so I can output some metadata of the items to a file.

 

This is what I have so far - I can't seem to use @EventHandler . How can I achieve this?

 

package com.example.examplemod;

import org.apache.logging.log4j.Logger;

import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;

@Mod(modid = ExampleMod.MODID, name = ExampleMod.NAME, version = ExampleMod.VERSION)
public class ExampleMod
{
    public static final String MODID = "examplemod";
    public static final String NAME = "Example Mod";
    public static final String VERSION = "1.0";

    private static Logger logger;

    @EventHandler
    public void preInit(FMLPreInitializationEvent event)
    {
        logger = event.getModLog();
    }

    @EventHandler
    public void init(FMLInitializationEvent event)
    {
        logger.info("Client Started");
        // ?
    }
}

 

  • Author

Ok, I've read that, but I can't find the right event type - it seems Forge doesn't support it. Isn't the idea to be able to modify the game, ie. change anything you want?

7 minutes ago, AntiRix said:

I can't find the right event type

GuiOpenEvent(client) PlayerContainerEvent.Open(server).

Although if the inventory in question isn't synced to the client at all times you need to handle one of the TickEvents(depends on the side you want to perform the actions) and check the current opened container. If it changes start scanning for changes in it's contents(switch a boolean to true or something and if it is true scan the inventory). Once it isn't empty anymore the items have been synced and you can do whatever it is you needed to do.

  • Author

I now have a simple setup which is able to detect the inventory opening and closing, but it always says the inventory is empty, probably because the server hasn't sent the items over yet. Should I keep waiting for the inventory to be populated, and if so, how can I know? I can't see any methods related to it.

 

private boolean inventory_open = false;
	
	@SubscribeEvent
	public void onPlayerTick(TickEvent.PlayerTickEvent event)
	{
		GuiScreen screen = Minecraft.getMinecraft().currentScreen;
		
		if (screen instanceof GuiInventory)
		{
			if (inventory_open) return;
			
			inventory_open = true;
			ExampleMod.logger.info("> Inventory opened");
		}
		else
		{
			if (!inventory_open) return;
			
			inventory_open = false;
			ExampleMod.logger.info("> Inventory closed");
			
			return;
		}
		
		Container cont = ((GuiInventory) screen).inventorySlots;
		
		for (int i = 0; i < cont.inventorySlots.size(); i++)
		{
			ExampleMod.logger.info("Slot " + i + ": " + cont.inventorySlots.get(i).getSlotTexture());
		}
	}

 

9 minutes ago, AntiRix said:

PlayerTickEvent

This is a common event, meaning it is fired for both the client and the server. You are using client-only classes so you must only subscribe your event handler to the bus on the client.

 

9 minutes ago, AntiRix said:

Should I keep waiting for the inventory to be populated, and if so, how can I know?

Just wait untill there is at least one item in the inventory. They all get synced at once so as long as there is at least one item there are others there too. Your current code only checks the inventory once on the first tick as it is opened which is probably too early.

 

Also Slot#getSlotTexture returns an ItemStack that is used as the background image for the slot, hence the name. You need Slot#getStack.

 

Also I think that the player's inventory is always known to the client so you can just get whatever is in it in the GuiOpenEvent.

  • Author

I'm already registering the event handler.

 

I'm dealing with custom inventories which servers show, not the normal player inventory

Edited by AntiRix

21 minutes ago, AntiRix said:

I'm already registering the event handler.

24 minutes ago, V0idWa1k3r said:

only subscribe your event handler to the bus on the client.

 

21 minutes ago, AntiRix said:

I'm dealing with custom inventories which servers show, not the normal player inventory

Are you sure? Because this

31 minutes ago, AntiRix said:

GuiInventory

Is the player's inventory GUI, and player's only.

  • Author

Right, I've changed that to GuiContainer and it's working for those inventories now. The only issue is that when changing inventories by clicking through the custom inventories, there's no tick where the currentScreen isn't an instance of GuiContainer. As such, I either need to compare all of the inventory contents to detect a change, or much easier, detect a change in the inventory title because each has a unique title. I can't find any methods in GuiContainer to get the name or title; are you aware of a way to get it?

 

	private boolean inventory_open = false;
	private boolean inventory_populated = false;
	
	@SubscribeEvent
	public void onPlayerTick(TickEvent.PlayerTickEvent event)
	{
		GuiScreen screen = Minecraft.getMinecraft().currentScreen;
		
		boolean inventory_open_in_tick = screen instanceof GuiContainer;
		
		if (!inventory_open && inventory_open_in_tick)
		{
			inventory_open = true;
			ExampleMod.logger.info("> Inventory opened");			
		}
		
		if (inventory_open && !inventory_open_in_tick)
		{
			inventory_open = false;
			inventory_populated = false;
			ExampleMod.logger.info("> Inventory closed");
			
			return;
		}
		
		if (!inventory_open_in_tick || inventory_populated) return;
		
		// inventory must be open and unpopulated by this point
		
		Container cont = ((GuiContainer) screen).inventorySlots;
		
		for (int i = 0; i < cont.inventorySlots.size(); i++)
		{
			ItemStack stack = cont.inventorySlots.get(i).getStack();
			String name = stack.getItem().getRegistryName().getResourcePath();
			
			if (name != "air")
			{
				inventory_populated = true;
				ExampleMod.logger.info("> Inventory populated");
				
				break;
			}
		}
		
		if (!inventory_populated) return;
		
		// inventory is now populated
		
		for (int i = 0; i < cont.inventorySlots.size(); i++)
		{
			ItemStack stack = cont.inventorySlots.get(i).getStack();
			String name = stack.getItem().getRegistryName().getResourcePath();
			
			ExampleMod.logger.info("> Slot " + i + ": " + name);
		}
	}

 

18 minutes ago, AntiRix said:

I either need to compare all of the inventory contents to detect a change, or much easier, detect a change in the inventory title because each has a unique title.

Why not compare container objects themselves?

private static GuiContainer lastInventory;

...

@SubscribeEvent
public static void handler(TickEvent.ClientTickEvent event)
{
	GuiScreen current = Minecraft.getMinecraft().currentScreen;
  	if (current != lastInventory)
    {
    	// Opened GUI changed.
    }
  
  	...
      
    lastInventory = current instanceof GuiContainer ? (GuiContainer)current : null;
}

 

  • Author

Ok, I've got that working now, but I really need to know the inventory title so I can perform actions on the correct inventory.

  • Author
3 hours ago, diesieben07 said:

You should check for specific GuiContainer instances instead.

 

How can I have anything to compare against? All I can go by is the inventory title, or by checking every item inside the inventory. These are custom inventories opened by server plugins.

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.