Java tutorial
package com.savageboy74.savagetech.handler.packets; /* * PacketPipeline.java * Copyright (C) 2014 Savage - github.com/savageboy74 * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ import com.savageboy74.savagetech.util.LogHelper; import com.savageboy74.savagetech.util.Reference; import com.savageboy74.savagetech.util.Strings; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.network.FMLEmbeddedChannel; import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.common.network.internal.FMLProxyPacket; import cpw.mods.fml.relauncher.Side; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToMessageCodec; import net.minecraft.client.Minecraft; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.network.INetHandler; import net.minecraft.network.NetHandlerPlayServer; import java.util.*; public class PacketPipeline extends MessageToMessageCodec<FMLProxyPacket, AbstractPacket> { private EnumMap<Side, FMLEmbeddedChannel> channels; private LinkedList<Class<? extends AbstractPacket>> packets = new LinkedList<Class<? extends AbstractPacket>>(); private boolean isPostInitialized = false; public boolean registerPacket(Class<? extends AbstractPacket> apClass) { if (this.packets.size() > 256) { LogHelper.error(Strings.Packets.MAX_SIZE); return false; } if (this.packets.contains(apClass)) { LogHelper.error(Strings.Packets.REGISTERED); return false; } if (this.isPostInitialized) { LogHelper.error(Strings.Packets.LATE_REGISTRATION); return false; } this.packets.add(apClass); return true; } public void init() { this.channels = NetworkRegistry.INSTANCE.newChannel(Reference.PACKET_CHANNEL, this); registerPackets(); } public void postInit() { if (isPostInitialized) { return; } isPostInitialized = true; Collections.sort(this.packets, new Comparator<Class<? extends AbstractPacket>>() { @Override public int compare(Class<? extends AbstractPacket> o1, Class<? extends AbstractPacket> o2) { int com = String.CASE_INSENSITIVE_ORDER.compare(o1.getCanonicalName(), o2.getCanonicalName()); if (com == 0) { com = o1.getCanonicalName().compareTo(o2.getCanonicalName()); } return com; } }); } public void registerPackets() { //Register packets } @Override protected void encode(ChannelHandlerContext ctx, AbstractPacket msg, List<Object> out) throws Exception { ByteBuf buffer = Unpooled.buffer(); Class<? extends AbstractPacket> apClass = msg.getClass(); if (!this.packets.contains(apClass)) { throw new NullPointerException(Strings.Packets.NOT_REGISTERED + apClass.getCanonicalName()); } byte discriminator = (byte) this.packets.indexOf(apClass); buffer.writeByte(discriminator); msg.encodeInto(ctx, buffer); FMLProxyPacket packet = new FMLProxyPacket(buffer.copy(), ctx.channel().attr(NetworkRegistry.FML_CHANNEL).get()); out.add(packet); } @Override protected void decode(ChannelHandlerContext ctx, FMLProxyPacket msg, List<Object> out) throws Exception { ByteBuf payload = msg.payload(); byte discriminator = payload.readByte(); Class<? extends AbstractPacket> apClass = this.packets.get(discriminator); if (apClass == null) { throw new NullPointerException(Strings.Packets.NOT_REGISTERED + apClass.getCanonicalName()); } AbstractPacket abstractPacket = apClass.newInstance(); abstractPacket.decodeInto(ctx, payload.slice()); EntityPlayer player; switch (FMLCommonHandler.instance().getEffectiveSide()) { case CLIENT: player = Minecraft.getMinecraft().thePlayer; abstractPacket.handleClientSide(player); break; case SERVER: INetHandler handler = ctx.channel().attr(NetworkRegistry.NET_HANDLER).get(); player = ((NetHandlerPlayServer) handler).playerEntity; abstractPacket.handleServerSide(player); break; default: } out.add(abstractPacket); } }