aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/1075-Improve-exact-choice-recipe-ingredients.patch
blob: 88c45348a20b440accb356968a818ca88f7fd9cc (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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
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

diff --git a/src/main/java/io/papermc/paper/inventory/recipe/ItemOrExact.java b/src/main/java/io/papermc/paper/inventory/recipe/ItemOrExact.java
new file mode 100644
index 0000000000000000000000000000000000000000..ce745e49cd54fe3ae187785563a1bd311d14b5b2
--- /dev/null
+++ b/src/main/java/io/papermc/paper/inventory/recipe/ItemOrExact.java
@@ -0,0 +1,63 @@
+package io.papermc.paper.inventory.recipe;
+
+import net.minecraft.core.Holder;
+import net.minecraft.world.item.ItemStack;
+
+public sealed interface ItemOrExact permits ItemOrExact.Item, ItemOrExact.Exact {
+
+    int getMaxStackSize();
+
+    boolean matches(ItemStack stack);
+
+    record Item(Holder<net.minecraft.world.item.Item> item) implements ItemOrExact {
+
+        public Item(final ItemStack stack) {
+            this(stack.getItemHolder());
+        }
+
+        @Override
+        public int getMaxStackSize() {
+            return this.item.value().getDefaultMaxStackSize();
+        }
+
+        @Override
+        public boolean matches(final ItemStack stack) {
+            return stack.is(this.item);
+        }
+
+        @Override
+        public boolean equals(final Object obj) {
+            if (!(obj instanceof final Item otherItem)) return false;
+            return this.item.equals(otherItem.item());
+        }
+
+        @Override
+        public int hashCode() {
+            return this.item.hashCode();
+        }
+    }
+
+    record Exact(ItemStack stack) implements ItemOrExact {
+
+        @Override
+        public int getMaxStackSize() {
+            return this.stack.getMaxStackSize();
+        }
+
+        @Override
+        public boolean matches(final ItemStack stack) {
+            return ItemStack.isSameItemSameComponents(this.stack, stack);
+        }
+
+        @Override
+        public boolean equals(final Object obj) {
+            if (!(obj instanceof final Exact otherExact)) return false;
+            return ItemStack.isSameItemSameComponents(this.stack, otherExact.stack);
+        }
+
+        @Override
+        public int hashCode() {
+            return ItemStack.hashItemAndComponents(this.stack);
+        }
+    }
+}
diff --git a/src/main/java/io/papermc/paper/inventory/recipe/StackedContentsExtrasMap.java b/src/main/java/io/papermc/paper/inventory/recipe/StackedContentsExtrasMap.java
new file mode 100644
index 0000000000000000000000000000000000000000..f47c12e9dd6cfa857ca07a764edc22de372e25b6
--- /dev/null
+++ b/src/main/java/io/papermc/paper/inventory/recipe/StackedContentsExtrasMap.java
@@ -0,0 +1,68 @@
+package io.papermc.paper.inventory.recipe;
+
+import it.unimi.dsi.fastutil.objects.Object2IntMap;
+import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
+import it.unimi.dsi.fastutil.objects.ObjectOpenCustomHashSet;
+import it.unimi.dsi.fastutil.objects.ObjectSet;
+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.CraftingInput;
+import net.minecraft.world.item.crafting.Ingredient;
+import net.minecraft.world.item.crafting.Recipe;
+
+public final class StackedContentsExtrasMap {
+
+    private final StackedContents<ItemOrExact> contents;
+    public Object2IntMap<ItemOrExact.Item> regularRemoved = new Object2IntOpenHashMap<>(); // needed for re-using the regular contents (for ShapelessRecipe)
+    public final ObjectSet<ItemStack> exactIngredients = new ObjectOpenCustomHashSet<>(ItemStackLinkedSet.TYPE_AND_TAG);
+
+    public StackedContentsExtrasMap(final StackedContents<ItemOrExact> contents) {
+        this.contents = contents;
+    }
+
+    public void initialize(final Recipe<?> recipe) {
+        this.exactIngredients.clear();
+        for (final Ingredient ingredient : recipe.placementInfo().ingredients()) {
+            if (ingredient.isExact()) {
+                this.exactIngredients.addAll(ingredient.itemStacks());
+            }
+        }
+    }
+
+    public void accountInput(final CraftingInput input) {
+        // similar logic to the CraftingInput constructor
+        for (final ItemStack item : input.items()) {
+            if (!item.isEmpty()) {
+                if (this.accountStack(item, 1)) {
+                    // if stack was accounted for as an exact ingredient, don't include it in the regular contents
+                    final ItemOrExact.Item asItem = new ItemOrExact.Item(item);
+                    if (this.contents.amounts.containsKey(asItem)) {
+                        final int amount = this.contents.amounts.removeInt(asItem);
+                        this.regularRemoved.put(asItem, amount);
+                    }
+                }
+            }
+        }
+    }
+
+    public void resetExtras() {
+        // clear previous extra ids
+        for (final ItemStack extra : this.exactIngredients) {
+            this.contents.amounts.removeInt(new ItemOrExact.Exact(extra));
+        }
+        for (final Object2IntMap.Entry<ItemOrExact.Item> entry : this.regularRemoved.object2IntEntrySet()) {
+            this.contents.amounts.addTo(entry.getKey(), entry.getIntValue());
+        }
+        this.exactIngredients.clear();
+        this.regularRemoved.clear();
+    }
+
+    public boolean accountStack(final ItemStack stack, final int count) {
+        if (this.exactIngredients.contains(stack)) {
+            this.contents.account(new ItemOrExact.Exact(stack), count);
+            return true;
+        }
+        return false;
+    }
+}
diff --git a/src/main/java/net/minecraft/recipebook/ServerPlaceRecipe.java b/src/main/java/net/minecraft/recipebook/ServerPlaceRecipe.java
index 6d3ccd19baab8f0571b71bd05979efb6b372a6f5..dc9b01db0853964da146ad4ccdc9e0e6f2a279f4 100644
--- a/src/main/java/net/minecraft/recipebook/ServerPlaceRecipe.java
+++ b/src/main/java/net/minecraft/recipebook/ServerPlaceRecipe.java
@@ -40,6 +40,7 @@ public class ServerPlaceRecipe<R extends Recipe<?>> {
             return RecipeBookMenu.PostPlaceAction.NOTHING;
         } else {
             StackedItemContents stackedItemContents = new StackedItemContents();
+            stackedItemContents.initializeExtras(recipe.value(), null); // Paper - Improve exact choice recipe ingredients
             inventory.fillStackedContents(stackedItemContents);
             handler.fillCraftSlotsStackedContents(stackedItemContents);
             return serverPlaceRecipe.tryPlaceRecipe(recipe, stackedItemContents);
@@ -99,7 +100,7 @@ public class ServerPlaceRecipe<R extends Recipe<?>> {
         }
 
         int j = this.calculateAmountToCraft(i, bl);
-        List<Holder<Item>> list = new ArrayList<>();
+        List<io.papermc.paper.inventory.recipe.ItemOrExact> list = new ArrayList<>(); // Paper - Improve exact choice recipe ingredients
         if (finder.canCraft(recipe.value(), j, list::add)) {
             int k = clampToMaxStackSize(j, list);
             if (k != j) {
@@ -114,7 +115,7 @@ public class ServerPlaceRecipe<R extends Recipe<?>> {
                 this.gridWidth, this.gridHeight, recipe.value(), recipe.value().placementInfo().slotsToIngredientIndex(), (slotx, index, x, y) -> {
                     if (slotx != -1) {
                         Slot slot2 = this.inputGridSlots.get(index);
-                        Holder<Item> holder = list.get(slotx);
+                        io.papermc.paper.inventory.recipe.ItemOrExact holder = list.get(slotx); // Paper - Improve exact choice recipe ingredients
                         int jx = k;
 
                         while (jx > 0) {
@@ -129,9 +130,11 @@ public class ServerPlaceRecipe<R extends Recipe<?>> {
         }
     }
 
-    private static int clampToMaxStackSize(int count, List<Holder<Item>> entries) {
-        for (Holder<Item> holder : entries) {
-            count = Math.min(count, holder.value().getDefaultMaxStackSize());
+    // Paper start - Improve exact choice recipe ingredients
+    private static int clampToMaxStackSize(int count, List<io.papermc.paper.inventory.recipe.ItemOrExact> entries) {
+        for (io.papermc.paper.inventory.recipe.ItemOrExact holder : entries) {
+            count = Math.min(count, holder.getMaxStackSize());
+            // Paper end - Improve exact choice recipe ingredients
         }
 
         return count;
@@ -160,7 +163,7 @@ public class ServerPlaceRecipe<R extends Recipe<?>> {
         }
     }
 
-    private int moveItemToGrid(Slot slot, Holder<Item> item, int count) {
+    private int moveItemToGrid(Slot slot, io.papermc.paper.inventory.recipe.ItemOrExact item, int count) { // Paper - Improve exact choice recipe ingredients
         ItemStack itemStack = slot.getItem();
         int i = this.inventory.findSlotMatchingCraftingIngredient(item, itemStack);
         if (i == -1) {
diff --git a/src/main/java/net/minecraft/world/entity/player/Inventory.java b/src/main/java/net/minecraft/world/entity/player/Inventory.java
index 36ba57e95b84f3d598bf4be624d6c88b05a6f9a6..110456deaa662bc1c0f6ba7878bb3074869a4350 100644
--- a/src/main/java/net/minecraft/world/entity/player/Inventory.java
+++ b/src/main/java/net/minecraft/world/entity/player/Inventory.java
@@ -188,11 +188,11 @@ public class Inventory implements Container, Nameable {
         return !stack.isDamaged() && !stack.isEnchanted() && !stack.has(DataComponents.CUSTOM_NAME);
     }
 
-    public int findSlotMatchingCraftingIngredient(Holder<Item> item, ItemStack stack) {
+    public int findSlotMatchingCraftingIngredient(io.papermc.paper.inventory.recipe.ItemOrExact item, ItemStack stack) { // Paper - Improve exact choice recipe ingredients
         for (int i = 0; i < this.items.size(); ++i) {
             ItemStack itemstack1 = (ItemStack) this.items.get(i);
 
-            if (!itemstack1.isEmpty() && itemstack1.is(item) && Inventory.isUsableForCrafting(itemstack1) && (stack.isEmpty() || ItemStack.isSameItemSameComponents(stack, itemstack1))) {
+            if (!itemstack1.isEmpty() && item.matches(itemstack1) && (!(item instanceof io.papermc.paper.inventory.recipe.ItemOrExact.Item) || Inventory.isUsableForCrafting(itemstack1)) && (stack.isEmpty() || ItemStack.isSameItemSameComponents(stack, itemstack1))) { // Paper - Improve exact choice recipe ingredients
                 return i;
             }
         }
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 b645962fbcebf937a477dc5c94d96be998a84105..f5d737d8a1ad10df8a74c11a6f6394ed0d2ca8be 100644
--- a/src/main/java/net/minecraft/world/entity/player/StackedContents.java
+++ b/src/main/java/net/minecraft/world/entity/player/StackedContents.java
@@ -13,7 +13,7 @@ import java.util.List;
 import javax.annotation.Nullable;
 
 public class StackedContents<T> {
-    public final Reference2IntOpenHashMap<T> amounts = new Reference2IntOpenHashMap<>();
+    public final it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap<T> amounts = new it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap<>(); // Paper - Improve exact choice recipe ingredients (don't use "reference" map)
 
     boolean hasAtLeast(T input, int minimum) {
         return this.amounts.getInt(input) >= minimum;
@@ -49,7 +49,7 @@ public class StackedContents<T> {
     List<T> getUniqueAvailableIngredientItems(Iterable<? extends StackedContents.IngredientInfo<T>> ingredients) {
         List<T> list = new ArrayList<>();
 
-        for (Entry<T> entry : Reference2IntMaps.fastIterable(this.amounts)) {
+        for (it.unimi.dsi.fastutil.objects.Object2IntMap.Entry<T> entry : it.unimi.dsi.fastutil.objects.Object2IntMaps.fastIterable(this.amounts)) { // Paper - Improve exact choice recipe ingredients (don't use "reference" map)
             if (entry.getIntValue() > 0 && anyIngredientMatches(ingredients, entry.getKey())) {
                 list.add(entry.getKey());
             }
@@ -71,13 +71,13 @@ public class StackedContents<T> {
     @VisibleForTesting
     public int getResultUpperBound(List<? extends StackedContents.IngredientInfo<T>> ingredients) {
         int i = Integer.MAX_VALUE;
-        ObjectIterable<Entry<T>> objectIterable = Reference2IntMaps.fastIterable(this.amounts);
+        ObjectIterable<it.unimi.dsi.fastutil.objects.Object2IntMap.Entry<T>> objectIterable = it.unimi.dsi.fastutil.objects.Object2IntMaps.fastIterable(this.amounts); // Paper - Improve exact choice recipe ingredients (don't use "reference" map)
 
         label31:
         for (StackedContents.IngredientInfo<T> ingredientInfo : ingredients) {
             int j = 0;
 
-            for (Entry<T> entry : objectIterable) {
+            for (it.unimi.dsi.fastutil.objects.Object2IntMap.Entry<T> entry : objectIterable) { // Paper - Improve exact choice recipe ingredients (don't use "reference" map)
                 int k = entry.getIntValue();
                 if (k > j) {
                     if (ingredientInfo.acceptsItem(entry.getKey())) {
diff --git a/src/main/java/net/minecraft/world/entity/player/StackedItemContents.java b/src/main/java/net/minecraft/world/entity/player/StackedItemContents.java
index 9436fb2bfc5f4463bd958c7cbf8d700dbf819890..209ddab9e96169fe4be373b2f6f289cf06f08537 100644
--- a/src/main/java/net/minecraft/world/entity/player/StackedItemContents.java
+++ b/src/main/java/net/minecraft/world/entity/player/StackedItemContents.java
@@ -9,9 +9,14 @@ import net.minecraft.world.item.crafting.PlacementInfo;
 import net.minecraft.world.item.crafting.Recipe;
 
 public class StackedItemContents {
-    private final StackedContents<Holder<Item>> raw = new StackedContents<>();
+    // Paper start - Improve exact choice recipe ingredients
+    private final StackedContents<io.papermc.paper.inventory.recipe.ItemOrExact> raw = new StackedContents<>();
+    @Nullable
+    private io.papermc.paper.inventory.recipe.StackedContentsExtrasMap extrasMap = null;
+    // Paper start - Improve exact choice recipe ingredients
 
     public void accountSimpleStack(ItemStack item) {
+        if (this.extrasMap != null && this.extrasMap.accountStack(item, Math.min(64, item.getCount()))) return; // Paper - Improve exact choice recipe ingredients; max of 64 due to accountStack method below
         if (Inventory.isUsableForCrafting(item)) {
             this.accountStack(item);
         }
@@ -24,36 +29,53 @@ public class StackedItemContents {
     public void accountStack(ItemStack item, int maxCount) {
         if (!item.isEmpty()) {
             int i = Math.min(maxCount, item.getCount());
-            this.raw.account(item.getItemHolder(), i);
+            if (this.extrasMap != null && !item.getComponentsPatch().isEmpty() && this.extrasMap.accountStack(item, i)) return; // Paper - Improve exact choice recipe ingredients; if an exact ingredient, don't include it
+            this.raw.account(new io.papermc.paper.inventory.recipe.ItemOrExact.Item(item.getItemHolder()), i);
         }
     }
 
-    public boolean canCraft(Recipe<?> recipe, @Nullable StackedContents.Output<Holder<Item>> itemCallback) {
+    // Paper start - Improve exact choice recipe ingredients
+    public void initializeExtras(final Recipe<?> recipe, @Nullable final net.minecraft.world.item.crafting.CraftingInput input) {
+        if (this.extrasMap == null) {
+            this.extrasMap = new io.papermc.paper.inventory.recipe.StackedContentsExtrasMap(this.raw);
+        }
+        this.extrasMap.initialize(recipe);
+        if (input != null) this.extrasMap.accountInput(input);
+    }
+
+    public void resetExtras() {
+        if (this.extrasMap != null && !this.raw.amounts.isEmpty()) {
+            this.extrasMap.resetExtras();
+        }
+    }
+    // Paper end - Improve exact choice recipe ingredients
+
+    public boolean canCraft(Recipe<?> recipe, @Nullable StackedContents.Output<io.papermc.paper.inventory.recipe.ItemOrExact> itemCallback) { // Paper - Improve exact choice recipe ingredients
         return this.canCraft(recipe, 1, itemCallback);
     }
 
-    public boolean canCraft(Recipe<?> recipe, int quantity, @Nullable StackedContents.Output<Holder<Item>> itemCallback) {
+    public boolean canCraft(Recipe<?> recipe, int quantity, @Nullable StackedContents.Output<io.papermc.paper.inventory.recipe.ItemOrExact> itemCallback) { // Paper - Improve exact choice recipe ingredients
         PlacementInfo placementInfo = recipe.placementInfo();
         return !placementInfo.isImpossibleToPlace() && this.canCraft(placementInfo.ingredients(), quantity, itemCallback);
     }
 
     public boolean canCraft(
-        List<? extends StackedContents.IngredientInfo<Holder<Item>>> rawIngredients, @Nullable StackedContents.Output<Holder<Item>> itemCallback
+        List<? extends StackedContents.IngredientInfo<io.papermc.paper.inventory.recipe.ItemOrExact>> rawIngredients, @Nullable StackedContents.Output<io.papermc.paper.inventory.recipe.ItemOrExact> itemCallback // Paper - Improve exact choice recipe ingredients
     ) {
         return this.canCraft(rawIngredients, 1, itemCallback);
     }
 
     private boolean canCraft(
-        List<? extends StackedContents.IngredientInfo<Holder<Item>>> rawIngredients, int quantity, @Nullable StackedContents.Output<Holder<Item>> itemCallback
+        List<? extends StackedContents.IngredientInfo<io.papermc.paper.inventory.recipe.ItemOrExact>> rawIngredients, int quantity, @Nullable StackedContents.Output<io.papermc.paper.inventory.recipe.ItemOrExact> itemCallback // Paper - Improve exact choice recipe ingredients
     ) {
         return this.raw.tryPick(rawIngredients, quantity, itemCallback);
     }
 
-    public int getBiggestCraftableStack(Recipe<?> recipe, @Nullable StackedContents.Output<Holder<Item>> itemCallback) {
+    public int getBiggestCraftableStack(Recipe<?> recipe, @Nullable StackedContents.Output<io.papermc.paper.inventory.recipe.ItemOrExact> itemCallback) { // Paper - Improve exact choice recipe ingredients
         return this.getBiggestCraftableStack(recipe, Integer.MAX_VALUE, itemCallback);
     }
 
-    public int getBiggestCraftableStack(Recipe<?> recipe, int max, @Nullable StackedContents.Output<Holder<Item>> itemCallback) {
+    public int getBiggestCraftableStack(Recipe<?> recipe, int max, @Nullable StackedContents.Output<io.papermc.paper.inventory.recipe.ItemOrExact> itemCallback) { // Paper - Improve exact choice recipe ingredients
         return this.raw.tryPickAll(recipe.placementInfo().ingredients(), max, itemCallback);
     }
 
diff --git a/src/main/java/net/minecraft/world/item/crafting/Ingredient.java b/src/main/java/net/minecraft/world/item/crafting/Ingredient.java
index 0bd0935e76038e07c063bbfc61fa1a6339c23eed..0b0054b3d5d56ba24e1aee0e3ab56ea5b01a82a8 100644
--- a/src/main/java/net/minecraft/world/item/crafting/Ingredient.java
+++ b/src/main/java/net/minecraft/world/item/crafting/Ingredient.java
@@ -25,7 +25,7 @@ import java.util.List;
 import javax.annotation.Nullable;
 // CraftBukkit end
 
-public final class Ingredient implements StackedContents.IngredientInfo<Holder<Item>>, Predicate<ItemStack> {
+public final class Ingredient implements StackedContents.IngredientInfo<io.papermc.paper.inventory.recipe.ItemOrExact>, Predicate<ItemStack> { // Paper - Improve exact choice recipe ingredients
 
     public static final StreamCodec<RegistryFriendlyByteBuf, Ingredient> CONTENTS_STREAM_CODEC = ByteBufCodecs.holderSet(Registries.ITEM).map(Ingredient::new, (recipeitemstack) -> {
         return recipeitemstack.values;
@@ -44,19 +44,23 @@ public final class Ingredient implements StackedContents.IngredientInfo<Holder<I
     private final HolderSet<Item> values;
     // CraftBukkit start
     @Nullable
-    private List<ItemStack> itemStacks;
+    private java.util.Set<ItemStack> itemStacks; // Paper - Improve exact choice recipe ingredients
 
     public boolean isExact() {
         return this.itemStacks != null;
     }
 
-    public List<ItemStack> itemStacks() {
+    public java.util.Set<ItemStack> itemStacks() { // Paper - Improve exact choice recipe ingredients
         return this.itemStacks;
     }
 
     public static Ingredient ofStacks(List<ItemStack> stacks) {
         Ingredient recipe = Ingredient.of(stacks.stream().map(ItemStack::getItem));
-        recipe.itemStacks = stacks;
+        // Paper start - Improve exact choice recipe ingredients
+        recipe.itemStacks = net.minecraft.world.item.ItemStackLinkedSet.createTypeAndComponentsSet();
+        recipe.itemStacks.addAll(stacks);
+        recipe.itemStacks = java.util.Collections.unmodifiableSet(recipe.itemStacks);
+        // Paper end - Improve exact choice recipe ingredients
         return recipe;
     }
     // CraftBukkit end
@@ -94,20 +98,22 @@ public final class Ingredient implements StackedContents.IngredientInfo<Holder<I
     public boolean test(ItemStack itemstack) {
         // CraftBukkit start
         if (this.isExact()) {
-            for (ItemStack itemstack1 : this.itemStacks()) {
-                if (itemstack1.getItem() == itemstack.getItem() && ItemStack.isSameItemSameComponents(itemstack, itemstack1)) {
-                    return true;
-                }
-            }
-
-            return false;
+            return this.itemStacks.contains(itemstack); // Paper - Improve exact choice recipe ingredients (hashing FTW!)
         }
         // CraftBukkit end
         return itemstack.is(this.values);
     }
 
-    public boolean acceptsItem(Holder<Item> holder) {
-        return this.values.contains(holder);
+    // Paper start - Improve exact choice recipe ingredients
+    @Override
+    public boolean acceptsItem(final io.papermc.paper.inventory.recipe.ItemOrExact holder) {
+        return switch (holder) {
+            case io.papermc.paper.inventory.recipe.ItemOrExact.Item(final Holder<Item> item) ->
+                !this.isExact() && this.values.contains(item);
+            case io.papermc.paper.inventory.recipe.ItemOrExact.Exact(final ItemStack exact) ->
+                this.isExact() && this.itemStacks.contains(exact);
+        };
+        // Paper end - Improve exact choice recipe ingredients
     }
 
     public boolean equals(Object object) {
@@ -137,6 +143,11 @@ public final class Ingredient implements StackedContents.IngredientInfo<Holder<I
     }
 
     public SlotDisplay display() {
+        // Paper start - show exact ingredients in recipe book
+        if (this.isExact()) {
+            return new SlotDisplay.Composite(this.itemStacks().stream().<SlotDisplay>map(SlotDisplay.ItemStackSlotDisplay::new).toList());
+        }
+        // Paper end - show exact ingredients in recipe book
         return (SlotDisplay) this.values.unwrap().map(SlotDisplay.TagSlotDisplay::new, (list) -> {
             return new SlotDisplay.Composite(list.stream().map(Ingredient::displayForSingleItem).toList());
         });
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 6ec7b234b468755835107be40d0080222c0b9263..12f95bee2a69fd5df7c4a165537e01299e60c5f6 100644
--- a/src/main/java/net/minecraft/world/item/crafting/ShapelessRecipe.java
+++ b/src/main/java/net/minecraft/world/item/crafting/ShapelessRecipe.java
@@ -80,7 +80,18 @@ public class ShapelessRecipe implements CraftingRecipe {
     }
 
     public boolean matches(CraftingInput input, Level world) {
-        return input.ingredientCount() != this.ingredients.size() ? false : (input.size() == 1 && this.ingredients.size() == 1 ? ((Ingredient) this.ingredients.getFirst()).test(input.getItem(0)) : input.stackedContents().canCraft((Recipe) this, (StackedContents.Output) null));
+        // Paper start - Improve exact choice recipe ingredients & unwrap ternary
+        if (input.ingredientCount() != this.ingredients.size()) {
+            return false;
+        }
+        if (input.size() == 1 && this.ingredients.size() == 1) {
+            return this.ingredients.getFirst().test(input.getItem(0));
+        }
+        input.stackedContents().initializeExtras(this, input);
+        boolean canCraft = input.stackedContents().canCraft(this, null);
+        input.stackedContents().resetExtras();
+        return canCraft;
+        // Paper end - Improve exact choice recipe ingredients & unwrap ternary
     }
 
     public ItemStack assemble(CraftingInput input, HolderLookup.Provider registries) {