Skip to content

Commit

Permalink
Rename CustomIngredient.getMatchingStacks & add CustomIngredient.toDi…
Browse files Browse the repository at this point in the history
…splay (#4152)

* Add CustomIngredient.toDisplay

* Imports

* Rename to match yarn.
  • Loading branch information
modmuss50 authored Oct 11, 2024
1 parent 90e7264 commit 6eee591
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.Ingredient;
import net.minecraft.recipe.display.SlotDisplay;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.registry.entry.RegistryEntryList;

import net.fabricmc.fabric.impl.recipe.ingredient.CustomIngredientImpl;

Expand Down Expand Up @@ -88,6 +90,20 @@ public interface CustomIngredient {
*/
CustomIngredientSerializer<?> getSerializer();

/**
* Returns a {@link SlotDisplay} representing this ingredient, this is synced to the client to display in the recipe book.
*
* @return a {@link SlotDisplay} instance.
*/
default SlotDisplay toDisplay() {
// Matches the vanilla logic in Ingredient.toDisplay()
return RegistryEntryList.of(getMatchingItems()).getStorage().map(
SlotDisplay.TagSlotDisplay::new,
(itemEntries) -> new SlotDisplay.CompositeSlotDisplay(
itemEntries.stream().map(Ingredient::createDisplayWithRemainder).toList()
));
}

/**
* {@return a new {@link Ingredient} behaving as defined by this custom ingredient}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.recipe.Ingredient;
import net.minecraft.recipe.display.SlotDisplay;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.registry.entry.RegistryEntryList;
import net.minecraft.util.Identifier;
Expand Down Expand Up @@ -104,4 +105,9 @@ public List<RegistryEntry<Item>> getMatchingItems() {
public boolean test(@Nullable ItemStack stack) {
return stack != null && customIngredient.test(stack);
}

@Override
public SlotDisplay toDisplay() {
return customIngredient.toDisplay();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.codec.PacketCodecs;
import net.minecraft.recipe.Ingredient;
import net.minecraft.recipe.display.SlotDisplay;
import net.minecraft.util.Identifier;

import net.fabricmc.fabric.api.recipe.v1.ingredient.CustomIngredient;
Expand Down Expand Up @@ -59,6 +60,13 @@ List<Ingredient> getIngredients() {
return ingredients;
}

@Override
public SlotDisplay toDisplay() {
return new SlotDisplay.CompositeSlotDisplay(
ingredients.stream().map(Ingredient::toDisplay).toList()
);
}

static class Serializer<I extends CombinedIngredient> implements CustomIngredientSerializer<I> {
private final Identifier identifier;
private final MapCodec<I> codec;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.recipe.Ingredient;
import net.minecraft.recipe.display.SlotDisplay;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.util.Identifier;

Expand Down Expand Up @@ -84,13 +85,20 @@ public boolean test(ItemStack stack) {

@Override
public List<RegistryEntry<Item>> getMatchingItems() {
return base.getMatchingItems().stream()
.filter(registryEntry -> {
ItemStack itemStack = registryEntry.value().getDefaultStack();
itemStack.applyChanges(components);
return base.test(itemStack);
})
.toList();
return base.getMatchingItems();
}

@Override
public SlotDisplay toDisplay() {
return new SlotDisplay.CompositeSlotDisplay(
base.getMatchingItems().stream().map(this::createEntryDisplay).toList()
);
}

private SlotDisplay createEntryDisplay(RegistryEntry<Item> entry) {
ItemStack stack = entry.value().getDefaultStack();
stack.applyChanges(components);
return new SlotDisplay.StackSlotDisplay(stack);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.codec.PacketCodecs;
import net.minecraft.recipe.Ingredient;
import net.minecraft.recipe.display.SlotDisplay;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.util.Identifier;

Expand Down Expand Up @@ -60,13 +61,20 @@ public boolean test(ItemStack stack) {

@Override
public List<RegistryEntry<Item>> getMatchingItems() {
return base.getMatchingItems().stream()
.filter(registryEntry -> {
ItemStack itemStack = registryEntry.value().getDefaultStack();
itemStack.apply(DataComponentTypes.CUSTOM_DATA, NbtComponent.DEFAULT, existingNbt -> NbtComponent.of(existingNbt.copyNbt().copyFrom(nbt)));
return base.test(itemStack);
})
.toList();
return base.getMatchingItems();
}

@Override
public SlotDisplay toDisplay() {
return new SlotDisplay.CompositeSlotDisplay(
base.getMatchingItems().stream().map(this::createEntryDisplay).toList()
);
}

private SlotDisplay createEntryDisplay(RegistryEntry<Item> entry) {
ItemStack stack = entry.value().getDefaultStack();
stack.apply(DataComponentTypes.CUSTOM_DATA, NbtComponent.DEFAULT, existingNbt -> NbtComponent.of(existingNbt.copyNbt().copyFrom(nbt)));
return new SlotDisplay.StackSlotDisplay(stack);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ accessWidener v2 named
extendable class net/minecraft/recipe/Ingredient
accessible method net/minecraft/recipe/Ingredient <init> (Lnet/minecraft/registry/entry/RegistryEntryList;)V
accessible field net/minecraft/recipe/Ingredient matchingItems Ljava/util/List;
accessible method net/minecraft/recipe/Ingredient createDisplayWithRemainder (Lnet/minecraft/registry/entry/RegistryEntry;)Lnet/minecraft/recipe/display/SlotDisplay;

accessible field net/minecraft/network/ClientConnection channel Lio/netty/channel/Channel;

Expand Down

0 comments on commit 6eee591

Please sign in to comment.