package net.minecraft.core.particles;

import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.StringReader;
import net.minecraft.commands.arguments.blocks.BlockStateParser;
import net.minecraft.core.Registry;
import net.minecraft.world.level.block.Block;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.level.block.state.BlockState;

public class BlockParticleOption implements ParticleOptions {
    public static final Deserializer<BlockParticleOption> DESERIALIZER;
    private final ParticleType<BlockParticleOption> type;
    private final BlockState state;
    
    public BlockParticleOption(final ParticleType<BlockParticleOption> gu, final BlockState byj) {
        this.type = gu;
        this.state = byj;
    }
    
    @Override
    public void writeToNetwork(final FriendlyByteBuf kv) {
        kv.writeVarInt(Block.BLOCK_STATE_REGISTRY.getId(this.state));
    }
    
    @Override
    public String writeToString() {
        return Registry.PARTICLE_TYPE.getKey(this.getType()) + " " + BlockStateParser.serialize(this.state);
    }
    
    @Override
    public ParticleType<BlockParticleOption> getType() {
        return this.type;
    }
    
    public BlockState getState() {
        return this.state;
    }
    
    static {
        DESERIALIZER = new Deserializer<BlockParticleOption>() {
            @Override
            public BlockParticleOption fromCommand(final ParticleType<BlockParticleOption> gu, final StringReader stringReader) throws CommandSyntaxException {
                stringReader.expect(' ');
                return new BlockParticleOption(gu, new BlockStateParser(stringReader, false).parse(false).getState());
            }
            
            @Override
            public BlockParticleOption fromNetwork(final ParticleType<BlockParticleOption> gu, final FriendlyByteBuf kv) {
                return new BlockParticleOption(gu, Block.BLOCK_STATE_REGISTRY.byId(kv.readVarInt()));
            }
        };
    }
}
