Minecraft Modding: Tips For GUI and Input

Key Binding


Check out my full tutorial at: Jabelar's Key Binding Tutorial.

Intercepting Mouse Input Buttons


Sometimes modders want to intercept mouse button input. Forge provides the MouseEvent which can be used to cancel the vanilla processing. In that event, since it only fires on the client side it is safe to call Minecraft.getMinecraft() to get access to the various fields you may need such as the player instance. Note though that if you cancel the event you should probably re-create much of the other Minecraft#runTickMouse() method's code since it will no longer execute on its own.

Note though that this event does not affect the mouse movement, only the mouse buttons. For movement, see the next section below.

Intercepting Mouse Input Movement


The Minecraft class includes a mouseHelper field with an instance of class called MouseHelper which simply contains the amount of movement since it was last updated. This class is used in the EntityRenderer#updateCameraAndRender()  method where it is updated by calling the mouseXYChange() method then the deltaX and deltaY are used for camera movement.

Since the Minecraft#mouseHelper filed is public, you can simply change it to replace it with an instance of a custom class that extends MouseHelper and overrides the mouseXYChange() method.

I found it easiest to use the MouseEvent and assign my custom MouseHelper class to Minecraft.getMinecraft().mouseHelper there.

General Concept For GUIs


You can open a GUI any time you want, whether it is based on something happening in the game, or a player interacting with an entity, or interacting with a block. The concept is the same in each case.

All GUIs are an instance of GuiScreen class. If you have a simple GUI you can extend GuiScreen directly, and if you have a container you should extend GuiContainer.

In the initGui() method you can create "buttons" from class GuiButton by instantiating them and adding them to the buttonList field. Note that a button really can be anything the user can interact with, so doesn't have to specifically be a button but could be slider, check box, or even an area where you paint something. Slots are handled separately (you don't need to create buttons for them) as part of making a Container for the GuiContainer.

In the updateScreen() you can process the logic for the general state of the GUI, in particular you can hide or reveal a button by setting its visible field appropriately. 

In the drawScreen() you of course draw the screen. You can use GL11 to set colors and such, use the fontRendererObj field to display text, use drawTexturedModalRect() to transfers areas of a texture resource to the screen.

The drawBackground() does what it says, but is important for GuiContainers because the GuiContainer will automatically draw the slots so if you want a graphic behind the slots you'll need to override this method.

Key Point: Things will draw in the order you code them to draw, so something drawn after will appear "in front" of the earlier thing. So if you want text in a box, draw the box then the text.

Recommendation: Check out all the draw methods in the GuiScreen class to get an idea of what you can do. You can draw text when hovering (i.e. tooltip) using the drawHovering(), draw gradients, draw the world background, and so forth.

The actionPerformed() method is very important because it is called if any of your buttons have been activated in any way. You can check for the button ID and the action type (e.g. right-click or left click) and then take the appropriate action.

Changing Vanilla GUIs


There is a GuiOpenEvent event for when the vanilla GUIs are opened. In that event you can cancel the default GUI and call your own instead.

Opening GUIs With Containers


Thanks to diesieben07 for this information.

To open a GUI with a Container you should use the player.openGui() method.

Warning: You need to be careful about what side you call the openGui() method from. If you only call that on the client, only the client will open it's GuiScreen, the server will not open the Container! If you call it on the server, the server will open the Container and then tell the client to open the GuiScreen. Alternatively you can call it on both sides, then the client will also preemptively open the GuiScreen. But you must not call it on the client only.

Blocks With Inventory GUIs


In 1.8 and beyond they introduced the concept of a GUI "handler". The handler ensures that networking messages are properly sent and handled (for example, server will open the container while client will open the corresponding GUI). See _Bedrock_Miner_'s tutorial on GUI handlers.

For 1.7.10 and earlier, check out my detailed tutorial: Jabelar's Blocks With GUIs Tutorial.

GUIs For Configuration Options.


Check out my full tutorial at: Jabelar's Configuration GUI Tutorial.

Create New Tab On Survival Inventory


You can use events to intercept when any GUI is opened. @SubscribeEvent to the GuiOpenEvent. Tabs are just custom rendered buttons, and if you have access to the GUI instance then you can just add buttons (custom GuiButton) to the buttonList (from GuiScreen). The GuiScreen is available as the event.gui field but unfortunately buttonList is a protected field without any getter or setter functions.

Therefore you have two options:
  1. Use Java Reflection to get the buttonList field anyway.
  2. Replace the whole GUI with a custom one that extends the same GUI class.

Draw The Texture Of An Item


See my modding tips for Items tutorial.

Adding Formatting And Color To Text and Chat


There are certain codes you can embed within a string to create formatting (colors, bold, etc.).  To do this you add an "escape character" (in this case the section symbol "§" which is also represented with
"\u00a7" and then a code character.

Here's a graphic showing the effect of different characters, for example, a "6" creates an orange ("gold") color and the "n" creates underlining.  The weird changing one is called obfuscated.




To make these a bit easier to use, there is an enumerated class called EnumChatFormatting.  For example, you can add EnumChatFormatting.RED to a string to have it display in red color.

Here is an example of a method that will make your text string a rainbow of color:

public static String stringToRainbow(String parString)
{
    int stringLength = parString.length();
    if (stringLength < 1)
    {
        return "";
    }
    String outputString = "";
    EnumChatFormatting[] colorChar = 
    {
        EnumChatFormatting.RED,
        EnumChatFormatting.GOLD,
        EnumChatFormatting.YELLOW,
        EnumChatFormatting.GREEN,
        EnumChatFormatting.AQUA,
        EnumChatFormatting.BLACK,
        EnumChatFormatting.LIGHT_PURPLE,
        EnumChatFormatting.DARK_PURPLE
    };
    for (int i = 0; i < stringLength; i++)
    {
        outputString = outputString+colorChar[i%8]+parString.substring(i, i+1);
    }
    return outputString;
}

Note that even though EnumChatFormatting is already an enumeration, I wanted the colors to be in a proper rainbow spectrum order so had to create an array to embody that.  Otherwise, I'm simply taking each character in the input string and putting in the next color in the rainbow.  Note that I use the modulo operator (%) to cycle through the colors as many times as needed depending on the length of the string.

Opening GUI From Chat Command


Thanks to diesieben07 for figuring this out.

If you ever try to make a GUI open from a chat command, you may find it doesn't work and that is because the chat command code first runs the command and then sets the GUI to null (because it wants to close the chat GUI).

So to open GUI from chat window, you'll have to implements a mechanism (maybe with client tick event) one tick later. In other words, the command sets an global int field to value of 1, client tick event would decrement that value, and if it equals 0 display the GUI, and if it equals -1 just keep it at -1.


14 comments:

  1. My girls, who beg me each day to look at all the new buildings they’ve created, broached the idea of an educational Minecraft before I could even mention it: “I like Minecraft better than my homework,” my 8-year-old told me this spring when I struggled to redirect her to that night’s math.
    How to Draw Tutorials “Maybe my homework could be on Minecraft? Like when we were learning shapes, I could go on Minecraft and make pyramids! And I could put up signs like, ‘A pyramid has a square on the bottom.’ ”

    ReplyDelete
  2. How do you check the action performed for 1.8.9?

    ReplyDelete
  3. Great article, thanks for sharing usefull information and i have seen more info on
    UI online training in Bangalore

    ReplyDelete
  4. Heya i’m for the first time here. I found this board and I find It truly useful & it helped me out a lot I hope to give something back and aid others like you helped me. If you have time you can check Dell Gift Card

    ReplyDelete
  5. Thanks for the sensible critique. Me and my neighbor were just preparing to do some research about this. We got a grab a book from our area library but I think I learned more clear from this post. I’m very glad to see such fantastic information being shared freely out there.

    야한소설

    ReplyDelete
  6. Having read this I thought it was very enlightening. I appreciate you taking the time and energy to put this informative article together. I once again find myself spending a significant amount of time both reading and commenting. But so what, it was still worth it!

    대딸방

    ReplyDelete
  7. I am so grateful for your post. Much thanks again. Really Great.스포츠마사지

    ReplyDelete
  8. Right now it seems like Drupal is the best blogging platform available right now. (from what I’ve read) Is that what you’re using on your blog? 출장마사지

    ReplyDelete
  9. Wow. It is such an amazing article. I am looking forward to reading more articles of yours in the future.. What American citizen need visa for Turkey ? Yes , of course all the American Citizens need a visa to visit . Because the USA is not included in the list of those countries which do not require a visa permit to visit Turkey.

    ReplyDelete
  10. Thank you for your treasured and beneficial information via the weblog. I am appreciating the way you shared the applicable, treasured, and perfect facts. We provide database services, If you have any query related to database services then visit our website will get complete information related to your query.

    ReplyDelete
  11. Wow! Such a very informative article... Thanks for sharing this information...please continue... The travelers who want to travel India for vacation, sightseeing, and purpose to enjoy need a tourist visa India. If you want to know more about it, just click and check.

    ReplyDelete
  12. Enthralling content alert! 🚀 Your blog has sparked my interest, and I must say, it's truly captivating. I've thoroughly enjoyed immersing myself in your blog posts. Additionally, I'm eager to subscribe to your feeds. Here's to seamless and swift access to your content. 📚Egypt visa for Australian tourist, For Australian tourists, obtaining an Egypt e-visa streamlines entry. Apply online, submit necessary documents, pay fees, and receive approval electronically. Enjoy Egypt's wonders hassle-free.

    ReplyDelete