Jump to content

[1.10.2] [ClientMod] What is FML's safe way of injecting elements to a gui?


Recommended Posts

Posted

From what I understand, FML is supposed to make sure all mods can work together by adding its own ways of adding stuff.....

I want to add a button element to the "esc menu" of the game that I can direct to my own gui....

The only way I know of to do that is by listening to gui changes and checking for that gui, then overriding it with my own clone of it...

I looked a bit online with no luck of finding any answer so I came to the forum as usall...

Doing stuff n' things

Posted

Or... you can just create your event handler and listen to GuiScreenEvent.InitGuiEvent.Pre, check whether given GUI is instance of GuiIngameMenu, and then add additional buttons to the button list via event.getButtonList() (which is a List<GuiButton>).

Blockbuster – simple machinimas and cinematics for Minecraft
Posted

Giant there something that prevents overlapping?

 

If I get you correctly (stupid autocorrect? :D), you'll need to calculate the button position yourself (to put your button under other buttons). Or you could just place your button in the corner.

 

By the way, you'll probably need to subscribe to ActionPerformedEvent also to add a callback when the button was clicked in GuiIngameMenu.

Blockbuster – simple machinimas and cinematics for Minecraft
Posted

Yes.. stupid auto correct...

I guess ill have to just place a button in hopes that no one uses its place..

But, is there a change you can show me an example on how you would register/use/imply the event on such button in the esc menu?

Im not on pc do i cant test stuff...

Doing stuff n' things

Posted

mod file :

//preinit

new AddButtonToGuiEvent(); //custom class

 

public class AddButtonToGuiEvent{

 

public AddButtonToGuiEvent(){

MinecraftForge.EVENT_BUS.register(this);

}

 

@SubscribeEvent

public void onGuiEvent(TheEventIwantToUse event){

event.getbuttonlist.add(new guibutton(stuff,foo,bar,things);

}

 

}

 

 

very rudamentary implementation. for example only.

Posted

Yes.. stupid auto correct...

I guess ill have to just place a button in hopes that no one uses its place..

But, is there a change you can show me an example on how you would register/use/imply the event on such button in the esc menu?

Im not on pc do i cant test stuff...

 

/* In your proxy on pre initialization (FMLPreInitializationEvent) or 
* initialization (FMLInitializationEvent): */

GuiEventHandler handler = new GuiEventHandler();
MinecraftForge.EVENT_BUS.register(handler);

/* The class itself */
public class GuiEventHandler
{
    @SubscribeEvent
    public void onGuiInit(InitGuiEvent.Pre event)
    {
        if (event.getGui() instanceof GuiIngameMenu)
        {
            /* Add button to top-left corner of the screen */
            event.getButtonList().add(new GuiButton(42, 10, 10, ...));
        }
    }
    
    @SubscribeEvent
    public void onGuiActionPerformed(ActionPerformedEvent event)
    {
        if (event.getGui() instanceof GuiIngameMenu && event.getButton().id == 42)
        {
            /* Do something... */
        }
    }
}

 

My code doesn't really differ from SenpaiSuburaki's code, but there are few important features added/changed.

 

  • Check for GuiIngameMenu – it is really important since without this check you may accidentally add your button to every GUI in the game.
  • ActionPerformed event listener was added – where the comment "Do something..." you should write your custom code that should happen after your added button was clicked (i.e. open other GUI, Minecraft.getMinecraft().displayGuiScreen(...)).
  • Moved registration of event handler outside of the class. Why? Because it's bad, in my opinion, since it will hardcode (add hard dependency, coupling) MinecraftForge.EVENT_BUS to this class.

 

The code provided above is untested, but I believe it should work with minor adjustments (i.e. add imports and change some parameters to desired values).

Blockbuster – simple machinimas and cinematics for Minecraft

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.

Announcements



×
×
  • Create New...

Important Information

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