Paper/patches/server/0956-Fix-issues-with-Recipe-API.patch
Spottedleaf 8c5b837e05 Rework async chunk api implementation
Firstly, the old methods all routed to the CompletableFuture method.
However, the CF method could not guarantee that if the caller
was off-main that the future would be "completed" on-main. Since
the callback methods used the CF one, this meant that the callback
methods did not guarantee that the callbacks were to be called on
the main thread.

Now, all methods route to getChunkAtAsync(x, z, gen, urgent, cb)
so that the methods with the callback are guaranteed to invoke
the callback on the main thread. The CF behavior remains unchanged;
it may still appear to complete on main if invoked off-main.

Secondly, remove the scheduleOnMain invocation in the async
chunk completion. This unnecessarily delays the callback
by 1 tick.

Thirdly, add getChunksAtAsync(minX, minZ, maxX, maxZ, ...) which
will load chunks within an area. This method is provided as a helper
as keeping all chunks loaded within an area can be complicated to
implement for plugins (due to the lacking ticket API), and is
already implemented internally anyways.

Fourthly, remove the ticket addition that occured with getChunkAt
and getChunkAtAsync. The ticket addition may delay the unloading
of the chunk unnecessarily. It also fixes a very rare timing bug
where the future/callback would be completed after the chunk
unloads.
2024-11-18 23:00:59 -08:00

115 lines
5.8 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Sun, 12 May 2024 15:49:36 -0700
Subject: [PATCH] Fix issues with Recipe API
diff --git a/src/main/java/net/minecraft/world/item/crafting/ShapedRecipe.java b/src/main/java/net/minecraft/world/item/crafting/ShapedRecipe.java
index dd02af6574dd97404bc9c02c9ead84e1dd537efe..980fea65899ef5f37808506b822fd3de5830d2c7 100644
--- a/src/main/java/net/minecraft/world/item/crafting/ShapedRecipe.java
+++ b/src/main/java/net/minecraft/world/item/crafting/ShapedRecipe.java
@@ -98,7 +98,7 @@ public class ShapedRecipe implements CraftingRecipe {
char c = 'a';
for (Optional<Ingredient> list : this.pattern.ingredients()) {
RecipeChoice choice = CraftRecipe.toBukkit(list);
- if (choice != null) {
+ if (choice != RecipeChoice.empty()) { // Paper
recipe.setIngredient(c, choice);
}
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftRecipe.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftRecipe.java
index a30950287646524c4906574d193ec7ce94b4eb34..3592091c6d1371224e82e1f95b003951ad2f8779 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftRecipe.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftRecipe.java
@@ -21,7 +21,7 @@ public interface CraftRecipe extends Recipe {
void addToCraftingManager();
default Optional<Ingredient> toNMSOptional(RecipeChoice bukkit, boolean requireNotEmpty) {
- return (bukkit == null) ? Optional.empty() : Optional.of(this.toNMS(bukkit, requireNotEmpty));
+ return (bukkit == null || bukkit == RecipeChoice.empty()) ? Optional.empty() : Optional.of(this.toNMS(bukkit, requireNotEmpty)); // Paper - support "empty" choices
}
default Ingredient toNMS(RecipeChoice bukkit, boolean requireNotEmpty) {
@@ -38,6 +38,13 @@ public interface CraftRecipe extends Recipe {
stack = Ingredient.of(((RecipeChoice.MaterialChoice) bukkit).getChoices().stream().map((mat) -> CraftItemType.bukkitToMinecraft(mat)));
} else if (bukkit instanceof RecipeChoice.ExactChoice) {
stack = Ingredient.ofStacks(((RecipeChoice.ExactChoice) bukkit).getChoices().stream().map((mat) -> CraftItemStack.asNMSCopy(mat)).toList());
+ // Paper start - support "empty" choices - legacy method that spigot might incorrectly call
+ // Their impl of Ingredient.of() will error, ingredients need at least one entry.
+ // Callers running into this exception may have passed an incorrect empty() recipe choice to a non-empty slot or
+ // spigot calls this method in a wrong place.
+ } else if (bukkit == RecipeChoice.empty()) {
+ throw new IllegalArgumentException("This ingredient cannot be empty");
+ // Paper end - support "empty" choices
} else {
throw new IllegalArgumentException("Unknown recipe stack instance " + bukkit);
}
@@ -51,14 +58,14 @@ public interface CraftRecipe extends Recipe {
}
public static RecipeChoice toBukkit(Optional<Ingredient> list) {
- return list.map(CraftRecipe::toBukkit).orElse(null);
+ return list.map(CraftRecipe::toBukkit).orElse(RecipeChoice.empty()); // Paper - fix issue with recipe API
}
public static RecipeChoice toBukkit(Ingredient list) {
List<Holder<Item>> items = list.items();
if (items.isEmpty()) {
- return null;
+ return RecipeChoice.empty(); // Paper - null breaks API contracts
}
if (list.isExact()) {
diff --git a/src/test/java/io/papermc/paper/inventory/recipe/TestRecipeChoice.java b/src/test/java/io/papermc/paper/inventory/recipe/TestRecipeChoice.java
new file mode 100644
index 0000000000000000000000000000000000000000..45ab2b6d32b29cb663df848534e1aa68293dd613
--- /dev/null
+++ b/src/test/java/io/papermc/paper/inventory/recipe/TestRecipeChoice.java
@@ -0,0 +1,25 @@
+package io.papermc.paper.inventory.recipe;
+
+import java.util.Iterator;
+import org.bukkit.Bukkit;
+import org.bukkit.inventory.Recipe;
+import org.bukkit.support.environment.AllFeatures;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+@AllFeatures
+class TestRecipeChoice {
+
+ @Test
+ void testRecipeChoices() {
+ final Iterator<Recipe> iter = Bukkit.recipeIterator();
+ boolean foundRecipes = false;
+ while (iter.hasNext()) {
+ foundRecipes = true;
+ assertDoesNotThrow(iter::next, "Failed to convert a recipe to Bukkit recipe!");
+ }
+ assertTrue(foundRecipes, "No recipes found!");
+ }
+}
diff --git a/src/test/java/org/bukkit/support/DummyServerHelper.java b/src/test/java/org/bukkit/support/DummyServerHelper.java
index 55c05c16a80c489cdda2fd03c943921d38d978e9..309d371247adcddf0a1b370cc5faff3e6e01cb0f 100644
--- a/src/test/java/org/bukkit/support/DummyServerHelper.java
+++ b/src/test/java/org/bukkit/support/DummyServerHelper.java
@@ -92,6 +92,15 @@ public final class DummyServerHelper {
// Paper end - testing additions
io.papermc.paper.configuration.GlobalConfigTestingBase.setupGlobalConfigForTest(); // Paper - configuration files - setup global configuration test base
+
+ // Paper start - add test for recipe conversion
+ when(instance.recipeIterator()).thenAnswer(ignored ->
+ com.google.common.collect.Iterators.transform(
+ RegistryHelper.getDataPack().getRecipeManager().recipes.byType.entries().iterator(),
+ input -> input.getValue().toBukkitRecipe()
+ )
+ );
+ // Paper end - add test for recipe conversion
return instance;
}
}