Jump to content

[SOLVED][1.8][FORGE] Ability to close a gui with a custom key?


Recommended Posts

Posted

So, after getting my custom inventory to work, I find it a tad bit annoying not having the ability to close it with my custom key ®. I have tried sending a packet to close the container (which I believe would tell the client to close the gui also), but the KeyInputEvent is not called while in a gui.

 

Here is my key handler class:

http://pastebin.com/jA9eTDub

 

And my Packet to close the container:

http://pastebin.com/uNNQyzVp

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Posted

Thank you for that, so, I put that in the keyTyped method in my GUI class. The only problem seems to be that, it will close and re-open. I find this to be that the KeyInputEvent gets called multiple times per press. Is there a way to counter this?

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Posted

KeyInputEvent fires both when the key is pressed and when the key is released; Keyboard#getEventKeyState() will be true if it was pressed, and false if it was released, or you can use the ClientTickEvent as diesieben suggests.

 

And yes, you can put the closing logic in your Gui's #keyTyped method if you want, or in the key handling section. I like to put it in my Gui so that the key doesn't close other screens (you can prevent that by checking the current screen's type, but it's easier to just put it in your Gui).

Posted

@diesieben07 Basically, I close the screen in my gui because KeyInputEvent doesn't fire in a gui (I don't know if that is normal, but that's what I found).

 

@coolAlias Thank you! I now check if the custom key is down, and if the current container is not my own before sending the packet to open the gui. Here's an example for those experiencing something similar:

 

int kb = Keyboard.getEventKey();
boolean isDown = Keyboard.getEventKeyState();

if(kb == keys[CUSTOM_INV].getKeyCode() && isDown && !(player.openContainer instanceof YourContainer)) {
System.out.println(player.openContainer);

if(player.openContainer instanceof YourContainer) {
	//Unused		
}else {
        PacketHandler.network.sendToServer(new PacketOpenGui(CUSTOM_INV));
	System.out.println("Packet was sent");
}
}

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Posted

That's what I did. I posted the fix to the code that was conflicting (reopening the gui after closing it). When I'd attempt to close the gui, I never checked in my KeyHandler if the key was down. So, when the key was released, it would send the packet to reopen the gui. Thus, constantly reopening it after closing it.

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

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.