package net.minecraft.tags;

import com.google.common.collect.Maps;
import java.util.Iterator;
import net.minecraft.resources.ResourceLocation;
import java.util.Map;
import net.minecraft.network.FriendlyByteBuf;
import java.util.function.Function;
import net.minecraft.core.Registry;

public class SynchronizableTagCollection<T> extends TagCollection<T> {
    private final Registry<T> registry;
    
    public SynchronizableTagCollection(final Registry<T> gb, final String string2, final String string3) {
        super(gb::getOptional, string2, false, string3);
        this.registry = gb;
    }
    
    public void serializeToNetwork(final FriendlyByteBuf kv) {
        final Map<ResourceLocation, Tag<T>> map3 = this.getAllTags();
        kv.writeVarInt(map3.size());
        for (final Map.Entry<ResourceLocation, Tag<T>> entry5 : map3.entrySet()) {
            kv.writeResourceLocation(entry5.getKey());
            kv.writeVarInt(entry5.getValue().getValues().size());
            for (final T object7 : entry5.getValue().getValues()) {
                kv.writeVarInt(this.registry.getId(object7));
            }
        }
    }
    
    public void loadFromNetwork(final FriendlyByteBuf kv) {
        final Map<ResourceLocation, Tag<T>> map3 = Maps.newHashMap();
        for (int integer4 = kv.readVarInt(), integer5 = 0; integer5 < integer4; ++integer5) {
            final ResourceLocation sm6 = kv.readResourceLocation();
            final int integer6 = kv.readVarInt();
            final Tag.Builder<T> a8 = Tag.Builder.<T>tag();
            for (int integer7 = 0; integer7 < integer6; ++integer7) {
                a8.add(this.registry.byId(kv.readVarInt()));
            }
            map3.put(sm6, a8.build(sm6));
        }
        this.replace(map3);
    }
}
