aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/0296-Improve-exact-choice-recipe-ingredients.patch
blob: 47a51d830f843995165376f6c96624ea2a723435 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Sun, 25 Jun 2023 23:10:14 -0700
Subject: [PATCH] Improve exact choice recipe ingredients

Fixes exact choices not working with recipe book clicks
and shapeless recipes.

== AT ==
public net.minecraft.world.item.ItemStackLinkedSet TYPE_AND_TAG
public net.minecraft.world.entity.player.StackedContents put(II)V

diff --git a/src/main/java/io/papermc/paper/inventory/recipe/RecipeBookExactChoiceRecipe.java b/src/main/java/io/papermc/paper/inventory/recipe/RecipeBookExactChoiceRecipe.java
new file mode 100644
index 0000000000000000000000000000000000000000..2a2f8327a5bd3983a3a13fd663beb98906f27312
--- /dev/null
+++ b/src/main/java/io/papermc/paper/inventory/recipe/RecipeBookExactChoiceRecipe.java
@@ -0,0 +1,30 @@
+package io.papermc.paper.inventory.recipe;
+
+import net.minecraft.world.Container;
+import net.minecraft.world.item.crafting.Ingredient;
+import net.minecraft.world.item.crafting.Recipe;
+
+public abstract class RecipeBookExactChoiceRecipe<C extends Container> implements Recipe<C> {
+
+    private boolean hasExactIngredients;
+
+    protected final void checkExactIngredients() {
+        // skip any special recipes
+        if (this.isSpecial()) {
+            this.hasExactIngredients = false;
+            return;
+        }
+        for (final Ingredient ingredient : this.getIngredients()) {
+            if (!ingredient.isEmpty() && ingredient.exact) {
+                this.hasExactIngredients = true;
+                return;
+            }
+        }
+        this.hasExactIngredients = false;
+    }
+
+    @Override
+    public final boolean hasExactIngredients() {
+        return this.hasExactIngredients;
+    }
+}
diff --git a/src/main/java/io/papermc/paper/inventory/recipe/StackedContentsExtraMap.java b/src/main/java/io/papermc/paper/inventory/recipe/StackedContentsExtraMap.java
new file mode 100644
index 0000000000000000000000000000000000000000..63db0b843c5bd11f979e613ba6cfac9d9da956bb
--- /dev/null
+++ b/src/main/java/io/papermc/paper/inventory/recipe/StackedContentsExtraMap.java
@@ -0,0 +1,79 @@
+package io.papermc.paper.inventory.recipe;
+
+import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
+import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
+import it.unimi.dsi.fastutil.ints.IntArrayList;
+import it.unimi.dsi.fastutil.ints.IntComparators;
+import it.unimi.dsi.fastutil.ints.IntList;
+import it.unimi.dsi.fastutil.objects.Object2IntMap;
+import it.unimi.dsi.fastutil.objects.Object2IntOpenCustomHashMap;
+import java.util.IdentityHashMap;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+import net.minecraft.core.registries.BuiltInRegistries;
+import net.minecraft.world.entity.player.StackedContents;
+import net.minecraft.world.item.ItemStack;
+import net.minecraft.world.item.ItemStackLinkedSet;
+import net.minecraft.world.item.crafting.Ingredient;
+import net.minecraft.world.item.crafting.Recipe;
+
+public final class StackedContentsExtraMap {
+
+    private final AtomicInteger idCounter = new AtomicInteger(BuiltInRegistries.ITEM.size()); // start at max vanilla stacked contents idx
+    private final Object2IntMap<ItemStack> exactChoiceIds = new Object2IntOpenCustomHashMap<>(ItemStackLinkedSet.TYPE_AND_TAG);
+    private final Int2ObjectMap<ItemStack> idToExactChoice = new Int2ObjectOpenHashMap<>();
+    private final StackedContents contents;
+    public final Map<Ingredient, IntList> extraStackingIds = new IdentityHashMap<>();
+
+    public StackedContentsExtraMap(final StackedContents contents, final Recipe<?> recipe) {
+        this.exactChoiceIds.defaultReturnValue(-1);
+        this.contents = contents;
+        this.initialize(recipe);
+    }
+
+    private void initialize(final Recipe<?> recipe) {
+        if (recipe.hasExactIngredients()) {
+            for (final Ingredient ingredient : recipe.getIngredients()) {
+                if (!ingredient.isEmpty() && ingredient.exact) {
+                    final net.minecraft.world.item.ItemStack[] items = ingredient.getItems();
+                    final IntList idList = new IntArrayList(items.length);
+                    for (final ItemStack item : items) {
+                        idList.add(this.registerExact(item)); // I think not copying the stack here is safe because cb copies the stack when creating the ingredient
+                        if (!item.hasTag()) {
+                            // add regular index if it's a plain itemstack but still registered as exact
+                            idList.add(StackedContents.getStackingIndex(item));
+                        }
+                    }
+                    idList.sort(IntComparators.NATURAL_COMPARATOR);
+                    this.extraStackingIds.put(ingredient, idList);
+                }
+            }
+        }
+    }
+
+    private int registerExact(final ItemStack exactChoice) {
+        final int existing = this.exactChoiceIds.getInt(exactChoice);
+        if (existing > -1) {
+            return existing;
+        }
+        final int id = this.idCounter.getAndIncrement();
+        this.exactChoiceIds.put(exactChoice, id);
+        this.idToExactChoice.put(id, exactChoice);
+        return id;
+    }
+
+    public ItemStack getById(int id) {
+        return this.idToExactChoice.get(id);
+    }
+
+    public boolean accountStack(final ItemStack stack, final int count) {
+        if (!this.exactChoiceIds.isEmpty()) {
+            final int id = this.exactChoiceIds.getInt(stack);
+            if (id >= 0) {
+                this.contents.put(id, count);
+                return true;
+            }
+        }
+        return false;
+    }
+}
diff --git a/src/main/java/io/papermc/paper/inventory/recipe/package-info.java b/src/main/java/io/papermc/paper/inventory/recipe/package-info.java
new file mode 100644
index 0000000000000000000000000000000000000000..413dfa52760db393ad6a8b5341200ee704a864fc
--- /dev/null
+++ b/src/main/java/io/papermc/paper/inventory/recipe/package-info.java
@@ -0,0 +1,5 @@
+@DefaultQualifier(NonNull.class)
+package io.papermc.paper.inventory.recipe;
+
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.framework.qual.DefaultQualifier;
diff --git a/src/main/java/net/minecraft/recipebook/ServerPlaceRecipe.java b/src/main/java/net/minecraft/recipebook/ServerPlaceRecipe.java
index 4303f2b4e55191f8a53170435c6d1263782d1c8d..a65d07cb3d2dae85225921498d1721dc3c6a6bf3 100644
--- a/src/main/java/net/minecraft/recipebook/ServerPlaceRecipe.java
+++ b/src/main/java/net/minecraft/recipebook/ServerPlaceRecipe.java
@@ -34,6 +34,7 @@ public class ServerPlaceRecipe<C extends Container> implements PlaceRecipe<Integ
             this.inventory = entity.getInventory();
             if (this.testClearGrid() || entity.isCreative()) {
                 this.stackedContents.clear();
+                this.stackedContents.initialize(recipe.value()); // Paper - better exact choice recipes
                 entity.getInventory().fillStackedContents(this.stackedContents);
                 this.menu.fillCraftSlotsStackedContents(this.stackedContents);
                 if (this.stackedContents.canCraft(recipe.value(), (IntList)null)) {
@@ -80,7 +81,7 @@ public class ServerPlaceRecipe<C extends Container> implements PlaceRecipe<Integ
             int l = k;
 
             for(int m : intList) {
-                int n = StackedContents.fromStackingIndex(m).getMaxStackSize();
+                int n = StackedContents.maxStackSizeFromStackingIndex(m, this.stackedContents); // Paper
                 if (n < l) {
                     l = n;
                 }
@@ -97,10 +98,21 @@ public class ServerPlaceRecipe<C extends Container> implements PlaceRecipe<Integ
     @Override
     public void addItemToSlot(Iterator<Integer> inputs, int slot, int amount, int gridX, int gridY) {
         Slot slot2 = this.menu.getSlot(slot);
-        ItemStack itemStack = StackedContents.fromStackingIndex(inputs.next());
+        // Paper start
+        final int itemId = inputs.next();
+        ItemStack itemStack = null;
+        boolean isExact = false;
+        if (this.stackedContents.extrasMap != null && itemId >= net.minecraft.core.registries.BuiltInRegistries.ITEM.size()) {
+            itemStack = StackedContents.fromStackingIndexExtras(itemId, this.stackedContents.extrasMap).copy();
+            isExact = true;
+        }
+        if (itemStack == null) {
+            itemStack = StackedContents.fromStackingIndex(itemId);
+        }
+        // Paper end
         if (!itemStack.isEmpty()) {
             for(int i = 0; i < amount; ++i) {
-                this.moveItemToGrid(slot2, itemStack);
+                this.moveItemToGrid(slot2, itemStack, isExact); // Paper
             }
         }
 
@@ -130,8 +142,14 @@ public class ServerPlaceRecipe<C extends Container> implements PlaceRecipe<Integ
         return i;
     }
 
+    @Deprecated @io.papermc.paper.annotation.DoNotUse // Paper
     protected void moveItemToGrid(Slot slot, ItemStack stack) {
-        int i = this.inventory.findSlotMatchingUnusedItem(stack);
+        // Paper start
+        this.moveItemToGrid(slot, stack, false);
+    }
+    protected void moveItemToGrid(Slot slot, ItemStack stack, final boolean isExact) {
+        int i = isExact ? this.inventory.findSlotMatchingItem(stack) : this.inventory.findSlotMatchingUnusedItem(stack);
+        // Paper end
         if (i != -1) {
             ItemStack itemStack = this.inventory.getItem(i);
             if (!itemStack.isEmpty()) {
diff --git a/src/main/java/net/minecraft/world/entity/player/StackedContents.java b/src/main/java/net/minecraft/world/entity/player/StackedContents.java
index 5cc5f67284ad01c9154876252450552ed37f24bf..26b236a764177ac16d53f5cbaf83d3e21d015ebc 100644
--- a/src/main/java/net/minecraft/world/entity/player/StackedContents.java
+++ b/src/main/java/net/minecraft/world/entity/player/StackedContents.java
@@ -21,8 +21,10 @@ import net.minecraft.world.item.crafting.RecipeHolder;
 public class StackedContents {
     private static final int EMPTY = 0;
     public final Int2IntMap contents = new Int2IntOpenHashMap();
+    @Nullable public io.papermc.paper.inventory.recipe.StackedContentsExtraMap extrasMap = null; // Paper
 
     public void accountSimpleStack(ItemStack stack) {
+        if (this.extrasMap != null && stack.hasTag() && this.extrasMap.accountStack(stack, Math.min(64, stack.getCount()))) return; // Paper - max of 64 due to accountStack method below
         if (!stack.isDamaged() && !stack.isEnchanted() && !stack.hasCustomHoverName()) {
             this.accountStack(stack);
         }
@@ -37,6 +39,7 @@ public class StackedContents {
         if (!stack.isEmpty()) {
             int i = getStackingIndex(stack);
             int j = Math.min(maxCount, stack.getCount());
+            if (this.extrasMap != null && stack.hasTag() && this.extrasMap.accountStack(stack, j)) return; // Paper - if an exact ingredient, don't include it
             this.put(i, j);
         }
 
@@ -84,6 +87,23 @@ public class StackedContents {
         return itemId == 0 ? ItemStack.EMPTY : new ItemStack(Item.byId(itemId));
     }
 
+    // Paper start
+    public void initialize(final Recipe<?> recipe) {
+        this.extrasMap = new io.papermc.paper.inventory.recipe.StackedContentsExtraMap(this, recipe);
+    }
+
+    public static int maxStackSizeFromStackingIndex(final int itemId, @Nullable final StackedContents contents) {
+        if (contents != null && contents.extrasMap != null && itemId >= BuiltInRegistries.ITEM.size()) {
+            return fromStackingIndexExtras(itemId, contents.extrasMap).getMaxStackSize();
+        }
+        return fromStackingIndex(itemId).getMaxStackSize();
+    }
+
+    public static ItemStack fromStackingIndexExtras(final int itemId, final io.papermc.paper.inventory.recipe.StackedContentsExtraMap extrasMap) {
+        return extrasMap.getById(itemId).copy();
+    }
+    // Paper end
+
     public void clear() {
         this.contents.clear();
     }
@@ -107,7 +127,7 @@ public class StackedContents {
             this.data = new BitSet(this.ingredientCount + this.itemCount + this.ingredientCount + this.ingredientCount * this.itemCount);
 
             for(int i = 0; i < this.ingredients.size(); ++i) {
-                IntList intList = this.ingredients.get(i).getStackingIds();
+                IntList intList = this.getStackingIds(this.ingredients.get(i)); // Paper
 
                 for(int j = 0; j < this.itemCount; ++j) {
                     if (intList.contains(this.items[j])) {
@@ -171,7 +191,7 @@ public class StackedContents {
             IntCollection intCollection = new IntAVLTreeSet();
 
             for(Ingredient ingredient : this.ingredients) {
-                intCollection.addAll(ingredient.getStackingIds());
+                intCollection.addAll(this.getStackingIds(ingredient)); // Paper
             }
 
             IntIterator intIterator = intCollection.iterator();
@@ -294,7 +314,7 @@ public class StackedContents {
             for(Ingredient ingredient : this.ingredients) {
                 int j = 0;
 
-                for(int k : ingredient.getStackingIds()) {
+                for(int k : this.getStackingIds(ingredient)) { // Paper
                     j = Math.max(j, StackedContents.this.contents.get(k));
                 }
 
@@ -305,5 +325,17 @@ public class StackedContents {
 
             return i;
         }
+
+        // Paper start - improve exact recipe choices
+        private IntList getStackingIds(final Ingredient ingredient) {
+            if (StackedContents.this.extrasMap != null) {
+                final IntList ids = StackedContents.this.extrasMap.extraStackingIds.get(ingredient);
+                if (ids != null) {
+                    return ids;
+                }
+            }
+            return ingredient.getStackingIds();
+        }
+        // Paper end - improve exact recipe choices
     }
 }
diff --git a/src/main/java/net/minecraft/world/item/crafting/AbstractCookingRecipe.java b/src/main/java/net/minecraft/world/item/crafting/AbstractCookingRecipe.java
index c864809c7d656cf8e3f1080c2c57aefd0a417240..760a429137350d8ae100e77273318f3ba1ccfcc5 100644
--- a/src/main/java/net/minecraft/world/item/crafting/AbstractCookingRecipe.java
+++ b/src/main/java/net/minecraft/world/item/crafting/AbstractCookingRecipe.java
@@ -6,7 +6,7 @@ import net.minecraft.world.Container;
 import net.minecraft.world.item.ItemStack;
 import net.minecraft.world.level.Level;
 
-public abstract class AbstractCookingRecipe implements Recipe<Container> {
+public abstract class AbstractCookingRecipe extends io.papermc.paper.inventory.recipe.RecipeBookExactChoiceRecipe<Container> implements Recipe<Container> { // Paper - improve exact recipe choices
     protected final RecipeType<?> type;
     protected final CookingBookCategory category;
     protected final String group;
@@ -23,6 +23,7 @@ public abstract class AbstractCookingRecipe implements Recipe<Container> {
         this.result = result;
         this.experience = experience;
         this.cookingTime = cookingTime;
+        this.checkExactIngredients(); // Paper - improve exact recipe choices
     }
 
     @Override
diff --git a/src/main/java/net/minecraft/world/item/crafting/Recipe.java b/src/main/java/net/minecraft/world/item/crafting/Recipe.java
index 75ae978b577b6b2399e1476328114e94abd7d6c4..80387cd1bee2bd4c024073cee74222828f9f2c17 100644
--- a/src/main/java/net/minecraft/world/item/crafting/Recipe.java
+++ b/src/main/java/net/minecraft/world/item/crafting/Recipe.java
@@ -65,4 +65,10 @@ public interface Recipe<C extends Container> {
     }
 
     org.bukkit.inventory.Recipe toBukkitRecipe(org.bukkit.NamespacedKey id); // CraftBukkit
+
+    // Paper start - improved exact choice recipes
+    default boolean hasExactIngredients() {
+        return false;
+    }
+    // Paper end
 }
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 4430c1161c3316566158ab7286e21495edb38991..17bef91546fa85d401b263c3a69fbf464f290eca 100644
--- a/src/main/java/net/minecraft/world/item/crafting/ShapedRecipe.java
+++ b/src/main/java/net/minecraft/world/item/crafting/ShapedRecipe.java
@@ -26,7 +26,7 @@ import org.bukkit.craftbukkit.inventory.CraftShapedRecipe;
 import org.bukkit.inventory.RecipeChoice;
 // CraftBukkit end
 
-public class ShapedRecipe implements CraftingRecipe {
+public class ShapedRecipe extends io.papermc.paper.inventory.recipe.RecipeBookExactChoiceRecipe<CraftingContainer> implements CraftingRecipe { // Paper - improve exact recipe choices
 
     final int width;
     final int height;
@@ -44,6 +44,7 @@ public class ShapedRecipe implements CraftingRecipe {
         this.recipeItems = ingredients;
         this.result = result;
         this.showNotification = showNotification;
+        this.checkExactIngredients(); // Paper - improve exact recipe choices
     }
 
     public ShapedRecipe(String group, CraftingBookCategory category, int width, int height, NonNullList<Ingredient> ingredients, ItemStack result) {
diff --git a/src/main/java/net/minecraft/world/item/crafting/ShapelessRecipe.java b/src/main/java/net/minecraft/world/item/crafting/ShapelessRecipe.java
index 7f78490ebf459595460ce656b9b9227e5d6b8de6..38f7d1ece27ec1a3deda21fb6a6f0e788c8ed718 100644
--- a/src/main/java/net/minecraft/world/item/crafting/ShapelessRecipe.java
+++ b/src/main/java/net/minecraft/world/item/crafting/ShapelessRecipe.java
@@ -20,7 +20,7 @@ import org.bukkit.craftbukkit.inventory.CraftRecipe;
 import org.bukkit.craftbukkit.inventory.CraftShapelessRecipe;
 // CraftBukkit end
 
-public class ShapelessRecipe implements CraftingRecipe {
+public class ShapelessRecipe extends io.papermc.paper.inventory.recipe.RecipeBookExactChoiceRecipe<CraftingContainer> implements CraftingRecipe { // Paper - improve exact recipe choices
 
     final String group;
     final CraftingBookCategory category;
@@ -32,6 +32,7 @@ public class ShapelessRecipe implements CraftingRecipe {
         this.category = category;
         this.result = result;
         this.ingredients = ingredients;
+        this.checkExactIngredients(); // Paper - improve exact recipe choices
     }
 
     // CraftBukkit start
@@ -77,6 +78,7 @@ public class ShapelessRecipe implements CraftingRecipe {
 
     public boolean matches(CraftingContainer inventory, Level world) {
         StackedContents autorecipestackmanager = new StackedContents();
+        autorecipestackmanager.initialize(this); // Paper - better exact choice recipes
         int i = 0;
 
         for (int j = 0; j < inventory.getContainerSize(); ++j) {