Advertisement
HalestormXV

PacketDispatcher

Mar 22nd, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.75 KB | None | 0 0
  1. package com.halestormxv.Main.handler.network;
  2.  
  3. import com.halestormxv.Main.MainRegistry;
  4. import com.halestormxv.Main.handler.network.packets.OpenGuiPacket;
  5. import com.halestormxv.Main.handler.network.packets.OpenGuiPacketAlt;
  6. import com.halestormxv.Main.handler.network.packets.SyncPlayerPropsMessage;
  7. import com.halestormxv.lib.RefStrings;
  8.  
  9. import cpw.mods.fml.common.network.NetworkRegistry;
  10. import cpw.mods.fml.common.network.simpleimpl.IMessage;
  11. import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper;
  12. import cpw.mods.fml.relauncher.Side;
  13. import net.minecraft.entity.player.EntityPlayer;
  14. import net.minecraft.entity.player.EntityPlayerMP;
  15.  
  16. /**
  17.  *
  18.  * This class will house the SimpleNetworkWrapper instance, which I will name 'dispatcher',
  19.  * as well as give us a logical place from which to register our packets. These two things
  20.  * could be done anywhere, however, even in your Main class, but I will be adding other
  21.  * functionality (see below) that gives this class a bit more utility.
  22.  *
  23.  * While unnecessary, I'm going to turn this class into a 'wrapper' for SimpleNetworkWrapper
  24.  * so that instead of writing "PacketDispatcher.dispatcher.{method}" I can simply write
  25.  * "PacketDispatcher.{method}" All this does is make it quicker to type and slightly shorter;
  26.  * if you do not care about that, then make the 'dispatcher' field public instead of private,
  27.  * or, if you do not want to add a new class just for one field and one static method that
  28.  * you could put anywhere, feel free to put them wherever.
  29.  *
  30.  * For further convenience, I have also added two extra sendToAllAround methods: one which
  31.  * takes an EntityPlayer and one which takes coordinates.
  32.  *
  33.  */
  34. public class PacketDispatcher
  35. {
  36.     // a simple counter will allow us to get rid of 'magic' numbers used during packet registration
  37.     private static byte packetId = 0;
  38.  
  39.     /**
  40.      * The SimpleNetworkWrapper instance is used both to register and send packets.
  41.      * Since I will be adding wrapper methods, this field is private, but you should
  42.      * make it public if you plan on using it directly.
  43.      */
  44.     private static final SimpleNetworkWrapper dispatcher = NetworkRegistry.INSTANCE.newSimpleChannel(RefStrings.MODID);
  45.  
  46.     /**
  47.      * Call this during pre-init or loading and register all of your packets (messages) here
  48.      */
  49.     public static final void registerPackets() {
  50.         // Using an incrementing field instead of hard-coded numerals, I don't need to think
  51.         // about what number comes next or if I missed on should I ever rearrange the order
  52.         // of registration (for instance, if you wanted to alphabetize them... yeah...)
  53.         // It's even easier if you create a convenient 'registerMessage' method:
  54.        
  55.         //PacketDispatcher.registerMessage(SyncPlayerPropsMessage.SyncPlayerPropsMessageHandler.class, SyncPlayerPropsMessage.class, Side.CLIENT);
  56.        
  57.         PacketDispatcher.registerMessage(SyncPlayerPropsMessage.Handler.class, SyncPlayerPropsMessage.class, Side.CLIENT);
  58.         PacketDispatcher.registerMessage(OpenGuiPacketAlt.Handler.class, OpenGuiPacketAlt.class, Side.SERVER);
  59.  
  60.         // If you don't want to make a 'registerMessage' method, you can do it directly:
  61.         //PacketDispatcher.dispatcher.registerMessage(OpenGuiPacketAlt.Handler.class, OpenGuiPacketAlt.class, packetId++, Side.SERVER);
  62.         //PacketDispatcher.dispatcher.registerMessage(SyncPlayerPropsMessage.SyncPlayerPropsMessageHandler.class, SyncPlayerPropsMessage.class, packetId++, Side.CLIENT);
  63.     }
  64.  
  65.     /**
  66.      * Registers a message and message handler
  67.      */
  68.     private static final void registerMessage(Class handlerClass, Class messageClass, Side side) {
  69.         PacketDispatcher.dispatcher.registerMessage(handlerClass, messageClass, packetId++, side);
  70.     }
  71.  
  72.     //========================================================//
  73.     // The following methods are the 'wrapper' methods; again,
  74.     // this just makes sending a message slightly more compact
  75.     // and is purely a matter of stylistic preference
  76.     //========================================================//
  77.  
  78.     /**
  79.      * Send this message to the specified player.
  80.      * See {@link SimpleNetworkWrapper#sendTo(IMessage, EntityPlayerMP)}
  81.      */
  82.     public static final void sendTo(IMessage message, EntityPlayerMP player) {
  83.         PacketDispatcher.dispatcher.sendTo(message, player);
  84.     }
  85.  
  86.     /**
  87.      * Send this message to everyone within a certain range of a point.
  88.      * See {@link SimpleNetworkWrapper#sendToDimension(IMessage, NetworkRegistry.TargetPoint)}
  89.      */
  90.     public static final void sendToAllAround(IMessage message, NetworkRegistry.TargetPoint point) {
  91.         PacketDispatcher.dispatcher.sendToAllAround(message, point);
  92.     }
  93.  
  94.     /**
  95.      * Sends a message to everyone within a certain range of the coordinates in the same dimension.
  96.      */
  97.     public static final void sendToAllAround(IMessage message, int dimension, double x, double y, double z,
  98.  
  99.             double range) {
  100.         PacketDispatcher.sendToAllAround(message, new NetworkRegistry.TargetPoint(dimension, x, y, z,
  101.  
  102.                 range));
  103.     }
  104.  
  105.     /**
  106.      * Sends a message to everyone within a certain range of the player provided.
  107.      */
  108.     public static final void sendToAllAround(IMessage message, EntityPlayer player, double range) {
  109.         PacketDispatcher.sendToAllAround(message, player.worldObj.provider.dimensionId, player.posX,
  110.  
  111.                 player.posY, player.posZ, range);
  112.     }
  113.  
  114.     /**
  115.      * Send this message to everyone within the supplied dimension.
  116.      * See {@link SimpleNetworkWrapper#sendToDimension(IMessage, int)}
  117.      */
  118.     public static final void sendToDimension(IMessage message, int dimensionId) {
  119.         PacketDispatcher.dispatcher.sendToDimension(message, dimensionId);
  120.     }
  121.  
  122.     /**
  123.      * Send this message to the server.
  124.      * See {@link SimpleNetworkWrapper#sendToServer(IMessage)}
  125.      */
  126.     public static final void sendToServer(IMessage message) {
  127.         PacketDispatcher.dispatcher.sendToServer(message);
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement