Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding PredicateChoice to Paper API (updated version) #12017

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
47 changes: 47 additions & 0 deletions paper-api/src/main/java/org/bukkit/inventory/RecipeChoice.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.bukkit.Tag;
import org.bukkit.material.MaterialData;
import org.jetbrains.annotations.NotNull;
import org.jspecify.annotations.NullMarked;

/**
* Represents a potential item match within a recipe. All choices within a
Expand Down Expand Up @@ -290,4 +291,50 @@ public String toString() {
}
// Paper end - check valid ingredients
}

// Paper start - add PredicateChoice
CPieter marked this conversation as resolved.
Show resolved Hide resolved
/**
* Represents a choice that will be valid only if an item matches the
* given predicate
*/
@NullMarked
public static class PredicateChoice implements RecipeChoice {
CPieter marked this conversation as resolved.
Show resolved Hide resolved

private final Predicate<ItemStack> predicate;
private final ItemStack exampleStack;

/**
* @param predicate The predicate to test the crafting inputs to. Mutating
* the ItemStack in the predicate is not supported.
* @param exampleStack An example ItemStack to be shown in the recipe book.
*/
public PredicateChoice(Predicate<ItemStack> predicate, ItemStack exampleStack) {
Preconditions.checkArgument(predicate != null, "The item predicate cannot be null");
Preconditions.checkArgument(exampleStack != null, "The example stack cannot be null");
Preconditions.checkArgument(!exampleStack.getType().isAir(), "Cannot have empty/air example stack");
CPieter marked this conversation as resolved.
Show resolved Hide resolved

this.predicate = predicate;
this.exampleStack = exampleStack;
CPieter marked this conversation as resolved.
Show resolved Hide resolved
}

public Predicate<ItemStack> getPredicate() {
return this.predicate;
}

@Override
public ItemStack getItemStack() {
return this.exampleStack.clone();
}

@Override
public PredicateChoice clone() {
return new PredicateChoice(this.predicate::test, this.exampleStack.clone());
}

@Override
public boolean test(final ItemStack itemStack) {
return this.predicate.test(itemStack);
}
}
// Paper end - add PredicateChoice
}
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ index 6bbe2e51ef71d193e0a5d3cace2b0ad1760ce759..83ccde54c625d40dc595e000c533f60a
}

diff --git a/net/minecraft/world/item/crafting/Ingredient.java b/net/minecraft/world/item/crafting/Ingredient.java
index e43641650d66a62b5b7b58c43833ce504970ab1e..879c8fe1f20decc793cfa39e686b61d521bd76ba 100644
index 78acbf3fe33bf68ba1319a44ed54c6f5c858a8e6..88fdaae0684eeef5aea6cdacccf48558d4f48bc8 100644
--- a/net/minecraft/world/item/crafting/Ingredient.java
+++ b/net/minecraft/world/item/crafting/Ingredient.java
@@ -21,7 +21,7 @@ import net.minecraft.world.item.Items;
Expand All @@ -353,8 +353,8 @@ index e43641650d66a62b5b7b58c43833ce504970ab1e..879c8fe1f20decc793cfa39e686b61d5
public static final StreamCodec<RegistryFriendlyByteBuf, Ingredient> CONTENTS_STREAM_CODEC = ByteBufCodecs.holderSet(Registries.ITEM)
.map(Ingredient::new, ingredient -> ingredient.values);
public static final StreamCodec<RegistryFriendlyByteBuf, Optional<Ingredient>> OPTIONAL_CONTENTS_STREAM_CODEC = ByteBufCodecs.holderSet(Registries.ITEM)
@@ -35,20 +35,24 @@ public final class Ingredient implements StackedContents.IngredientInfo<Holder<I
private final HolderSet<Item> values;
@@ -36,20 +36,24 @@ public final class Ingredient implements StackedContents.IngredientInfo<Holder<I
public Predicate<ItemStack> stackPredicate; // Paper - add PredicateChoice
// CraftBukkit start
@javax.annotation.Nullable
- private java.util.List<ItemStack> itemStacks;
Expand All @@ -381,8 +381,8 @@ index e43641650d66a62b5b7b58c43833ce504970ab1e..879c8fe1f20decc793cfa39e686b61d5
return recipe;
}
// CraftBukkit end
@@ -81,21 +85,22 @@ public final class Ingredient implements StackedContents.IngredientInfo<Holder<I
public boolean test(ItemStack stack) {
@@ -87,21 +91,22 @@ public final class Ingredient implements StackedContents.IngredientInfo<Holder<I
// Paper end
// CraftBukkit start
if (this.isExact()) {
- for (ItemStack itemstack1 : this.itemStacks()) {
Expand Down Expand Up @@ -413,7 +413,7 @@ index e43641650d66a62b5b7b58c43833ce504970ab1e..879c8fe1f20decc793cfa39e686b61d5
}

@Override
@@ -120,6 +125,11 @@ public final class Ingredient implements StackedContents.IngredientInfo<Holder<I
@@ -126,6 +131,11 @@ public final class Ingredient implements StackedContents.IngredientInfo<Holder<I
}

public SlotDisplay display() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
--- a/net/minecraft/world/item/crafting/Ingredient.java
+++ b/net/minecraft/world/item/crafting/Ingredient.java
@@ -33,6 +_,25 @@
@@ -33,6 +_,26 @@
public static final Codec<Ingredient> CODEC = ExtraCodecs.nonEmptyHolderSet(NON_AIR_HOLDER_SET_CODEC)
.xmap(Ingredient::new, ingredient -> ingredient.values);
private final HolderSet<Item> values;
+ public Predicate<ItemStack> stackPredicate; // Paper - add PredicateChoice
+ // CraftBukkit start
+ @javax.annotation.Nullable
+ private java.util.List<ItemStack> itemStacks;
Expand All @@ -26,10 +27,15 @@

private Ingredient(HolderSet<Item> values) {
values.unwrap().ifRight(list -> {
@@ -60,6 +_,17 @@
@@ -60,6 +_,22 @@

@Override
public boolean test(ItemStack stack) {
+ // Paper start - add PredicateChoice
+ if (this.stackPredicate != null) {
+ return this.stackPredicate.test(stack);
+ }
+ // Paper end
CPieter marked this conversation as resolved.
Show resolved Hide resolved
+ // CraftBukkit start
+ if (this.isExact()) {
+ for (ItemStack itemstack1 : this.itemStacks()) {
Expand Down
CPieter marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import com.google.common.base.Preconditions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import org.bukkit.NamespacedKey;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
Expand All @@ -32,7 +35,14 @@ static Ingredient toIngredient(RecipeChoice bukkit, boolean requireNotEmpty) {

if (bukkit == null) {
stack = Ingredient.of();
} else if (bukkit instanceof RecipeChoice.MaterialChoice) {
}
// Paper start - add PredicateChoice
CPieter marked this conversation as resolved.
Show resolved Hide resolved
else if (bukkit instanceof RecipeChoice.PredicateChoice predicateChoice) {
stack = Ingredient.ofStacks(Collections.singletonList(CraftItemStack.asNMSCopy(predicateChoice.getItemStack())));
stack.stackPredicate = nmsStack -> predicateChoice.test(CraftItemStack.asBukkitCopy(nmsStack));
}
// Paper end
else if (bukkit instanceof RecipeChoice.MaterialChoice) {
stack = Ingredient.of(((RecipeChoice.MaterialChoice) bukkit).getChoices().stream().map((mat) -> CraftItemType.bukkitToMinecraft(mat)));
} else if (bukkit instanceof RecipeChoice.ExactChoice) {
stack = Ingredient.ofStacks(((RecipeChoice.ExactChoice) bukkit).getChoices().stream().map((mat) -> CraftItemStack.asNMSCopy(mat)).toList());
Expand Down