-
-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[1.20.4] BlockTagIngredient Backport (#989)
- Loading branch information
Showing
5 changed files
with
190 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/main/java/net/neoforged/neoforge/common/crafting/BlockTagIngredient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Copyright (c) NeoForged and contributors | ||
* SPDX-License-Identifier: LGPL-2.1-only | ||
*/ | ||
|
||
package net.neoforged.neoforge.common.crafting; | ||
|
||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
import javax.annotation.Nullable; | ||
import net.minecraft.core.Holder; | ||
import net.minecraft.core.registries.BuiltInRegistries; | ||
import net.minecraft.core.registries.Registries; | ||
import net.minecraft.network.chat.Component; | ||
import net.minecraft.tags.TagKey; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraft.world.item.crafting.Ingredient; | ||
import net.minecraft.world.level.block.Block; | ||
import net.minecraft.world.level.block.Blocks; | ||
import net.neoforged.neoforge.common.NeoForgeMod; | ||
|
||
/** | ||
* {@link Ingredient} that matches {@link ItemStack}s of {@link Block}s from a {@link TagKey<Block>}, useful in cases | ||
* like {@code "minecraft:convertable_to_mud"} where there isn't an accompanying item tag | ||
* <p> | ||
* Notice: This should not be used as a replacement for the normal {@link Ingredient#of(TagKey)}, | ||
* This should only be used when there is no way an item tag can be used in your use case | ||
*/ | ||
public class BlockTagIngredient extends Ingredient { | ||
public static final Codec<BlockTagIngredient> CODEC = RecordCodecBuilder.create(i -> i | ||
.group(TagKey.codec(Registries.BLOCK).fieldOf("tag").forGetter(BlockTagIngredient::getTag)) | ||
.apply(i, BlockTagIngredient::new)); | ||
|
||
protected final TagKey<Block> tag; | ||
@Nullable | ||
protected ItemStack[] itemStacks; | ||
|
||
public BlockTagIngredient(TagKey<Block> tag) { | ||
super(Stream.of(new BlockTagValue(tag)), NeoForgeMod.BLOCK_TAG_INGREDIENT); | ||
this.tag = tag; | ||
} | ||
|
||
@Override | ||
public ItemStack[] getItems() { | ||
List<ItemStack> list = new ArrayList<>(); | ||
|
||
for (Value value : values) { | ||
list.addAll(value.getItems()); | ||
} | ||
|
||
return itemStacks = list.toArray(ItemStack[]::new); | ||
} | ||
|
||
@Override | ||
public boolean test(@Nullable ItemStack stack) { | ||
if (stack == null) { | ||
return false; | ||
} | ||
|
||
this.getItems(); | ||
for (ItemStack itemStack : itemStacks) { | ||
if (itemStack.is(stack.getItem())) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public TagKey<Block> getTag() { | ||
return tag; | ||
} | ||
|
||
@Override | ||
public IngredientType<?> getType() { | ||
return NeoForgeMod.BLOCK_TAG_INGREDIENT.get(); | ||
} | ||
|
||
public record BlockTagValue(TagKey<Block> tag) implements Ingredient.Value { | ||
@Override | ||
public Collection<ItemStack> getItems() { | ||
List<ItemStack> list = new ArrayList<>(); | ||
|
||
for (Holder<Block> holder : BuiltInRegistries.BLOCK.getTagOrEmpty(this.tag)) { | ||
ItemStack stack = new ItemStack(holder.value()); | ||
if (!stack.isEmpty()) { | ||
list.add(stack); | ||
} | ||
} | ||
|
||
if (list.isEmpty()) | ||
list.add(new ItemStack(Blocks.BARRIER).setHoverName(Component.literal("Empty Tag: " + this.tag.location()))); | ||
|
||
return list; | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...ted/resources/data/neotests_block_tag_ingredient/advancements/recipes/misc/block_tag.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"parent": "minecraft:recipes/root", | ||
"criteria": { | ||
"has_item": { | ||
"conditions": { | ||
"items": [ | ||
{ | ||
"items": [ | ||
"minecraft:water_bucket" | ||
] | ||
} | ||
] | ||
}, | ||
"trigger": "minecraft:inventory_changed" | ||
}, | ||
"has_the_recipe": { | ||
"conditions": { | ||
"recipe": "neotests_block_tag_ingredient:block_tag" | ||
}, | ||
"trigger": "minecraft:recipe_unlocked" | ||
} | ||
}, | ||
"requirements": [ | ||
[ | ||
"has_the_recipe", | ||
"has_item" | ||
] | ||
], | ||
"rewards": { | ||
"recipes": [ | ||
"neotests_block_tag_ingredient:block_tag" | ||
] | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
tests/src/generated/resources/data/neotests_block_tag_ingredient/recipes/block_tag.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"type": "minecraft:crafting_shapeless", | ||
"category": "misc", | ||
"ingredients": [ | ||
{ | ||
"type": "neoforge:block_tag", | ||
"tag": "minecraft:convertable_to_mud" | ||
}, | ||
{ | ||
"item": "minecraft:water_bucket" | ||
} | ||
], | ||
"result": { | ||
"item": "minecraft:mud" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters