fslib.net.FSChannelHandler.java Source code

Java tutorial

Introduction

Here is the source code for fslib.net.FSChannelHandler.java

Source

/**
 * fslib is an open-source, high-level API for MinecraftForge, providing maximal coverage.
 *
 * Copyright (C) 2015 Company <support@company.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package fslib.net;

import java.util.List;

import fslib.FSLib;
import fslib.plugin.FSPlugin;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageCodec;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.internal.FMLProxyPacket;

public class FSChannelHandler implements ChannelHandler {

    public class FMLToPacketCodec extends MessageToMessageCodec<FMLProxyPacket, Packet> {

        @Override
        public void encode(ChannelHandlerContext ctx, Packet msg, List<Object> out) throws Exception {
            PacketBuffer buf = new PacketBuffer(Unpooled.buffer());
            FSPlugin plugin = msg.owner();
            if (plugin == null)
                throw new IllegalStateException("cannot send packet with null owner plugin");
            ByteBufUtils.writeUTF8String(buf, plugin.name());
            PacketHandler handler = FSLib.PACKET_CONTROLLER.handler(plugin);
            if (handler == null)
                throw new IllegalStateException(
                        "no PacketHandler for plugin {" + plugin.name() + " v" + plugin.version() + "}");
            handler.encode(ctx, msg, buf);
            FMLProxyPacket proxy = new FMLProxyPacket(buf, "fslib");
            out.add(proxy);
        }

        @Override
        public void decode(ChannelHandlerContext ctx, FMLProxyPacket msg, List<Object> out) {
            if (!"fslib".equals(msg.channel()))
                throw new IllegalStateException("packet not sent by fslib found {" + msg.channel() + "}");
            PacketBuffer buf = new PacketBuffer(msg.payload());
            String owner = ByteBufUtils.readUTF8String(buf);
            FSPlugin plugin = FSLib.PLUGIN_CONTROLLER.plugin(owner);
            if (plugin == null)
                throw new IllegalStateException("received packet from unregistered owner {" + owner + "}");
            PacketHandler handler = FSLib.PACKET_CONTROLLER.handler(plugin);
            if (handler == null)
                throw new IllegalStateException(
                        "no PacketHandler for plugin {" + plugin.name() + " v" + plugin.version() + "}");
            Packet packet = handler.decode(ctx, buf);
            if (packet == null)
                throw new IllegalStateException("null packet returned from PacketHandler from {" + plugin.name()
                        + " v" + plugin.version() + "}");
            out.add(packet);
        }

    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        ctx.pipeline().addLast("fslib:FMLToPacketCodec", new FMLToPacketCodec());
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.flush();
    }

}