I'm making a Minecraft 1.21 mod using Fabric. I've got a ScreenHandler similar to the…
- 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.
- Assistant
To access the player's recipe book and filter the recipes based on unlock status in your ScreenHandler, you need to cast the
playerto aServerPlayerEntity. ThegetRecipeBook()method isn't available inPlayerEntity, but it is available in bothServerPlayerEntity(server-side) andClientPlayerEntity(client-side). SinceScreenHandleroperates on the server side, you should useServerPlayerEntity.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: Theplayerreference in yourScreenHandleris of typePlayerEntity, which doesn't have thegetRecipeBook()method. By checking if the player is an instance ofServerPlayerEntityand casting it, you gain access to server-specific methods, includinggetRecipeBook(). -
Accessing the Recipe Book: Once you have the
ServerPlayerEntity, you can callgetRecipeBook()to obtain the player'sRecipeBook. From there, you can get the set of recipes the player has unlocked. -
Filtering Recipes: Use the unlocked recipes from the
RecipeBookto 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 ScreenHandlerBy casting the player to
ServerPlayerEntityand accessing theRecipeBook, 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.
-