package net.minecraft.commands.synchronization.brigadier;

import com.mojang.brigadier.arguments.ArgumentType;
import com.google.gson.JsonObject;
import net.minecraft.network.FriendlyByteBuf;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import net.minecraft.commands.synchronization.ArgumentSerializer;

public class IntegerArgumentSerializer implements ArgumentSerializer<IntegerArgumentType> {
    @Override
    public void serializeToNetwork(final IntegerArgumentType integerArgumentType, final FriendlyByteBuf kv) {
        final boolean boolean4 = integerArgumentType.getMinimum() != Integer.MIN_VALUE;
        final boolean boolean5 = integerArgumentType.getMaximum() != Integer.MAX_VALUE;
        kv.writeByte(BrigadierArgumentSerializers.createNumberFlags(boolean4, boolean5));
        if (boolean4) {
            kv.writeInt(integerArgumentType.getMinimum());
        }
        if (boolean5) {
            kv.writeInt(integerArgumentType.getMaximum());
        }
    }
    
    @Override
    public IntegerArgumentType deserializeFromNetwork(final FriendlyByteBuf kv) {
        final byte byte3 = kv.readByte();
        final int integer4 = BrigadierArgumentSerializers.numberHasMin(byte3) ? kv.readInt() : Integer.MIN_VALUE;
        final int integer5 = BrigadierArgumentSerializers.numberHasMax(byte3) ? kv.readInt() : Integer.MAX_VALUE;
        return IntegerArgumentType.integer(integer4, integer5);
    }
    
    @Override
    public void serializeToJson(final IntegerArgumentType integerArgumentType, final JsonObject jsonObject) {
        if (integerArgumentType.getMinimum() != Integer.MIN_VALUE) {
            jsonObject.addProperty("min", integerArgumentType.getMinimum());
        }
        if (integerArgumentType.getMaximum() != Integer.MAX_VALUE) {
            jsonObject.addProperty("max", integerArgumentType.getMaximum());
        }
    }
}
