Paper/nms-patches/CraftingManager.patch

113 lines
5.1 KiB
Diff
Raw Normal View History

2015-05-25 12:37:24 +02:00
--- a/net/minecraft/server/CraftingManager.java
+++ b/net/minecraft/server/CraftingManager.java
2020-06-25 02:00:00 +02:00
@@ -23,11 +23,13 @@
2019-06-21 12:00:00 +02: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 {
private static final Gson a = (new GsonBuilder()).setPrettyPrinting().disableHtmlEscaping().create();
2019-04-25 04:00:00 +02:00
private static final Logger LOGGER = LogManager.getLogger();
2019-06-21 12:00:00 +02:00
- public Map<Recipes<?>, Map<MinecraftKey, IRecipe<?>>> recipes = ImmutableMap.of();
+ public Map<Recipes<?>, Object2ObjectLinkedOpenHashMap<MinecraftKey, IRecipe<?>>> recipes = ImmutableMap.of(); // CraftBukkit
private boolean d;
2019-06-21 12:00:00 +02:00
public CraftingManager() {
2020-06-25 02:00:00 +02:00
@@ -36,7 +38,12 @@
2019-04-23 04:00:00 +02:00
2020-06-25 02:00:00 +02:00
protected void a(Map<MinecraftKey, JsonElement> map, IResourceManager iresourcemanager, GameProfilerFiller gameprofilerfiller) {
2019-06-21 12:00:00 +02:00
this.d = false;
- 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 12:00:00 +02:00
Iterator iterator = map.entrySet().iterator();
2019-04-23 04:00:00 +02:00
2019-06-21 12:00:00 +02:00
while (iterator.hasNext()) {
2020-06-25 02:00:00 +02:00
@@ -46,24 +53,42 @@
2019-06-21 12:00:00 +02:00
try {
2020-06-25 02:00:00 +02:00
IRecipe<?> irecipe = a(minecraftkey, ChatDeserializer.m((JsonElement) entry.getValue(), "top element"));
2019-06-21 12:00:00 +02:00
- ((Builder) map1.computeIfAbsent(irecipe.g(), (recipes) -> {
- return ImmutableMap.builder();
- })).put(minecraftkey, irecipe);
+ // CraftBukkit start - SPIGOT-4638: last recipe gets priority
+ (map1.computeIfAbsent(irecipe.g(), (recipes) -> {
+ return new Object2ObjectLinkedOpenHashMap<>();
+ })).putAndMoveToFirst(minecraftkey, irecipe);
+ // CraftBukkit end
} catch (IllegalArgumentException | JsonParseException jsonparseexception) {
CraftingManager.LOGGER.error("Parsing error loading recipe {}", minecraftkey, jsonparseexception);
}
}
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
}));
CraftingManager.LOGGER.info("Loaded {} recipes", map1.size());
}
2019-06-21 12:00:00 +02:00
+ // CraftBukkit start
+ public void addRecipe(IRecipe<?> irecipe) {
+ Object2ObjectLinkedOpenHashMap<MinecraftKey, IRecipe<?>> map = this.recipes.get(irecipe.g()); // CraftBukkit
+
+ if (map.containsKey(irecipe.getKey())) {
+ throw new IllegalStateException("Duplicate recipe ignored with ID " + irecipe.getKey());
+ } else {
+ map.putAndMoveToFirst(irecipe.getKey(), irecipe); // CraftBukkit - SPIGOT-4638: last recipe gets priority
+ }
+ }
+ // CraftBukkit end
+
2019-04-23 04:00:00 +02:00
public <C extends IInventory, T extends IRecipe<C>> Optional<T> craft(Recipes<T> recipes, C c0, World world) {
2020-06-25 02:00:00 +02:00
- return this.b(recipes).values().stream().flatMap((irecipe) -> {
2019-04-23 04:00:00 +02:00
+ // CraftBukkit start
2020-06-25 02:00:00 +02:00
+ Optional<T> recipe = this.b(recipes).values().stream().flatMap((irecipe) -> {
2019-04-23 04:00:00 +02:00
return SystemUtils.a(recipes.a(irecipe, world, c0));
}).findFirst();
+ c0.setCurrentRecipe(recipe.orElse(null)); // CraftBukkit - Clear recipe when no recipe is found
+ // CraftBukkit end
+ return recipe;
}
2020-06-25 02:00:00 +02:00
public <C extends IInventory, T extends IRecipe<C>> List<T> a(Recipes<T> recipes) {
@@ -81,7 +106,7 @@
2019-04-23 04:00:00 +02:00
}
2020-06-25 02:00:00 +02:00
private <C extends IInventory, T extends IRecipe<C>> Map<MinecraftKey, IRecipe<C>> b(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
2019-04-23 04:00:00 +02:00
public <C extends IInventory, T extends IRecipe<C>> NonNullList<ItemStack> c(Recipes<T> recipes, C c0, World world) {
2020-06-25 02:00:00 +02:00
@@ -102,7 +127,7 @@
2019-04-23 04:00:00 +02:00
2020-08-11 23:00:00 +02:00
public Optional<? extends IRecipe<?>> getRecipe(MinecraftKey minecraftkey) {
2019-04-23 04:00:00 +02: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 04:00:00 +02:00
}
2020-06-25 02:00:00 +02:00
@@ -125,4 +150,14 @@
2019-06-21 12:00:00 +02:00
return new JsonSyntaxException("Invalid or unsupported recipe type '" + s + "'");
2019-04-23 04:00:00 +02:00
})).a(minecraftkey, jsonobject);
}
2019-06-21 12:00:00 +02:00
+
+ // CraftBukkit start
+ public void clearRecipes() {
+ this.recipes = Maps.newHashMap();
2019-06-21 12:00:00 +02:00
+
+ for (Recipes<?> recipeType : IRegistry.RECIPE_TYPE) {
+ this.recipes.put(recipeType, new Object2ObjectLinkedOpenHashMap<>());
2019-06-21 12:00:00 +02:00
+ }
+ }
+ // CraftBukkit end
}