Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fr.shyfe.dcedo.common;
- import cpw.mods.fml.common.network.FMLEmbeddedChannel;
- import cpw.mods.fml.common.network.FMLOutboundHandler;
- import cpw.mods.fml.common.network.FMLOutboundHandler.OutboundTarget;
- import cpw.mods.fml.common.network.NetworkRegistry;
- import cpw.mods.fml.common.network.NetworkRegistry.TargetPoint;
- import cpw.mods.fml.common.network.internal.FMLProxyPacket;
- import cpw.mods.fml.relauncher.Side;
- import cpw.mods.fml.relauncher.SideOnly;
- import io.netty.buffer.ByteBuf;
- import io.netty.buffer.Unpooled;
- import io.netty.channel.Channel;
- import io.netty.channel.ChannelHandler;
- import io.netty.channel.ChannelHandler.Sharable;
- import io.netty.channel.ChannelHandlerContext;
- import io.netty.handler.codec.MessageToMessageCodec;
- import io.netty.util.Attribute;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.EnumMap;
- import java.util.LinkedList;
- import java.util.List;
- import net.minecraft.client.Minecraft;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.entity.player.EntityPlayerMP;
- import net.minecraft.network.INetHandler;
- import net.minecraft.network.NetHandlerPlayServer;
- public class PacketPipeline extends MessageToMessageCodec<FMLProxyPacket, AbstractPacket>
- {
- private EnumMap<Side, FMLEmbeddedChannel> channels;
- private LinkedList<Class<? extends AbstractPacket>> packets = new LinkedList();
- private boolean isPostInitialised = false;
- public boolean registerPacket(Class<? extends AbstractPacket> clazz)
- {
- if (this.packets.size() > 256) {
- return false;
- }
- if (this.packets.contains(clazz)) {
- return false;
- }
- if (this.isPostInitialised) {
- return false;
- }
- this.packets.add(clazz);
- return true;
- }
- protected void encode(ChannelHandlerContext ctx, AbstractPacket msg, List<Object> out)
- throws Exception
- {
- ByteBuf buffer = Unpooled.buffer();
- Class<? extends AbstractPacket> clazz = msg.getClass();
- if (!this.packets.contains(msg.getClass())) {
- throw new NullPointerException("No Packet Registered for: " + msg.getClass().getCanonicalName());
- }
- byte discriminator = (byte)this.packets.indexOf(clazz);
- buffer.writeByte(discriminator);
- msg.encodeInto(ctx, buffer);
- FMLProxyPacket proxyPacket = new FMLProxyPacket(buffer.copy(), (String)ctx.channel().attr(NetworkRegistry.FML_CHANNEL).get());
- out.add(proxyPacket);
- }
- protected void decode(ChannelHandlerContext ctx, FMLProxyPacket msg, List<Object> out)
- throws Exception
- {
- ByteBuf payload = msg.payload();
- byte discriminator = payload.readByte();
- Class<? extends AbstractPacket> clazz = (Class)this.packets.get(discriminator);
- if (clazz == null) {
- throw new NullPointerException("No packet registered for discriminator: " + discriminator);
- }
- AbstractPacket pkt = (AbstractPacket)clazz.newInstance();
- pkt.decodeInto(ctx, payload.slice());
- switch (cpw.mods.fml.common.FMLCommonHandler.instance().getEffectiveSide())
- {
- case CLIENT:
- EntityPlayer player = getClientPlayer();
- pkt.handleClientSide(player);
- break;
- case SERVER:
- INetHandler netHandler = (INetHandler)ctx.channel().attr(NetworkRegistry.NET_HANDLER).get();
- EntityPlayer player1 = ((NetHandlerPlayServer)netHandler).playerEntity;
- pkt.handleServerSide(player1);
- break;
- }
- out.add(pkt);
- }
- public void initialise()
- {
- this.channels = NetworkRegistry.INSTANCE.newChannel("DCEDOInventory", new ChannelHandler[] { this });
- registerPackets();
- }
- public void registerPackets()
- {
- registerPacket(OpenGuiPacket.class);
- registerPacket(SyncPlayerPropsPacket.class);
- }
- public void postInitialise()
- {
- if (this.isPostInitialised) {
- return;
- }
- this.isPostInitialised = true;
- Collections.sort(this.packets, new Comparator()
- {
- public int compare(Class<? extends AbstractPacket> clazz1, Class<? extends AbstractPacket> clazz2)
- {
- int com = String.CASE_INSENSITIVE_ORDER.compare(clazz1.getCanonicalName(), clazz2.getCanonicalName());
- if (com == 0) {
- com = clazz1.getCanonicalName().compareTo(clazz2.getCanonicalName());
- }
- return com;
- }
- @Override
- public int compare(Object o1, Object o2)
- {
- // TODO Auto-generated method stub
- return 0;
- }
- });
- }
- @SideOnly(Side.CLIENT)
- private EntityPlayer getClientPlayer()
- {
- return Minecraft.getMinecraft().thePlayer;
- }
- public void sendToAll(AbstractPacket message)
- {
- ((FMLEmbeddedChannel)this.channels.get(Side.SERVER)).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.ALL);
- ((FMLEmbeddedChannel)this.channels.get(Side.SERVER)).writeAndFlush(message);
- }
- public void sendTo(AbstractPacket message, EntityPlayerMP player)
- {
- ((FMLEmbeddedChannel)this.channels.get(Side.SERVER)).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.PLAYER);
- ((FMLEmbeddedChannel)this.channels.get(Side.SERVER)).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(player);
- ((FMLEmbeddedChannel)this.channels.get(Side.SERVER)).writeAndFlush(message);
- }
- public void sendToAllAround(AbstractPacket message, NetworkRegistry.TargetPoint point)
- {
- ((FMLEmbeddedChannel)this.channels.get(Side.SERVER)).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.ALLAROUNDPOINT);
- ((FMLEmbeddedChannel)this.channels.get(Side.SERVER)).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(point);
- ((FMLEmbeddedChannel)this.channels.get(Side.SERVER)).writeAndFlush(message);
- }
- public void sendToDimension(AbstractPacket message, int dimensionId)
- {
- ((FMLEmbeddedChannel)this.channels.get(Side.SERVER)).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.DIMENSION);
- ((FMLEmbeddedChannel)this.channels.get(Side.SERVER)).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(Integer.valueOf(dimensionId));
- ((FMLEmbeddedChannel)this.channels.get(Side.SERVER)).writeAndFlush(message);
- }
- public void sendToServer(AbstractPacket message)
- {
- ((FMLEmbeddedChannel)this.channels.get(Side.CLIENT)).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.TOSERVER);
- ((FMLEmbeddedChannel)this.channels.get(Side.CLIENT)).writeAndFlush(message);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement