BlueGit

timfa copperfarmer

Fix Paper 26.2 recipe, cargo, and harvest API incompatibilities.

Co-authored-by: Cursor <cursoragent@cursor.com>

timfa Tim Falken · pushed by timfa

Authored 2026-08-14 · 3 months ago

Hash
2c1ce12f10f002d6f12753137947dcb7534fd316
Parent
0b180da6
3 files changed +81 −27
src/main/java/nl/timfalken/copperfarmer/FarmerCargo.java +1 −1
@@ -127,7 +127,7 @@ final class FarmerCargo {
127 127 golem.getPersistentDataContainer().set(
128 128 keys.cargo,
129 129 PersistentDataType.BYTE_ARRAY,
130 ItemStack.serializeItemsAsBytes(items)
130 ItemStack.serializeItemsAsBytes(items.toArray(ItemStack[]::new))
131 131 );
132 132 }
133 133 }
@@ -127,7 +127,7 @@ final class FarmerCargo {
127 golem.getPersistentDataContainer().set(
127 golem.getPersistentDataContainer().set(
128 keys.cargo,
128 keys.cargo,
129 PersistentDataType.BYTE_ARRAY,
129 PersistentDataType.BYTE_ARRAY,
130 ItemStack.serializeItemsAsBytes(items)
130 ItemStack.serializeItemsAsBytes(items.toArray(ItemStack[]::new))
131 );
131 );
132 }
132 }
133}
133}
src/main/java/nl/timfalken/copperfarmer/FarmerManager.java +1 −1
@@ -316,7 +316,7 @@ final class FarmerManager {
316 316 BlockData oldData = cropBlock.getBlockData().clone();
317 317 List<ItemStack> calculatedDrops = new ArrayList<>(cropBlock.getDrops(tool.clone(), golem));
318 318
319 cropBlock.setType(Material.AIR, false);
319 cropBlock.setType(Material.AIR, true);
320 320 List<ItemStack> overflow = cargo.addAll(calculatedDrops);
321 321 for (ItemStack item : overflow) {
322 322 cropBlock.getWorld().dropItemNaturally(cropBlock.getLocation().add(0.5, 0.5, 0.5), item);
@@ -316,7 +316,7 @@ final class FarmerManager {
316 BlockData oldData = cropBlock.getBlockData().clone();
316 BlockData oldData = cropBlock.getBlockData().clone();
317 List<ItemStack> calculatedDrops = new ArrayList<>(cropBlock.getDrops(tool.clone(), golem));
317 List<ItemStack> calculatedDrops = new ArrayList<>(cropBlock.getDrops(tool.clone(), golem));
318
318
319 cropBlock.setType(Material.AIR, false);
319 cropBlock.setType(Material.AIR, true);
320 List<ItemStack> overflow = cargo.addAll(calculatedDrops);
320 List<ItemStack> overflow = cargo.addAll(calculatedDrops);
321 for (ItemStack item : overflow) {
321 for (ItemStack item : overflow) {
322 cropBlock.getWorld().dropItemNaturally(cropBlock.getLocation().add(0.5, 0.5, 0.5), item);
322 cropBlock.getWorld().dropItemNaturally(cropBlock.getLocation().add(0.5, 0.5, 0.5), item);
src/main/java/nl/timfalken/copperfarmer/SeedRecipeCache.java +79 −25
@@ -11,10 +11,12 @@ import org.bukkit.plugin.Plugin;
11 11
12 12 import java.util.ArrayList;
13 13 import java.util.EnumMap;
14 import java.util.EnumSet;
14 15 import java.util.Iterator;
15 16 import java.util.List;
16 17 import java.util.Map;
17 18 import java.util.Set;
19 import java.util.function.Predicate;
18 20
19 21 final class SeedRecipeCache {
20 22 record Conversion(Material input, int inputCount, ItemStack output) {}
@@ -37,33 +39,20 @@ final class SeedRecipeCache {
37 39
38 40 Iterator<Recipe> iterator = Bukkit.recipeIterator();
39 41 while (iterator.hasNext()) {
40 Recipe recipe = iterator.next();
42 Recipe recipe;
43 try {
44 recipe = iterator.next();
45 } catch (RuntimeException ex) {
46 inspected++;
47 plugin.getLogger().warning("Skipping unreadable recipe: " + ex.getMessage());
48 continue;
49 }
41 50 inspected++;
42 51
43 ItemStack result = recipe.getResult();
44 if (result.isEmpty() || !acceptedSeedOutputs.contains(result.getType())) continue;
45
46 List<RecipeChoice> ingredients = ingredientChoices(recipe);
47 if (ingredients == null || ingredients.isEmpty()) continue;
48
49 // We deliberately only infer conversions where every ingredient can be
50 // satisfied by the same material. That safely models "crop -> seed"
51 // without trying to become a general-purpose autocrafter.
52 for (Material material : Material.values()) {
53 if (!material.isItem() || material.isAir()) continue;
54 ItemStack test = new ItemStack(material);
55 boolean allMatch = true;
56 for (RecipeChoice choice : ingredients) {
57 if (choice == null || !choice.test(test)) {
58 allMatch = false;
59 break;
60 }
61 }
62 if (!allMatch) continue;
63
64 Conversion conversion = new Conversion(material, ingredients.size(), result.clone());
65 byInput.computeIfAbsent(material, ignored -> new ArrayList<>()).add(conversion);
66 cached++;
52 try {
53 cacheIfConversion(recipe);
54 } catch (RuntimeException | LinkageError ex) {
55 plugin.getLogger().warning("Skipping recipe while building seed cache: " + ex.getMessage());
67 56 }
68 57 }
69 58
@@ -71,6 +60,71 @@ final class SeedRecipeCache {
71 60 + " recipes, cached " + cached + " crop-to-seed material mappings.");
72 61 }
73 62
63 private void cacheIfConversion(Recipe recipe) {
64 ItemStack result = recipe.getResult();
65 if (result.isEmpty() || !acceptedSeedOutputs.contains(result.getType())) return;
66
67 List<RecipeChoice> ingredients = ingredientChoices(recipe);
68 if (ingredients == null || ingredients.isEmpty()) return;
69
70 // Only infer conversions where every ingredient can be satisfied by the
71 // same material. That models "crop -> seed" without becoming a general
72 // autocrafter. Match via concrete choice types: RecipeChoice is an
73 // interface on Paper 26.2, so RecipeChoice.test() can throw
74 // IncompatibleClassChangeError if invoked as a class method.
75 Set<Material> common = null;
76 for (RecipeChoice choice : ingredients) {
77 Set<Material> matching = materialsMatching(choice);
78 if (matching.isEmpty()) return;
79 if (common == null) {
80 common = EnumSet.copyOf(matching);
81 } else {
82 common.retainAll(matching);
83 }
84 if (common.isEmpty()) return;
85 }
86 if (common == null) return;
87
88 for (Material material : common) {
89 Conversion conversion = new Conversion(material, ingredients.size(), result.clone());
90 byInput.computeIfAbsent(material, ignored -> new ArrayList<>()).add(conversion);
91 cached++;
92 }
93 }
94
95 private Set<Material> materialsMatching(RecipeChoice choice) {
96 if (choice == null) return Set.of();
97
98 // ItemTypeChoice implementations also extend MaterialChoice and override getChoices().
99 if (choice instanceof RecipeChoice.MaterialChoice materialChoice) {
100 Set<Material> materials = EnumSet.noneOf(Material.class);
101 for (Material material : materialChoice.getChoices()) {
102 if (material != null && material.isItem() && !material.isAir()) {
103 materials.add(material);
104 }
105 }
106 return materials;
107 }
108
109 if (choice instanceof RecipeChoice.ExactChoice exactChoice) {
110 Set<Material> materials = EnumSet.noneOf(Material.class);
111 for (ItemStack stack : exactChoice.getChoices()) {
112 if (stack == null || stack.isEmpty()) continue;
113 Material material = stack.getType();
114 if (material.isItem() && !material.isAir()) materials.add(material);
115 }
116 return materials;
117 }
118
119 Predicate<ItemStack> predicate = choice;
120 Set<Material> materials = EnumSet.noneOf(Material.class);
121 for (Material material : Material.values()) {
122 if (!material.isItem() || material.isAir()) continue;
123 if (predicate.test(new ItemStack(material))) materials.add(material);
124 }
125 return materials;
126 }
127
74 128 private List<RecipeChoice> ingredientChoices(Recipe recipe) {
75 129 if (recipe instanceof ShapelessRecipe shapeless) {
76 130 return new ArrayList<>(shapeless.getChoiceList());
@@ -11,10 +11,12 @@ import org.bukkit.plugin.Plugin;
11
11
12import java.util.ArrayList;
12import java.util.ArrayList;
13import java.util.EnumMap;
13import java.util.EnumMap;
14import java.util.EnumSet;
14import java.util.Iterator;
15import java.util.Iterator;
15import java.util.List;
16import java.util.List;
16import java.util.Map;
17import java.util.Map;
17import java.util.Set;
18import java.util.Set;
19import java.util.function.Predicate;
18
20
19final class SeedRecipeCache {
21final class SeedRecipeCache {
20 record Conversion(Material input, int inputCount, ItemStack output) {}
22 record Conversion(Material input, int inputCount, ItemStack output) {}
@@ -37,33 +39,20 @@ final class SeedRecipeCache {
37
39
38 Iterator<Recipe> iterator = Bukkit.recipeIterator();
40 Iterator<Recipe> iterator = Bukkit.recipeIterator();
39 while (iterator.hasNext()) {
41 while (iterator.hasNext()) {
40 Recipe recipe = iterator.next();
42 Recipe recipe;
43 try {
44 recipe = iterator.next();
45 } catch (RuntimeException ex) {
46 inspected++;
47 plugin.getLogger().warning("Skipping unreadable recipe: " + ex.getMessage());
48 continue;
49 }
41 inspected++;
50 inspected++;
42
51
43 ItemStack result = recipe.getResult();
52 try {
44 if (result.isEmpty() || !acceptedSeedOutputs.contains(result.getType())) continue;
53 cacheIfConversion(recipe);
45
54 } catch (RuntimeException | LinkageError ex) {
46 List<RecipeChoice> ingredients = ingredientChoices(recipe);
55 plugin.getLogger().warning("Skipping recipe while building seed cache: " + ex.getMessage());
47 if (ingredients == null || ingredients.isEmpty()) continue;
48
49 // We deliberately only infer conversions where every ingredient can be
50 // satisfied by the same material. That safely models "crop -> seed"
51 // without trying to become a general-purpose autocrafter.
52 for (Material material : Material.values()) {
53 if (!material.isItem() || material.isAir()) continue;
54 ItemStack test = new ItemStack(material);
55 boolean allMatch = true;
56 for (RecipeChoice choice : ingredients) {
57 if (choice == null || !choice.test(test)) {
58 allMatch = false;
59 break;
60 }
61 }
62 if (!allMatch) continue;
63
64 Conversion conversion = new Conversion(material, ingredients.size(), result.clone());
65 byInput.computeIfAbsent(material, ignored -> new ArrayList<>()).add(conversion);
66 cached++;
67 }
56 }
68 }
57 }
69
58
@@ -71,6 +60,71 @@ final class SeedRecipeCache {
71 + " recipes, cached " + cached + " crop-to-seed material mappings.");
60 + " recipes, cached " + cached + " crop-to-seed material mappings.");
72 }
61 }
73
62
63 private void cacheIfConversion(Recipe recipe) {
64 ItemStack result = recipe.getResult();
65 if (result.isEmpty() || !acceptedSeedOutputs.contains(result.getType())) return;
66
67 List<RecipeChoice> ingredients = ingredientChoices(recipe);
68 if (ingredients == null || ingredients.isEmpty()) return;
69
70 // Only infer conversions where every ingredient can be satisfied by the
71 // same material. That models "crop -> seed" without becoming a general
72 // autocrafter. Match via concrete choice types: RecipeChoice is an
73 // interface on Paper 26.2, so RecipeChoice.test() can throw
74 // IncompatibleClassChangeError if invoked as a class method.
75 Set<Material> common = null;
76 for (RecipeChoice choice : ingredients) {
77 Set<Material> matching = materialsMatching(choice);
78 if (matching.isEmpty()) return;
79 if (common == null) {
80 common = EnumSet.copyOf(matching);
81 } else {
82 common.retainAll(matching);
83 }
84 if (common.isEmpty()) return;
85 }
86 if (common == null) return;
87
88 for (Material material : common) {
89 Conversion conversion = new Conversion(material, ingredients.size(), result.clone());
90 byInput.computeIfAbsent(material, ignored -> new ArrayList<>()).add(conversion);
91 cached++;
92 }
93 }
94
95 private Set<Material> materialsMatching(RecipeChoice choice) {
96 if (choice == null) return Set.of();
97
98 // ItemTypeChoice implementations also extend MaterialChoice and override getChoices().
99 if (choice instanceof RecipeChoice.MaterialChoice materialChoice) {
100 Set<Material> materials = EnumSet.noneOf(Material.class);
101 for (Material material : materialChoice.getChoices()) {
102 if (material != null && material.isItem() && !material.isAir()) {
103 materials.add(material);
104 }
105 }
106 return materials;
107 }
108
109 if (choice instanceof RecipeChoice.ExactChoice exactChoice) {
110 Set<Material> materials = EnumSet.noneOf(Material.class);
111 for (ItemStack stack : exactChoice.getChoices()) {
112 if (stack == null || stack.isEmpty()) continue;
113 Material material = stack.getType();
114 if (material.isItem() && !material.isAir()) materials.add(material);
115 }
116 return materials;
117 }
118
119 Predicate<ItemStack> predicate = choice;
120 Set<Material> materials = EnumSet.noneOf(Material.class);
121 for (Material material : Material.values()) {
122 if (!material.isItem() || material.isAir()) continue;
123 if (predicate.test(new ItemStack(material))) materials.add(material);
124 }
125 return materials;
126 }
127
74 private List<RecipeChoice> ingredientChoices(Recipe recipe) {
128 private List<RecipeChoice> ingredientChoices(Recipe recipe) {
75 if (recipe instanceof ShapelessRecipe shapeless) {
129 if (recipe instanceof ShapelessRecipe shapeless) {
76 return new ArrayList<>(shapeless.getChoiceList());
130 return new ArrayList<>(shapeless.getChoiceList());