-
-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a BlockTagIngredient for when an item tag cannot be used/doesn't …
…exist (#976)
- Loading branch information
Showing
5 changed files
with
195 additions
and
0 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
111 changes: 111 additions & 0 deletions
111
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,111 @@ | ||
/* | ||
* Copyright (c) NeoForged and contributors | ||
* SPDX-License-Identifier: LGPL-2.1-only | ||
*/ | ||
|
||
package net.neoforged.neoforge.common.crafting; | ||
|
||
import com.mojang.serialization.MapCodec; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
import net.minecraft.core.Holder; | ||
import net.minecraft.core.component.DataComponents; | ||
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; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
/** | ||
* {@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 implements ICustomIngredient { | ||
public static final MapCodec<BlockTagIngredient> CODEC = TagKey.codec(Registries.BLOCK) | ||
.xmap(BlockTagIngredient::new, BlockTagIngredient::getTag).fieldOf("tag"); | ||
|
||
protected final TagKey<Block> tag; | ||
|
||
@Nullable | ||
protected ItemStack[] itemStacks; | ||
|
||
public BlockTagIngredient(TagKey<Block> tag) { | ||
this.tag = tag; | ||
} | ||
|
||
protected void dissolve() { | ||
if (itemStacks == null) { | ||
List<ItemStack> list = new ArrayList<>(); | ||
for (Holder<Block> block : BuiltInRegistries.BLOCK.getTagOrEmpty(tag)) { | ||
ItemStack stack = new ItemStack(block.value()); | ||
if (!stack.isEmpty()) { | ||
list.add(stack); | ||
} | ||
} | ||
|
||
if (list.isEmpty()) { | ||
ItemStack itemStack = new ItemStack(Blocks.BARRIER); | ||
itemStack.set(DataComponents.CUSTOM_NAME, Component.literal("Empty Tag: " + this.tag.location())); | ||
list.add(itemStack); | ||
} | ||
|
||
itemStacks = list.toArray(ItemStack[]::new); | ||
} | ||
} | ||
|
||
@Override | ||
public Stream<ItemStack> getItems() { | ||
dissolve(); | ||
return Stream.of(itemStacks); | ||
} | ||
|
||
@Override | ||
public boolean test(@Nullable ItemStack stack) { | ||
if (stack == null) | ||
return false; | ||
|
||
dissolve(); | ||
for (ItemStack itemStack : itemStacks) { | ||
if (itemStack.is(stack.getItem())) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public TagKey<Block> getTag() { | ||
return tag; | ||
} | ||
|
||
@Override | ||
public boolean isSimple() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public IngredientType<?> getType() { | ||
return NeoForgeMod.BLOCK_TAG_INGREDIENT.get(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (!(o instanceof BlockTagIngredient that)) return false; | ||
return tag.equals(that.tag); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return tag.hashCode(); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...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,32 @@ | ||
{ | ||
"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" | ||
] | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
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,17 @@ | ||
{ | ||
"type": "minecraft:crafting_shapeless", | ||
"category": "misc", | ||
"ingredients": [ | ||
{ | ||
"type": "neoforge:block_tag", | ||
"tag": "minecraft:convertable_to_mud" | ||
}, | ||
{ | ||
"item": "minecraft:water_bucket" | ||
} | ||
], | ||
"result": { | ||
"count": 1, | ||
"id": "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