From 94ea770212041a19240394ed3121d869baeb1ec4 Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Sat, 9 Nov 2024 13:53:53 -0800 Subject: [PATCH] Re-add exact choice shapeless support (#11546) * Re-add exact choice shapeless support * don't re-create maps every shapeless match * add missing paper comment with last patch --- ...-onEntityRemove-for-all-online-playe.patch | 4 +- ...rove-exact-choice-recipe-ingredients.patch | 434 +++++++++++++++++ ...rove-exact-choice-recipe-ingredients.patch | 436 ------------------ 3 files changed, 436 insertions(+), 438 deletions(-) create mode 100644 patches/server/1061-Improve-exact-choice-recipe-ingredients.patch delete mode 100644 patches/unapplied/server/0277-Improve-exact-choice-recipe-ingredients.patch diff --git a/patches/server/1060-Call-CraftPlayer-onEntityRemove-for-all-online-playe.patch b/patches/server/1060-Call-CraftPlayer-onEntityRemove-for-all-online-playe.patch index d50af99648..fd0f3377d3 100644 --- a/patches/server/1060-Call-CraftPlayer-onEntityRemove-for-all-online-playe.patch +++ b/patches/server/1060-Call-CraftPlayer-onEntityRemove-for-all-online-playe.patch @@ -5,7 +5,7 @@ Subject: [PATCH] Call CraftPlayer#onEntityRemove for all online players diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java -index 5964d601c05176f48167cc92057a59e52a4da92b..3204497dea30365d7d23bb877af811248168afbf 100644 +index 5964d601c05176f48167cc92057a59e52a4da92b..3b6b6483bf855493948417f44f06427b625bc910 100644 --- a/src/main/java/net/minecraft/server/level/ServerLevel.java +++ b/src/main/java/net/minecraft/server/level/ServerLevel.java @@ -2780,7 +2780,7 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe @@ -13,7 +13,7 @@ index 5964d601c05176f48167cc92057a59e52a4da92b..3204497dea30365d7d23bb877af81124 entity.valid = false; if (!(entity instanceof ServerPlayer)) { - for (ServerPlayer player : ServerLevel.this.players) { -+ for (ServerPlayer player : server.getPlayerList().players) { ++ for (ServerPlayer player : ServerLevel.this.server.getPlayerList().players) { // Paper - call onEntityRemove for all online players player.getBukkitEntity().onEntityRemove(entity); } } diff --git a/patches/server/1061-Improve-exact-choice-recipe-ingredients.patch b/patches/server/1061-Improve-exact-choice-recipe-ingredients.patch new file mode 100644 index 0000000000..24495c4ce6 --- /dev/null +++ b/patches/server/1061-Improve-exact-choice-recipe-ingredients.patch @@ -0,0 +1,434 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Jake Potrebic +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..5bcc814d5a1b88991e9c1324b88a919ca199fcda +--- /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 is(ItemStack stack); ++ ++ record Item(Holder item) implements ItemOrExact { ++ ++ public Item(final ItemStack stack) { ++ this(stack.getItemHolder()); ++ } ++ ++ @Override ++ public int getMaxStackSize() { ++ return this.item.value().getDefaultMaxStackSize(); ++ } ++ ++ @Override ++ public boolean is(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 is(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..bdb876a0e687d4b9e885118523e91185a164b27c +--- /dev/null ++++ b/src/main/java/io/papermc/paper/inventory/recipe/StackedContentsExtrasMap.java +@@ -0,0 +1,67 @@ ++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.Recipe; ++ ++public final class StackedContentsExtrasMap { ++ ++ private final StackedContents contents; ++ public Object2IntMap regularRemoved = new Object2IntOpenHashMap<>(); // needed for re-using the regular contents (for ShapelessRecipe) ++ public final ObjectSet exactIngredients = new ObjectOpenCustomHashSet<>(ItemStackLinkedSet.TYPE_AND_TAG); ++ ++ public StackedContentsExtrasMap(final StackedContents contents) { ++ this.contents = contents; ++ } ++ ++ public void initialize(final Recipe recipe) { ++ this.exactIngredients.clear(); ++ for (final StackedContents.IngredientInfo info : recipe.placementInfo().unpackedIngredients()) { ++ if (info.isExact()) { ++ this.exactIngredients.addAll(info.allowedItems().stream().map(o -> ((ItemOrExact.Exact) o).stack()).toList()); ++ } ++ } ++ } ++ ++ 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 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 462a970ffa610bc1eb3c813dafb768c014d077d1..1afb544fb028b645821063ba1eaa9e3c45cee63f 100644 +--- a/src/main/java/net/minecraft/recipebook/ServerPlaceRecipe.java ++++ b/src/main/java/net/minecraft/recipebook/ServerPlaceRecipe.java +@@ -41,6 +41,7 @@ public class ServerPlaceRecipe> { + 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); +@@ -100,9 +101,9 @@ public class ServerPlaceRecipe> { + } + + int j = this.calculateAmountToCraft(i, bl); +- List> list = new ArrayList<>(); ++ List list = new ArrayList<>(); // Paper - Improve exact choice recipe ingredients + if (finder.canCraft(recipe.value(), j, list::add)) { +- OptionalInt optionalInt = list.stream().mapToInt(item -> item.value().getDefaultMaxStackSize()).min(); ++ OptionalInt optionalInt = list.stream().mapToInt(io.papermc.paper.inventory.recipe.ItemOrExact::getMaxStackSize).min(); // Paper - Improve exact choice recipe ingredients + if (optionalInt.isPresent()) { + j = Math.min(j, optionalInt.getAsInt()); + } +@@ -119,7 +120,7 @@ public class ServerPlaceRecipe> { + int kx = k; + + while (kx > 0) { +- Holder holder = list.get(jx); ++ io.papermc.paper.inventory.recipe.ItemOrExact holder = list.get(jx); // Paper - Improve exact choice recipe ingredients + kx = this.moveItemToGrid(slot2, holder, kx); + if (kx == -1) { + return; +@@ -155,7 +156,7 @@ public class ServerPlaceRecipe> { + } + } + +- private int moveItemToGrid(Slot slot, Holder item, int count) { ++ private int moveItemToGrid(Slot slot, io.papermc.paper.inventory.recipe.ItemOrExact item, int count) { // Paper - Improve exact choice recipe ingredients + int i = this.inventory.findSlotMatchingCraftingIngredient(item); + if (i == -1) { + return -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 ad82e5aeb565b23c3ec565fa60e1f31d1710bd4e..0e214d502998e9eb959952b257844529992df0df 100644 +--- a/src/main/java/net/minecraft/world/entity/player/Inventory.java ++++ b/src/main/java/net/minecraft/world/entity/player/Inventory.java +@@ -201,11 +201,11 @@ public class Inventory implements Container, Nameable { + return !stack.isDamaged() && !stack.isEnchanted() && !stack.has(DataComponents.CUSTOM_NAME); + } + +- public int findSlotMatchingCraftingIngredient(Holder item) { ++ public int findSlotMatchingCraftingIngredient(io.papermc.paper.inventory.recipe.ItemOrExact item) { // Paper - Improve exact choice recipe ingredients + for (int i = 0; i < this.items.size(); ++i) { + ItemStack itemstack = (ItemStack) this.items.get(i); + +- if (!itemstack.isEmpty() && itemstack.is(item) && Inventory.isUsableForCrafting(itemstack)) { ++ if (!itemstack.isEmpty() && item.is(itemstack) && (!(item instanceof io.papermc.paper.inventory.recipe.ItemOrExact.Item) || isUsableForCrafting(itemstack))) { // 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 3bc7c9b3df5547c6564696fce67417c96ffdebf9..9ad3c4510f0e0530dfbbc3ba7e6d19b7678e1294 100644 +--- a/src/main/java/net/minecraft/world/entity/player/StackedContents.java ++++ b/src/main/java/net/minecraft/world/entity/player/StackedContents.java +@@ -10,7 +10,7 @@ import java.util.Set; + import javax.annotation.Nullable; + + public class StackedContents { +- public final Reference2IntOpenHashMap amounts = new Reference2IntOpenHashMap<>(); ++ public final it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap amounts = new it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap<>(); // Paper - Improve exact choice recipe ingredients (don't use "reference" map) + + boolean hasAnyAmount(T input) { + return this.amounts.getInt(input) > 0; +@@ -47,12 +47,13 @@ public class StackedContents { + this.put(input, count); + } + +- public static record IngredientInfo(List allowedItems) { +- public IngredientInfo(List allowedItems) { ++ public static record IngredientInfo(List allowedItems, boolean isExact) { // Paper - Improve exact choice recipe ingredients ++ public IngredientInfo(List allowedItems, boolean isExact) { // Paper - Improve exact choice recipe ingredients + if (allowedItems.isEmpty()) { + throw new IllegalArgumentException("Ingredients can't be empty"); + } else { + this.allowedItems = allowedItems; ++ this.isExact = isExact; // Paper - Improve exact choice recipe ingredients + } + } + } +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 cc6c5cf0d1034ed074b6c354c307a0c2613f85c7..3f075c5c12f7ce331200fddb1b6602380d6f5f3e 100644 +--- a/src/main/java/net/minecraft/world/entity/player/StackedItemContents.java ++++ b/src/main/java/net/minecraft/world/entity/player/StackedItemContents.java +@@ -12,9 +12,14 @@ import net.minecraft.world.item.crafting.PlacementInfo; + import net.minecraft.world.item.crafting.Recipe; + + public class StackedItemContents { +- private final StackedContents> raw = new StackedContents<>(); ++ // Paper start - Improve exact choice recipe ingredients ++ private final StackedContents 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); + } +@@ -27,39 +32,56 @@ 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 static StackedContents.IngredientInfo> convertIngredientContents(Stream> items) { +- List> list = items.sorted(Comparator.comparingInt(item -> BuiltInRegistries.ITEM.getId(item.value()))).toList(); +- return new StackedContents.IngredientInfo<>(list); ++ // 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(); ++ } ++ } ++ ++ public static StackedContents.IngredientInfo convertIngredientContents(Stream items, boolean isExact) { ++ List list = items.sorted(Comparator.comparingInt(item -> isExact ? ItemStack.hashItemAndComponents(((io.papermc.paper.inventory.recipe.ItemOrExact.Exact) item).stack()) : BuiltInRegistries.ITEM.getId(((io.papermc.paper.inventory.recipe.ItemOrExact.Item) item).item().value()))).toList(); ++ return new StackedContents.IngredientInfo<>(list, isExact); ++ // Paper end - Improve exact choice recipe ingredients + } + +- public boolean canCraft(Recipe recipe, @Nullable StackedContents.Output> itemCallback) { ++ public boolean canCraft(Recipe recipe, @Nullable StackedContents.Output itemCallback) { // Paper - Improve exact choice recipe ingredients + return this.canCraft(recipe, 1, itemCallback); + } + +- public boolean canCraft(Recipe recipe, int quantity, @Nullable StackedContents.Output> itemCallback) { ++ public boolean canCraft(Recipe recipe, int quantity, @Nullable StackedContents.Output itemCallback) { // Paper - Improve exact choice recipe ingredients + PlacementInfo placementInfo = recipe.placementInfo(); + return !placementInfo.isImpossibleToPlace() && this.canCraft(placementInfo.unpackedIngredients(), quantity, itemCallback); + } + +- public boolean canCraft(List>> rawIngredients, @Nullable StackedContents.Output> itemCallback) { ++ public boolean canCraft(List> rawIngredients, @Nullable StackedContents.Output itemCallback) { // Paper - Improve exact choice recipe ingredients + return this.canCraft(rawIngredients, 1, itemCallback); + } + + private boolean canCraft( +- List>> rawIngredients, int quantity, @Nullable StackedContents.Output> itemCallback ++ List> rawIngredients, int quantity, @Nullable StackedContents.Output itemCallback // Paper - Improve exact choice recipe ingredients + ) { + return this.raw.tryPick(rawIngredients, quantity, itemCallback); + } + +- public int getBiggestCraftableStack(Recipe recipe, @Nullable StackedContents.Output> itemCallback) { ++ public int getBiggestCraftableStack(Recipe recipe, @Nullable StackedContents.Output 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> itemCallback) { ++ public int getBiggestCraftableStack(Recipe recipe, int max, @Nullable StackedContents.Output itemCallback) { // Paper - Improve exact choice recipe ingredients + return this.raw.tryPickAll(recipe.placementInfo().unpackedIngredients(), 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 2dce801e06687c218be3333ac9f000bae09f0caf..812f919a7a7e309c8513f44104f092496037608f 100644 +--- a/src/main/java/net/minecraft/world/item/crafting/Ingredient.java ++++ b/src/main/java/net/minecraft/world/item/crafting/Ingredient.java +@@ -139,6 +139,11 @@ public final class Ingredient implements Predicate { + } + + public SlotDisplay display() { ++ // Paper start - show exact ingredients in recipe book ++ if (this.isExact()) { ++ return new SlotDisplay.Composite(this.itemStacks().stream().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/PlacementInfo.java b/src/main/java/net/minecraft/world/item/crafting/PlacementInfo.java +index 43124a000f48e243d75df64051fbda816035af25..400fd1cdfd17d4d34552f9bb3d91bc71fd394301 100644 +--- a/src/main/java/net/minecraft/world/item/crafting/PlacementInfo.java ++++ b/src/main/java/net/minecraft/world/item/crafting/PlacementInfo.java +@@ -11,26 +11,28 @@ import net.minecraft.world.item.Item; + public class PlacementInfo { + public static final PlacementInfo NOT_PLACEABLE = new PlacementInfo(List.of(), List.of(), List.of()); + private final List ingredients; +- private final List>> unpackedIngredients; ++ private final List> unpackedIngredients; // Paper - Improve exact choice recipe ingredients + private final List> slotInfo; + + private PlacementInfo( +- List ingredients, List>> rawIngredients, List> placementSlots ++ List ingredients, List> rawIngredients, List> placementSlots // Paper - Improve exact choice recipe ingredients + ) { + this.ingredients = ingredients; + this.unpackedIngredients = rawIngredients; + this.slotInfo = placementSlots; + } + +- public static StackedContents.IngredientInfo> ingredientToContents(Ingredient ingredient) { +- return StackedItemContents.convertIngredientContents(ingredient.items().stream()); ++ // Paper start - Improve exact choice recipe ingredients ++ public static StackedContents.IngredientInfo ingredientToContents(Ingredient ingredient) { ++ return StackedItemContents.convertIngredientContents(ingredient.isExact() ? ingredient.itemStacks().stream().map(io.papermc.paper.inventory.recipe.ItemOrExact.Exact::new) : ingredient.items().stream().map(io.papermc.paper.inventory.recipe.ItemOrExact.Item::new), ingredient.isExact()); ++ // Paper end - Improve exact choice recipe ingredients + } + + public static PlacementInfo create(Ingredient ingredient) { + if (ingredient.items().isEmpty()) { + return NOT_PLACEABLE; + } else { +- StackedContents.IngredientInfo> ingredientInfo = ingredientToContents(ingredient); ++ StackedContents.IngredientInfo ingredientInfo = ingredientToContents(ingredient); // Paper - Improve exact choice recipe ingredients + PlacementInfo.SlotInfo slotInfo = new PlacementInfo.SlotInfo(0); + return new PlacementInfo(List.of(ingredient), List.of(ingredientInfo), List.of(Optional.of(slotInfo))); + } +@@ -39,7 +41,7 @@ public class PlacementInfo { + public static PlacementInfo createFromOptionals(List> ingredients) { + int i = ingredients.size(); + List list = new ArrayList<>(i); +- List>> list2 = new ArrayList<>(i); ++ List> list2 = new ArrayList<>(i); // Paper - Improve exact choice recipe ingredients + List> list3 = new ArrayList<>(i); + int j = 0; + +@@ -63,7 +65,7 @@ public class PlacementInfo { + + public static PlacementInfo create(List ingredients) { + int i = ingredients.size(); +- List>> list = new ArrayList<>(i); ++ List> list = new ArrayList<>(i); // Paper - Improve exact choice recipe ingredients + List> list2 = new ArrayList<>(i); + + for (int j = 0; j < i; j++) { +@@ -87,7 +89,7 @@ public class PlacementInfo { + return this.ingredients; + } + +- public List>> unpackedIngredients() { ++ public List> unpackedIngredients() { // Paper - Improve exact choice recipe ingredients + return this.unpackedIngredients; + } + +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) { diff --git a/patches/unapplied/server/0277-Improve-exact-choice-recipe-ingredients.patch b/patches/unapplied/server/0277-Improve-exact-choice-recipe-ingredients.patch deleted file mode 100644 index 9c0b7b8275..0000000000 --- a/patches/unapplied/server/0277-Improve-exact-choice-recipe-ingredients.patch +++ /dev/null @@ -1,436 +0,0 @@ -From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 -From: Jake Potrebic -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 -public net.minecraft.world.entity.player.StackedContents take(II)I - -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..ef68600f6b59674ddea6c77f7e412902888e39b7 ---- /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 implements Recipe { -+ -+ 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..568ba6aed2e74b8d84f4e82c1e785ef1587e2617 ---- /dev/null -+++ b/src/main/java/io/papermc/paper/inventory/recipe/StackedContentsExtraMap.java -@@ -0,0 +1,109 @@ -+package io.papermc.paper.inventory.recipe; -+ -+import it.unimi.dsi.fastutil.ints.Int2IntArrayMap; -+import it.unimi.dsi.fastutil.ints.Int2IntMap; -+import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap; -+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.CraftingInput; -+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 -+ public final Object2IntMap exactChoiceIds = new Object2IntOpenCustomHashMap<>(ItemStackLinkedSet.TYPE_AND_TAG); -+ private final Int2ObjectMap idToExactChoice = new Int2ObjectOpenHashMap<>(); -+ private final StackedContents contents; -+ public final Map 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.getComponentsPatch().isEmpty()) { -+ // 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 Int2IntMap regularRemoved = new Int2IntArrayMap(); -+ 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)) { -+ // remove one of the items if it was added to the contents as a non-extra item -+ final int plainStackIdx = StackedContents.getStackingIndex(item); -+ if (this.contents.take(plainStackIdx, 1) == plainStackIdx) { -+ this.regularRemoved.put(plainStackIdx, 1); -+ } -+ } -+ } -+ } -+ } -+ -+ public void resetExtras() { -+ // clear previous extra ids -+ for (final int extraId : this.exactChoiceIds.values()) { -+ this.contents.contents.remove(extraId); -+ } -+ for (final Int2IntMap.Entry entry : this.regularRemoved.int2IntEntrySet()) { -+ this.contents.put(entry.getIntKey(), entry.getIntValue()); -+ } -+ } -+ -+ 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 0bd749af8014dd437229594ef6981a2ead803990..6d1f9c15dc99917a2ac966ea38ef1970f4f0289c 100644 ---- a/src/main/java/net/minecraft/recipebook/ServerPlaceRecipe.java -+++ b/src/main/java/net/minecraft/recipebook/ServerPlaceRecipe.java -@@ -31,6 +31,7 @@ public class ServerPlaceRecipe> imple - this.inventory = entity.getInventory(); - if (this.testClearGrid() || entity.isCreative()) { - this.stackedContents.clear(); -+ this.stackedContents.initializeExtras(recipe.value(), null); // Paper - Improve exact choice recipe ingredients - entity.getInventory().fillStackedContents(this.stackedContents); - this.menu.fillCraftSlotsStackedContents(this.stackedContents); - if (this.stackedContents.canCraft(recipe.value(), null)) { -@@ -77,7 +78,7 @@ public class ServerPlaceRecipe> imple - int l = k; - - for (int m : intList) { -- ItemStack itemStack2 = StackedContents.fromStackingIndex(m); -+ ItemStack itemStack2 = StackedContents.fromStackingIndexWithExtras(m, this.stackedContents); // Paper - Improve exact choice recipe ingredients - if (!itemStack2.isEmpty()) { - int n = itemStack2.getMaxStackSize(); - if (n < l) { -@@ -96,12 +97,22 @@ public class ServerPlaceRecipe> imple - @Override - public void addItemToSlot(Integer input, int slot, int amount, int gridX, int gridY) { - Slot slot2 = this.menu.getSlot(slot); -- ItemStack itemStack = StackedContents.fromStackingIndex(input); -+ // Paper start - Improve exact choice recipe ingredients -+ ItemStack itemStack = null; -+ boolean isExact = false; -+ if (this.stackedContents.extrasMap != null && input >= net.minecraft.core.registries.BuiltInRegistries.ITEM.size()) { -+ itemStack = StackedContents.fromStackingIndexExtras(input, this.stackedContents.extrasMap).copy(); -+ isExact = true; -+ } -+ if (itemStack == null) { -+ itemStack = StackedContents.fromStackingIndex(input); -+ } -+ // Paper end - Improve exact choice recipe ingredients - if (!itemStack.isEmpty()) { - int i = amount; - - while (i > 0) { -- i = this.moveItemToGrid(slot2, itemStack, i); -+ i = this.moveItemToGrid(slot2, itemStack, i, isExact); // Paper - Improve exact choice recipe ingredients - if (i == -1) { - return; - } -@@ -133,8 +144,15 @@ public class ServerPlaceRecipe> imple - return i; - } - -+ @Deprecated @io.papermc.paper.annotation.DoNotUse // Paper - Improve exact choice recipe ingredients -+ - protected int moveItemToGrid(Slot slot, ItemStack stack, int i) { -- int j = this.inventory.findSlotMatchingUnusedItem(stack); -+ // Paper start - Improve exact choice recipe ingredients -+ return this.moveItemToGrid(slot, stack, i, false); -+ } -+ protected int moveItemToGrid(Slot slot, ItemStack stack, int i, final boolean isExact) { -+ int j = isExact ? this.inventory.findSlotMatchingItem(stack) : this.inventory.findSlotMatchingUnusedItem(stack); -+ // Paper end - Improve exact choice recipe ingredients - if (j == -1) { - return -1; - } else { -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 fa5576e41baec4b52c7ebb877924eb91d3775a2d..fcabf630ce1e4949d00f485a5bff66dd1e54a277 100644 ---- a/src/main/java/net/minecraft/world/entity/player/StackedContents.java -+++ b/src/main/java/net/minecraft/world/entity/player/StackedContents.java -@@ -22,8 +22,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 - Improve exact choice recipe ingredients - - public void accountSimpleStack(ItemStack stack) { -+ if (this.extrasMap != null && !stack.getComponentsPatch().isEmpty() && this.extrasMap.accountStack(stack, Math.min(64, stack.getCount()))) return; // Paper - Improve exact choice recipe ingredients; max of 64 due to accountStack method below - if (!stack.isDamaged() && !stack.isEnchanted() && !stack.has(DataComponents.CUSTOM_NAME)) { - 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.getComponentsPatch().isEmpty() && this.extrasMap.accountStack(stack, j)) return; // Paper - Improve exact choice recipe ingredients; if an exact ingredient, don't include it - this.put(i, j); - } - } -@@ -83,6 +86,31 @@ public class StackedContents { - return itemId == 0 ? ItemStack.EMPTY : new ItemStack(Item.byId(itemId)); - } - -+ // Paper start - Improve exact choice recipe ingredients -+ public void initializeExtras(final Recipe recipe, @Nullable final net.minecraft.world.item.crafting.CraftingInput input) { -+ this.extrasMap = new io.papermc.paper.inventory.recipe.StackedContentsExtraMap(this, recipe); -+ if (input != null) this.extrasMap.accountInput(input); -+ } -+ -+ public void resetExtras() { -+ if (this.extrasMap != null && !this.contents.isEmpty()) { -+ this.extrasMap.resetExtras(); -+ } -+ this.extrasMap = null; -+ } -+ -+ public static ItemStack fromStackingIndexWithExtras(final int itemId, @Nullable final StackedContents contents) { -+ if (contents != null && contents.extrasMap != null && itemId >= BuiltInRegistries.ITEM.size()) { -+ return fromStackingIndexExtras(itemId, contents.extrasMap); -+ } -+ return fromStackingIndex(itemId); -+ } -+ -+ public static ItemStack fromStackingIndexExtras(final int itemId, final io.papermc.paper.inventory.recipe.StackedContentsExtraMap extrasMap) { -+ return extrasMap.getById(itemId).copy(); -+ } -+ // Paper end - Improve exact choice recipe ingredients -+ - public void clear() { - this.contents.clear(); - } -@@ -106,7 +134,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 - Improve exact choice recipe ingredients - - for (int j = 0; j < this.itemCount; j++) { - if (intList.contains(this.items[j])) { -@@ -169,7 +197,7 @@ public class StackedContents { - IntCollection intCollection = new IntAVLTreeSet(); - - for (Ingredient ingredient : this.ingredients) { -- intCollection.addAll(ingredient.getStackingIds()); -+ intCollection.addAll(this.getStackingIds(ingredient)); // Paper - Improve exact choice recipe ingredients - } - - IntIterator intIterator = intCollection.iterator(); -@@ -298,7 +326,7 @@ public class StackedContents { - for (Ingredient ingredient : this.ingredients) { - int j = 0; - -- for (int k : ingredient.getStackingIds()) { -+ for (int k : this.getStackingIds(ingredient)) { // Paper - Improve exact choice recipe ingredients - j = Math.max(j, StackedContents.this.contents.get(k)); - } - -@@ -309,5 +337,17 @@ public class StackedContents { - - return i; - } -+ -+ // Paper start - Improve exact choice recipe ingredients -+ 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 choice recipe ingredients - } - } -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 f3b6466089ee8be59747a16aac2cac84be30617d..45c80500201aabc1e8643427ebfb8818ab966750 100644 ---- a/src/main/java/net/minecraft/world/item/crafting/AbstractCookingRecipe.java -+++ b/src/main/java/net/minecraft/world/item/crafting/AbstractCookingRecipe.java -@@ -5,7 +5,7 @@ import net.minecraft.core.NonNullList; - import net.minecraft.world.item.ItemStack; - import net.minecraft.world.level.Level; - --public abstract class AbstractCookingRecipe implements Recipe { -+public abstract class AbstractCookingRecipe extends io.papermc.paper.inventory.recipe.RecipeBookExactChoiceRecipe implements Recipe { // Paper - improve exact recipe choices - protected final RecipeType type; - protected final CookingBookCategory category; - protected final String group; -@@ -24,6 +24,7 @@ public abstract class AbstractCookingRecipe implements Recipe - 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 3cab383e01c124349f3f96bcbcfe91356d51aa30..b57568d5e9c4c148a4b3c303c925a813fdd5dc67 100644 ---- a/src/main/java/net/minecraft/world/item/crafting/Recipe.java -+++ b/src/main/java/net/minecraft/world/item/crafting/Recipe.java -@@ -73,4 +73,10 @@ public interface Recipe { - } - - 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 59372daacd6fef45373c0557ccebb6ff5f16f174..63cf2b66f51df68aa3f6d98c69368ce454869d64 100644 ---- a/src/main/java/net/minecraft/world/item/crafting/ShapedRecipe.java -+++ b/src/main/java/net/minecraft/world/item/crafting/ShapedRecipe.java -@@ -17,7 +17,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 implements CraftingRecipe { // Paper - improve exact recipe choices - - final ShapedRecipePattern pattern; - final ItemStack result; -@@ -31,6 +31,7 @@ public class ShapedRecipe implements CraftingRecipe { - this.pattern = raw; - this.result = result; - this.showNotification = showNotification; -+ this.checkExactIngredients(); // Paper - improve exact recipe choices - } - - public ShapedRecipe(String group, CraftingBookCategory category, ShapedRecipePattern raw, 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 62401d045245ec7e303ec526c09b5e6fa4c9f17b..213ee4aa988dd4c2a5a7be99b1d13f67338e5209 100644 ---- a/src/main/java/net/minecraft/world/item/crafting/ShapelessRecipe.java -+++ b/src/main/java/net/minecraft/world/item/crafting/ShapelessRecipe.java -@@ -19,7 +19,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 implements CraftingRecipe { // Paper - improve exact recipe choices - - final String group; - final CraftingBookCategory category; -@@ -31,6 +31,7 @@ public class ShapelessRecipe implements CraftingRecipe { - this.category = category; - this.result = result; - this.ingredients = ingredients; -+ this.checkExactIngredients(); // Paper - improve exact recipe choices - } - - // CraftBukkit start -@@ -75,7 +76,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(this, (IntList) null)); -+ // Paper start - unwrap ternary & better exact choice recipes -+ 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); // setup stacked contents for this recipe -+ final boolean canCraft = input.stackedContents().canCraft(this, null); -+ input.stackedContents().resetExtras(); -+ return canCraft; -+ // Paper end - unwrap ternary & better exact choice recipes - } - - public ItemStack assemble(CraftingInput input, HolderLookup.Provider lookup) {