Skip to content

Commit

Permalink
re-add partialnbt
Browse files Browse the repository at this point in the history
needs testing
  • Loading branch information
jaredlll08 committed Apr 22, 2021
1 parent 4aaca4f commit 4b76027
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/main/java/com/blamejared/crafttweaker/CraftTweaker.java
Expand Up @@ -3,6 +3,7 @@
import com.blamejared.crafttweaker.api.CraftTweakerAPI;
import com.blamejared.crafttweaker.api.CraftTweakerRegistry;
import com.blamejared.crafttweaker.api.ScriptLoadingOptions;
import com.blamejared.crafttweaker.api.ingredients.PartialNBTIngredient;
import com.blamejared.crafttweaker.api.item.IngredientList;
import com.blamejared.crafttweaker.api.logger.LogLevel;
import com.blamejared.crafttweaker.api.managers.IRecipeManager;
Expand Down Expand Up @@ -43,6 +44,7 @@
import net.minecraftforge.client.event.RecipesUpdatedEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.crafting.CraftingHelper;
import net.minecraftforge.common.crafting.IIngredientSerializer;
import net.minecraftforge.event.AddReloadListenerEvent;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.event.RegistryEvent;
Expand Down Expand Up @@ -91,6 +93,7 @@ public class CraftTweaker {
public static IRecipeSerializer SHAPED_SERIALIZER;
@SuppressWarnings("rawtypes")
public static IRecipeSerializer SCRIPT_SERIALIZER;
public static IIngredientSerializer<?> PARTIAL_NBT_INGREDIENT_SERIALIZER;
public static IRecipeType<ScriptRecipe> RECIPE_TYPE_SCRIPTS;
public static boolean serverOverride = true;
private static Set<String> PATRON_LIST = new HashSet<>();
Expand Down Expand Up @@ -124,7 +127,9 @@ public CraftTweaker() {
ForgeRegistries.RECIPE_SERIALIZERS.register(SCRIPT_SERIALIZER);

RECIPE_TYPE_SCRIPTS = IRecipeType.register(MODID + ":scripts");

PARTIAL_NBT_INGREDIENT_SERIALIZER = new PartialNBTIngredient.Serializer();
CraftingHelper.register(new ResourceLocation(MODID, "partial_nbt"), PARTIAL_NBT_INGREDIENT_SERIALIZER);

CraftingHelper.register(new ResourceLocation(MODID, "list"), IngredientList.Serializer.INSTANCE);

CraftTweakerRegistries.init();
Expand Down
@@ -0,0 +1,102 @@
package com.blamejared.crafttweaker.api.ingredients;

import com.blamejared.crafttweaker.api.data.NBTConverter;
import com.blamejared.crafttweaker.impl.data.MapData;
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.network.PacketBuffer;
import net.minecraftforge.common.crafting.CraftingHelper;
import net.minecraftforge.common.crafting.IIngredientSerializer;
import net.minecraftforge.common.crafting.NBTIngredient;

import javax.annotation.Nullable;
import java.util.stream.Stream;

public class PartialNBTIngredient extends Ingredient {

private final ItemStack stack;

public PartialNBTIngredient(ItemStack stack) {

super(Stream.of(new Ingredient.SingleItemList(stack)));
this.stack = stack;
}

@Override
public boolean test(@Nullable ItemStack input) {

if(input == null) {
return false;
}
CompoundNBT stack1Tag = this.stack.getTag();
CompoundNBT stack2Tag = input.getTag();
if(stack1Tag == null && stack2Tag == null) {
return true;
}
// Lets just use the partial nbt
MapData stack2Data = (MapData) NBTConverter.convert(stack2Tag);
MapData stack1Data = (MapData) NBTConverter.convert(stack1Tag);
boolean contains;
if(stack1Data == null) {
contains = true;
} else {
contains = stack2Data != null && stack2Data.contains(stack1Data);
}


return this.stack.getItem() == input.getItem() && this.stack.getDamage() == input.getDamage() && contains;
}

@Override
public boolean isSimple() {

return false;
}

@Override
public IIngredientSerializer<? extends Ingredient> getSerializer() {

return PartialNBTIngredient.Serializer.INSTANCE;
}

@Override
public JsonElement serialize() {

JsonObject json = new JsonObject();
json.addProperty("type", CraftingHelper.getID(NBTIngredient.Serializer.INSTANCE).toString());
json.addProperty("item", stack.getItem().getRegistryName().toString());
json.addProperty("count", stack.getCount());
if(stack.hasTag()) {
json.addProperty("nbt", stack.getTag().toString());
}
return json;
}

public static class Serializer implements IIngredientSerializer<PartialNBTIngredient> {

public static final PartialNBTIngredient.Serializer INSTANCE = new PartialNBTIngredient.Serializer();

@Override
public PartialNBTIngredient parse(PacketBuffer buffer) {

return new PartialNBTIngredient(buffer.readItemStack());
}

@Override
public PartialNBTIngredient parse(JsonObject json) {

return new PartialNBTIngredient(CraftingHelper.getItemStack(json, true));
}

@Override
public void write(PacketBuffer buffer, PartialNBTIngredient ingredient) {

buffer.writeItemStack(ingredient.stack);
}

}

}
Expand Up @@ -3,6 +3,7 @@
import com.blamejared.crafttweaker.api.CraftTweakerAPI;
import com.blamejared.crafttweaker.api.data.IData;
import com.blamejared.crafttweaker.api.data.NBTConverter;
import com.blamejared.crafttweaker.api.ingredients.PartialNBTIngredient;
import com.blamejared.crafttweaker.api.item.IItemStack;
import com.blamejared.crafttweaker.impl.actions.items.ActionSetFood;
import com.blamejared.crafttweaker.impl.data.MapData;
Expand Down Expand Up @@ -165,7 +166,7 @@ public Ingredient asVanillaIngredient() {
if(!getInternal().hasTag()) {
return Ingredient.fromStacks(getImmutableInternal());
}
return new NBTIngredient(getImmutableInternal()) {};
return new PartialNBTIngredient(getImmutableInternal());
}

@Override
Expand Down
Expand Up @@ -4,6 +4,7 @@
import com.blamejared.crafttweaker.api.annotations.ZenRegister;
import com.blamejared.crafttweaker.api.data.IData;
import com.blamejared.crafttweaker.api.data.NBTConverter;
import com.blamejared.crafttweaker.api.ingredients.PartialNBTIngredient;
import com.blamejared.crafttweaker.api.item.IItemStack;
import com.blamejared.crafttweaker.impl.actions.items.ActionSetFood;
import com.blamejared.crafttweaker.impl.data.MapData;
Expand Down Expand Up @@ -172,7 +173,7 @@ public Ingredient asVanillaIngredient() {
if(!getInternal().hasTag()) {
return Ingredient.fromStacks(getImmutableInternal());
}
return new NBTIngredient(getImmutableInternal()) {};
return new PartialNBTIngredient(getImmutableInternal());
}

@Override
Expand Down

0 comments on commit 4b76027

Please sign in to comment.