2021-03-15 23:00:00 +01:00
--- a/net/minecraft/world/item/crafting/CraftingManager.java
+++ b/net/minecraft/world/item/crafting/CraftingManager.java
2024-04-23 17:15:00 +02:00
@@ -34,6 +34,11 @@
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
2023-12-05 17:40:00 +01:00
+// CraftBukkit start
2024-04-23 17:15:00 +02:00
+import com.google.common.collect.LinkedHashMultimap;
+import com.google.common.collect.Maps;
2023-12-05 17:40:00 +01:00
+// CraftBukkit end
2019-06-21 12:00:00 +02:00
+
public class CraftingManager extends ResourceDataJson {
2021-06-11 07:00:00 +02:00
private static final Gson GSON = (new GsonBuilder()).setPrettyPrinting().disableHtmlEscaping().create();
2024-04-23 17:15:00 +02:00
@@ -70,19 +75,39 @@
}
2019-02-26 01:17:42 +01:00
}
2019-06-21 12:00:00 +02:00
2024-04-23 17:15:00 +02:00
- this.byType = builder.build();
- this.byName = com_google_common_collect_immutablemap_builder.build();
+ // CraftBukkit start - mutable
+ this.byType = LinkedHashMultimap.create(builder.build());
+ this.byName = Maps.newHashMap(com_google_common_collect_immutablemap_builder.build());
+ // CraftBukkit end
CraftingManager.LOGGER.info("Loaded {} recipes", this.byType.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) {
2024-04-23 17:15:00 +02:00
+ Collection<RecipeHolder<?>> map = this.byType.get(irecipe.value().getType()); // CraftBukkit
2019-06-21 12:00:00 +02:00
+
2024-04-23 17:15:00 +02:00
+ if (byName.containsKey(irecipe.id())) {
2023-09-21 18:40:00 +02:00
+ throw new IllegalStateException("Duplicate recipe ignored with ID " + irecipe.id());
2019-06-21 12:00:00 +02:00
+ } else {
2024-04-23 17:15:00 +02:00
+ map.add(irecipe);
2023-09-21 18:40:00 +02:00
+ 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) {
2024-04-23 17:15:00 +02:00
- return this.byType(recipes).stream().filter((recipeholder) -> {
2019-04-23 04:00:00 +02:00
+ // CraftBukkit start
2024-04-23 17:15:00 +02:00
+ List<RecipeHolder<T>> list = this.byType(recipes).stream().filter((recipeholder) -> {
2023-09-21 18:40:00 +02:00
return recipeholder.value().matches(c0, world);
2024-04-23 17:15:00 +02:00
- }).findFirst();
+ }).toList();
+ Optional<RecipeHolder<T>> recipe = (list.isEmpty()) ? Optional.empty() : Optional.of(list.getLast()); // CraftBukkit - SPIGOT-4638: last recipe gets priority
2019-04-23 04:00:00 +02:00
+ 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
2024-04-23 17:15:00 +02:00
public <C extends IInventory, T extends IRecipe<C>> Optional<RecipeHolder<T>> getRecipeFor(Recipes<T> recipes, C c0, World world, @Nullable MinecraftKey minecraftkey) {
@@ -94,9 +119,14 @@
}
}
- return this.byType(recipes).stream().filter((recipeholder1) -> {
+ // CraftBukkit start
+ List<RecipeHolder<T>> list = this.byType(recipes).stream().filter((recipeholder1) -> {
return recipeholder1.value().matches(c0, world);
- }).findFirst();
+ }).toList();
+ Optional<RecipeHolder<T>> recipe = (list.isEmpty()) ? Optional.empty() : Optional.of(list.getLast()); // CraftBukkit - SPIGOT-4638: last recipe gets priority
+ c0.setCurrentRecipe(recipe.orElse(null)); // CraftBukkit - Clear recipe when no recipe is found
+ return recipe;
+ // CraftBukkit end
}
public <C extends IInventory, T extends IRecipe<C>> List<RecipeHolder<T>> getAllRecipesFor(Recipes<T> recipes) {
@@ -112,7 +142,7 @@
2019-04-23 04:00:00 +02:00
}
2014-11-25 22:32:16 +01:00
2024-04-23 17:15:00 +02:00
private <C extends IInventory, T extends IRecipe<C>> Collection<RecipeHolder<T>> byType(Recipes<T> recipes) {
- return this.byType.get(recipes);
+ return (Collection) this.byType.get(recipes); // CraftBukkit - decompile error
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) {
2024-04-23 17:15:00 +02:00
@@ -139,7 +169,7 @@
private <T extends IRecipe<?>> RecipeHolder<T> byKeyTyped(Recipes<T> recipes, MinecraftKey minecraftkey) {
RecipeHolder<?> recipeholder = (RecipeHolder) this.byName.get(minecraftkey);
2021-06-11 07:00:00 +02:00
2024-04-23 17:15:00 +02:00
- return recipeholder != null && recipeholder.value().getType().equals(recipes) ? recipeholder : null;
+ return recipeholder != null && recipeholder.value().getType().equals(recipes) ? (RecipeHolder) recipeholder : null; // CraftBukkit - decompile error
}
2021-06-11 07:00:00 +02:00
2024-04-23 17:15:00 +02:00
public Collection<RecipeHolder<?>> getOrderedRecipes() {
@@ -175,10 +205,31 @@
com_google_common_collect_immutablemap_builder.put(recipeholder.id(), recipeholder);
}
- this.byType = builder.build();
- this.byName = com_google_common_collect_immutablemap_builder.build();
+ // CraftBukkit start - mutable
+ this.byType = LinkedHashMultimap.create(builder.build());
+ this.byName = Maps.newHashMap(com_google_common_collect_immutablemap_builder.build());
+ // CraftBukkit end
2023-12-05 17:40:00 +01:00
}
2019-06-21 12:00:00 +02:00
+ // CraftBukkit start
2021-12-13 01:25:22 +01:00
+ public boolean removeRecipe(MinecraftKey mcKey) {
2024-04-23 17:15:00 +02:00
+ Iterator<RecipeHolder<?>> iter = byType.values().iterator();
+ while (iter.hasNext()) {
+ RecipeHolder<?> recipe = iter.next();
+ if (recipe.id().equals(mcKey)) {
+ iter.remove();
+ }
2021-12-13 01:25:22 +01:00
+ }
+
+ return byName.remove(mcKey) != null;
2023-12-05 17:40:00 +01:00
+ }
+
2019-06-22 10:24:23 +02:00
+ public void clearRecipes() {
2024-04-23 17:15:00 +02:00
+ this.byType = LinkedHashMultimap.create();
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