--- a/net/minecraft/world/item/crafting/CraftingManager.java +++ b/net/minecraft/world/item/crafting/CraftingManager.java @@ -37,11 +37,13 @@ import net.minecraft.world.level.World; import org.slf4j.Logger; +import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap; // CraftBukkit + public class CraftingManager extends ResourceDataJson { private static final Gson GSON = (new GsonBuilder()).setPrettyPrinting().disableHtmlEscaping().create(); private static final Logger LOGGER = LogUtils.getLogger(); - public Map, Map>> recipes = ImmutableMap.of(); + public Map, Object2ObjectLinkedOpenHashMap>> recipes = ImmutableMap.of(); // CraftBukkit private Map> byName = ImmutableMap.of(); private boolean hasErrors; @@ -51,7 +53,12 @@ protected void apply(Map map, IResourceManager iresourcemanager, GameProfilerFiller gameprofilerfiller) { this.hasErrors = false; - Map, Builder>> map1 = Maps.newHashMap(); + // CraftBukkit start - SPIGOT-5667 make sure all types are populated and mutable + Map, Object2ObjectLinkedOpenHashMap>> map1 = Maps.newHashMap(); + for (Recipes recipeType : BuiltInRegistries.RECIPE_TYPE) { + map1.put(recipeType, new Object2ObjectLinkedOpenHashMap<>()); + } + // CraftBukkit end Builder> builder = ImmutableMap.builder(); Iterator iterator = map.entrySet().iterator(); @@ -62,8 +69,10 @@ try { RecipeHolder recipeholder = fromJson(minecraftkey, ChatDeserializer.convertToJsonObject((JsonElement) entry.getValue(), "top element")); - ((Builder) map1.computeIfAbsent(recipeholder.value().getType(), (recipes) -> { - return ImmutableMap.builder(); + // CraftBukkit start + (map1.computeIfAbsent(recipeholder.value().getType(), (recipes) -> { + return new Object2ObjectLinkedOpenHashMap<>(); + // CraftBukkit end })).put(minecraftkey, recipeholder); builder.put(minecraftkey, recipeholder); } catch (IllegalArgumentException | JsonParseException jsonparseexception) { @@ -72,20 +81,37 @@ } this.recipes = (Map) map1.entrySet().stream().collect(ImmutableMap.toImmutableMap(Entry::getKey, (entry1) -> { - return ((Builder) entry1.getValue()).build(); + return (entry1.getValue()); // CraftBukkit })); - this.byName = builder.build(); + this.byName = Maps.newHashMap(builder.build()); // CraftBukkit CraftingManager.LOGGER.info("Loaded {} recipes", map1.size()); } + // CraftBukkit start + public void addRecipe(RecipeHolder irecipe) { + Object2ObjectLinkedOpenHashMap> map = this.recipes.get(irecipe.value().getType()); // CraftBukkit + + if (byName.containsKey(irecipe.id()) || map.containsKey(irecipe.id())) { + throw new IllegalStateException("Duplicate recipe ignored with ID " + irecipe.id()); + } else { + map.putAndMoveToFirst(irecipe.id(), irecipe); // CraftBukkit - SPIGOT-4638: last recipe gets priority + byName.put(irecipe.id(), irecipe); + } + } + // CraftBukkit end + public boolean hadErrorsLoading() { return this.hasErrors; } public > Optional> getRecipeFor(Recipes recipes, C c0, World world) { - return this.byType(recipes).values().stream().filter((recipeholder) -> { + // CraftBukkit start + Optional> recipe = this.byType(recipes).values().stream().filter((recipeholder) -> { return recipeholder.value().matches(c0, world); }).findFirst(); + c0.setCurrentRecipe(recipe.orElse(null)); // CraftBukkit - Clear recipe when no recipe is found + return recipe; + // CraftBukkit end } public > Optional>> getRecipeFor(Recipes recipes, C c0, World world, @Nullable MinecraftKey minecraftkey) { @@ -119,7 +145,7 @@ } private > Map> byType(Recipes recipes) { - return (Map) this.recipes.getOrDefault(recipes, Collections.emptyMap()); + return (Map) this.recipes.getOrDefault(recipes, new Object2ObjectLinkedOpenHashMap<>()); // CraftBukkit } public > NonNullList getRemainingItemsFor(Recipes recipes, C c0, World world) { @@ -166,12 +192,12 @@ public void replaceRecipes(Iterable> iterable) { this.hasErrors = false; - Map, Map>> map = Maps.newHashMap(); + Map, Object2ObjectLinkedOpenHashMap>> map = Maps.newHashMap(); // CraftBukkit Builder> builder = ImmutableMap.builder(); iterable.forEach((recipeholder) -> { Map> map1 = (Map) map.computeIfAbsent(recipeholder.value().getType(), (recipes) -> { - return Maps.newHashMap(); + return new Object2ObjectLinkedOpenHashMap<>(); // CraftBukkit }); MinecraftKey minecraftkey = recipeholder.id(); RecipeHolder recipeholder1 = (RecipeHolder) map1.put(minecraftkey, recipeholder); @@ -182,8 +208,28 @@ } }); this.recipes = ImmutableMap.copyOf(map); - this.byName = builder.build(); + this.byName = Maps.newHashMap(builder.build()); // CraftBukkit + } + + // CraftBukkit start + public boolean removeRecipe(MinecraftKey mcKey) { + for (Object2ObjectLinkedOpenHashMap> recipes : recipes.values()) { + recipes.remove(mcKey); + } + + return byName.remove(mcKey) != null; + } + + public void clearRecipes() { + this.recipes = Maps.newHashMap(); + + for (Recipes recipeType : BuiltInRegistries.RECIPE_TYPE) { + this.recipes.put(recipeType, new Object2ObjectLinkedOpenHashMap<>()); + } + + this.byName = Maps.newHashMap(); } + // CraftBukkit end public static > CraftingManager.a createCheck(final Recipes recipes) { return new CraftingManager.a() {