cn.lambdacraft.core.network.MsgKeyUsing.java Source code

Java tutorial

Introduction

Here is the source code for cn.lambdacraft.core.network.MsgKeyUsing.java

Source

/** 
 * Copyright (c) LambdaCraft Modding Team, 2013
 * ??LambdaCraft ? 2013.
 * http://lambdacraft.half-life.cn/
 * 
 * LambdaCraft is open-source. It is distributed under the terms of the
 * LambdaCraft Open Source License. It grants rights to read, modify, compile
 * or run the code. It does *NOT* grant the right to redistribute this software
 * or its modifications in any form, binary or source, except if expressively
 * granted by the copyright holder.
 *
 * LambdaCraft???LambdaCraft??????
 * ?? ??????????
 */
package cn.lambdacraft.core.network;

import io.netty.buffer.ByteBuf;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.World;
import cn.lambdacraft.core.client.key.UsingUtils;
import cn.liutils.api.util.BlockPos;
import cn.liutils.api.util.Motion3D;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;

/**
 * ???
 * @author WeAthFolD
 */
public class MsgKeyUsing implements IMessage {

    boolean isUsing;

    public MsgKeyUsing(boolean b) {
        isUsing = b;
    }

    public MsgKeyUsing() {

    }

    @Override
    public void fromBytes(ByteBuf buf) {
        isUsing = buf.readBoolean();
    }

    @Override
    public void toBytes(ByteBuf buf) {
        buf.writeBoolean(isUsing);
    }

    public static class Handler implements IMessageHandler<MsgKeyUsing, IMessage> {

        @Override
        public IMessage onMessage(MsgKeyUsing msg, MessageContext ctx) {
            EntityPlayer thePlayer = ctx.getServerHandler().playerEntity;
            World world = thePlayer.worldObj;

            if (msg.isUsing) {

                Motion3D begin = new Motion3D(thePlayer, true);
                MovingObjectPosition mop = world.rayTraceBlocks(begin.getPosVec(world),
                        begin.move(8.0).getPosVec(world));
                if (mop == null || mop.sideHit == -1)
                    return null;
                Block block = world.getBlock(mop.blockX, mop.blockY, mop.blockZ);
                UsingUtils.useBlock(new BlockPos(mop.blockX, mop.blockY, mop.blockZ, block), world, thePlayer);

            } else {

                UsingUtils.stopUsingBlock(world, thePlayer);

            }
            return null;
        }

    }

}