<commit_before>package sx.lambda.mstojcevich.voxel.net.packet.shared

import groovy.transform.CompileStatic
import io.netty.channel.ChannelHandlerContext
import sx.lambda.mstojcevich.voxel.net.packet.server.PacketEndChunkGroup
import sx.lambda.mstojcevich.voxel.net.packet.server.PacketEntityPosition
import sx.lambda.mstojcevich.voxel.net.packet.server.PacketKick
import sx.lambda.mstojcevich.voxel.net.packet.server.PacketStartChunkGroup
import sx.lambda.mstojcevich.voxel.VoxelGame
import sx.lambda.mstojcevich.voxel.entity.EntityPosition
import sx.lambda.mstojcevich.voxel.entity.player.Player
import sx.lambda.mstojcevich.voxel.net.packet.SharedPacket
import sx.lambda.mstojcevich.voxel.net.packet.server.PacketChunkData
import sx.lambda.mstojcevich.voxel.server.VoxelGameServer
import sx.lambda.mstojcevich.voxel.server.net.ConnectedClient
import sx.lambda.mstojcevich.voxel.world.chunk.IChunk

@CompileStatic
class PacketPlayerPosition implements SharedPacket {

    private float x, y, z

    public PacketPlayerPosition(EntityPosition pos) {
        this(pos.x, pos.y, pos.z)
    }

    public PacketPlayerPosition(float x, float y, float z) {
        this.x = x
        this.y = y
        this.z = z
    }

    @Override
    //TODO Right now we're going to trust the client, but we definitely shouldn't in the future
    void handleServerReceive(VoxelGameServer server, ChannelHandlerContext ctx) {
        ConnectedClient cc = server.getClient(ctx);
        Player p = cc.player;

        if(p != null) {
            p.getPosition().setPos(x, y, z)

            for(ConnectedClient client : server.clientList) {
                if(client.context != ctx)
                    client.context.writeAndFlush(new PacketEntityPosition(p))
            }

            if(cc.lastChunkSendPos == null) {
                sendChunks(server, ctx, p, server.config.viewDistance)
            } else {
                if (cc.info == null) {
                    if(cc.lastChunkSendPos.planeDistance(p.getPosition()) >= ((server.config.viewDistance/2f)*server.getWorld().getChunkSize()))
                        sendChunks(server, ctx, p, server.config.viewDistance)
                } else {
                    int viewDistance = Math.min(server.config.viewDistance, cc.info.viewDistance);
                    if(cc.lastChunkSendPos.planeDistance(p.getPosition()) >= ((viewDistance/2f)*server.getWorld().getChunkSize()))
                        sendChunks(server, ctx, p, viewDistance)
                }
            }

        } else {
            ctx.writeAndFlush(new PacketKick("Sent position before ready"))
            ctx.disconnect()
            server.rmClient(ctx)
        }
    }

    @Override
    void handleClientReceive(ChannelHandlerContext ctx) {
        VoxelGame.instance.getPlayer().getPosition().setPos(x, y, z)
    }

    private void sendChunks(VoxelGameServer server, ChannelHandlerContext ctx, Player p, int viewDistance) {
        //TODO only send chunks that the client doesn't already have
        ConnectedClient client = server.getClient(ctx)
        IChunk[] chunkList = server.getWorld().getChunksInRange(p.getPosition(), viewDistance)
        ctx.writeAndFlush(new PacketStartChunkGroup())
        for (IChunk c : chunkList) {
            if (!client.hadChunks.contains(c)) {
                ctx.writeAndFlush(new PacketChunkData(c))<commit_msg>Remove TODO<commit_after>package sx.lambda.mstojcevich.voxel.net.packet.shared

import groovy.transform.CompileStatic
import io.netty.channel.ChannelHandlerContext
import sx.lambda.mstojcevich.voxel.net.packet.server.PacketEndChunkGroup
import sx.lambda.mstojcevich.voxel.net.packet.server.PacketEntityPosition
import sx.lambda.mstojcevich.voxel.net.packet.server.PacketKick
import sx.lambda.mstojcevich.voxel.net.packet.server.PacketStartChunkGroup
import sx.lambda.mstojcevich.voxel.VoxelGame
import sx.lambda.mstojcevich.voxel.entity.EntityPosition
import sx.lambda.mstojcevich.voxel.entity.player.Player
import sx.lambda.mstojcevich.voxel.net.packet.SharedPacket
import sx.lambda.mstojcevich.voxel.net.packet.server.PacketChunkData
import sx.lambda.mstojcevich.voxel.server.VoxelGameServer
import sx.lambda.mstojcevich.voxel.server.net.ConnectedClient
import sx.lambda.mstojcevich.voxel.world.chunk.IChunk

@CompileStatic
class PacketPlayerPosition implements SharedPacket {

    private float x, y, z

    public PacketPlayerPosition(EntityPosition pos) {
        this(pos.x, pos.y, pos.z)
    }

    public PacketPlayerPosition(float x, float y, float z) {
        this.x = x
        this.y = y
        this.z = z
    }

    @Override
    //TODO Right now we're going to trust the client, but we definitely shouldn't in the future
    void handleServerReceive(VoxelGameServer server, ChannelHandlerContext ctx) {
        ConnectedClient cc = server.getClient(ctx);
        Player p = cc.player;

        if(p != null) {
            p.getPosition().setPos(x, y, z)

            for(ConnectedClient client : server.clientList) {
                if(client.context != ctx)
                    client.context.writeAndFlush(new PacketEntityPosition(p))
            }

            if(cc.lastChunkSendPos == null) {
                sendChunks(server, ctx, p, server.config.viewDistance)
            } else {
                if (cc.info == null) {
                    if(cc.lastChunkSendPos.planeDistance(p.getPosition()) >= ((server.config.viewDistance/2f)*server.getWorld().getChunkSize()))
                        sendChunks(server, ctx, p, server.config.viewDistance)
                } else {
                    int viewDistance = Math.min(server.config.viewDistance, cc.info.viewDistance);
                    if(cc.lastChunkSendPos.planeDistance(p.getPosition()) >= ((viewDistance/2f)*server.getWorld().getChunkSize()))
                        sendChunks(server, ctx, p, viewDistance)
                }
            }

        } else {
            ctx.writeAndFlush(new PacketKick("Sent position before ready"))
            ctx.disconnect()
            server.rmClient(ctx)
        }
    }

    @Override
    void handleClientReceive(ChannelHandlerContext ctx) {
        VoxelGame.instance.getPlayer().getPosition().setPos(x, y, z)
    }

    private void sendChunks(VoxelGameServer server, ChannelHandlerContext ctx, Player p, int viewDistance) {
        ConnectedClient client = server.getClient(ctx)
        IChunk[] chunkList = server.getWorld().getChunksInRange(p.getPosition(), viewDistance)
        ctx.writeAndFlush(new PacketStartChunkGroup())
        for (IChunk c : chunkList) {
            if (!client.hadChunks.contains(c)) {
                ctx.writeAndFlush(new PacketChunkData(c))
{'fraction_non_alphanumeric': 0.10121457489878542, 'fraction_numerical': 0.000599790073474284, 'mean_word_length': 2.789772727272727, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2897639', 'n_tokens_mistral': 2094, 'n_tokens_neox': 2018, 'n_words': 346}