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 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 ItemParticleOption(final ParticleType<ItemParticleOption> gu, final ItemStack ben) {
        this.type = gu;
        this.itemStack = ben;
    }
    
    @Override
    public void writeToNetwork(final FriendlyByteBuf kv) {
        kv.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> gu, final StringReader stringReader) throws CommandSyntaxException {
                stringReader.expect(' ');
                final ItemParser el4 = new ItemParser(stringReader, false).parse();
                final ItemStack ben5 = new ItemInput(el4.getItem(), el4.getNbt()).createItemStack(1, false);
                return new ItemParticleOption(gu, ben5);
            }
            
            @Override
            public ItemParticleOption fromNetwork(final ParticleType<ItemParticleOption> gu, final FriendlyByteBuf kv) {
                return new ItemParticleOption(gu, kv.readItem());
            }
        };
    }
}
