diff --git a/src/main/java/org/betterx/bclib/mixin/common/RecipeManagerAccessor.java b/src/main/java/org/betterx/bclib/mixin/common/RecipeManagerAccessor.java index d30a860a..197a9d46 100644 --- a/src/main/java/org/betterx/bclib/mixin/common/RecipeManagerAccessor.java +++ b/src/main/java/org/betterx/bclib/mixin/common/RecipeManagerAccessor.java @@ -1,6 +1,7 @@ package org.betterx.bclib.mixin.common; import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.Container; import net.minecraft.world.item.crafting.Recipe; import net.minecraft.world.item.crafting.RecipeManager; import net.minecraft.world.item.crafting.RecipeType; @@ -13,14 +14,14 @@ import java.util.Map; @Mixin(RecipeManager.class) public interface RecipeManagerAccessor { @Accessor("recipes") - Map, Map>> bclib_getRecipes(); + > Map, Map> bclib_getRecipes(); @Accessor("recipes") - void bclib_setRecipes(Map, Map>> recipes); + > void bclib_setRecipes(Map, Map> recipes); @Accessor("byName") - Map> bclib_getRecipesByName(); + > Map bclib_getRecipesByName(); @Accessor("byName") - void bclib_setRecipesByName(Map> recipes); + > void bclib_setRecipesByName(Map recipes); } \ No newline at end of file diff --git a/src/main/java/org/betterx/bclib/mixin/common/RecipeManagerMixin.java b/src/main/java/org/betterx/bclib/mixin/common/RecipeManagerMixin.java index 066618ab..5e5af94c 100644 --- a/src/main/java/org/betterx/bclib/mixin/common/RecipeManagerMixin.java +++ b/src/main/java/org/betterx/bclib/mixin/common/RecipeManagerMixin.java @@ -21,9 +21,7 @@ import java.util.Optional; @Mixin(RecipeManager.class) public abstract class RecipeManagerMixin { @Shadow - private > Map> byType(RecipeType type) { - return null; - } + protected abstract > Map byType(RecipeType recipeType); @Inject(method = "getRecipeFor", at = @At(value = "HEAD"), cancellable = true) private > void bclib_getRecipeFor( diff --git a/src/main/java/org/betterx/bclib/recipes/BCLRecipeManager.java b/src/main/java/org/betterx/bclib/recipes/BCLRecipeManager.java index 97dba05d..597df223 100644 --- a/src/main/java/org/betterx/bclib/recipes/BCLRecipeManager.java +++ b/src/main/java/org/betterx/bclib/recipes/BCLRecipeManager.java @@ -12,7 +12,6 @@ import net.minecraft.world.level.ItemLike; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; -import com.google.common.collect.ImmutableList; import com.google.common.collect.Maps; import java.util.*; @@ -20,53 +19,65 @@ import java.util.Map.Entry; import java.util.function.Function; public class BCLRecipeManager { - private static final Map, Map>> RECIPES = Maps.newHashMap(); - private static final Map, Object> SORTED = Maps.newHashMap(); + private static final Map> _RECIPES = Maps.newHashMap(); + private static final Map> _SORTED = Maps.newHashMap(); private static final String MINECRAFT = "minecraft"; + @SuppressWarnings("unchecked") + private static > Map, Map> RECIPES() { + return (Map, Map>) _RECIPES; + } + + @SuppressWarnings("unchecked") + private static > Map, List> SORTED() { + return (Map, List>) _SORTED; + } + public static > Optional getSortedRecipe( RecipeType type, C inventory, Level level, - Function, Map>> getter + Function, Map> getter ) { - List> recipes = (List>) SORTED.computeIfAbsent(type, t -> { - Collection> values = getter.apply(type).values(); - List> list = new ArrayList<>(values); + List recipes = BCLRecipeManager.SORTED().computeIfAbsent(type, t -> { + Collection values = getter.apply(type).values(); + List list = new ArrayList<>(values); list.sort((v1, v2) -> { boolean b1 = v1.getId().getNamespace().equals(MINECRAFT); boolean b2 = v2.getId().getNamespace().equals(MINECRAFT); return b1 ^ b2 ? (b1 ? 1 : -1) : v1.getId().compareTo(v2.getId()); }); - return ImmutableList.copyOf(list); + return list; }); - return (Optional) recipes.stream().filter(recipe -> recipe.matches(inventory, level)).findFirst(); + return recipes.stream().filter(recipe -> recipe.matches(inventory, level)).findFirst(); } - public static void addRecipe(RecipeType type, Recipe recipe) { - Map> list = RECIPES.computeIfAbsent(type, i -> Maps.newHashMap()); + public static > void addRecipe(RecipeType type, T recipe) { + Map list = BCLRecipeManager.RECIPES().computeIfAbsent(type, i -> Maps.newHashMap()); list.put(recipe.getId(), recipe); } - public static > T getRecipe(RecipeType type, ResourceLocation id) { - Map> map = RECIPES.get(type); - return map != null ? (T) map.get(id) : null; + public static > T getRecipe(RecipeType type, ResourceLocation id) { + Map map = BCLRecipeManager.RECIPES().get(type); + return map != null ? map.get(id) : null; } - public static Map, Map>> getMap(Map, Map>> recipes) { - Map, Map>> result = Maps.newHashMap(); + public static > Map, Map> getMap( + Map, Map> recipes + ) { + Map, Map> result = Maps.newHashMap(); - for (RecipeType type : recipes.keySet()) { - Map> typeList = Maps.newHashMap(); + for (RecipeType type : recipes.keySet()) { + Map typeList = Maps.newHashMap(); typeList.putAll(recipes.get(type)); result.put(type, typeList); } - SORTED.clear(); - RECIPES.forEach((type, list) -> { + SORTED().clear(); + BCLRecipeManager.RECIPES().forEach((type, list) -> { if (list != null) { - Map> typeList = result.computeIfAbsent(type, i -> Maps.newHashMap()); - for (Entry> entry : list.entrySet()) { + Map typeList = result.computeIfAbsent(type, i -> Maps.newHashMap()); + for (Entry entry : list.entrySet()) { ResourceLocation id = entry.getKey(); typeList.computeIfAbsent(id, i -> entry.getValue()); } @@ -76,14 +87,17 @@ public class BCLRecipeManager { return result; } - public static Map> getMapByName(Map> recipes) { - Map> result = CollectionsUtil.getMutable(recipes); - RECIPES.values() - .forEach(map -> map.forEach((location, recipe) -> result.computeIfAbsent(location, i -> recipe))); + public static > Map getMapByName(Map recipes) { + Map result = CollectionsUtil.getMutable(recipes); + BCLRecipeManager.RECIPES().values() + .forEach(map -> map.forEach((location, recipe) -> result.computeIfAbsent( + location, + i -> recipe + ))); return result; } - public static , T extends Recipe> S registerSerializer( + public static , T extends Recipe> S registerSerializer( String modID, String id, S serializer @@ -91,7 +105,7 @@ public class BCLRecipeManager { return Registry.register(Registry.RECIPE_SERIALIZER, modID + ":" + id, serializer); } - public static > RecipeType registerType(String modID, String type) { + public static > RecipeType registerType(String modID, String type) { ResourceLocation recipeTypeId = new ResourceLocation(modID, type); return Registry.register(Registry.RECIPE_TYPE, recipeTypeId, new RecipeType() { public String toString() { diff --git a/src/main/java/org/betterx/bclib/recipes/GridRecipe.java b/src/main/java/org/betterx/bclib/recipes/GridRecipe.java index aa1fe9ab..d0445075 100644 --- a/src/main/java/org/betterx/bclib/recipes/GridRecipe.java +++ b/src/main/java/org/betterx/bclib/recipes/GridRecipe.java @@ -22,7 +22,7 @@ public class GridRecipe { private ItemLike output; private String group; - private RecipeType type; + private RecipeType type; private boolean shaped; private String[] shape; private final Map materialKeys = Maps.newHashMap();