Search code examples
javaminecraftminecraft-fabric

How can I get the player that opens a crafting table in a Fabric mod?


How can I get the player that opens a crafting table in a Fabric mod?

My current code to get inventory title/type

@Mixin(HandledScreens.class)
public class FastCraftMixin {
    @Inject(at = @At("HEAD"), method = "open")
    private static <T extends ScreenHandler> void open(ScreenHandlerType<T> type, MinecraftClient client, int id, Text title, CallbackInfo ci) {
        System.out.println(title.toString());
        
    }
}

Solution

  • There is only one MinecraftClient object and that class only exists on the client. Only one player can be playing on a client. If you're writing a function that has a MinecraftClient parameter then you know the player is the player. You could get it with client.player (as already mentioned) but you should also be aware that on clients, you can also use MinecraftClient.getInstance().player since there is only one.