package top.r3944realms.lib39.core.sync;

import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.players.PlayerList;
import top.r3944realms.lib39.core.event.FabricCommonEventHandler;
import top.r3944realms.lib39.core.network.toClient.SyncNBTLookupDataEntityS2CPayload;

import java.util.List;

/**
 * The interface Fabric update.
 */
public interface IFabricUpdate extends IUpdate {
    default void update() {
        ServerLevel serverLevel = FabricCommonEventHandler.getServerLevel();
        if (serverLevel != null) {
            PlayerList playerList = serverLevel.getServer().getPlayerList();
            List<ServerPlayer> players = playerList.getPlayers();
            for (ServerPlayer player : players) {
                if (ServerPlayNetworking.canSend(player, SyncNBTLookupDataEntityS2CPayload.TYPE)) {
                    ServerPlayNetworking.send(player, new SyncNBTLookupDataEntityS2CPayload(getSyncData().entityId(), getSyncData()));
                }
            }
        }
    }
}
