Skip to content

Commit

Permalink
Fix JEI integration for MC 1.21
Browse files Browse the repository at this point in the history
 - Use the client side registry access (possible now that we've moved
   JEI to the client).

 - Generate unique ids for JEI recipes.
  • Loading branch information
SquidDev committed Jun 22, 2024
1 parent 28f75a0 commit efd9a0f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import mezz.jei.api.registration.IAdvancedRegistration;
import mezz.jei.api.registration.ISubtypeRegistration;
import mezz.jei.api.runtime.IJeiRuntime;
import net.minecraft.client.Minecraft;
import net.minecraft.core.RegistryAccess;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;

Expand All @@ -44,15 +46,15 @@ public void registerItemSubtypes(ISubtypeRegistration subtypeRegistry) {

@Override
public void registerAdvanced(IAdvancedRegistration registry) {
registry.addRecipeManagerPlugin(new RecipeResolver());
registry.addRecipeManagerPlugin(new RecipeResolver(getRegistryAccess()));
}

@Override
public void onRuntimeAvailable(IJeiRuntime runtime) {
var registry = runtime.getRecipeManager();

// Register all turtles/pocket computers (not just vanilla upgrades) as upgrades on JEI.
var upgradeItems = RecipeModHelpers.getExtraStacks(RecipeModHelpers.getEmptyRegistryAccess());
var upgradeItems = RecipeModHelpers.getExtraStacks(getRegistryAccess());
if (!upgradeItems.isEmpty()) {
runtime.getIngredientManager().addIngredientsAtRuntime(VanillaTypes.ITEM_STACK, upgradeItems);
}
Expand Down Expand Up @@ -99,4 +101,8 @@ public void onRuntimeAvailable(IJeiRuntime runtime) {
* Distinguishes disks by colour.
*/
private static final IIngredientSubtypeInterpreter<ItemStack> diskSubtype = (stack, ctx) -> Integer.toString(DiskItem.getColour(stack));

private static RegistryAccess getRegistryAccess() {
return Minecraft.getInstance().level.registryAccess();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package dan200.computercraft.client.integration.jei;

import dan200.computercraft.api.ComputerCraftAPI;
import dan200.computercraft.shared.integration.RecipeModHelpers;
import dan200.computercraft.shared.integration.UpgradeRecipeGenerator;
import dan200.computercraft.shared.pocket.items.PocketComputerItem;
import dan200.computercraft.shared.turtle.items.TurtleItem;
Expand All @@ -14,6 +13,7 @@
import mezz.jei.api.recipe.RecipeType;
import mezz.jei.api.recipe.advanced.IRecipeManagerPlugin;
import mezz.jei.api.recipe.category.IRecipeCategory;
import net.minecraft.core.HolderLookup;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.CraftingRecipe;
Expand All @@ -22,8 +22,19 @@
import java.util.List;

class RecipeResolver implements IRecipeManagerPlugin {
private static final ResourceLocation RECIPE_ID = ResourceLocation.fromNamespaceAndPath(ComputerCraftAPI.MOD_ID, "upgrade");
private final UpgradeRecipeGenerator<RecipeHolder<CraftingRecipe>> resolver = new UpgradeRecipeGenerator<>(x -> new RecipeHolder<>(RECIPE_ID, x), RecipeModHelpers.getEmptyRegistryAccess());
private final UpgradeRecipeGenerator<RecipeHolder<CraftingRecipe>> resolver;

/**
* We need to generate unique ids for each recipe, as JEI will attempt to deduplicate them otherwise.
*/
private int nextId = 0;

RecipeResolver(HolderLookup.Provider registries) {
resolver = new UpgradeRecipeGenerator<>(
x -> new RecipeHolder<>(ResourceLocation.fromNamespaceAndPath(ComputerCraftAPI.MOD_ID, "upgrade_" + nextId++), x),
registries
);
}

@Override
public <V> List<RecipeType<?>> getRecipeTypes(IFocus<V> focus) {
Expand Down Expand Up @@ -60,7 +71,7 @@ public <T> List<T> getRecipes(IRecipeCategory<T> recipeCategory) {
return List.of();
}

@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings({ "unchecked", "rawtypes", "UnusedVariable" })
private static <T, U> List<T> cast(RecipeType<U> ignoredType, List<U> from) {
return (List) from;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import net.minecraft.core.Holder;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
Expand Down Expand Up @@ -77,18 +76,6 @@ public static List<ItemStack> getExtraStacks(HolderLookup.Provider registries) {
return upgradeItems;
}

/**
* A temporary function to denote places where we need a {@link HolderLookup.Provider} within our recipe mods, but
* don't have access to one.
*
* @return The empty recipe mod access.
* @deprecated We should get the registry access from a more sensible place.
*/
@Deprecated
public static HolderLookup.Provider getEmptyRegistryAccess() {
return RegistryAccess.EMPTY;
}

static <T> void forEachRegistry(HolderLookup.Provider registries, ResourceKey<Registry<T>> registry, Consumer<Holder.Reference<T>> consumer) {
registries.lookup(registry).map(HolderLookup::listElements).orElse(Stream.empty()).forEach(consumer);
}
Expand Down

0 comments on commit efd9a0f

Please sign in to comment.