ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/crafting/TargetNBTIngredient.java

76 lines
2.5 KiB
Java
Raw Normal View History

2021-10-12 23:19:47 +02:00
package de.ellpeck.actuallyadditions.mod.crafting;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.network.PacketBuffer;
import net.minecraft.tags.ITag;
import net.minecraft.util.IItemProvider;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.crafting.IIngredientSerializer;
2021-11-13 18:02:42 +01:00
import javax.annotation.Nonnull;
2021-10-12 23:19:47 +02:00
import java.util.stream.Stream;
public class TargetNBTIngredient extends Ingredient {
public TargetNBTIngredient(Stream<? extends IItemList> itemLists) {
super(itemLists);
}
@Override
2021-11-13 18:02:42 +01:00
@Nonnull
2021-10-12 23:19:47 +02:00
public IIngredientSerializer<? extends Ingredient> getSerializer() {
return SERIALIZER;
}
public static TargetNBTIngredient of(IItemProvider itemProvider) {
return new TargetNBTIngredient(Stream.of(new SingleItemList(new ItemStack(itemProvider))));
}
public static TargetNBTIngredient of(ItemStack itemStack) {
return new TargetNBTIngredient(Stream.of(new SingleItemList(itemStack)));
}
2021-11-13 18:02:42 +01:00
@Nonnull
public static TargetNBTIngredient of(@Nonnull ITag tag) {
2021-10-12 23:19:47 +02:00
return new TargetNBTIngredient(Stream.of(new TagList(tag)));
}
@Override
2021-11-13 18:02:42 +01:00
@Nonnull
2021-10-12 23:19:47 +02:00
public JsonElement toJson() {
JsonObject tmp = super.toJson().getAsJsonObject();
tmp.addProperty("type", Serializer.NAME.toString());
return tmp;
}
public static Serializer SERIALIZER = new Serializer();
public static class Serializer implements IIngredientSerializer<TargetNBTIngredient> {
public static ResourceLocation NAME = new ResourceLocation(ActuallyAdditions.MODID, "nbt_target");
@Override
2021-11-13 18:02:42 +01:00
@Nonnull
2021-10-12 23:19:47 +02:00
public TargetNBTIngredient parse(PacketBuffer buffer) {
return new TargetNBTIngredient(Stream.generate(() -> new SingleItemList(buffer.readItem())).limit(buffer.readVarInt()));
}
@Override
2021-11-13 18:02:42 +01:00
@Nonnull
public TargetNBTIngredient parse(@Nonnull JsonObject json) {
2021-10-12 23:19:47 +02:00
return new TargetNBTIngredient(Stream.of(Ingredient.valueFromJson(json)));
}
@Override
public void write(PacketBuffer buffer, TargetNBTIngredient ingredient) {
ItemStack[] items = ingredient.getItems();
buffer.writeVarInt(items.length);
for (ItemStack stack : items)
buffer.writeItem(stack);
}
}
}