Port exact choice improvements (#11705)

This commit is contained in:
Jake Potrebic 2024-12-05 01:51:01 -08:00 committed by GitHub
parent ae060b31f0
commit c54c062e6f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -13,7 +13,7 @@ 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 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 new file mode 100644
index 0000000000000000000000000000000000000000..caaad8f54fb892dfd6c7d4e02ab9c32997f89a6a index 0000000000000000000000000000000000000000..ce745e49cd54fe3ae187785563a1bd311d14b5b2
--- /dev/null --- /dev/null
+++ b/src/main/java/io/papermc/paper/inventory/recipe/ItemOrExact.java +++ b/src/main/java/io/papermc/paper/inventory/recipe/ItemOrExact.java
@@ -0,0 +1,63 @@ @@ -0,0 +1,63 @@
@ -41,7 +41,7 @@ index 0000000000000000000000000000000000000000..caaad8f54fb892dfd6c7d4e02ab9c329
+ +
+ @Override + @Override
+ public boolean matches(final ItemStack stack) { + public boolean matches(final ItemStack stack) {
+ return stack.is(this.item) && stack.getComponentsPatch().isEmpty(); + return stack.is(this.item);
+ } + }
+ +
+ @Override + @Override
@ -82,10 +82,10 @@ index 0000000000000000000000000000000000000000..caaad8f54fb892dfd6c7d4e02ab9c329
+} +}
diff --git a/src/main/java/io/papermc/paper/inventory/recipe/StackedContentsExtrasMap.java b/src/main/java/io/papermc/paper/inventory/recipe/StackedContentsExtrasMap.java 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 new file mode 100644
index 0000000000000000000000000000000000000000..bdb876a0e687d4b9e885118523e91185a164b27c index 0000000000000000000000000000000000000000..f47c12e9dd6cfa857ca07a764edc22de372e25b6
--- /dev/null --- /dev/null
+++ b/src/main/java/io/papermc/paper/inventory/recipe/StackedContentsExtrasMap.java +++ b/src/main/java/io/papermc/paper/inventory/recipe/StackedContentsExtrasMap.java
@@ -0,0 +1,67 @@ @@ -0,0 +1,68 @@
+package io.papermc.paper.inventory.recipe; +package io.papermc.paper.inventory.recipe;
+ +
+import it.unimi.dsi.fastutil.objects.Object2IntMap; +import it.unimi.dsi.fastutil.objects.Object2IntMap;
@ -96,6 +96,7 @@ index 0000000000000000000000000000000000000000..bdb876a0e687d4b9e885118523e91185
+import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.ItemStack;
+import net.minecraft.world.item.ItemStackLinkedSet; +import net.minecraft.world.item.ItemStackLinkedSet;
+import net.minecraft.world.item.crafting.CraftingInput; +import net.minecraft.world.item.crafting.CraftingInput;
+import net.minecraft.world.item.crafting.Ingredient;
+import net.minecraft.world.item.crafting.Recipe; +import net.minecraft.world.item.crafting.Recipe;
+ +
+public final class StackedContentsExtrasMap { +public final class StackedContentsExtrasMap {
@ -110,9 +111,9 @@ index 0000000000000000000000000000000000000000..bdb876a0e687d4b9e885118523e91185
+ +
+ public void initialize(final Recipe<?> recipe) { + public void initialize(final Recipe<?> recipe) {
+ this.exactIngredients.clear(); + this.exactIngredients.clear();
+ for (final StackedContents.IngredientInfo<ItemOrExact> info : recipe.placementInfo().unpackedIngredients()) { + for (final Ingredient ingredient : recipe.placementInfo().ingredients()) {
+ if (info.isExact()) { + if (ingredient.isExact()) {
+ this.exactIngredients.addAll(info.allowedItems().stream().map(o -> ((ItemOrExact.Exact) o).stack()).toList()); + this.exactIngredients.addAll(ingredient.itemStacks());
+ } + }
+ } + }
+ } + }
@ -154,10 +155,10 @@ index 0000000000000000000000000000000000000000..bdb876a0e687d4b9e885118523e91185
+ } + }
+} +}
diff --git a/src/main/java/net/minecraft/recipebook/ServerPlaceRecipe.java b/src/main/java/net/minecraft/recipebook/ServerPlaceRecipe.java diff --git a/src/main/java/net/minecraft/recipebook/ServerPlaceRecipe.java b/src/main/java/net/minecraft/recipebook/ServerPlaceRecipe.java
index 462a970ffa610bc1eb3c813dafb768c014d077d1..1afb544fb028b645821063ba1eaa9e3c45cee63f 100644 index 6d3ccd19baab8f0571b71bd05979efb6b372a6f5..dc9b01db0853964da146ad4ccdc9e0e6f2a279f4 100644
--- a/src/main/java/net/minecraft/recipebook/ServerPlaceRecipe.java --- a/src/main/java/net/minecraft/recipebook/ServerPlaceRecipe.java
+++ b/src/main/java/net/minecraft/recipebook/ServerPlaceRecipe.java +++ b/src/main/java/net/minecraft/recipebook/ServerPlaceRecipe.java
@@ -41,6 +41,7 @@ public class ServerPlaceRecipe<R extends Recipe<?>> { @@ -40,6 +40,7 @@ public class ServerPlaceRecipe<R extends Recipe<?>> {
return RecipeBookMenu.PostPlaceAction.NOTHING; return RecipeBookMenu.PostPlaceAction.NOTHING;
} else { } else {
StackedItemContents stackedItemContents = new StackedItemContents(); StackedItemContents stackedItemContents = new StackedItemContents();
@ -165,88 +166,109 @@ index 462a970ffa610bc1eb3c813dafb768c014d077d1..1afb544fb028b645821063ba1eaa9e3c
inventory.fillStackedContents(stackedItemContents); inventory.fillStackedContents(stackedItemContents);
handler.fillCraftSlotsStackedContents(stackedItemContents); handler.fillCraftSlotsStackedContents(stackedItemContents);
return serverPlaceRecipe.tryPlaceRecipe(recipe, stackedItemContents); return serverPlaceRecipe.tryPlaceRecipe(recipe, stackedItemContents);
@@ -100,9 +101,9 @@ public class ServerPlaceRecipe<R extends Recipe<?>> { @@ -99,7 +100,7 @@ public class ServerPlaceRecipe<R extends Recipe<?>> {
} }
int j = this.calculateAmountToCraft(i, bl); int j = this.calculateAmountToCraft(i, bl);
- List<Holder<Item>> list = new ArrayList<>(); - List<Holder<Item>> list = new ArrayList<>();
+ List<io.papermc.paper.inventory.recipe.ItemOrExact> list = new ArrayList<>(); // Paper - Improve exact choice recipe ingredients + List<io.papermc.paper.inventory.recipe.ItemOrExact> list = new ArrayList<>(); // Paper - Improve exact choice recipe ingredients
if (finder.canCraft(recipe.value(), j, list::add)) { if (finder.canCraft(recipe.value(), j, list::add)) {
- OptionalInt optionalInt = list.stream().mapToInt(item -> item.value().getDefaultMaxStackSize()).min(); int k = clampToMaxStackSize(j, list);
+ OptionalInt optionalInt = list.stream().mapToInt(io.papermc.paper.inventory.recipe.ItemOrExact::getMaxStackSize).min(); // Paper - Improve exact choice recipe ingredients if (k != j) {
if (optionalInt.isPresent()) { @@ -114,7 +115,7 @@ public class ServerPlaceRecipe<R extends Recipe<?>> {
j = Math.min(j, optionalInt.getAsInt()); this.gridWidth, this.gridHeight, recipe.value(), recipe.value().placementInfo().slotsToIngredientIndex(), (slotx, index, x, y) -> {
} if (slotx != -1) {
@@ -119,7 +120,7 @@ public class ServerPlaceRecipe<R extends Recipe<?>> { Slot slot2 = this.inputGridSlots.get(index);
int kx = k; - 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 (kx > 0) { while (jx > 0) {
- Holder<Item> holder = list.get(jx); @@ -129,9 +130,11 @@ public class ServerPlaceRecipe<R extends Recipe<?>> {
+ 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; - private static int clampToMaxStackSize(int count, List<Holder<Item>> entries) {
@@ -155,7 +156,7 @@ public class ServerPlaceRecipe<R extends Recipe<?>> { - 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, 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 + 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); ItemStack itemStack = slot.getItem();
int i = this.inventory.findSlotMatchingCraftingIngredient(item, itemStack);
if (i == -1) { 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 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..0990bcf65f484b9a019c91fcdae1783bac6388da 100644 index 36ba57e95b84f3d598bf4be624d6c88b05a6f9a6..110456deaa662bc1c0f6ba7878bb3074869a4350 100644
--- a/src/main/java/net/minecraft/world/entity/player/Inventory.java --- a/src/main/java/net/minecraft/world/entity/player/Inventory.java
+++ b/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 { @@ -188,11 +188,11 @@ public class Inventory implements Container, Nameable {
return !stack.isDamaged() && !stack.isEnchanted() && !stack.has(DataComponents.CUSTOM_NAME); return !stack.isDamaged() && !stack.isEnchanted() && !stack.has(DataComponents.CUSTOM_NAME);
} }
- public int findSlotMatchingCraftingIngredient(Holder<Item> item) { - public int findSlotMatchingCraftingIngredient(Holder<Item> item, ItemStack stack) {
+ public int findSlotMatchingCraftingIngredient(io.papermc.paper.inventory.recipe.ItemOrExact item) { // Paper - Improve exact choice recipe ingredients + 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) { for (int i = 0; i < this.items.size(); ++i) {
ItemStack itemstack = (ItemStack) this.items.get(i); ItemStack itemstack1 = (ItemStack) this.items.get(i);
- if (!itemstack.isEmpty() && itemstack.is(item) && Inventory.isUsableForCrafting(itemstack)) { - if (!itemstack1.isEmpty() && itemstack1.is(item) && Inventory.isUsableForCrafting(itemstack1) && (stack.isEmpty() || ItemStack.isSameItemSameComponents(stack, itemstack1))) {
+ if (!itemstack.isEmpty() && item.matches(itemstack) && (!(item instanceof io.papermc.paper.inventory.recipe.ItemOrExact.Item) || isUsableForCrafting(itemstack))) { // Paper - Improve exact choice recipe ingredients + 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; 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 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 index b645962fbcebf937a477dc5c94d96be998a84105..f5d737d8a1ad10df8a74c11a6f6394ed0d2ca8be 100644
--- a/src/main/java/net/minecraft/world/entity/player/StackedContents.java --- a/src/main/java/net/minecraft/world/entity/player/StackedContents.java
+++ b/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; @@ -13,7 +13,7 @@ import java.util.List;
import javax.annotation.Nullable; import javax.annotation.Nullable;
public class StackedContents<T> { public class StackedContents<T> {
- public final Reference2IntOpenHashMap<T> amounts = new Reference2IntOpenHashMap<>(); - 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) + 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 hasAnyAmount(T input) { boolean hasAtLeast(T input, int minimum) {
return this.amounts.getInt(input) > 0; return this.amounts.getInt(input) >= minimum;
@@ -47,12 +47,13 @@ public class StackedContents<T> { @@ -49,7 +49,7 @@ public class StackedContents<T> {
this.put(input, count); List<T> getUniqueAvailableIngredientItems(Iterable<? extends StackedContents.IngredientInfo<T>> ingredients) {
} List<T> list = new ArrayList<>();
- public static record IngredientInfo<T>(List<T> allowedItems) { - for (Entry<T> entry : Reference2IntMaps.fastIterable(this.amounts)) {
- public IngredientInfo(List<T> allowedItems) { + 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)
+ public static record IngredientInfo<T>(List<T> allowedItems, boolean isExact) { // Paper - Improve exact choice recipe ingredients if (entry.getIntValue() > 0 && anyIngredientMatches(ingredients, entry.getKey())) {
+ public IngredientInfo(List<T> allowedItems, boolean isExact) { // Paper - Improve exact choice recipe ingredients list.add(entry.getKey());
if (allowedItems.isEmpty()) {
throw new IllegalArgumentException("Ingredients can't be empty");
} else {
this.allowedItems = allowedItems;
+ this.isExact = isExact; // Paper - Improve exact choice recipe ingredients
}
}
} }
@@ -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 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 index 9436fb2bfc5f4463bd958c7cbf8d700dbf819890..209ddab9e96169fe4be373b2f6f289cf06f08537 100644
--- a/src/main/java/net/minecraft/world/entity/player/StackedItemContents.java --- a/src/main/java/net/minecraft/world/entity/player/StackedItemContents.java
+++ b/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; @@ -9,9 +9,14 @@ import net.minecraft.world.item.crafting.PlacementInfo;
import net.minecraft.world.item.crafting.Recipe; import net.minecraft.world.item.crafting.Recipe;
public class StackedItemContents { public class StackedItemContents {
@ -262,7 +284,7 @@ index cc6c5cf0d1034ed074b6c354c307a0c2613f85c7..3f075c5c12f7ce331200fddb1b660238
if (Inventory.isUsableForCrafting(item)) { if (Inventory.isUsableForCrafting(item)) {
this.accountStack(item); this.accountStack(item);
} }
@@ -27,39 +32,56 @@ public class StackedItemContents { @@ -24,36 +29,53 @@ public class StackedItemContents {
public void accountStack(ItemStack item, int maxCount) { public void accountStack(ItemStack item, int maxCount) {
if (!item.isEmpty()) { if (!item.isEmpty()) {
int i = Math.min(maxCount, item.getCount()); int i = Math.min(maxCount, item.getCount());
@ -272,9 +294,7 @@ index cc6c5cf0d1034ed074b6c354c307a0c2613f85c7..3f075c5c12f7ce331200fddb1b660238
} }
} }
- public static StackedContents.IngredientInfo<Holder<Item>> convertIngredientContents(Stream<Holder<Item>> items) { - public boolean canCraft(Recipe<?> recipe, @Nullable StackedContents.Output<Holder<Item>> itemCallback) {
- List<Holder<Item>> list = items.sorted(Comparator.comparingInt(item -> BuiltInRegistries.ITEM.getId(item.value()))).toList();
- return new StackedContents.IngredientInfo<>(list);
+ // Paper start - Improve exact choice recipe ingredients + // Paper start - Improve exact choice recipe ingredients
+ public void initializeExtras(final Recipe<?> recipe, @Nullable final net.minecraft.world.item.crafting.CraftingInput input) { + public void initializeExtras(final Recipe<?> recipe, @Nullable final net.minecraft.world.item.crafting.CraftingInput input) {
+ if (this.extrasMap == null) { + if (this.extrasMap == null) {
@ -289,14 +309,8 @@ index cc6c5cf0d1034ed074b6c354c307a0c2613f85c7..3f075c5c12f7ce331200fddb1b660238
+ this.extrasMap.resetExtras(); + this.extrasMap.resetExtras();
+ } + }
+ } + }
+
+ public static StackedContents.IngredientInfo<io.papermc.paper.inventory.recipe.ItemOrExact> convertIngredientContents(Stream<io.papermc.paper.inventory.recipe.ItemOrExact> items, boolean isExact) {
+ List<io.papermc.paper.inventory.recipe.ItemOrExact> 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 + // Paper end - Improve exact choice recipe ingredients
} +
- public boolean canCraft(Recipe<?> recipe, @Nullable StackedContents.Output<Holder<Item>> itemCallback) {
+ public boolean canCraft(Recipe<?> recipe, @Nullable StackedContents.Output<io.papermc.paper.inventory.recipe.ItemOrExact> itemCallback) { // Paper - 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); return this.canCraft(recipe, 1, itemCallback);
} }
@ -304,17 +318,19 @@ index cc6c5cf0d1034ed074b6c354c307a0c2613f85c7..3f075c5c12f7ce331200fddb1b660238
- public boolean canCraft(Recipe<?> recipe, int quantity, @Nullable StackedContents.Output<Holder<Item>> 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 + 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(); PlacementInfo placementInfo = recipe.placementInfo();
return !placementInfo.isImpossibleToPlace() && this.canCraft(placementInfo.unpackedIngredients(), quantity, itemCallback); return !placementInfo.isImpossibleToPlace() && this.canCraft(placementInfo.ingredients(), quantity, itemCallback);
} }
- public boolean canCraft(List<StackedContents.IngredientInfo<Holder<Item>>> rawIngredients, @Nullable StackedContents.Output<Holder<Item>> itemCallback) { public boolean canCraft(
+ public boolean canCraft(List<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 - 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); return this.canCraft(rawIngredients, 1, itemCallback);
} }
private boolean canCraft( private boolean canCraft(
- List<StackedContents.IngredientInfo<Holder<Item>>> rawIngredients, int quantity, @Nullable StackedContents.Output<Holder<Item>> itemCallback - List<? extends StackedContents.IngredientInfo<Holder<Item>>> rawIngredients, int quantity, @Nullable StackedContents.Output<Holder<Item>> itemCallback
+ List<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 + 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); return this.raw.tryPick(rawIngredients, quantity, itemCallback);
} }
@ -326,14 +342,82 @@ index cc6c5cf0d1034ed074b6c354c307a0c2613f85c7..3f075c5c12f7ce331200fddb1b660238
- public int getBiggestCraftableStack(Recipe<?> recipe, int max, @Nullable StackedContents.Output<Holder<Item>> 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 + 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().unpackedIngredients(), max, itemCallback); 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 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 index 0bd0935e76038e07c063bbfc61fa1a6339c23eed..0b0054b3d5d56ba24e1aee0e3ab56ea5b01a82a8 100644
--- a/src/main/java/net/minecraft/world/item/crafting/Ingredient.java --- a/src/main/java/net/minecraft/world/item/crafting/Ingredient.java
+++ b/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<ItemStack> { @@ -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() { public SlotDisplay display() {
@ -345,71 +429,6 @@ index 2dce801e06687c218be3333ac9f000bae09f0caf..812f919a7a7e309c8513f44104f09249
return (SlotDisplay) this.values.unwrap().map(SlotDisplay.TagSlotDisplay::new, (list) -> { return (SlotDisplay) this.values.unwrap().map(SlotDisplay.TagSlotDisplay::new, (list) -> {
return new SlotDisplay.Composite(list.stream().map(Ingredient::displayForSingleItem).toList()); 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<Ingredient> ingredients;
- private final List<StackedContents.IngredientInfo<Holder<Item>>> unpackedIngredients;
+ private final List<StackedContents.IngredientInfo<io.papermc.paper.inventory.recipe.ItemOrExact>> unpackedIngredients; // Paper - Improve exact choice recipe ingredients
private final List<Optional<PlacementInfo.SlotInfo>> slotInfo;
private PlacementInfo(
- List<Ingredient> ingredients, List<StackedContents.IngredientInfo<Holder<Item>>> rawIngredients, List<Optional<PlacementInfo.SlotInfo>> placementSlots
+ List<Ingredient> ingredients, List<StackedContents.IngredientInfo<io.papermc.paper.inventory.recipe.ItemOrExact>> rawIngredients, List<Optional<PlacementInfo.SlotInfo>> placementSlots // Paper - Improve exact choice recipe ingredients
) {
this.ingredients = ingredients;
this.unpackedIngredients = rawIngredients;
this.slotInfo = placementSlots;
}
- public static StackedContents.IngredientInfo<Holder<Item>> ingredientToContents(Ingredient ingredient) {
- return StackedItemContents.convertIngredientContents(ingredient.items().stream());
+ // Paper start - Improve exact choice recipe ingredients
+ public static StackedContents.IngredientInfo<io.papermc.paper.inventory.recipe.ItemOrExact> 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<Holder<Item>> ingredientInfo = ingredientToContents(ingredient);
+ StackedContents.IngredientInfo<io.papermc.paper.inventory.recipe.ItemOrExact> 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<Optional<Ingredient>> ingredients) {
int i = ingredients.size();
List<Ingredient> list = new ArrayList<>(i);
- List<StackedContents.IngredientInfo<Holder<Item>>> list2 = new ArrayList<>(i);
+ List<StackedContents.IngredientInfo<io.papermc.paper.inventory.recipe.ItemOrExact>> list2 = new ArrayList<>(i); // Paper - Improve exact choice recipe ingredients
List<Optional<PlacementInfo.SlotInfo>> list3 = new ArrayList<>(i);
int j = 0;
@@ -63,7 +65,7 @@ public class PlacementInfo {
public static PlacementInfo create(List<Ingredient> ingredients) {
int i = ingredients.size();
- List<StackedContents.IngredientInfo<Holder<Item>>> list = new ArrayList<>(i);
+ List<StackedContents.IngredientInfo<io.papermc.paper.inventory.recipe.ItemOrExact>> list = new ArrayList<>(i); // Paper - Improve exact choice recipe ingredients
List<Optional<PlacementInfo.SlotInfo>> list2 = new ArrayList<>(i);
for (int j = 0; j < i; j++) {
@@ -87,7 +89,7 @@ public class PlacementInfo {
return this.ingredients;
}
- public List<StackedContents.IngredientInfo<Holder<Item>>> unpackedIngredients() {
+ public List<StackedContents.IngredientInfo<io.papermc.paper.inventory.recipe.ItemOrExact>> 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 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 index 6ec7b234b468755835107be40d0080222c0b9263..12f95bee2a69fd5df7c4a165537e01299e60c5f6 100644
--- a/src/main/java/net/minecraft/world/item/crafting/ShapelessRecipe.java --- a/src/main/java/net/minecraft/world/item/crafting/ShapelessRecipe.java