Advertisement
techbrew

Sample Forge Client Mod class for World Id custom packets

Aug 21st, 2014
510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.08 KB | None | 0 0
  1. import cpw.mods.fml.client.FMLClientHandler;
  2. import cpw.mods.fml.common.FMLLog;
  3. import cpw.mods.fml.common.eventhandler.SubscribeEvent;
  4. import cpw.mods.fml.common.network.*;
  5. import cpw.mods.fml.common.network.simpleimpl.*;
  6. import cpw.mods.fml.relauncher.Side;
  7. import cpw.mods.fml.relauncher.SideOnly;
  8. import io.netty.buffer.ByteBuf;
  9. import net.minecraft.client.Minecraft;
  10. import net.minecraftforge.common.MinecraftForge;
  11. import net.minecraftforge.event.entity.EntityJoinWorldEvent;
  12. import net.minecraftforge.event.world.WorldEvent;
  13. import net.techbrew.journeymap.JourneyMap;
  14.  
  15. /**
  16.  * Sample Forge Client class for communicating with the WorldInfo Bukkit plugin.
  17.  * Forge version used for this example: 1.7.10-Recommended 10.13.0.1180
  18.  *
  19.  * @see http://dev.bukkit.org/bukkit-plugins/worldinfo/
  20.  * @author techbrew
  21.  */
  22. public class WorldInfoHandler
  23. {
  24.     // Channel name
  25.     public static final String CHANNEL_NAME = "world_info";
  26.  
  27.     // Packet discriminator for World ID message
  28.     public static final int PACKET_WORLDID = 0;
  29.  
  30.     // Minimum time in millis that must pass before subsequent requests can be made
  31.     public static final int MIN_DELAY_MS = 1000;
  32.  
  33.     // Timestamp in millis of the last request by client
  34.     private static long lastRequest;
  35.  
  36.     // Timestamp in millis of the last response from server
  37.     private static long lastResponse;
  38.  
  39.     // Network wrapper of the channel for requests/response
  40.     private SimpleNetworkWrapper channel;
  41.  
  42.     // Handle to Minecraft client
  43.     Minecraft mc = FMLClientHandler.instance().getClient();
  44.  
  45.     /**
  46.      * Default constructor.
  47.      */
  48.     public WorldInfoHandler()
  49.     {
  50.         try
  51.         {
  52.             channel = NetworkRegistry.INSTANCE.newSimpleChannel(CHANNEL_NAME);
  53.             if (channel != null)
  54.             {
  55.                 channel.registerMessage(WorldIdListener.class, WorldIdMessage.class, PACKET_WORLDID, Side.CLIENT);
  56.                 FMLLog.info("Registered channel: %s", CHANNEL_NAME);
  57.                 MinecraftForge.EVENT_BUS.register(this);
  58.             }
  59.         }
  60.         catch(Throwable t)
  61.         {
  62.             FMLLog.severe("Failed to register channel %s: %s", CHANNEL_NAME, t);
  63.         }
  64.     }
  65.  
  66.     /**
  67.      * Request a World ID from the server by sending a blank WorldUidMessage.
  68.      */
  69.     private void requestWorldUid()
  70.     {
  71.         long now = System.currentTimeMillis();
  72.         if(lastRequest + MIN_DELAY_MS < now && lastResponse + MIN_DELAY_MS < now)
  73.         {
  74.             FMLLog.info("Requesting World ID");
  75.             channel.sendToServer(new WorldIdMessage());
  76.             lastRequest = System.currentTimeMillis();
  77.         }
  78.     }
  79.  
  80.     /**
  81.      * Use the world Load event as a trigger to request the World ID.
  82.      * @param event
  83.      */
  84.     @SideOnly(Side.CLIENT)
  85.     @SubscribeEvent
  86.     public void on(WorldEvent.Load event)
  87.     {
  88.         if(!mc.isSingleplayer() && mc.thePlayer!=null)
  89.         {
  90.             requestWorldUid();
  91.         }
  92.     }
  93.  
  94.     /**
  95.      * Use the EntityJoinWorldEvent of the player as a trigger to request the World ID.
  96.      *
  97.      * @param event
  98.      */
  99.     @SideOnly(Side.CLIENT)
  100.     @SubscribeEvent
  101.     public void on(EntityJoinWorldEvent event)
  102.     {
  103.         if(!mc.isSingleplayer() && mc.thePlayer!=null && !mc.thePlayer.isDead)
  104.         {
  105.             if(event.entity.getUniqueID().equals(mc.thePlayer.getUniqueID()))
  106.             {
  107.                 requestWorldUid();
  108.             }
  109.         }
  110.     }
  111.  
  112.     /**
  113.      * Simple message listener for WorldUidMesssages received from the server.
  114.      */
  115.     public static class WorldIdListener implements IMessageHandler<WorldIdMessage, IMessage>
  116.     {
  117.         @SideOnly(Side.CLIENT)
  118.         @Override
  119.         public IMessage onMessage(WorldIdMessage message, MessageContext ctx)
  120.         {
  121.             lastResponse = System.currentTimeMillis();
  122.             FMLLog.info("Got the worldUid from server: %s" , message.worldUid);
  123.             // Do something with it
  124.             return null;
  125.         }
  126.     }
  127.  
  128.     /**
  129.      * Simple message to get a World ID from the server.
  130.      * Send a blank one from the client as a request.
  131.      */
  132.     public static class WorldIdMessage implements IMessage
  133.     {
  134.         private String worldUid;
  135.  
  136.         public WorldIdMessage()
  137.         {
  138.         }
  139.  
  140.         public String getWorldUid()
  141.         {
  142.             return worldUid;
  143.         }
  144.  
  145.         @Override
  146.         public void fromBytes(ByteBuf buf)
  147.         {
  148.             try
  149.             {
  150.                 worldUid = ByteBufUtils.readUTF8String(buf);
  151.             }
  152.             catch(Throwable t)
  153.             {
  154.                 FMLLog.severe("Failed to read message: %s", t);
  155.             }
  156.         }
  157.  
  158.         @Override
  159.         public void toBytes(ByteBuf buf)
  160.         {
  161.             try
  162.             {
  163.                 if(worldUid!=null)
  164.                 {
  165.                     ByteBufUtils.writeUTF8String(buf, worldUid);
  166.                 }
  167.             }
  168.             catch(Throwable t)
  169.             {
  170.                 FMLLog.severe("Failed to read message: %s", t);
  171.             }
  172.         }
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement