craftbukkit/nms-patches/net/minecraft/world/item/crafting/CraftingManager.patch

130 lines
5.8 KiB
Diff
Raw Normal View History

2021-03-16 09:00:00 +11:00
--- a/net/minecraft/world/item/crafting/CraftingManager.java
+++ b/net/minecraft/world/item/crafting/CraftingManager.java
@@ -34,11 +34,13 @@
2019-06-21 20:00:00 +10:00
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
+import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap; // CraftBukkit
+
public class CraftingManager extends ResourceDataJson {
2021-06-11 15:00:00 +10:00
private static final Gson GSON = (new GsonBuilder()).setPrettyPrinting().disableHtmlEscaping().create();
2019-04-25 12:00:00 +10:00
private static final Logger LOGGER = LogManager.getLogger();
2019-06-21 20:00:00 +10:00
- public Map<Recipes<?>, Map<MinecraftKey, IRecipe<?>>> recipes = ImmutableMap.of();
+ public Map<Recipes<?>, Object2ObjectLinkedOpenHashMap<MinecraftKey, IRecipe<?>>> recipes = ImmutableMap.of(); // CraftBukkit
2021-06-11 15:00:00 +10:00
private boolean hasErrors;
2019-06-21 20:00:00 +10:00
public CraftingManager() {
2021-03-16 09:00:00 +11:00
@@ -47,7 +49,12 @@
2019-04-23 12:00:00 +10:00
2021-11-22 09:00:00 +11:00
protected void apply(Map<MinecraftKey, JsonElement> map, IResourceManager iresourcemanager, GameProfilerFiller gameprofilerfiller) {
2021-06-11 15:00:00 +10:00
this.hasErrors = false;
2019-06-21 20:00:00 +10:00
- Map<Recipes<?>, Builder<MinecraftKey, IRecipe<?>>> map1 = Maps.newHashMap();
+ // CraftBukkit start - SPIGOT-5667 make sure all types are populated and mutable
+ Map<Recipes<?>, Object2ObjectLinkedOpenHashMap<MinecraftKey, IRecipe<?>>> map1 = Maps.newHashMap();
+ for (Recipes<?> recipeType : IRegistry.RECIPE_TYPE) {
+ map1.put(recipeType, new Object2ObjectLinkedOpenHashMap<>());
+ }
+ // CraftBukkit end
2019-06-21 20:00:00 +10:00
Iterator iterator = map.entrySet().iterator();
2019-04-23 12:00:00 +10:00
2019-06-21 20:00:00 +10:00
while (iterator.hasNext()) {
@@ -57,8 +64,10 @@
2019-06-21 20:00:00 +10:00
try {
2021-11-22 09:00:00 +11:00
IRecipe<?> irecipe = fromJson(minecraftkey, ChatDeserializer.convertToJsonObject((JsonElement) entry.getValue(), "top element"));
2019-06-21 20:00:00 +10:00
2021-11-22 09:00:00 +11:00
- ((Builder) map1.computeIfAbsent(irecipe.getType(), (recipes) -> {
2019-06-21 20:00:00 +10:00
- return ImmutableMap.builder();
+ // CraftBukkit start
2021-11-22 09:00:00 +11:00
+ (map1.computeIfAbsent(irecipe.getType(), (recipes) -> {
2019-06-21 20:00:00 +10:00
+ return new Object2ObjectLinkedOpenHashMap<>();
+ // CraftBukkit end
})).put(minecraftkey, irecipe);
2019-06-21 20:00:00 +10:00
} catch (IllegalArgumentException | JsonParseException jsonparseexception) {
CraftingManager.LOGGER.error("Parsing error loading recipe {}", minecraftkey, jsonparseexception);
@@ -66,19 +75,35 @@
}
2019-06-21 20:00:00 +10:00
this.recipes = (Map) map1.entrySet().stream().collect(ImmutableMap.toImmutableMap(Entry::getKey, (entry1) -> {
- return ((Builder) entry1.getValue()).build();
+ return (entry1.getValue()); // CraftBukkit
}));
CraftingManager.LOGGER.info("Loaded {} recipes", map1.size());
}
2019-06-21 20:00:00 +10:00
+ // CraftBukkit start
+ public void addRecipe(IRecipe<?> irecipe) {
2021-11-22 09:00:00 +11:00
+ Object2ObjectLinkedOpenHashMap<MinecraftKey, IRecipe<?>> map = this.recipes.get(irecipe.getType()); // CraftBukkit
2019-06-21 20:00:00 +10:00
+
2021-11-22 09:00:00 +11:00
+ if (map.containsKey(irecipe.getId())) {
+ throw new IllegalStateException("Duplicate recipe ignored with ID " + irecipe.getId());
2019-06-21 20:00:00 +10:00
+ } else {
2021-11-22 09:00:00 +11:00
+ map.putAndMoveToFirst(irecipe.getId(), irecipe); // CraftBukkit - SPIGOT-4638: last recipe gets priority
2019-06-21 20:00:00 +10:00
+ }
+ }
+ // CraftBukkit end
+
2021-11-22 09:00:00 +11:00
public boolean hadErrorsLoading() {
2021-06-11 15:00:00 +10:00
return this.hasErrors;
}
2021-11-22 09:00:00 +11:00
public <C extends IInventory, T extends IRecipe<C>> Optional<T> getRecipeFor(Recipes<T> recipes, C c0, World world) {
- return this.byType(recipes).values().stream().flatMap((irecipe) -> {
2019-04-23 12:00:00 +10:00
+ // CraftBukkit start
2021-11-22 09:00:00 +11:00
+ Optional<T> recipe = this.byType(recipes).values().stream().flatMap((irecipe) -> {
return SystemUtils.toStream(recipes.tryMatch(irecipe, world, c0));
2019-04-23 12:00:00 +10:00
}).findFirst();
+ c0.setCurrentRecipe(recipe.orElse(null)); // CraftBukkit - Clear recipe when no recipe is found
+ // CraftBukkit end
+ return recipe;
}
2021-11-22 09:00:00 +11:00
public <C extends IInventory, T extends IRecipe<C>> List<T> getAllRecipesFor(Recipes<T> recipes) {
2021-06-11 15:00:00 +10:00
@@ -96,7 +121,7 @@
2019-04-23 12:00:00 +10:00
}
2021-11-22 09:00:00 +11:00
private <C extends IInventory, T extends IRecipe<C>> Map<MinecraftKey, IRecipe<C>> byType(Recipes<T> recipes) {
2019-06-21 20:00:00 +10:00
- return (Map) this.recipes.getOrDefault(recipes, Collections.emptyMap());
+ return (Map) this.recipes.getOrDefault(recipes, new Object2ObjectLinkedOpenHashMap<>()); // CraftBukkit
2019-04-23 12:00:00 +10:00
}
2015-02-26 22:41:06 +00:00
2021-11-22 09:00:00 +11:00
public <C extends IInventory, T extends IRecipe<C>> NonNullList<ItemStack> getRemainingItemsFor(Recipes<T> recipes, C c0, World world) {
2021-06-11 15:00:00 +10:00
@@ -117,7 +142,7 @@
2019-04-23 12:00:00 +10:00
2021-11-22 09:00:00 +11:00
public Optional<? extends IRecipe<?>> byKey(MinecraftKey minecraftkey) {
2019-04-23 12:00:00 +10:00
return this.recipes.values().stream().map((map) -> {
- return (IRecipe) map.get(minecraftkey);
+ return map.get(minecraftkey); // CraftBukkit - decompile error
}).filter(Objects::nonNull).findFirst();
2017-05-14 12:00:00 +10:00
}
2021-06-11 15:00:00 +10:00
@@ -143,11 +168,11 @@
2021-11-22 09:00:00 +11:00
public void replaceRecipes(Iterable<IRecipe<?>> iterable) {
2021-06-11 15:00:00 +10:00
this.hasErrors = false;
- Map<Recipes<?>, Map<MinecraftKey, IRecipe<?>>> map = Maps.newHashMap();
+ Map<Recipes<?>, Object2ObjectLinkedOpenHashMap<MinecraftKey, IRecipe<?>>> map = Maps.newHashMap(); // CraftBukkit
iterable.forEach((irecipe) -> {
2021-11-22 09:00:00 +11:00
Map<MinecraftKey, IRecipe<?>> map1 = (Map) map.computeIfAbsent(irecipe.getType(), (recipes) -> {
2021-06-11 15:00:00 +10:00
- return Maps.newHashMap();
+ return new Object2ObjectLinkedOpenHashMap<>(); // CraftBukkit
});
2021-11-22 09:00:00 +11:00
IRecipe<?> irecipe1 = (IRecipe) map1.put(irecipe.getId(), irecipe);
2021-06-11 15:00:00 +10:00
@@ -157,4 +182,14 @@
});
this.recipes = ImmutableMap.copyOf(map);
2019-04-23 12:00:00 +10:00
}
2019-06-21 20:00:00 +10:00
+
+ // CraftBukkit start
+ public void clearRecipes() {
+ this.recipes = Maps.newHashMap();
2019-06-21 20:00:00 +10:00
+
+ for (Recipes<?> recipeType : IRegistry.RECIPE_TYPE) {
+ this.recipes.put(recipeType, new Object2ObjectLinkedOpenHashMap<>());
2019-06-21 20:00:00 +10:00
+ }
+ }
+ // CraftBukkit end
}