package cubicchunks.util;

import cubicchunks.network.AbstractClientMessageHandler;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.network.ThreadQuickExitException;
import net.minecraft.util.IThreadListener;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

public class PacketUtils {

    private static final int MASK_6 = (1 << 6) - 1;
    private static final int MASK_7 = (1 << 7) - 1;

    public static void write(ByteBuf buf, BlockPos pos) {
        writeSignedVarInt(buf, pos.getX());
        writeSignedVarInt(buf, pos.getY());
        writeSignedVarInt(buf, pos.getZ());
    }

    public static BlockPos readBlockPos(ByteBuf buf) {
        return new BlockPos(readSignedVarInt(buf), readSignedVarInt(buf), readSignedVarInt(buf));
    }

    public static void write(ByteBuf buf, CubePos pos) {
        writeSignedVarInt(buf, pos.getX());
        writeSignedVarInt(buf, pos.getY());
        writeSignedVarInt(buf, pos.getZ());
    }

    public static CubePos readCubePos(ByteBuf buf) {
        return new CubePos(readSignedVarInt(buf), readSignedVarInt(buf), readSignedVarInt(buf));
    }

    /**
     * Writes signed int with variable length encoding, using at most 5 bytes.
     *
     * Unlike vanilla/forge one, this ensures that value and ~value are written using the same amount of bytes
     */
    public static void writeSignedVarInt(ByteBuf buf, int i) {
        int signBit = (i >>> 31) << 6;
        int val = i < 0 ? ~i : i;
        assert val >= 0;

        writeVarIntByte(buf, (val & MASK_6) | signBit, (val >>= 6) > 0);
        while (val > 0) {
            writeVarIntByte(buf, (val & MASK_7), (val >>= 7) > 0);
        }
    }

    /**
     * Reads signed int with variable length encoding, using at most 5 bytes.
     *
     * @see PacketUtils#writeSignedVarInt(ByteBuf, int)
     */
    public static int readSignedVarInt(ByteBuf buf) {
        int val = 0;
        int b = buf.readUnsignedByte();
        boolean sign = ((b >> 6) & 1) != 0;

        val |= b & MASK_6;
        int shift = 6;
        while ((b & 0x80) != 0) {
            if (shift > Integer.SIZE) {
                throw new RuntimeException("VarInt too big");
            }
            b = buf.readUnsignedByte();
            val |= (b & MASK_7) << shift;
            shift += 7;
        }
        return sign ? ~val : val;
    }

    private static void writeVarIntByte(ByteBuf buf, int i, boolean hasMore) {
        buf.writeByte(i | (hasMore ? 0x80 : 0));
    }

    public static <T extends IMessage> void ensureMainThread(AbstractClientMessageHandler<T> handler,
            EntityPlayer player, T message, MessageContext ctx) {
        IThreadListener taskQueue = Minecraft.getMinecraft();
        if (!taskQueue.isCallingFromMinecraftThread()) {
            taskQueue.addScheduledTask(() -> handler.handleClientMessage(player, message, ctx));
            throw ThreadQuickExitException.INSTANCE;
        }
    }
}

@@ -1,8 +1,1225 @@
+/*%0A *  This file is part of Cubic Chunks Mod, licensed under the MIT License (MIT).%0A *%0A *  Copyright (c) 2015 contributors%0A *%0A *  Permission is hereby granted, free of charge, to any person obtaining a copy%0A *  of this software and associated documentation files (the %22Software%22), to deal%0A *  in the Software without restriction, including without limitation the rights%0A *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell%0A *  copies of the Software, and to permit persons to whom the Software is%0A *  furnished to do so, subject to the following conditions:%0A *%0A *  The above copyright notice and this permission notice shall be included in%0A *  all copies or substantial portions of the Software.%0A *%0A *  THE SOFTWARE IS PROVIDED %22AS IS%22, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR%0A *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,%0A *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE%0A *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER%0A *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,%0A *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN%0A *  THE SOFTWARE.%0A */%0A
 package 

Add missing license header