Jump to content

Error opening GUI with a key [1.16.5]


Grookey

Recommended Posts

    @SubscribeEvent
    public static void gauntletGUI(final TickEvent.PlayerTickEvent event) {
        PlayerEntity player = event.player;
        ItemStack stack = event.player.getMainHandItem();

        if (keyOpenGauntlet.isDown() && keyOpenGauntlet.consumeClick() && stack.getItem().equals(ItemInit.INFINITY_GAUNTLET.get())) {
            Main.NETWORK.sendToServer(new InputMessage(stack));
        }
    }

This is the event

 

    public static void handle(InputMessage message, Supplier<NetworkEvent.Context> contextSupplier) {
        NetworkEvent.Context context = contextSupplier.get();
        context.enqueueWork( () ->
                {
                    ServerPlayerEntity player = context.getSender();
                    INamedContainerProvider ncp = new InfinityGauntlet.InfinityGauntletContainerProvider(player.getMainHandItem().getItem(), message.stack);
                    NetworkHooks.openGui(player, ncp);
                }
                );
        context.setPacketHandled(true);

    }

This opens the GUI.

 

So the problem is that when I press the key in this case is B the GUI opens and instantly closes.

https://github.com/Manueh333/Marvel-MOD

Link to comment
Share on other sites

Ok, so I have put this code and it doesn't work, I have to forms of opening the GUI one is on the useOn  void of the item and this  form work but the other form is to press 'B' but when the GUI opens it closes. If I put "return true;" in stillValid the GUI didn't close so it's obvious that the problem is there but I dont understand why 

    @Override
    public boolean stillValid(PlayerEntity player) {
        return (player.getMainHandItem().equals(heldItem));

    }

 

Link to comment
Share on other sites

stillValid() runs on the server, where are you storing "heldItem" when you press B?

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

ItemStack does not override equals, this means the == operator is used,
you need to use IForgeItemStack#equals (requires an ItemStack and a boolean)

or you check the Item of the ItemStack, therefore you can use a instanceof check, equals or the == operator depends on your purpose

Link to comment
Share on other sites

4 minutes ago, Grookey said:
16 minutes ago, Luis_ST said:

ItemStack does not override equals, this means the == operator is used,
you need to use IForgeItemStack#equals (requires an ItemStack and a boolean)

or you check the Item of the ItemStack, therefore you can use a instanceof check, equals or the == operator depends on your purpose

this is the issue

5 minutes ago, Grookey said:

This don't work

is the code called, use debugger to check

Link to comment
Share on other sites

8 minutes ago, Luis_ST said:

ItemStack does not override equals, this means the == operator is used,
you need to use IForgeItemStack#equals (requires an ItemStack and a boolean)

or you check the Item of the ItemStack, therefore you can use a instanceof check, equals or the == operator depends on your purpose

I dont understand what you mean, can you make an example pls

 

12 minutes ago, Luis_ST said:

is the code called, use debugger to check

yeah the code is called

Link to comment
Share on other sites

33 minutes ago, Grookey said:

I dont understand what you mean, can you make an example pls

when exactly should the player be able to open the container?
please be as detailed as possible

33 minutes ago, Grookey said:

yeah the code is called

is the Packet received on server and how did you test if it works?

Edited by Luis_ST
Link to comment
Share on other sites

8 minutes ago, Luis_ST said:

is the Packet received on server and how did you test if it works?

6urtiuV.png

I tested it with a breakpoint and pressing B at the game

 

8 minutes ago, Luis_ST said:

when exactly should the player be able to open the container?
please be as detailed as possible

You said I have to use a IForgeItemStack#equals with a ItemStack and a boolean so how?

Edited by Grookey
Link to comment
Share on other sites

 

You want to retrieve the held item on the server.

Instead you are retrieving it on the client which will not be the same item stack as the held item on the server. It will be a copy. So == will be false.

 

But I think you actually just need the following

        return (player.getMainHandItem().sameItem(heldItem));

to check the items are the same type. You are not interested in the stack count or tags?

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

3 minutes ago, warjort said:

You are not interested in the stack count or tags?

Actually, yes the Item is a container that contains items so its important that

 

But I tried and It worked

Also it would be good to make the item cant be moved in the inventory when my GUI is opened is there any form to do this?

Edited by Grookey
Link to comment
Share on other sites

Then use one of the ItemStack.matches() or the equals(ItemStack, boolean) defined by the IForgeItemStack interface.

I am not sure of your requirements so you will have to figure out which one yourself 🙂

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

24 minutes ago, warjort said:
 return (player.getMainHandItem().sameItem(heldItem));

I thought that I need the tags but not because the item contain items if you add or remove some of the item it closes the GUI and the content of the container is not updated so I finally used this code

Any of the codes worked for me because when i change a Item of the inside of the container it closes and not updates, and like after the problem is only when I use the key B to open the GUI

    @SubscribeEvent
    public static void gauntletGUI(TickEvent.PlayerTickEvent event) {
        PlayerEntity player = event.player;
        ItemStack stack = event.player.getItemBySlot(EquipmentSlotType.MAINHAND);

        if (keyOpenGauntlet.isDown() && keyOpenGauntlet.consumeClick() && stack.getItem().equals(ItemInit.INFINITY_GAUNTLET.get())) {
            Main.NETWORK.sendToServer(new InputMessage(stack));
        }
    }

 

Edited by Grookey
Link to comment
Share on other sites

KeyBindings are client side only, you need to use TickEvent.ClientTickEvent instead

why did you call KeyBinding#consumeClick, you should only check KeyBinding#isDown?

last but not least i would recommend you to check the Item also on server side (in the handle method of the Message), since you should never trust the client

Link to comment
Share on other sites

43 minutes ago, Luis_ST said:

TickEvent.ClientTickEvent instead

In ClickTickEvent i dont know how to get the player

 

43 minutes ago, Luis_ST said:

i would recommend you to check the Item also on server side (in the handle method of the Message)

    public static void handle(InputMessage message, Supplier<NetworkEvent.Context> contextSupplier) {
        NetworkEvent.Context context = contextSupplier.get();
        ServerPlayerEntity player = context.getSender();
        ItemStack gauntlet = player.getMainHandItem();
        if(gauntlet.getItem() instanceof InfinityGauntlet) {
            INamedContainerProvider ncp = new InfinityGauntlet.InfinityGauntletContainerProvider((InfinityGauntlet) gauntlet.getItem(), gauntlet);
            NetworkHooks.openGui(player, ncp);
            context.setPacketHandled(false);
        }


    }

You were right I check the item on the server and this code works 
Thanks

Link to comment
Share on other sites

5 minutes ago, Luis_ST said:

Minecraft.getInstance()

    @SubscribeEvent
    public static void gauntletGUI(final TickEvent.ClientTickEvent event) {
        PlayerEntity player = Minecraft.getInstance().player;
        if(player != null)  {
            ItemStack stack = player.getMainHandItem();

            if (keyOpenGauntlet.isDown() && stack.getItem().equals(ItemInit.INFINITY_GAUNTLET.get())) {
                Main.NETWORK.sendToServer(new InputMessage(stack));
            }
        }

    }

It works. Thanks! :D

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.



×
×
  • Create New...

Important Information

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