org.starnub.starbounddata.packets.entity.EntityDestroyPacket.java Source code

Java tutorial

Introduction

Here is the source code for org.starnub.starbounddata.packets.entity.EntityDestroyPacket.java

Source

/*
 * Copyright (C) 2014 www.StarNub.org - Underbalanced
 *
 * This file is part of org.starnub a Java Wrapper for Starbound.
 *
 * This above mentioned StarNub software 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
 * any later version. This above mentioned CodeHome software
 * 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 in
 * this StarNub Software.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.starnub.starbounddata.packets.entity;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import org.starnub.starbounddata.packets.Packet;
import org.starnub.starbounddata.packets.Packets;
import org.starnub.starbounddata.types.entity.EntityVLQId;

/**
 * Represents the EntityDestroy and methods to generate a packet data for StarNub and Plugins
 * <p>
 * Notes: This packet can be edited freely. Please be cognisant of what values you change and how they will be interpreted by the starnubclient.
 * <p>
 * Packet Direction: Bidirectional
 * <p>
 * Starbound 1.0 Compliant (Versions 622, Update 1)
 *
 * @author Daniel (Underbalanced) (www.StarNub.org)
 * @since 1.0 Beta
 */
public class EntityDestroyPacket extends Packet {

    private EntityVLQId entityId = new EntityVLQId();
    private boolean death;

    /**
     * Recommended: For internal use with StarNub Player Sessions
     * <p>
     * Uses: This is used to pre-construct packets for a specific side of a connection
     * <p>
     * @param DIRECTION       Direction representing the direction the packet is heading
     * @param SENDER_CTX      ChannelHandlerContext which represents the sender of this packets context (Context can be written to)
     * @param DESTINATION_CTX ChannelHandlerContext which represents the destination of this packets context (Context can be written to)
     */
    public EntityDestroyPacket(Direction DIRECTION, ChannelHandlerContext SENDER_CTX,
            ChannelHandlerContext DESTINATION_CTX) {
        super(DIRECTION, Packets.ENTITYDESTROY.getPacketId(), SENDER_CTX, DESTINATION_CTX);
    }

    /**
     * Recommended: For Plugin Developers & Anyone else.
     * <p>
     * Uses: This is used to construct a packet for with no destination. This CAN ONLY be routed by using (routeToGroup, routeToGroupNoFlush) methods
     * <p>
     * @param entityId
     * @param death
     */
    public EntityDestroyPacket(EntityVLQId entityId, boolean death) {
        super(Packets.ENTITYDESTROY.getDirection(), Packets.ENTITYDESTROY.getPacketId());
        this.entityId = entityId;
        this.death = death;
    }

    /**
     * Recommended: For internal StarNub use with copying
     * <p>
     * Uses: This will construct a new packet from a packet
     *
     * @param packet DamageRequestPacket representing the packet to construct from
     */
    public EntityDestroyPacket(EntityDestroyPacket packet) {
        super(packet);
        this.entityId = packet.getEntityId();
        this.death = packet.isDeath();
    }

    public EntityVLQId getEntityId() {
        return entityId;
    }

    public void setEntityId(EntityVLQId entityId) {
        this.entityId = entityId;
    }

    public boolean isDeath() {
        return death;
    }

    public void setDeath(boolean death) {
        this.death = true;
    }

    /**
     * This will provide a new object while copying all of the internal data as well into this
     * new Object
     *
     * @return DamageRequestPacket the new copied object
     */
    @Override
    public EntityDestroyPacket copy() {
        return new EntityDestroyPacket(this);
    }

    /**
     * Uses: This method will read in a {@link io.netty.buffer.ByteBuf} into this packets fields
     * <p>
     *
     * @param in ByteBuf representing the reason to be read into the packet
     */
    @Override
    public void read(ByteBuf in) {
        this.entityId.read(in);
        this.death = in.readBoolean();
    }

    /**
     * Recommended: For internal use with StarNub Player Sessions
     * <p>
     * Uses: This method will write to a {@link io.netty.buffer.ByteBuf} using this packets fields
     * <p>
     *
     * @param out ByteBuf representing the space to write out the packet reason
     */
    @Override
    public void write(ByteBuf out) {
        this.entityId.write(out);
        out.writeBoolean(death);
    }

    @Override
    public String toString() {
        return "EntityDestroyPacket{" + "entityId=" + entityId + ", death=" + death + "} " + super.toString();
    }
}