cn.academy.misc.msg.TeleportMsg.java Source code

Java tutorial

Introduction

Here is the source code for cn.academy.misc.msg.TeleportMsg.java

Source

/**
 * Copyright (c) Lambda Innovation, 2013-2015
 * ??Lambda Innovation
 * http://www.lambdacraft.cn/
 *
 * AcademyCraft is open-source, and it is distributed under 
 * the terms of GNU General Public License. You can modify
 * and distribute freely as long as you follow the license.
 * AcademyCraft??GNU???
 * ????
 * http://www.gnu.org/licenses/gpl.html
 */
package cn.academy.misc.msg;

import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import cn.annoreg.core.RegistrationClass;
import cn.annoreg.mc.RegMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;

/**
 * Force teleports player to some location. Fired in client.
 * @author WeathFolD
 */
@RegistrationClass
public class TeleportMsg implements IMessage {

    byte dim;
    float x, y, z;

    public TeleportMsg(int _dim, float _x, float _y, float _z) {
        dim = (byte) _dim;
        x = _x;
        y = _y;
        z = _z;
    }

    public TeleportMsg() {
    }

    @Override
    public void fromBytes(ByteBuf buf) {
        dim = buf.readByte();
        x = buf.readFloat();
        y = buf.readFloat();
        z = buf.readFloat();
    }

    @Override
    public void toBytes(ByteBuf buf) {
        buf.writeByte(dim).writeFloat(x).writeFloat(y).writeFloat(z);
    }

    public static class Handler<T extends TeleportMsg> implements IMessageHandler<T, IMessage> {

        @Override
        public IMessage onMessage(T msg, MessageContext ctx) {
            EntityPlayerMP player = ctx.getServerHandler().playerEntity;
            if (player.worldObj.provider.dimensionId != msg.dim)
                player.travelToDimension(msg.dim);
            player.setPositionAndUpdate(msg.x, msg.y, msg.z);
            return null;
        }

    }

}