com.crowsofwar.avatar.common.network.packets.PacketSUseStatusControl.java Source code

Java tutorial

Introduction

Here is the source code for com.crowsofwar.avatar.common.network.packets.PacketSUseStatusControl.java

Source

/* 
  This file is part of AvatarMod.
    
  AvatarMod is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
      
  AvatarMod is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
      
  You should have received a copy of the GNU General Public License
  along with AvatarMod. If not, see <http://www.gnu.org/licenses/>.
*/

package com.crowsofwar.avatar.common.network.packets;

import com.crowsofwar.avatar.AvatarLog;
import com.crowsofwar.avatar.AvatarLog.WarningType;
import com.crowsofwar.avatar.common.bending.StatusControl;
import com.crowsofwar.avatar.common.network.PacketRedirector;
import com.crowsofwar.avatar.common.util.Raytrace;

import io.netty.buffer.ByteBuf;
import net.minecraftforge.fml.relauncher.Side;

/**
 * 
 * 
 * @author CrowsOfWar
 */
public class PacketSUseStatusControl extends AvatarPacket<PacketSUseStatusControl> {

    private StatusControl statusControl;
    private Raytrace.Result raytrace;

    public PacketSUseStatusControl() {
    }

    public PacketSUseStatusControl(StatusControl control, Raytrace.Result raytrace) {
        this.statusControl = control;
        this.raytrace = raytrace;
    }

    @Override
    public void fromBytes(ByteBuf buf) {
        int id = buf.readInt();
        statusControl = StatusControl.lookup(id);
        if (statusControl == null) {
            AvatarLog.warn(WarningType.POSSIBLE_HACKING,
                    "Player trying to crash the server?? While sending UseStatusControl packet, sent invalid id "
                            + id);
            return; // TODO Cancel packet processing
        }

        raytrace = Raytrace.Result.fromBytes(buf);
    }

    @Override
    public void toBytes(ByteBuf buf) {
        buf.writeInt(statusControl.id());
        raytrace.toBytes(buf);
    }

    @Override
    protected Side getRecievedSide() {
        return Side.SERVER;
    }

    @Override
    protected AvatarPacket.Handler<PacketSUseStatusControl> getPacketHandler() {
        return PacketRedirector::redirectMessage;
    }

    public StatusControl getStatusControl() {
        return statusControl;
    }

    public Raytrace.Result getRaytrace() {
        return raytrace;
    }
}