Search code examples
javaminecraftminecraft-fabric

How to get the title of the open inventory using a fabric 1.16.5 mod


I'm using fabric 1.16.5 to develop a client-side mod for minecraft, and I am trying to get the title of the open inventory (see example below).

inventory example

In the example, the title is "Sell Items - 0" (with the emoji).

What I've tried:

  • MinecraftClient.getInstance().player.inventory.getName() -> Returns "Inventory"
  • MinecraftClient.getInstance().player.inventory.getDisplayName() -> Returns "Inventory"
  • MinecraftClient.getInstance().player.inventory.getCustomName() -> Returns null

Because of the results, I'm guessing that inventory is returning the bottom inventory, the survival inventory. How can I get the custom inventory provided by the server (the top inventory).

All help is appreciated.


Solution

  • This can be achieved through Mixins.
    More specifically, this can be achieved by Mixining into HandledScreens's open(ScreenHandlerType<T>, MinecraftClient, int, Text) method.

    First, create the mixin class and register it in your mod's mod.mixins.json's client section.

    import net.minecraft.client.gui.screen.ingame.HandledScreens;
    import org.spongepowered.asm.mixin.Mixin;
    
    @Mixin(HandledScreens.class)
    public class HandledScreensMixin {
    
    }
    

    Next, inject into the head of open(ScreenHandlerType<T>, MinecraftClient, int, Text):

    @Inject(at = @At("HEAD"), method = "open")
    private static <T extends ScreenHandler> void open(ScreenHandlerType<T> type, MinecraftClient client, int id, Text title, CallbackInfo ci) {
    
    }
    

    In order to obtain a String version of the title, you can use title.getString().
    What you choose to do next depends on what your goal is.

    For example, you could use a class with a getter and a setter so that you can access the value later.