public class ClientNetworkEvent {

    @SubscribeEvent
    public void onClientJoined(ClientConnectedToServerEvent event) {
        EntityPlayer entityplayer = FMLClientHandler.instance().getClientPlayerEntity();
        World world = entityplayer.worldObj;
        BlockUtil.setLittleController(new LittlePlayerController(FMLClientHandler.instance().getClient(), (NetHandlerPlayClient) event.handler),
                                      world.getWorldInfo().getGameType());
    }

} public class ClientBlockChangeExecutor implements IPacketExecutor {

    @Override
    public void execute(PacketUpdate packet, World world, EntityPlayer entityplayer) {
        if (packet instanceof PacketLittleBlockChange
            && packet.getCommand().equals(CommandLib.UPDATE_CLIENT)
            && packet.targetExists(world)) {
        	Block block = ((PacketLittleBlockChange) packet).getBlock();
        	int metadata = ((PacketLittleBlockChange) packet).getMetadata();
            ((World) LittleBlocks.proxy.getLittleWorld(world,
                                                       false)).setBlock(packet.xPosition,
                                                                        packet.yPosition,
                                                                        packet.zPosition,
                                                                        block,
                                                                        metadata,
                                                                        3);
        }
    }

} public class ClientBlockEventExecutor implements IPacketExecutor {

    @Override
    public void execute(PacketUpdate packet, World world, EntityPlayer entityplayer) {
        if (packet instanceof PacketLittleBlocksEvents) {
            PacketLittleBlocksEvents packetEvent = (PacketLittleBlocksEvents) packet;
            ((World) LittleBlocks.proxy.getLittleWorld(world,
                                                       false)).addBlockEvent(packetEvent.xPosition,
                                                                             packetEvent.yPosition,
                                                                             packetEvent.zPosition,
                                                                             packetEvent.getBlock(),
                                                                             packetEvent.getInstrumentType(),
                                                                             packetEvent.getPitch());
        }
    }

} public class ClientCopierNotifyExecutor implements IPacketExecutor {

    @Override
    public void execute(PacketUpdate packet, World world, EntityPlayer entityplayer) {
        if (packet instanceof PacketLittleNotify
            && packet.getCommand().equals(CommandLib.COPIER_MESSAGE)) {
            String message = LanguageRegistry.instance().getStringLocalization(MessageLib.DENY_COPY);
            if (message.equals("")) message = LanguageRegistry.instance().getStringLocalization(MessageLib.DENY_WAND);
            ChatHelper.addColouredMessageToPlayer(entityplayer,
                                                  EnumChatFormatting.BLUE,
                                                  message);
        }
    }

} public class ClientLittleCollectionExecutor implements IPacketExecutor {

    @Override
    public void execute(PacketUpdate packet, World world, EntityPlayer entityplayer) {
        if (packet instanceof PacketLittleBlocksCollection
            && packet.getCommand().equals(CommandLib.ENTITY_COLLECTION)) {
            Entity entity = world.getEntityByID(((PacketLittleBlocksCollection) packet).getEntityId());
            if (entity instanceof EntityItemLittleBlocksCollection) {
                ((EntityItemLittleBlocksCollection) entity).setCollection(((PacketLittleBlocksCollection) packet).itemstackCollection);
            }
        }
    }

} public class ClientPacketLittleBlocksLoginExecutor implements IPacketExecutor {

    @Override
    public void execute(PacketUpdate packet, World world, EntityPlayer entityplayer) {
        if (packet instanceof PacketLittleBlocksSettings
            && packet.getCommand() == CommandLib.SETTINGS) {
            ConfigurationLib.littleBlocksClip = ((PacketLittleBlocksSettings) packet).getClipMode();
        }
    }

} public class NetworkEvent {

    @SubscribeEvent
    public void onClientJoined(ServerConnectionFromClientEvent event) {
        EntityPlayer entityplayer = ((NetHandlerPlayServer) event.handler).playerEntity;
        World world = entityplayer.worldObj;
        EnumWandAction.getWandActionForPlayer(entityplayer);
        PacketLittleBlocksSettings packet = new PacketLittleBlocksSettings();
        packet.setCommand(CommandLib.SETTINGS);
        packet.setClipMode(ConfigurationLib.littleBlocksClip);
        PacketHelper.sendToPlayer(packet,
                                  (EntityPlayerMP) entityplayer);
        List<TileEntity> tileEntities = world.loadedTileEntityList;
        for (TileEntity tileentity : tileEntities) {
            if (tileentity instanceof TileEntityLittleChunk) {
                world.markBlockForUpdate(tileentity.xCoord,
                                         tileentity.yCoord + 1,
                                         tileentity.zCoord);
            }
        }
    }

} public class PacketLittleBlockCollectionHandler extends SubPacketHandler {

    @Override
    protected PacketUpdate createNewPacket() {
        return new PacketLittleBlocksCollection();
    }

} public class PacketLittleBlockEventHandler extends SubPacketHandler {

    @Override
    protected PacketUpdate createNewPacket() {
        return new PacketLittleBlocksEvents();
    }

} public class PacketLittleBlockHandler extends SubPacketHandler {

    @Override
    protected PacketUpdate createNewPacket() {
        return new PacketLittleBlock();
    }

} public class PacketLittleBlocksHandler extends SubPacketHandler {

    @Override
    protected PacketUpdate createNewPacket() {
        return new PacketLittleBlockChange();
    }

} public class PacketLittleNotifyHandler extends SubPacketHandler {

    @Override
    protected PacketUpdate createNewPacket() {
        return new PacketLittleNotify();
    }

} public class PacketLoginHandler extends SubPacketHandler {

    @Override
    protected PacketUpdate createNewPacket() {
        return new PacketLittleBlocksSettings();
    }

} public class PacketLittleBlock extends PacketUpdate {

    private ItemStack itemStack;

    @Override
    public void writeData(ChannelHandlerContext ctx, ByteBuf data) {
        super.writeData(ctx,
                        data);
        if (this.itemStack == null) {
            data.writeBoolean(false);
        } else {
            data.writeBoolean(true);
            NBTHelper.writeItemStack(data,
                                     this.itemStack);
        }
    }

    @Override
    public void readData(ChannelHandlerContext ctx, ByteBuf data) {
        super.readData(ctx,
                       data);
        if (data.readBoolean() == true) {
            this.itemStack = NBTHelper.readItemStack(data);
        }
    }

    public PacketLittleBlock() {
        super(PacketIds.TILE);
        this.setChannel(CoreLib.MOD_CHANNEL);
    }

    /**
     * CLICK BLOCK
     */
    public PacketLittleBlock(int x, int y, int z, int face) {
        this();
        this.setPosition(x,
                         y,
                         z,
                         face);
        this.setCommand(CommandLib.BLOCK_CLICKED);
    }

    /**
     * ACTIVATE BLOCK
     */
    public PacketLittleBlock(int x, int y, int z, int direction, ItemStack itemStack, float xOff, float yOff, float zOff) {
        this();
        this.setPosition(x,
                         y,
                         z,
                         direction);
        this.setHitVectors(xOff,
                           yOff,
                           zOff);
        this.itemStack = itemStack != null ? itemStack.copy() : null;
        this.setCommand(CommandLib.BLOCK_ACTIVATED);
    }

    public int getDirection() {
        return this.side;
    }

    public ItemStack getItemStack() {
        return this.itemStack;
    }

    @Override
    public boolean targetExists(World world) {
        return false;
    }
} public class PacketLittleBlockChange extends PacketUpdate {

    @Override
    public void writeData(ChannelHandlerContext ctx, ByteBuf data) {
        super.writeData(ctx,
                        data);
    }

    @Override
    public void readData(ChannelHandlerContext ctx, ByteBuf data) {
        super.readData(ctx,
                       data);
    }

    public PacketLittleBlockChange() {
        super(PacketIds.UPDATE);
        this.setChannel(CoreLib.MOD_CHANNEL);
    }

    public PacketLittleBlockChange(int x, int y, int z, World world) {
        this();
        int blockID = Block.getIdFromBlock(world.getBlock(x,
                y,
                z));
        int metadata = world.getBlockMetadata(
        		x,
                y,
                z);
        int bam = (blockID << 4) + metadata;
        this.setPosition(x,
                         y,
                         z,
                         bam);
        this.setCommand(CommandLib.UPDATE_CLIENT);
    }

    @Override
    public boolean targetExists(World world) {
        if (world.getBlock(this.xPosition >> 3,
                           this.yPosition >> 3,
                           this.zPosition >> 3) == ConfigurationLib.littleChunk) {
            return true;
        }
        return false;
    }

    public int getBlockID() {
        return this.side >> 4;
    }

    public Block getBlock() {
        return Block.getBlockById(this.getBlockID());
    }

    public int getMetadata() {
        return this.side & 15;
    }
} public class PacketLittleBlocksEvents extends PacketUpdate {

    /** 1=Double Bass, 2=Snare Drum, 3=Clicks / Sticks, 4=Bass Drum, 5=Harp */
    public int getInstrumentType() {
        return this.payload.getIntPayload(0);
    }

    /**
     * The pitch of the note (between 0-24 inclusive where 0 is the lowest and
     * 24 is the highest).
     */
    public int getPitch() {
        return this.payload.getIntPayload(1);
    }

    /** The block ID this action is set for. */
    public int getBlockId() {
        return this.payload.getIntPayload(2);
    }

    public Block getBlock() {
        return Block.getBlockById(this.getBlockId());
    }

    public PacketLittleBlocksEvents() {
        super(PacketLib.PACKETID_EVENT, new PacketPayload(3, 0, 0, 0));
        this.setChannel(CoreLib.MOD_CHANNEL);
        this.setCommand(CommandLib.BLOCK_EVENT);
    }

    public PacketLittleBlocksEvents(int x, int y, int z, int blockId, int type, int pitch) {
        this();
        this.setPosition(x,
                         y,
                         z,
                         0);
        this.setInstrumentType(type);
        this.setPitch(pitch);
        this.setBlockId(blockId);
    }

    /** 1=Double Bass, 2=Snare Drum, 3=Clicks / Sticks, 4=Bass Drum, 5=Harp */
    public void setInstrumentType(int type) {
        this.payload.setIntPayload(0,
                                   type);
    }

    /**
     * The pitch of the note (between 0-24 inclusive where 0 is the lowest and
     * 24 is the highest).
     */
    public void setPitch(int pitch) {
        this.payload.setIntPayload(1,
                                   pitch);
    }

    /** The block ID this action is set for. */
    public void setBlockId(int blockId) {
        this.payload.setIntPayload(2,
                                   blockId);
    }

    @Override
    public boolean targetExists(World world) {
        return false;
    }

    // .addBlockEvent(xLocation, yLocation, zLocation, blockId, instrumentType,
    // pitch);
} public class PacketLittleNotify extends PacketNotifyPlayer {

    public PacketLittleNotify() {
        super();
        this.setChannel(CoreLib.MOD_CHANNEL);
    }

    public PacketLittleNotify(String command) {
        this();
        this.setCommand(command);
    }

    @Override
    public boolean targetExists(World world) {
        return false;
    }

} public class PacketLittleBlockActivatedExecutor implements IPacketExecutor {

    @Override
    public void execute(PacketUpdate packet, World world, EntityPlayer entityplayer) {
        if (packet instanceof PacketLittleBlock
            && packet.getCommand().equals(CommandLib.BLOCK_ACTIVATED)) {
        	ILittleWorld littleworld = LittleBlocks.proxy.getLittleWorld(world,
                    false);
        	//System.out.println(littleworld);
	            ((BlockLittleChunk) ConfigurationLib.littleChunk).onServerBlockActivated((World) littleworld,
	                                                                                     entityplayer,
	                                                                                     // littlePacket.getItemStack(),
	                                                                                     packet.xPosition,
	                                                                                     packet.yPosition,
	                                                                                     packet.zPosition,
	                                                                                     packet.side,
	                                                                                     packet.hitX,
	                                                                                     packet.hitY,
	                                                                                     packet.hitZ);
        }
    }
} public class PacketLittleBlockClickedExecutor implements IPacketExecutor {

    @Override
    public void execute(PacketUpdate packet, World world, EntityPlayer entityplayer) {
        if (packet instanceof PacketLittleBlock
            && packet.getCommand().equals(CommandLib.BLOCK_CLICKED)) {
            ((BlockLittleChunk) ConfigurationLib.littleChunk).onServerBlockClicked((World) LittleBlocks.proxy.getLittleWorld(world,
                                                                                                                             false),
                                                                                   packet.xPosition,
                                                                                   packet.yPosition,
                                                                                   packet.zPosition,
                                                                                   packet.side,
                                                                                   entityplayer);
        }
    }
} public class PacketLittleWandSwitchExecutor implements IPacketExecutor {

    @Override
    public void execute(PacketUpdate packet, World world, EntityPlayer entityplayer) {
        if (packet instanceof PacketLittleNotify
            && packet.getCommand().equals(CommandLib.WAND_SWITCH)) {
            EnumWandAction.setNextActionForPlayer(entityplayer);
        }
    }
}
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.slimevoid.library.network.PacketIds;
import net.slimevoid.library.network.handlers.PacketPipeline;
import net.slimevoid.library.util.helpers.PacketHelper;
import net.slimevoid.littleblocks.client.network.ClientNetworkEvent;
import net.slimevoid.littleblocks.client.network.packets.executors.ClientBlockChangeExecutor;
import net.slimevoid.littleblocks.client.network.packets.executors.ClientBlockEventExecutor;
import net.slimevoid.littleblocks.client.network.packets.executors.ClientCopierNotifyExecutor;
import net.slimevoid.littleblocks.client.network.packets.executors.ClientLittleCollectionExecutor;
import net.slimevoid.littleblocks.client.network.packets.executors.ClientPacketLittleBlocksLoginExecutor;
import net.slimevoid.littleblocks.network.NetworkEvent;
import net.slimevoid.littleblocks.network.handlers.PacketLittleBlockCollectionHandler;
import net.slimevoid.littleblocks.network.handlers.PacketLittleBlockEventHandler;
import net.slimevoid.littleblocks.network.handlers.PacketLittleBlockHandler;
import net.slimevoid.littleblocks.network.handlers.PacketLittleBlocksHandler;
import net.slimevoid.littleblocks.network.handlers.PacketLittleNotifyHandler;
import net.slimevoid.littleblocks.network.handlers.PacketLoginHandler;
import net.slimevoid.littleblocks.network.packets.PacketLittleBlock;
import net.slimevoid.littleblocks.network.packets.PacketLittleBlockChange;
import net.slimevoid.littleblocks.network.packets.PacketLittleBlocksEvents;
import net.slimevoid.littleblocks.network.packets.PacketLittleNotify;
import net.slimevoid.littleblocks.network.packets.executors.PacketLittleBlockActivatedExecutor;
import net.slimevoid.littleblocks.network.packets.executors.PacketLittleBlockClickedExecutor;
import net.slimevoid.littleblocks.network.packets.executors.PacketLittleWandSwitchExecutor;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
package net.slimevoid.littleblocks.core.lib;


public class PacketLib {

    public final static String   CHANNEL_COMMAND_BLOCK = "MC|AdvCdm";
    public final static String   LITTLEWORLD           = "LW:";
    public final static int      PACKETID_EVENT        = PacketIds.PLAYER + 100;
    public final static int      BLOCK_CLICK           = 0;
    public final static int      DIG_ONGOING           = 1;
    public final static int      DIG_BROKEN            = 2;

    public static PacketPipeline handler               = new PacketPipeline();

    @SideOnly(Side.CLIENT)
    public static void registerClientPacketHandlers() {
        MinecraftForge.EVENT_BUS.register(new ClientNetworkEvent());

        handler.getPacketHandler(PacketIds.LOGIN).registerClientExecutor(CommandLib.SETTINGS,
                                                                         new ClientPacketLittleBlocksLoginExecutor());

        handler.getPacketHandler(PacketIds.UPDATE).registerClientExecutor(CommandLib.UPDATE_CLIENT,
                                                                          new ClientBlockChangeExecutor());

        handler.getPacketHandler(PacketIds.ENTITY).registerClientExecutor(CommandLib.ENTITY_COLLECTION,
                                                                          new ClientLittleCollectionExecutor());

        handler.getPacketHandler(PacketIds.PLAYER).registerClientExecutor(CommandLib.COPIER_MESSAGE,
                                                                          new ClientCopierNotifyExecutor());

        handler.getPacketHandler(PACKETID_EVENT).registerClientExecutor(CommandLib.BLOCK_EVENT,
                                                                        new ClientBlockEventExecutor());
    }

    public static void registerPacketHandlers() {
        MinecraftForge.EVENT_BUS.register(new NetworkEvent());

        PacketLittleNotifyHandler playerHandler = new PacketLittleNotifyHandler();
        playerHandler.registerServerExecutor(CommandLib.WAND_SWITCH,
                                             new PacketLittleWandSwitchExecutor());

        handler.registerPacketHandler(PacketIds.PLAYER,
                                      playerHandler);

        PacketLittleBlockHandler littleBlockHandler = new PacketLittleBlockHandler();
        littleBlockHandler.registerServerExecutor(CommandLib.BLOCK_ACTIVATED,
                                                  new PacketLittleBlockActivatedExecutor());
        littleBlockHandler.registerServerExecutor(CommandLib.BLOCK_CLICKED,
                                                  new PacketLittleBlockClickedExecutor());

        handler.registerPacketHandler(PacketIds.LOGIN,
                                      new PacketLoginHandler());

        handler.registerPacketHandler(PacketIds.TILE,
                                      littleBlockHandler);

        handler.registerPacketHandler(PacketIds.ENTITY,
                                      new PacketLittleBlockCollectionHandler());

        handler.registerPacketHandler(PACKETID_EVENT,
                                      new PacketLittleBlockEventHandler());