This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

server

By: Edgu on Jun 29th, 2014  |  syntax: Java  |  size: 4.27 KB  |  views: 24  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package com.edgu.bmo.server;
  2.  
  3. import java.io.IOException;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6.  
  7. import com.edgu.bmo.server.packets.PacketNewBomb;
  8.  
  9. import com.edgu.bmo.server.packets.PacketNewPlayer;
  10. import com.edgu.bmo.server.packets.PacketRemoveBomb;
  11. import com.edgu.bmo.server.packets.PacketRemovePlayer;
  12. import com.edgu.bmo.server.packets.PacketSetGrassCollideTile;
  13. import com.edgu.bmo.server.packets.PacketSetGrassTile;
  14. import com.edgu.bmo.server.packets.PacketUpdateRotation;
  15. import com.edgu.bmo.server.packets.PacketUpdateX;
  16. import com.edgu.bmo.server.packets.PacketUpdateY;
  17. import com.esotericsoftware.kryonet.Connection;
  18. import com.esotericsoftware.kryonet.Listener;
  19. import com.esotericsoftware.kryonet.Server;
  20.  
  21. public class BMOServer extends Listener {
  22.  
  23.         private static Server server;
  24.         private static final int tcpPort = 54555, udpPort = 54555;
  25.         private static Map<Integer, Player> players = new HashMap<Integer, Player>();
  26.        
  27.         private static CommandListener commandListener = new CommandListener();
  28.         private static Log log = new Log();
  29.  
  30.         public static void main (String[] args) throws IOException {
  31.                 server = new Server();
  32.                 server.getKryo().register(PacketNewPlayer.class);
  33.                 server.getKryo().register(PacketRemovePlayer.class);
  34.                 server.getKryo().register(PacketUpdateRotation.class);
  35.                 server.getKryo().register(PacketUpdateX.class);
  36.                 server.getKryo().register(PacketUpdateY.class);
  37.                 server.getKryo().register(PacketNewBomb.class);
  38.                 server.getKryo().register(PacketRemoveBomb.class);
  39.                 server.getKryo().register(PacketSetGrassTile.class);
  40.                 server.getKryo().register(PacketSetGrassCollideTile.class);
  41.                 server.bind(tcpPort, udpPort);
  42.                 server.start();
  43.                 server.addListener(new BMOServer());
  44.                 log.output("[Launch] <Server is ready on 54555 Port. [TCP]>");
  45.                 Thread thread = new Thread(commandListener);
  46.                 thread.start();
  47.         }
  48.  
  49.         public void connected (Connection connection) {
  50.                 Player player = new Player();
  51.                 player.connection = connection;
  52.                
  53.                 PacketNewPlayer packet = new PacketNewPlayer();
  54.                 packet.id = connection.getID();
  55.                 server.sendToAllExceptUDP(connection.getID(), packet);
  56.  
  57.                 for (Player p : players.values()) {
  58.                         PacketNewPlayer packet2 = new PacketNewPlayer();
  59.                         packet2.id = p.connection.getID();
  60.                         connection.sendTCP(packet2);
  61.                 }
  62.                
  63.                 players.put(connection.getID(), player);
  64.                 log.output("[Connected] <Player: " + connection.getID() + ", IP: " + connection.getRemoteAddressTCP().getHostString() + ">");
  65.         }
  66.  
  67.         public void received (Connection connection, Object object) {
  68.                 if (object instanceof PacketUpdateX) {
  69.                         PacketUpdateX packet = (PacketUpdateX) object;
  70.                         players.get(connection.getID()).x = packet.x;
  71.                         packet.id = connection.getID();
  72.                         server.sendToAllExceptUDP(connection.getID(), packet);
  73.                 } else if (object instanceof PacketUpdateY) {
  74.                         PacketUpdateY packet = (PacketUpdateY) object;
  75.                         players.get(connection.getID()).y = packet.y;
  76.                         packet.id = connection.getID();
  77.                         server.sendToAllExceptUDP(connection.getID(), packet);
  78.                 } else if (object instanceof PacketUpdateRotation) {
  79.                         PacketUpdateRotation packet = (PacketUpdateRotation) object;
  80.                         players.get(connection.getID()).rotation = packet.rotation;
  81.                         packet.id = connection.getID();
  82.                         server.sendToAllExceptUDP(connection.getID(), packet);
  83.                 } else if (object instanceof PacketNewBomb) {
  84.                         PacketNewBomb packet = (PacketNewBomb) object;
  85.                         server.sendToAllExceptUDP(connection.getID(), packet);
  86.                 } else if (object instanceof PacketRemoveBomb) {
  87.                         PacketRemoveBomb packet = (PacketRemoveBomb) object;
  88.                         server.sendToAllExceptUDP(connection.getID(), packet);
  89.                 } else if (object instanceof PacketSetGrassTile) {
  90.                         PacketSetGrassTile packet = (PacketSetGrassTile) object;
  91.                         server.sendToAllExceptUDP(connection.getID(), packet);
  92.                 } else if (object instanceof PacketSetGrassCollideTile) {
  93.                         PacketSetGrassCollideTile packet = (PacketSetGrassCollideTile) object;
  94.                         server.sendToAllExceptUDP(connection.getID(), packet);
  95.                 }
  96.         }
  97.  
  98.         public void disconnected (Connection connection){
  99.                 players.remove(connection.getID());
  100.                 PacketRemovePlayer packet = new PacketRemovePlayer();
  101.                 packet.id = connection.getID();
  102.                 server.sendToAllExceptUDP(connection.getID(), packet);
  103.                 log.output("[Disconnected] <Player: " + connection.getID() + ">");
  104.         }
  105. }
clone this paste RAW Paste Data