Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

I'm making a Minecraft 1.21 mod using Fabric. I've got a ScreenHandler similar to the…

  1. User

    I'm making a Minecraft 1.21 mod using Fabric. I've got a ScreenHandler similar to the stone cutter (no block entity because it just opens a menu). I want to show available recipes similar to the stone cutter, but only ones that the player has unlocked (through special means). I've figured out how to add custom recipes and have those show up, but I can't figure out how to filter so it only shows ones the player has unlocked. The issue is that player.getRecipeBook isn't available in the regular PlayerEntity class, and I can't import ClientPlayerEntity in my ScreenHandler class. I tried accessing the method using player.getClass().getMethdod("getRecipeBook").invoke(player), but that just crashes.

  2. Assistant

    To access the player's recipe book and filter the recipes based on unlock status in your ScreenHandler, you need to cast the player to a ServerPlayerEntity. The getRecipeBook() method isn't available in PlayerEntity, but it is available in both ServerPlayerEntity (server-side) and ClientPlayerEntity (client-side). Since ScreenHandler operates on the server side, you should use ServerPlayerEntity.

    Here's how you can modify your code:

    if (player instanceof ServerPlayerEntity serverPlayer) {
        RecipeBook recipeBook = serverPlayer.getRecipeBook();
        // Now you can access the unlocked recipes
        // For example:
        // Set<Recipe<?>> unlockedRecipes = recipeBook.getRecipes();
        // Then filter your available recipes based on this set
    } else {
        // Handle the case where the player isn't a ServerPlayerEntity
    }
    

    Explanation:

    • Casting to ServerPlayerEntity: The player reference in your ScreenHandler is of type PlayerEntity, which doesn't have the getRecipeBook() method. By checking if the player is an instance of ServerPlayerEntity and casting it, you gain access to server-specific methods, including getRecipeBook().

    • Accessing the Recipe Book: Once you have the ServerPlayerEntity, you can call getRecipeBook() to obtain the player's RecipeBook. From there, you can get the set of recipes the player has unlocked.

    • Filtering Recipes: Use the unlocked recipes from the RecipeBook to filter your custom recipes. This ensures that only recipes the player has unlocked will be displayed in your ScreenHandler, similar to how the stone cutter works.

    Note: Avoid using reflection (like getClass().getMethod("getRecipeBook")) as it can lead to crashes if the method isn't found or accessible. Casting to the correct class is a safer and more reliable approach.

    Example of Filtering:

    // Assume 'allRecipes' is the list of all your custom recipes
    List<Recipe<?>> allRecipes = ...;
    Set<Recipe<?>> unlockedRecipes = recipeBook.values();
    
    // Filter the recipes
    List<Recipe<?>> availableRecipes = allRecipes.stream()
        .filter(unlockedRecipes::contains)
        .collect(Collectors.toList());
    
    // Now use 'availableRecipes' in your ScreenHandler
    

    By casting the player to ServerPlayerEntity and accessing the RecipeBook, you can effectively filter and display only the recipes the player has unlocked in your custom ScreenHandler.


    This approach should resolve your issue and allow you to display only the unlocked recipes in your mod's GUI.