Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.27 KB | None | 0 0
  1. package server.model.players;
  2.  
  3. import java.net.InetSocketAddress;
  4.  
  5. import server.Config;
  6. import server.Server;
  7. import server.model.npcs.NPCHandler;
  8. import server.util.Misc;
  9. import server.util.Stream;
  10. import java.util.ArrayList;
  11.  
  12. public class PlayerHandler{
  13.  
  14.  
  15.  
  16. public static Player players[] = new Player[Config.MAX_PLAYERS];
  17. public static String messageToAll = "";
  18. public static int playerCount = 0;
  19. public static String playersCurrentlyOn[] = new String[Config.MAX_PLAYERS];
  20. public static boolean updateAnnounced;
  21. public static boolean updateRunning;
  22. public static int updateSeconds;
  23. public static long updateStartTime;
  24. private boolean kickAllPlayers = false;
  25. public static PlayerSave save;
  26.  
  27. static {
  28. Runtime.getRuntime().addShutdownHook(new Thread()
  29.  
  30. {
  31.  
  32. public void run() {
  33.  
  34. System.out.println("Saving players...");
  35.  
  36. synchronized(this) {
  37.  
  38. for(int i = 0; i < Config.MAX_PLAYERS; i++)
  39.  
  40. if(players[i] != null) {
  41. save.saveGame((Client)players[i]);
  42. Client o = (Client) Server.playerHandler.players[i];
  43. //if() {
  44. o.getTradeAndDuel().declineTrade();
  45. //}
  46. }
  47.  
  48. }
  49.  
  50. }
  51.  
  52. });
  53.  
  54. }
  55.  
  56. public boolean newPlayerClient(Client client1)
  57. {
  58. int slot = -1;
  59. for(int i = 1; i < Config.MAX_PLAYERS; i++) {
  60. if((players[i] == null) || players[i].disconnected) {
  61. slot = i;
  62. break;
  63. }
  64. }
  65. if(slot == -1)
  66. return false;
  67. client1.handler = this;
  68. client1.playerId = slot;
  69. players[slot] = client1;
  70. players[slot].isActive = true;
  71. players[slot].connectedFrom = ((InetSocketAddress) client1.getSession().getRemoteAddress()).getAddress().getHostAddress();
  72. if(Config.SERVER_DEBUG)
  73. Misc.println("Player Slot "+slot+" slot 0 "+players[0]+" Player Hit "+players[slot]);
  74. return true;
  75. }
  76.  
  77. public void destruct() {
  78. for(int i = 0; i < Config.MAX_PLAYERS; i++) {
  79. if(players[i] == null)
  80. continue;
  81. players[i].destruct();
  82. players[i] = null;
  83. }
  84. }
  85.  
  86. public static int getPlayerCount() {
  87. return playerCount;
  88. }
  89.  
  90. public void updatePlayerNames() {
  91. playerCount = 0;
  92. for(int i = 0; i < Config.MAX_PLAYERS; i++) {
  93. if(players[i] != null) {
  94. playersCurrentlyOn[i] = players[i].playerName;
  95. playerCount++;
  96. } else {
  97. playersCurrentlyOn[i] = "";
  98. }
  99. }
  100. }
  101.  
  102. public static boolean isPlayerOn(String playerName) {
  103. //synchronized (PlayerHandler.players) {
  104. for(int i = 0; i < Config.MAX_PLAYERS; i++) {
  105. if(playersCurrentlyOn[i] != null){
  106. if(playersCurrentlyOn[i].equalsIgnoreCase(playerName)) {
  107. return true;
  108. }
  109. }
  110. }
  111. return false;
  112. //}
  113. }
  114.  
  115. public void process() {
  116. //synchronized (PlayerHandler.players) {
  117.  
  118. updatePlayerNames();
  119.  
  120. if(kickAllPlayers) {
  121. for(int i = 1; i < Config.MAX_PLAYERS; i++) {
  122. if(players[i] != null) {
  123. players[i].disconnected = true;
  124. }
  125. }
  126. }
  127.  
  128. for(int i = 0; i < Config.MAX_PLAYERS; i++) {
  129. if(players[i] == null || !players[i].isActive) continue;
  130. try {
  131.  
  132. if(players[i].disconnected && (System.currentTimeMillis() - players[i].logoutDelay > 10000 || players[i].properLogout || kickAllPlayers)) {
  133. if(players[i].inTrade) {
  134. Client o = (Client) Server.playerHandler.players[players[i].tradeWith];
  135. if(o != null) {
  136. o.getTradeAndDuel().declineTrade();
  137. }
  138. }
  139. if(players[i].duelStatus == 5) {
  140. Client o = (Client) Server.playerHandler.players[players[i].duelingWith];
  141. if(o != null) {
  142. o.getTradeAndDuel().duelVictory();
  143. }
  144. } else if (players[i].duelStatus <= 4 && players[i].duelStatus >= 1) {
  145. Client o = (Client) Server.playerHandler.players[players[i].duelingWith];
  146. if(o != null) {
  147. o.getTradeAndDuel().declineDuel();
  148. }
  149. }
  150. Client o = (Client) Server.playerHandler.players[i];
  151. if(PlayerSave.saveGame(o)) {
  152. System.out.println("Game saved for player "+players[i].playerName);
  153. removePlayer(players[i]);
  154. players[i] = null;
  155. } else {
  156. System.out.println("Could not save for "+players[i].playerName);
  157. }
  158.  
  159. continue;
  160. }
  161.  
  162. players[i].preProcessing();
  163. while(players[i].processQueuedPackets());
  164. players[i].process();
  165. players[i].postProcessing();
  166. players[i].getNextPlayerMovement();
  167.  
  168. } catch(Exception e) {
  169. e.printStackTrace();
  170. }
  171. }
  172.  
  173.  
  174. for(int i = 0; i < Config.MAX_PLAYERS; i++) {
  175. if(players[i] == null || !players[i].isActive) continue;
  176. try {
  177. if(players[i].disconnected && (System.currentTimeMillis() - players[i].logoutDelay > 10000 || players[i].properLogout || kickAllPlayers)) {
  178. if(players[i].inTrade) {
  179. Client o = (Client) Server.playerHandler.players[players[i].tradeWith];
  180. if(o != null) {
  181. o.getTradeAndDuel().declineTrade();
  182. }
  183. }
  184. if(players[i].duelStatus == 5) {
  185. Client o1 = (Client) Server.playerHandler.players[players[i].duelingWith];
  186. if(o1 != null) {
  187. o1.getTradeAndDuel().duelVictory();
  188. }
  189. } else if (players[i].duelStatus <= 4 && players[i].duelStatus >= 1) {
  190. Client o1 = (Client) Server.playerHandler.players[players[i].duelingWith];
  191. if(o1 != null) {
  192. o1.getTradeAndDuel().declineDuel();
  193. }
  194. }
  195.  
  196. Client o1 = (Client) Server.playerHandler.players[i];
  197. if(PlayerSave.saveGame(o1)){
  198. System.out.println("Game saved for player "+players[i].playerName);
  199. removePlayer(players[i]);
  200. players[i] = null;
  201. } else {
  202. System.out.println("Could not save for "+players[i].playerName);
  203. }
  204.  
  205. } else {
  206. Client o = (Client) Server.playerHandler.players[i];
  207. //if(o.g) {
  208. if(!players[i].initialized) {
  209. players[i].initialize();
  210. players[i].initialized = true;
  211. }
  212. else {
  213. players[i].update();
  214. }
  215. //}
  216. }
  217. } catch(Exception e) {
  218. e.printStackTrace();
  219. }
  220. }
  221.  
  222. if(updateRunning && !updateAnnounced) {
  223. updateAnnounced = true;
  224. Server.UpdateServer = true;
  225. }
  226. if(updateRunning && (System.currentTimeMillis() - updateStartTime > (updateSeconds*1000))) {
  227. for(Player p : PlayerHandler.players) {
  228. if(p == null)
  229. continue;
  230. PlayerSave.saveGame((Client)p);
  231. }
  232. System.exit(0);
  233. }
  234.  
  235. for(int i = 0; i < Config.MAX_PLAYERS; i++) {
  236. if(players[i] == null || !players[i].isActive) continue;
  237. try {
  238. players[i].clearUpdateFlags();
  239. } catch(Exception e) {
  240. e.printStackTrace();
  241. }
  242. }
  243. //}
  244. }
  245.  
  246. public void updateNPC(Player plr, Stream str) {
  247. //synchronized(plr) {
  248. updateBlock.currentOffset = 0;
  249.  
  250. str.createFrameVarSizeWord(65);
  251. str.initBitAccess();
  252.  
  253. str.writeBits(8, plr.npcListSize);
  254. int size = plr.npcListSize;
  255. plr.npcListSize = 0;
  256. for(int i = 0; i < size; i++) {
  257. if(plr.RebuildNPCList == false && plr.withinDistance(plr.npcList[i]) == true) {
  258. plr.npcList[i].updateNPCMovement(str);
  259. plr.npcList[i].appendNPCUpdateBlock(updateBlock);
  260. plr.npcList[plr.npcListSize++] = plr.npcList[i];
  261. } else {
  262. int id = plr.npcList[i].npcId;
  263. plr.npcInListBitmap[id>>3] &= ~(1 << (id&7));
  264. str.writeBits(1, 1);
  265. str.writeBits(2, 3);
  266. }
  267. }
  268.  
  269.  
  270. for(int i = 0; i < NPCHandler.maxNPCs; i++) {
  271. if(Server.npcHandler.npcs[i] != null) {
  272. int id = Server.npcHandler.npcs[i].npcId;
  273. if (plr.RebuildNPCList == false && (plr.npcInListBitmap[id>>3]&(1 << (id&7))) != 0) {
  274.  
  275. } else if (plr.withinDistance(Server.npcHandler.npcs[i]) == false) {
  276.  
  277. } else {
  278. plr.addNewNPC(Server.npcHandler.npcs[i], str, updateBlock);
  279. }
  280. }
  281. }
  282.  
  283. plr.RebuildNPCList = false;
  284.  
  285. if(updateBlock.currentOffset > 0) {
  286. str.writeBits(14, 16383);
  287. str.finishBitAccess();
  288. str.writeBytes(updateBlock.buffer, updateBlock.currentOffset, 0);
  289. } else {
  290. str.finishBitAccess();
  291. }
  292. str.endFrameVarSizeWord();
  293. //}
  294. }
  295.  
  296. private Stream updateBlock = new Stream(new byte[Config.BUFFER_SIZE]);
  297.  
  298. public void updatePlayer(Player plr, Stream str) {
  299. synchronized(plr) {
  300. updateBlock.currentOffset = 0;
  301. if(updateRunning && !updateAnnounced) {
  302. str.createFrame(114);
  303. str.writeWordBigEndian(updateSeconds*50/30);
  304. }
  305. plr.updateThisPlayerMovement(str);
  306. boolean saveChatTextUpdate = plr.isChatTextUpdateRequired();
  307. plr.setChatTextUpdateRequired(false);
  308. plr.appendPlayerUpdateBlock(updateBlock);
  309. plr.setChatTextUpdateRequired(saveChatTextUpdate);
  310. str.writeBits(8, plr.playerListSize);
  311. int size = plr.playerListSize;
  312. plr.playerListSize = 0;
  313. for(int i = 0; i < size; i++) {
  314. if(!plr.didTeleport && !plr.playerList[i].didTeleport && plr.withinDistance(plr.playerList[i])) {
  315. plr.playerList[i].updatePlayerMovement(str);
  316. plr.playerList[i].appendPlayerUpdateBlock(updateBlock);
  317. plr.playerList[plr.playerListSize++] = plr.playerList[i];
  318. } else {
  319. int id = plr.playerList[i].playerId;
  320. plr.playerInListBitmap[id>>3] &= ~(1 << (id&7));
  321. str.writeBits(1, 1);
  322. str.writeBits(2, 3);
  323. }
  324. }
  325.  
  326. int j = 0;
  327. if (plr.didTeleport)
  328. {
  329. plr.updateVisiblePlayers(); // so if we teleport and we are in our original region we are added back to the list for all the players that can see us
  330. }
  331.  
  332. int[] addPlayers = toIntArray(plr.addPlayerList);
  333. int addSize = plr.addPlayerSize;
  334.  
  335. if (size + addSize > 255)
  336. {
  337. addSize = size - 255;
  338. }
  339.  
  340. for (int i = 0; i < addSize; i++)
  341. {
  342. int id = addPlayers[i];
  343.  
  344. if(players[id] == null || !players[id].isActive || players[id] == plr)
  345. continue;
  346.  
  347. if(!plr.withinDistance(players[id]) || (plr.playerInListBitmap[id >> 3] & (1 << (id & 7))) != 0)
  348. {
  349. continue;
  350. }
  351.  
  352. plr.addNewPlayer(players[id], str, updateBlock);
  353. plr.addPlayerSize--; // you could just put these in player.java
  354. plr.addPlayerList.remove((Integer)id); // but for the sake of the tutorial, it's right here.
  355. }
  356.  
  357. if (plr.addPlayerSize > 0)
  358. {
  359. plr.addPlayerSize = 0;
  360. plr.addPlayerList.clear();
  361. }
  362.  
  363. if (updateBlock.currentOffset > 0)
  364. {
  365. str.writeBits(11, 2047);
  366. str.finishBitAccess();
  367. str.writeBytes(updateBlock.buffer, updateBlock.currentOffset, 0);
  368. }
  369. else
  370. {
  371. str.finishBitAccess();
  372. }
  373.  
  374. str.endFrameVarSizeWord();
  375. }
  376.  
  377. }
  378. public static int[] toIntArray(ArrayList<Integer> integerList)
  379. {
  380. int[] intArray = new int[integerList.size()];
  381.  
  382. for (int i = 0; i < integerList.size(); i++)
  383. {
  384. intArray[i] = integerList.get(i);
  385. }
  386.  
  387. return intArray;
  388. }
  389.  
  390. public void removePlayer(Player plr) {
  391. if(plr.privateChat != 2) {
  392. for(int i = 1; i < Config.MAX_PLAYERS; i++) {
  393. if (players[i] == null || players[i].isActive == false) continue;
  394. Client o = (Client)Server.playerHandler.players[i];
  395. if(o != null) {
  396. o.getPA().updatePM(plr.playerId, 0);
  397. }
  398. }
  399. }
  400. plr.destruct();
  401. }
  402.  
  403. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement