Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2016
461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.11 KB | None | 0 0
  1. package com.chris.testing;
  2.  
  3. import java.util.function.Consumer;
  4. import java.util.logging.Logger;
  5.  
  6. import javax.inject.Inject;
  7.  
  8. import org.spongepowered.api.Game;
  9. import org.spongepowered.api.Platform.Type;
  10. import org.spongepowered.api.entity.living.player.Player;
  11. import org.spongepowered.api.event.Listener;
  12. import org.spongepowered.api.event.game.state.GameStartedServerEvent;
  13. import org.spongepowered.api.event.network.ClientConnectionEvent;
  14. import org.spongepowered.api.network.ChannelBinding;
  15. import org.spongepowered.api.network.ChannelBuf;
  16. import org.spongepowered.api.network.RawDataListener;
  17. import org.spongepowered.api.network.RemoteConnection;
  18. import org.spongepowered.api.plugin.Plugin;
  19.  
  20. @Plugin(id = "NetworkTest", name = "NetworkTest", version = "0.1")
  21. public class NetworkTest implements RawDataListener
  22. {
  23.     @Inject Game game;
  24.     @Inject Logger logger;
  25.    
  26.     private ChannelBinding.RawDataChannel rawDataChannel;
  27.    
  28.     public NetworkTest()
  29.     {
  30.     }
  31.    
  32.     @Listener
  33.     public void onInit(GameStartedServerEvent event)
  34.     {
  35.         rawDataChannel = this.game.getChannelRegistrar().createRawChannel(this, "TEST_CHANNEL");
  36.         rawDataChannel.addListener(this);
  37.     }
  38.    
  39.     @Listener
  40.     public void onPlayerJoin(ClientConnectionEvent.Join event)
  41.     {
  42.         logger.info("sending packet to : " + event.getTargetEntity().getName());
  43.         sendPacket(event.getTargetEntity(), new String[] {"Test"});
  44.     }
  45.  
  46.     public void handlePayload(ChannelBuf data, RemoteConnection connection, Type side)
  47.     {
  48.         logger.info(new String(data.array()).trim());
  49.     }
  50.    
  51.     public void sendPacket(Player player, String[] messages)
  52.     {
  53.         rawDataChannel.sendTo(player, buf ->
  54.         {
  55.             for(String message : messages)
  56.             {
  57.                 buf.writeString(message);
  58.             }
  59.         });
  60.     }
  61.    
  62.     public void sendPacket(Player player, Consumer<ChannelBuf> payload)
  63.     {
  64.         rawDataChannel.sendTo(player, payload);
  65.     }
  66.    
  67.     public void sendToAll(String[] messages)
  68.     {
  69.         rawDataChannel.sendToAll(buf ->{
  70.             for(String message : messages)
  71.             {
  72.                 buf.writeString(message);
  73.             }
  74.         });
  75.     }
  76.    
  77.     public void sendToAll(Consumer<ChannelBuf> payload)
  78.     {
  79.         rawDataChannel.sendToAll(payload);
  80.     }
  81.    
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement