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