Jump to content

[1.15] Adding a Button to a player inventory


Swirls

Recommended Posts

Hello,

 

So short story, I am trying to make a button in a player inventory that will lead to a custom GUI.

For version 1.12 I think you use `InitGuiEvent`.

I've been told i could use method called `addWidget` but I am not sure what event should I use.

Any help would be really appreciated.

 

Thanks.

Link to comment
Share on other sites

Yes, you need to subscribe to `GuiScreenEvent.InitGuiEvent.Pr

e event)` in order to add widgets, the event itself has a method `addWidget`.
Here's an example of how it could be used:

@SubscribeEvent
public void addCustomButtonToInventory(GuiScreenEvent.InitGuiEvent.Pre event)
{
  if (event.getGui() instanceof InventoryScreen)
  {
    event.addWidget(new Button(x, y, width, height, text, button -> {
      Minecraft.getInstance().displayGuiScreen(new CustomGUI());
    }));
  }
}

If you aren't familiar with the `button -> {...}` syntax, this Button constructor takes an Button.IPressable as a parameter, which is the behavior that happens when you click. Equivalent code would look like this:

@SubscribeEvent
public void addCustomButtonToInventory(GuiScreenEvent.InitGuiEvent.Pre event)
{
  if (event.getGui() instanceof InventoryScreen)
  {
    event.addWidget(new Button(x, y, width, height, text, new CustomAction()));
  }
}

class CustomAction implements IPressable {
  public void onPress(Button button) {
    Minecraft.getInstance().displayGuiScreen(new CustomGUI());
  }
}

Hope this helps!

  • Like 1
  • Thanks 1

Have you ever want the new operator to return a type that you didn't ask for? Well, now you can!

Link to comment
Share on other sites

1 minute ago, Vinyarion said:

Yes, you need to subscribe to `GuiScreenEvent.InitGuiEvent.Pr

e event)` in order to add widgets, the event itself has a method `addWidget`.
Here's an example of how it could be used:


@SubscribeEvent
public void addCustomButtonToInventory(GuiScreenEvent.InitGuiEvent.Pre event)
{
  if (event.getGui() instanceof InventoryScreen)
  {
    event.addWidget(new Button(x, y, width, height, text, button -> {
      Minecraft.getInstance().displayGuiScreen(new CustomGUI());
    }));
  }
}

If you aren't familiar with the `button -> {...}` syntax, this Button constructor takes an Button.IPressable as a parameter, which is the behavior that happens when you click. Equivalent code would look like this:


@SubscribeEvent
public void addCustomButtonToInventory(GuiScreenEvent.InitGuiEvent.Pre event)
{
  if (event.getGui() instanceof InventoryScreen)
  {
    event.addWidget(new Button(x, y, width, height, text, new CustomAction()));
  }
}

class CustomAction implements IPressable {
  public void onPress(Button button) {
    Minecraft.getInstance().displayGuiScreen(new CustomGUI());
  }
}

Hope this helps!

Exactly what I needed, explained well. Thank you very much for your time!

Link to comment
Share on other sites

1 hour ago, Swirls said:

@Vinyarion, sorry for mentioning you!
So when adding widget  


x, y, width, height, text,

is a position of the button in pixels?

Yes

1 hour ago, Swirls said:

And what does "text" do?

It puts text on the button so that the button has the text "text"

1 hour ago, Swirls said:

And can I remove CustomAction so i can only test a button without function to it?

  

No, but if you want no functionality, leave the function empty.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

54 minutes ago, Draco18s said:

Yes

It puts text on the button so that the button has the text "text"

No, but if you want no functionality, leave the function empty.

Oh yea, thanks, this might be stupid question but i dont seem to understand where should i call the event? 

Link to comment
Share on other sites

10 minutes ago, Swirls said:

but i dont seem to understand where should i call the event? 

You mean this:

17 hours ago, Swirls said:

public void addCustomButtonToInventory(GuiScreenEvent.InitGuiEvent.Pre event)

 

Its an event handler. You don't call it. It's called automatically.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

unknown.png

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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
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.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • You have a mod trying to load client classes on the server. The error message does not say which mod. I can see oculus mentioned previously which is a client side mod not needed on the server. You probably have others?
    • Use a mod like https://www.curseforge.com/minecraft/mc-mods/spark to diagnose what is taking time on the server thread.
    • In my mod, I need to make a special kind of damage. I put the value of it into a custom DamageSource class that extends the original one, then I applied it on entities with LivingHurtEvent in an event handler. In 1.19.3, the constructor of the DamageSource was simple. All you needed was a simple String. It worked pretty well. But in 1.19.4, Mojang added something like tons of DamageTypes and Holders. I checked the source code of net.minecraft.world.damagesource.DamageSources, but I cannot understand it at all. So my question is:  How can I create custom DamageSources in 1.19.4? (I'm not a native English speaker, and I'm new to forge modding and this forum. If there are any mistakes, please correct me!)  
    • The game crashed whilst unexpected error Error: java.lang.NullPointerException: Unexpected error ---- Minecraft Crash Report ---- WARNING: coremods are present:   llibrary (llibrary-core-1.0.11-1.12.2.jar)   TransformLoader (DynamicSurroundings-1.12.2-3.6.1.0.jar)   Inventory Tweaks Coremod (InventoryTweaks-1.63.jar)   XaeroWorldMapPlugin (XaerosWorldMap_1.29.2_Forge_1.12.jar)   OpenModsCorePlugin (OpenModsLib-1.12.2-0.12.2.jar)   LoadingPlugin (ResourceLoader-MC1.12.1-1.5.3.jar)   CTMCorePlugin (CTM-MC1.12.2-1.0.2.31.jar)   XaeroMinimapPlugin (Xaeros_Minimap_23.2.0_Forge_1.12.jar) Contact their authors BEFORE contacting forge // I feel sad now :( Time: 3/25/23 2:27 PM Description: Unexpected error java.lang.NullPointerException: Unexpected error     at xaero.map.core.XaeroWorldMapCore.onMinecraftRunTick(XaeroWorldMapCore.java:192)     at net.minecraft.client.Minecraft.func_71411_J(Minecraft.java:1073)     at net.minecraft.client.Minecraft.func_99999_d(Minecraft.java:398)     at net.minecraft.client.main.Main.main(SourceFile:123)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     at java.lang.reflect.Method.invoke(Method.java:497)     at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)     at net.minecraft.launchwrapper.Launch.main(Launch.java:28) A detailed walkthrough of the error, its code path and all known details is as follows: --------------------------------------------------------------------------------------- -- System Details -- Details:     Minecraft Version: 1.12.2     Operating System: Windows 10 (amd64) version 10.0     Java Version: 1.8.0_51, Oracle Corporation     Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Oracle Corporation     Memory: 464818384 bytes (443 MB) / 1073741824 bytes (1024 MB) up to 8589934592 bytes (8192 MB)     JVM Flags: 8 total; -XX:HeapDumpPath=MojangTricksIntelDriversForPerformance_javaw.exe_minecraft.exe.heapdump -Xmx8G -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:G1NewSizePercent=20 -XX:G1ReservePercent=20 -XX:MaxGCPauseMillis=50 -XX:G1HeapRegionSize=32M     IntCache: cache: 0, tcache: 0, allocated: 0, tallocated: 0     FML: MCP 9.42 Powered by Forge 14.23.5.2859 43 mods loaded, 43 mods active     States: 'U' = Unloaded 'L' = Loaded 'C' = Constructed 'H' = Pre-initialized 'I' = Initialized 'J' = Post-initialized 'A' = Available 'D' = Disabled 'E' = Errored     | State | ID                 | Version                  | Source                                        | Signature                                |     |:----- |:------------------ |:------------------------ |:--------------------------------------------- |:---------------------------------------- |     | L     | minecraft          | 1.12.2                   | minecraft.jar                                 | None                                     |     | L     | mcp                | 9.42                     | minecraft.jar                                 | None                                     |     | L     | FML                | 8.0.99.99                | forge-1.12.2-14.23.5.2859.jar                 | e3c3d50c7c986df74c645c0ac54639741c90a557 |     | L     | forge              | 14.23.5.2859             | forge-1.12.2-14.23.5.2859.jar                 | e3c3d50c7c986df74c645c0ac54639741c90a557 |     | L     | xaerominimap_core  | 1.12.2-1.0               | minecraft.jar                                 | None                                     |     | L     | xaeroworldmap_core | 1.12.2-1.0               | minecraft.jar                                 | None                                     |     | L     | openmodscore       | 0.12.2                   | minecraft.jar                                 | None                                     |     | L     | ambientenvironment | 1.0.1                    | AmbientEnvironment-1.0.2.jar                  | None                                     |     | L     | appleskin          | 1.0.14                   | AppleSkin-mc1.12-1.0.14.jar                   | None                                     |     | L     | biomesoplenty      | 7.0.1.2445               | BiomesOPlenty-1.12.2-7.0.1.2445-universal.jar | None                                     |     | L     | chameleon          | 1.12-4.1.3               | Chameleon-1.12-4.1.3.jar                      | None                                     |     | L     | clumps             | 3.1.2                    | Clumps-3.1.2.jar                              | None                                     |     | L     | collective         | 3.0                      | collective-1.12.2-3.0.jar                     | None                                     |     | L     | ctm                | MC1.12.2-1.0.2.31        | CTM-MC1.12.2-1.0.2.31.jar                     | None                                     |     | L     | customspawner      | 3.11.4                   | CustomMobSpawner-3.11.5.jar                   | None                                     |     | L     | mocreatures        | 12.0.5                   | DrZharks MoCreatures Mod-12.0.5.jar           | None                                     |     | L     | dsurround          | 3.6.1.0                  | DynamicSurroundings-1.12.2-3.6.1.0.jar        | None                                     |     | L     | fastleafdecay      | v14                      | FastLeafDecay-v14.jar                         | None                                     |     | L     | fastbench          | 1.7.4                    | FastWorkbench-1.12.2-1.7.4.jar                | None                                     |     | L     | fossil             | 8.0.5                    | fossilsarcheology-8.0.6.jar                   | None                                     |     | L     | cfm                | 6.3.0                    | furniture-6.3.2-1.12.2.jar                    | None                                     |     | L     | hats               | 7.1.1                    | Hats-1.12.2-7.1.1.jar                         | None                                     |     | L     | waila              | 1.8.26                   | Hwyla-1.8.26-B41_1.12.2.jar                   | None                                     |     | L     | ichunutil          | 7.2.2                    | iChunUtil-1.12.2-7.2.2.jar                    | None                                     |     | L     | inventorypets      | 2.0.15                   | inventorypets-1.12-2.0.15.jar                 | None                                     |     | L     | inventorytweaks    | 1.63+release.109.220f184 | InventoryTweaks-1.63.jar                      | None                                     |     | L     | jei                | 4.16.1.1001              | jei_1.12.2-4.16.1.1001.jar                    | None                                     |     | L     | mantle             | 1.12-1.3.3.55            | Mantle-1.12-1.3.3.55.jar                      | None                                     |     | L     | mca                | 6.1.0                    | MCA-1.12.2-6.1.0-universal.jar                | None                                     |     | L     | openblocks         | 1.8.1                    | OpenBlocks-1.12.2-1.8.1.jar                   | None                                     |     | L     | openmods           | 0.12.2                   | OpenModsLib-1.12.2-0.12.2.jar                 | None                                     |     | L     | harvestcraft       | 1.12.2zb                 | Pam's+HarvestCraft+1.12.2zg.jar               | None                                     |     | L     | simplerecipes      | 1.12.2c                  | Pam's+Simple+Recipes+1.12.2c.jar              | None                                     |     | L     | reskin             | 1.2.0                    | reskin-1.2.0.jar                              | None                                     |     | L     | resourceloader     | 1.5.3                    | ResourceLoader-MC1.12.1-1.5.3.jar             | None                                     |     | L     | simpleminingdrills | 1.4.0                    | simpleminingdrills+1.12.2.jar                 | None                                     |     | L     | tconstruct         | 1.12.2-2.13.0.183        | TConstruct-1.12.2-2.13.0.183.jar              | None                                     |     | L     | tinkersjei         | 1.2                      | tinkersjei-1.2.jar                            | None                                     |     | L     | travelersbackpack  | 1.0.35                   | TravelersBackpack-1.12.2-1.0.35.jar           | None                                     |     | L     | twilightforest     | 3.11.1021                | twilightforest-1.12.2-3.11.1021-universal.jar | None                                     |     | L     | xaerominimap       | 23.2.0                   | Xaeros_Minimap_23.2.0_Forge_1.12.jar          | None                                     |     | L     | xaeroworldmap      | 1.29.2                   | XaerosWorldMap_1.29.2_Forge_1.12.jar          | None                                     |     | L     | llibrary           | 1.7.20                   | llibrary-1.7.20-1.12.2.jar                    | None                                     |     Loaded coremods (and transformers):  llibrary (llibrary-core-1.0.11-1.12.2.jar)   net.ilexiconn.llibrary.server.core.plugin.LLibraryTransformer   net.ilexiconn.llibrary.server.core.patcher.LLibraryRuntimePatcher TransformLoader (DynamicSurroundings-1.12.2-3.6.1.0.jar)    Inventory Tweaks Coremod (InventoryTweaks-1.63.jar)   invtweaks.forge.asm.ContainerTransformer XaeroWorldMapPlugin (XaerosWorldMap_1.29.2_Forge_1.12.jar)   xaero.map.core.transformer.ChunkTransformer   xaero.map.core.transformer.NetHandlerPlayClientTransformer   xaero.map.core.transformer.EntityPlayerTransformer   xaero.map.core.transformer.AbstractClientPlayerTransformer   xaero.map.core.transformer.WorldClientTransformer   xaero.map.core.transformer.PlayerListTransformer   xaero.map.core.transformer.SaveFormatTransformer   xaero.map.core.transformer.BiomeColorHelperTransformer   xaero.map.core.transformer.MinecraftTransformer OpenModsCorePlugin (OpenModsLib-1.12.2-0.12.2.jar)   openmods.core.OpenModsClassTransformer LoadingPlugin (ResourceLoader-MC1.12.1-1.5.3.jar)   lumien.resourceloader.asm.ClassTransformer CTMCorePlugin (CTM-MC1.12.2-1.0.2.31.jar)   team.chisel.ctm.client.asm.CTMTransformer XaeroMinimapPlugin (Xaeros_Minimap_23.2.0_Forge_1.12.jar)   xaero.common.core.transformer.ChunkTransformer   xaero.common.core.transformer.NetHandlerPlayClientTransformer   xaero.common.core.transformer.EntityPlayerTransformer   xaero.common.core.transformer.AbstractClientPlayerTransformer   xaero.common.core.transformer.WorldClientTransformer   xaero.common.core.transformer.EntityPlayerSPTransformer   xaero.common.core.transformer.PlayerListTransformer   xaero.common.core.transformer.SaveFormatTransformer   xaero.common.core.transformer.GuiIngameForgeTransformer   xaero.common.core.transformer.GuiBossOverlayTransformer   xaero.common.core.transformer.ModelRendererTransformer     GL info: ' Vendor: 'ATI Technologies Inc.' Version: '4.6.0 Compatibility Profile Context 23.3.2.230315' Renderer: 'Radeon RX 560 Series'     Launched Version: 1.12.2-forge-14.23.5.2859     LWJGL: 2.9.4     OpenGL: Radeon RX 560 Series GL version 4.6.0 Compatibility Profile Context 23.3.2.230315, ATI Technologies Inc.     GL Caps: Using GL 1.3 multitexturing. Using GL 1.3 texture combiners. Using framebuffer objects because OpenGL 3.0 is supported and separate blending is supported. Shaders are available because OpenGL 2.1 is supported. VBOs are available because OpenGL 1.5 is supported.     Using VBOs: Yes     Is Modded: Definitely; Client brand changed to 'fml,forge'     Type: Client (map_client.txt)     Resource Packs:      Current Language: English (US)     Profiler Position: N/A (disabled)     CPU: 12x Intel(R) Core(TM) i5-10400 CPU @ 2.90GHz Idk what to do pls help
    • Hi I’m running same mod on a server and realized they are spawning in chests which I don’t want, if I were to edit this data pack to disable them spawning in chests how would I do that? Explain to me like I’m 8
  • Topics

×
×
  • Create New...

Important Information

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