Advertisement
Atijaf

AbstractMessage

Feb 1st, 2016
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.91 KB | None | 0 0
  1. public abstract class AbstractMessage<T extends AbstractMessage<T>> implements IMessage, IMessageHandler <T, IMessage>
  2. {
  3.     /**
  4.      * Some PacketBuffer methods throw IOException - default handling propagates the exception.
  5.      * if an IOException is expected but should not be fatal, handle it within this method.
  6.      */
  7.     protected abstract void read(PacketBuffer buffer) throws IOException;
  8.  
  9.     /**
  10.      * Some PacketBuffer methods throw IOException - default handling propagates the exception.
  11.      * if an IOException is expected but should not be fatal, handle it within this method.
  12.      */
  13.     protected abstract void write(PacketBuffer buffer) throws IOException;
  14.  
  15.     /**
  16.      * Called on whichever side the message is received;
  17.      * for bidirectional packets, be sure to check side
  18.      * If {@link #requiresMainThread()} returns true, this method is guaranteed
  19.      * to be called on the main Minecraft thread for this side.
  20.      */
  21.     public abstract void process(EntityPlayer player, Side side);
  22.  
  23.     /**
  24.      * If message is sent to the wrong side, an exception will be thrown during handling
  25.      * @return True if the message is allowed to be handled on the given side
  26.      */
  27.     protected boolean isValidOnSide(Side side) {
  28.         return true; // default allows handling on both sides, i.e. a bidirectional packet
  29.     }
  30.  
  31.     /**
  32.      * Whether this message requires the main thread to be processed (i.e. it
  33.      * requires that the world, player, and other objects are in a valid state).
  34.      */
  35.     protected boolean requiresMainThread() {
  36.         return true;
  37.     }
  38.  
  39.     @Override
  40.     public void fromBytes(ByteBuf buffer) {
  41.         try {
  42.             read(new PacketBuffer(buffer));
  43.         } catch (IOException e) {
  44.             throw Throwables.propagate(e);
  45.         }
  46.     }
  47.  
  48.     @Override
  49.     public void toBytes(ByteBuf buffer) {
  50.         try {
  51.             write(new PacketBuffer(buffer));
  52.         } catch (IOException e) {
  53.             throw Throwables.propagate(e);
  54.         }
  55.     }
  56.  
  57.     //=====================================================================//
  58.     /*
  59.      * Make the implementation final so child classes don't need to bother
  60.      * with it, since the message class shouldn't have anything to do with
  61.      * the handler. This is simply to avoid having to have:
  62.      *
  63.      * public static class Handler extends GenericMessageHandler<OpenGuiMessage> {}
  64.      *
  65.      * in every single message class for the sole purpose of registration.
  66.      */
  67.     @Override
  68.     public final IMessage onMessage(T msg, MessageContext ctx) {
  69.         if (!msg.isValidOnSide(ctx.side)) {
  70.             throw new RuntimeException("Invalid side " + ctx.side.name() + " for " + msg.getClass().getSimpleName());
  71.         } else if (msg.requiresMainThread()) {
  72.             checkThreadAndEnqueue(msg, ctx);
  73.         } else {
  74.             msg.process(EnergyTools.proxy.getPlayerEntity(ctx), ctx.side);
  75.         }
  76.         return null;
  77.     }
  78.  
  79.     /**
  80.      * 1.8 ONLY: Ensures that the message is being handled on the main thread
  81.      */
  82.     private static final <T extends AbstractMessage<T>> void checkThreadAndEnqueue(final AbstractMessage<T> msg, final MessageContext ctx) {
  83.         IThreadListener thread = EnergyTools.proxy.getThreadFromContext(ctx);
  84.         // pretty much copied straight from vanilla code, see {@link PacketThreadUtil#checkThreadAndEnqueue}
  85.         if (!thread.isCallingFromMinecraftThread()) {
  86.             thread.addScheduledTask(new Runnable() {
  87.                 public void run() {
  88.                     msg.process(EnergyTools.proxy.getPlayerEntity(ctx), ctx.side);
  89.                 }
  90.             });
  91.         }
  92.     }
  93.  
  94.     /**
  95.      * Messages that can only be sent from the server to the client should use this class
  96.      */
  97.     public static abstract class AbstractClientMessage<T extends AbstractMessage<T>> extends AbstractMessage<T> {
  98.         @Override
  99.         protected final boolean isValidOnSide(Side side) {
  100.             return side.isClient();
  101.         }
  102.     }
  103.  
  104.     /**
  105.      * Messages that can only be sent from the client to the server should use this class
  106.      */
  107.     public static abstract class AbstractServerMessage<T extends AbstractMessage<T>> extends AbstractMessage<T> {
  108.         @Override
  109.         protected final boolean isValidOnSide(Side side) {
  110.             return side.isServer();
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement