Advertisement
Guest User

Untitled

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