Solved (instance issues) Unable to verify if a players open inventory is custom

poptart

New member
Feb 1, 2024
2
0
1
Hello, first off I'm sorry if the title doesn't make sense. I'm very new to Java so feel free to help me rename it.

I'm currently trying to make a Gui/inventory that opens and allows me to handle player clicks. In it's current state the Gui opens and the event handler works fine. The part where I'm getting stuck is when trying to verify if a player has my custom Gui/inventory open vs their own inventory or a chest.

I've used https://www.spigotmc.org/wiki/creating-a-gui-inventory/ as a guide for building the gui.

I have 3 files:

When using the "/test" command, a player should have a Gui/inventory open up with items inside of it. This works just fine with my code.

Where my problem arises is when I try to do the following code in my TestGui class:
Code:
if (!e.getInventory().equals(TestGui)) return;

TestGui appears to be "null". I expected this to stop running an onInventoryClick event handler if the inventory is not equal to TestGui.

I've achieved this functionality previously on a 1.20.2 paper server but can't get this working again. After some googling I can't seem to figure this out with my current knowledge of Java. I've seen reference to paper's inventory handler guide but when I google about it it seems that that is not the correct way to handle this.

If anyone can help point me in the right direction I'd be very appreciative.
 

stefvanschie

Moderator
Staff member
Dec 17, 2021
102
3
16
18
You are using different instances of TestGui across your code. When registering the events you create a new TestGui instance. This will have the TestGui field as null as it was not initialized. When executing the command, you create another instance of TestGui. This second instance will have its TestGui field set, but this is a different instance from the first one. Hence, the listeners, who are registered on the first instance, will still be looking at the uninitialized TestGui field. You should ensure you're working with the correct instances. Seeing your current code, my recommendation would be to have a single instance and keep track of a set of inventories to compare against.
 
  • Like
Reactions: poptart

poptart

New member
Feb 1, 2024
2
0
1
You are using different instances of TestGui across your code. When registering the events you create a new TestGui instance. This will have the TestGui field as null as it was not initialized. When executing the command, you create another instance of TestGui. This second instance will have its TestGui field set, but this is a different instance from the first one. Hence, the listeners, who are registered on the first instance, will still be looking at the uninitialized TestGui field. You should ensure you're working with the correct instances. Seeing your current code, my recommendation would be to have a single instance and keep track of a set of inventories to compare against.

Appreciate this! I went back and spent some time learning about instances/objects and have fixed my code by having a single object initialized and calling that throughout my code.

Thanks for helping a newbie!