Replace forge nbt ingredient with ours, tweak base laputa shard recipe

The problem with the Forge NBT ingredient is that it strictly matches
the stack tags. If you rename an item, or add other data, it won't work.
This replacement works with any additional tags added onto the item,
and optionally can match stacks without any data at all.
The last part is used mostly as an upgrade option for people who crafted
laputa shards and got one without NBT - new ones will gain the proper
data anyways.

Closes #3234
This commit is contained in:
Hubry committed 2020-04-15 22:59:43 +02:00
1 parent 2592415358
commit 66388ac52d
25 files changed
+205 -55

No files matched your search

@@ -15,7 +15,6 @@ import net.minecraft.world.storage.loot.conditions.LootConditionManager;
import net.minecraft.world.storage.loot.functions.LootFunctionManager;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.capabilities.CapabilityManager;
import net.minecraftforge.common.crafting.CraftingHelper;
import net.minecraftforge.fml.DeferredWorkQueue;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.ModList;
@@ -59,7 +58,6 @@ import vazkii.botania.common.core.handler.ConfigHandler;
import vazkii.botania.common.core.handler.EquipmentHandler;
import vazkii.botania.common.core.handler.IMCHandler;
import vazkii.botania.common.core.handler.IMCSender;
import vazkii.botania.common.core.handler.InternalMethodHandler;
import vazkii.botania.common.core.handler.ManaNetworkHandler;
import vazkii.botania.common.core.helper.ColorHelper;
import vazkii.botania.common.core.loot.BindUuid;
@@ -68,7 +66,6 @@ import vazkii.botania.common.core.loot.LootHandler;
import vazkii.botania.common.core.loot.TrueGuardianKiller;
import vazkii.botania.common.core.proxy.IProxy;
import vazkii.botania.common.core.proxy.ServerProxy;
import vazkii.botania.common.crafting.FluxfieldCondition;
import vazkii.botania.common.impl.corporea.CorporeaItemStackMatcher;
import vazkii.botania.common.impl.corporea.CorporeaStringMatcher;
import vazkii.botania.common.lib.LibMisc;
@@ -223,8 +220,6 @@ public class Botania {
CriteriaTriggers.register(LokiPlaceTrigger.INSTANCE);
CriteriaTriggers.register(AlfPortalBreadTrigger.INSTANCE);
CraftingHelper.register(FluxfieldCondition.SERIALIZER);
ModBlocks.addDispenserBehaviours();
ModFeatures.addWorldgen();
@@ -0,0 +1,131 @@
/*
* This class is distributed as part of the Botania Mod.
* Get the Source Code in github:
* https://github.com/Vazkii/Botania
*
* Botania is Open Source and distributed under the
* Botania License: http://botaniamod.net/license.php
*/
package vazkii.botania.common.crafting;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.INBT;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.JSONUtils;
import net.minecraftforge.common.crafting.CraftingHelper;
import net.minecraftforge.common.crafting.IIngredientSerializer;
import vazkii.botania.common.core.helper.ItemNBTHelper;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.stream.Stream;
/**
* NBT ingredient that accepts items containing the same NBT data as the provided template stack.
* In addition, an option to accept items without any NBT data is provided.
*
* @see ItemNBTHelper#matchTag(INBT, INBT)
*/
public class FuzzyNBTIngredient extends Ingredient {
public static final IIngredientSerializer<FuzzyNBTIngredient> SERIALIZER = new Serializer();
private static final String ACCEPTS_EMPTY_TAG = "accepts_empty_tag";
private final ItemStack stack;
private final boolean acceptsEmptyTag;
/**
* Constructs the ingredient.
*
* @param stack Template stack for this ingredient
* @param acceptsEmptyTag Whether lack of NBT data is accepted by this ingredient in addition to the
* stack's data.
* @throws IllegalArgumentException if the stack has no NBT data.
*/
public FuzzyNBTIngredient(ItemStack stack, boolean acceptsEmptyTag) {
super(Stream.of(new Ingredient.SingleItemList(stack)));
this.stack = stack;
this.acceptsEmptyTag = acceptsEmptyTag;
if (!stack.hasTag()) {
throw new IllegalArgumentException("This ingredient type requires a stack with NBT!");
}
}
/**
* Constructs the ingredient.
*
* @param stack Template stack for this ingredient
* @throws IllegalArgumentException if the stack has no NBT data.
*/
public FuzzyNBTIngredient(ItemStack stack) {
this(stack, false);
}
@Override
public boolean test(@Nullable ItemStack input) {
if (input == null || stack.getItem() != input.getItem()) {
return false;
}
CompoundNBT tag = input.getTag();
if (acceptsEmptyTag && (tag == null || tag.isEmpty())) {
return true;
}
return ItemNBTHelper.matchTag(stack.getTag(), tag);
}
@Override
public boolean isSimple() {
return false;
}
@Nonnull
@Override
public JsonElement serialize() {
JsonObject json = new JsonObject();
json.addProperty("type", CraftingHelper.getID(SERIALIZER).toString());
json.addProperty("item", stack.getItem().getRegistryName().toString());
json.addProperty("count", stack.getCount());
json.addProperty("nbt", stack.getTag().toString());
json.addProperty(ACCEPTS_EMPTY_TAG, acceptsEmptyTag);
return json;
}
@Nonnull
@Override
public ItemStack[] getMatchingStacks() {
return new ItemStack[] { stack };
}
@Nonnull
@Override
public IIngredientSerializer<? extends Ingredient> getSerializer() {
return SERIALIZER;
}
private static class Serializer implements IIngredientSerializer<FuzzyNBTIngredient> {
@Nonnull
@Override
public FuzzyNBTIngredient parse(@Nonnull PacketBuffer buffer) {
return new FuzzyNBTIngredient(buffer.readItemStack(), buffer.readBoolean());
}
@Nonnull
@Override
public FuzzyNBTIngredient parse(@Nonnull JsonObject json) {
boolean acceptsEmptyTag = JSONUtils.getBoolean(json, ACCEPTS_EMPTY_TAG, false);
return new FuzzyNBTIngredient(CraftingHelper.getItemStack(json, true), acceptsEmptyTag);
}
@Override
public void write(@Nonnull PacketBuffer buffer, @Nonnull FuzzyNBTIngredient ingredient) {
buffer.writeItemStack(ingredient.stack);
buffer.writeBoolean(ingredient.acceptsEmptyTag);
}
}
}
@@ -19,6 +19,7 @@ import net.minecraft.item.crafting.IRecipeSerializer;
import net.minecraft.potion.EffectInstance;
import net.minecraft.potion.Effects;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.common.crafting.CraftingHelper;
import net.minecraftforge.common.extensions.IForgeContainerType;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
@@ -38,6 +39,8 @@ import vazkii.botania.client.gui.box.GuiBaubleBox;
import vazkii.botania.common.block.ModBlocks;
import vazkii.botania.common.core.BotaniaCreativeTab;
import vazkii.botania.common.core.handler.ModSounds;
import vazkii.botania.common.crafting.FluxfieldCondition;
import vazkii.botania.common.crafting.FuzzyNBTIngredient;
import vazkii.botania.common.crafting.recipe.*;
import vazkii.botania.common.item.brew.ItemBrewBase;
import vazkii.botania.common.item.brew.ItemIncenseStick;
@@ -110,6 +113,7 @@ import vazkii.botania.common.lib.LibMisc;
import java.util.Locale;
import static vazkii.botania.common.block.ModBlocks.register;
import static vazkii.botania.common.lib.ResourceLocationHelper.prefix;
@Mod.EventBusSubscriber(modid = LibMisc.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD)
@ObjectHolder(LibMisc.MOD_ID)
@@ -725,6 +729,9 @@ public final class ModItems {
register(r, SpellClothRecipe.SERIALIZER, "spell_cloth_apply");
register(r, TerraPickTippingRecipe.SERIALIZER, "terra_pick_tipping");
register(r, TwigWandRecipe.SERIALIZER, "twig_wand");
CraftingHelper.register(FluxfieldCondition.SERIALIZER);
CraftingHelper.register(prefix("fuzzy_nbt"), FuzzyNBTIngredient.SERIALIZER);
}
@SubscribeEvent
@@ -47,6 +47,7 @@ import vazkii.botania.common.block.ModBlocks;
import vazkii.botania.common.block.ModFluffBlocks;
import vazkii.botania.common.block.ModSubtiles;
import vazkii.botania.common.crafting.FluxfieldCondition;
import vazkii.botania.common.crafting.FuzzyNBTIngredient;
import vazkii.botania.common.crafting.recipe.*;
import vazkii.botania.common.item.ModItems;
import vazkii.botania.common.lib.LibBlockNames;
@@ -1258,7 +1259,7 @@ public class RecipeProvider extends net.minecraft.data.RecipeProvider {
.build(consumer);
ShapedRecipeBuilder.shapedRecipe(ModItems.waterRod)
.key('B', nbtIngredient(PotionUtils.addPotionToItemStack(new ItemStack(Items.POTION), Potions.WATER)))
.key('B', new FuzzyNBTIngredient(PotionUtils.addPotionToItemStack(new ItemStack(Items.POTION), Potions.WATER)))
.key('R', ModTags.Items.RUNES_WATER)
.key('T', ModItems.livingwoodTwig)
.patternLine(" B")
@@ -1369,14 +1370,15 @@ public class RecipeProvider extends net.minecraft.data.RecipeProvider {
.patternLine("PDP")
.patternLine("ASE")
.addCriterion("has_item", hasItem(ModItems.lifeEssence))
.build(consumer, "botania:laputa_shard_0");
.build(WrapperResult.transformJson(consumer, json -> json.getAsJsonObject("result").addProperty("nbt", "{level:0}")),
"botania:laputa_shard_0");
for (int i = 1; i < 20; i++) {
final int outputLevel = i;
ItemStack stack = new ItemStack(ModItems.laputaShard);
stack.getOrCreateTag().putInt("level", i - 1);
ShapelessRecipeBuilder.shapelessRecipe(ModItems.laputaShard)
.addIngredient(ModItems.lifeEssence)
.addIngredient(nbtIngredient(stack))
.addIngredient(new FuzzyNBTIngredient(stack, i == 1))
.setGroup("botania:laputa_shard_upgrade")
.addCriterion("has_item", hasItem(ModItems.laputaShard))
.build(WrapperResult.transformJson(consumer, json -> json.getAsJsonObject("result").addProperty("nbt", "{level:" + outputLevel + "}")
@@ -2658,12 +2660,6 @@ public class RecipeProvider extends net.minecraft.data.RecipeProvider {
CustomRecipeBuilder.func_218656_a(serializer).build(consumer, prefix("dynamic/" + serializer.getRegistryName().getPath()).toString());
}
// Blame Forge for making the constructor protected.
// TODO maybe use a bit fuzzier ingredient, it will break if any mod throws random NBT data at the item
private static NBTIngredient nbtIngredient(ItemStack stack) {
return new NBTIngredient(stack) {};
}
@Override
public String getName() {
return "Botania crafting recipes";