package net.minecraft.tags;

import java.util.Set;
import com.google.common.collect.ImmutableSet;
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> gl, final String string2, final String string3) {
        super(gl::getOptional, string2, string3);
        this.registry = gl;
    }
    
    public void serializeToNetwork(final FriendlyByteBuf mg) {
        final Map<ResourceLocation, Tag<T>> map3 = this.getAllTags();
        mg.writeVarInt(map3.size());
        for (final Map.Entry<ResourceLocation, Tag<T>> entry5 : map3.entrySet()) {
            mg.writeResourceLocation(entry5.getKey());
            mg.writeVarInt(entry5.getValue().getValues().size());
            for (final T object7 : entry5.getValue().getValues()) {
                mg.writeVarInt(this.registry.getId(object7));
            }
        }
    }
    
    public void loadFromNetwork(final FriendlyByteBuf mg) {
        final Map<ResourceLocation, Tag<T>> map3 = Maps.newHashMap();
        for (int integer4 = mg.readVarInt(), integer5 = 0; integer5 < integer4; ++integer5) {
            final ResourceLocation uh6 = mg.readResourceLocation();
            final int integer6 = mg.readVarInt();
            final ImmutableSet.Builder<T> builder8 = ImmutableSet.<T>builder();
            for (int integer7 = 0; integer7 < integer6; ++integer7) {
                builder8.add(this.registry.byId(mg.readVarInt()));
            }
            map3.put(uh6, Tag.<T>fromSet(builder8.build()));
        }
        this.replace(map3);
    }
}
