2

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
KingsDev
  • 654
  • 5
  • 21
  • there isn't a `player.openedinventory` ? Because for me, the given object `player.inventory` refer to the inv at the bottom – Elikill58 Dec 11 '21 at 10:04
  • There is no ```player.openedinventory``` or similar. I did point out in the post that I thought ```player.inventory``` was the bottom inventory. – KingsDev Dec 11 '21 at 22:10
  • Did you find a way ? – Elikill58 May 20 '22 at 16:23
  • Yeah, but I'd have to go digging to find it. Did you need it? – KingsDev May 21 '22 at 09:55
  • If you need lot of search, no. I think I have a way. But, if you find it again, you can self answer to your question – Elikill58 May 21 '22 at 23:40
  • @Elikill58 9 months later but I've had another look at the question and figured out how to solve it. Did you still need it? – KingsDev Sep 02 '22 at 11:55
  • 1
    Thanks for news. I think I fix it since this date too, I don't remember exactly for what I needed it. But it's perfect if you self-answered – Elikill58 Sep 02 '22 at 12:02

1 Answers1

1

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.

KingsDev
  • 654
  • 5
  • 21