package net.minecraft.core.particles;

import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.commands.arguments.item.ItemParser;
import com.mojang.brigadier.StringReader;
import net.minecraft.commands.arguments.item.ItemInput;
import net.minecraft.core.Registry;
import net.minecraft.network.FriendlyByteBuf;
import com.mojang.serialization.Codec;
import net.minecraft.world.item.ItemStack;

public class ItemParticleOption implements ParticleOptions {
    public static final Deserializer<ItemParticleOption> DESERIALIZER;
    private final ParticleType<ItemParticleOption> type;
    private final ItemStack itemStack;
    
    public static Codec<ItemParticleOption> codec(final ParticleType<ItemParticleOption> hg) {
        return (Codec<ItemParticleOption>)ItemStack.CODEC.xmap(bki -> new ItemParticleOption(hg, bki), he -> he.itemStack);
    }
    
    public ItemParticleOption(final ParticleType<ItemParticleOption> hg, final ItemStack bki) {
        this.type = hg;
        this.itemStack = bki;
    }
    
    @Override
    public void writeToNetwork(final FriendlyByteBuf mg) {
        mg.writeItem(this.itemStack);
    }
    
    @Override
    public String writeToString() {
        return Registry.PARTICLE_TYPE.getKey(this.getType()) + " " + new ItemInput(this.itemStack.getItem(), this.itemStack.getTag()).serialize();
    }
    
    @Override
    public ParticleType<ItemParticleOption> getType() {
        return this.type;
    }
    
    public ItemStack getItem() {
        return this.itemStack;
    }
    
    static {
        DESERIALIZER = new Deserializer<ItemParticleOption>() {
            @Override
            public ItemParticleOption fromCommand(final ParticleType<ItemParticleOption> hg, final StringReader stringReader) throws CommandSyntaxException {
                stringReader.expect(' ');
                final ItemParser ev4 = new ItemParser(stringReader, false).parse();
                final ItemStack bki5 = new ItemInput(ev4.getItem(), ev4.getNbt()).createItemStack(1, false);
                return new ItemParticleOption(hg, bki5);
            }
            
            @Override
            public ItemParticleOption fromNetwork(final ParticleType<ItemParticleOption> hg, final FriendlyByteBuf mg) {
                return new ItemParticleOption(hg, mg.readItem());
            }
        };
    }
}
