Search in sources :

Example 1 with IPacketIDReceiver

use of icbm.classic.lib.network.IPacketIDReceiver in project ICBM-Classic by BuiltBrokenModding.

the class PacketTile method handle.

/**
 * Called to handler a packet when it is received
 *
 * @param player - player who received the packet
 * @param tile   - tile who is receiving the packet
 */
public void handle(EntityPlayer player, TileEntity tile) {
    // TODO add checksum or hash to verify the packet is sent to the correct tile
    final Location location = new Location(player.world, x, y, z);
    if (tile == null) {
        ICBMClassic.logger().error(new PacketTileReadException(location, "Null tile"));
    } else if (tile.isInvalid()) {
        ICBMClassic.logger().error(new PacketTileReadException(location, "Invalidated tile"));
    } else if (tile instanceof IPacketIDReceiver) {
        if (((IPacketIDReceiver) tile).shouldReadPacket(player, location, this)) {
            try {
                IPacketIDReceiver receiver = (IPacketIDReceiver) tile;
                receiver.read(dataToRead, id, player, this);
            } catch (IndexOutOfBoundsException e) {
                ICBMClassic.logger().error(new PacketTileReadException(location, "Packet was read past its size."));
                ICBMClassic.logger().error("Error: ", e);
            } catch (NullPointerException e) {
                ICBMClassic.logger().error(new PacketTileReadException(location, "Null pointer while reading data", e));
                ICBMClassic.logger().error("Error: ", e);
            } catch (Exception e) {
                ICBMClassic.logger().error(new PacketTileReadException(location, "Failed to read packet", e));
                ICBMClassic.logger().error("Error: ", e);
            }
        } else {
            ICBMClassic.logger().error("Error: " + tile + " rejected packet " + this + " due to invalid conditions.");
        }
    } else {
        ICBMClassic.logger().error(new PacketTileReadException(location, "Unsupported action for " + tile));
    }
}
Also used : IPacketIDReceiver(icbm.classic.lib.network.IPacketIDReceiver) PacketTileReadException(icbm.classic.lib.network.ex.PacketTileReadException) PacketTileReadException(icbm.classic.lib.network.ex.PacketTileReadException) Location(icbm.classic.lib.transform.vector.Location)

Aggregations

IPacketIDReceiver (icbm.classic.lib.network.IPacketIDReceiver)1 PacketTileReadException (icbm.classic.lib.network.ex.PacketTileReadException)1 Location (icbm.classic.lib.transform.vector.Location)1