Advertisement
xXm0dzXx

Untitled

Apr 15th, 2012
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.32 KB | None | 0 0
  1.  
  2. package net.minecraft.src;
  3.  
  4. import java.io.*;
  5. import java.net.InetSocketAddress;
  6. import java.net.Socket;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import net.minecraft.client.Minecraft;
  10. import org.lwjgl.input.Keyboard;
  11.  
  12. public class GuiMultiplayer extends GuiScreen
  13. {
  14. /** Number of outstanding ThreadPollServers threads */
  15. private static int threadsPending = 0;
  16.  
  17. /** Lock object for use with synchronized() */
  18. private static Object lock = new Object();
  19.  
  20. /**
  21. * A reference to the screen object that created this. Used for navigating between screens.
  22. */
  23. private GuiScreen parentScreen;
  24.  
  25. /** Slot container for the server list */
  26. private GuiSlotServer serverSlotContainer;
  27.  
  28. /** List of ServerNBTStorage objects */
  29. private static List serverList;
  30.  
  31. public static int spamServer;
  32.  
  33. /** Index of the currently selected server */
  34. private int selectedServer;
  35.  
  36. /** The 'Edit' button */
  37. private GuiButton buttonEdit;
  38.  
  39. /** The 'Join Server' button */
  40. private GuiButton buttonSelect;
  41.  
  42. /** The 'Delete' button */
  43. private GuiButton buttonDelete;
  44.  
  45. /** The 'Delete' button was clicked */
  46. private boolean deleteClicked;
  47.  
  48. /** The 'Add server' button was clicked */
  49. private boolean addClicked;
  50.  
  51. /** The 'Edit' button was clicked */
  52. private boolean editClicked;
  53.  
  54. /** The 'Direct Connect' button was clicked */
  55. private boolean directClicked;
  56.  
  57. /** This GUI's lag tooltip text or null if no lag icon is being hovered. */
  58. private String lagTooltip;
  59.  
  60. /**
  61. * Temporary ServerNBTStorage used by the Edit/Add/Direct Connect dialogs
  62. */
  63. private ServerNBTStorage tempServer;
  64.  
  65. public GuiMultiplayer(GuiScreen par1GuiScreen)
  66. {
  67. serverList = new ArrayList();
  68. selectedServer = -1;
  69. deleteClicked = false;
  70. addClicked = false;
  71. editClicked = false;
  72. directClicked = false;
  73. lagTooltip = null;
  74. tempServer = null;
  75. parentScreen = par1GuiScreen;
  76. }
  77.  
  78. /**
  79. * Called from the main game loop to update the screen.
  80. */
  81. public void updateScreen()
  82. {
  83. }
  84.  
  85. /**
  86. * Adds the buttons (and other controls) to the screen in question.
  87. */
  88. public void initGui()
  89. {
  90. loadServerList();
  91. Keyboard.enableRepeatEvents(true);
  92. controlList.clear();
  93. serverSlotContainer = new GuiSlotServer(this);
  94. initGuiControls();
  95. }
  96.  
  97. /**
  98. * Load the server list from servers.dat
  99. */
  100. private void loadServerList()
  101. {
  102. try
  103. {
  104. NBTTagCompound nbttagcompound = CompressedStreamTools.read(new File(mc.mcDataDir, "servers.dat"));
  105. NBTTagList nbttaglist = nbttagcompound.getTagList("servers");
  106. serverList.clear();
  107.  
  108. for (int i = 0; i < nbttaglist.tagCount(); i++)
  109. {
  110. serverList.add(ServerNBTStorage.createServerNBTStorage((NBTTagCompound)nbttaglist.tagAt(i)));
  111. }
  112. }
  113. catch (Exception exception)
  114. {
  115. exception.printStackTrace();
  116. }
  117. }
  118.  
  119. /**
  120. * Save the server list to servers.dat
  121. */
  122. private void saveServerList()
  123. {
  124. try
  125. {
  126. NBTTagList nbttaglist = new NBTTagList();
  127.  
  128. for (int i = 0; i < serverList.size(); i++)
  129. {
  130. nbttaglist.appendTag(((ServerNBTStorage)serverList.get(i)).getCompoundTag());
  131. }
  132.  
  133. NBTTagCompound nbttagcompound = new NBTTagCompound();
  134. nbttagcompound.setTag("servers", nbttaglist);
  135. CompressedStreamTools.safeWrite(nbttagcompound, new File(mc.mcDataDir, "servers.dat"));
  136. }
  137. catch (Exception exception)
  138. {
  139. exception.printStackTrace();
  140. }
  141. }
  142.  
  143. /**
  144. * Populate the GuiScreen controlList
  145. */
  146. public void initGuiControls()
  147. {
  148. StringTranslate stringtranslate = StringTranslate.getInstance();
  149. controlList.add(buttonEdit = new GuiButton(7, width / 2 - 154, height - 28, 70, 20, stringtranslate.translateKey("selectServer.edit")));
  150. controlList.add(buttonDelete = new GuiButton(2, width / 2 - 74, height - 28, 70, 20, stringtranslate.translateKey("selectServer.delete")));
  151. controlList.add(buttonSelect = new GuiButton(1, width / 2 - 154, height - 52, 100, 20, stringtranslate.translateKey("selectServer.select")));
  152. controlList.add(new GuiButton(4, width / 2 - 50, height - 52, 100, 20, stringtranslate.translateKey("selectServer.direct")));
  153. controlList.add(new GuiButton(3, width / 2 + 4 + 50, height - 52, 100, 20, stringtranslate.translateKey("selectServer.add")));
  154. controlList.add(new GuiButton(8, width / 2 + 4, height - 28, 70, 20, stringtranslate.translateKey("selectServer.refresh")));
  155. controlList.add(new GuiButton(0, width / 2 + 4 + 76, height - 28, 75, 20, stringtranslate.translateKey("gui.cancel")));
  156. boolean flag = selectedServer >= 0 && selectedServer < serverSlotContainer.getSize();
  157. buttonSelect.enabled = flag;
  158. buttonEdit.enabled = flag;
  159. buttonDelete.enabled = flag;
  160. }
  161.  
  162. /**
  163. * Called when the screen is unloaded. Used to disable keyboard repeat events
  164. */
  165. public void onGuiClosed()
  166. {
  167. Keyboard.enableRepeatEvents(false);
  168. }
  169.  
  170. /**
  171. * Fired when a control is clicked. This is the equivalent of ActionListener.actionPerformed(ActionEvent e).
  172. */
  173. protected void actionPerformed(GuiButton par1GuiButton)
  174. {
  175. if (!par1GuiButton.enabled)
  176. {
  177. return;
  178. }
  179.  
  180. if (par1GuiButton.id == 2)
  181. {
  182. String s = ((ServerNBTStorage)serverList.get(selectedServer)).name;
  183.  
  184. if (s != null)
  185. {
  186. deleteClicked = true;
  187. StringTranslate stringtranslate = StringTranslate.getInstance();
  188. String s1 = stringtranslate.translateKey("selectServer.deleteQuestion");
  189. String s2 = (new StringBuilder()).append("'").append(s).append("' ").append(stringtranslate.translateKey("selectServer.deleteWarning")).toString();
  190. String s3 = stringtranslate.translateKey("selectServer.deleteButton");
  191. String s4 = stringtranslate.translateKey("gui.cancel");
  192. GuiYesNo guiyesno = new GuiYesNo(this, s1, s2, s3, s4, selectedServer);
  193. mc.displayGuiScreen(guiyesno);
  194. }
  195. }
  196. else if (par1GuiButton.id == 1)
  197. {
  198. spamServer = selectedServer;
  199. joinServer(selectedServer);
  200. }
  201. else if (par1GuiButton.id == 4)
  202. {
  203. directClicked = true;
  204. mc.displayGuiScreen(new GuiScreenServerList(this, tempServer = new ServerNBTStorage(StatCollector.translateToLocal("selectServer.defaultName"), "")));
  205. }
  206. else if (par1GuiButton.id == 3)
  207. {
  208. addClicked = true;
  209. mc.displayGuiScreen(new GuiScreenAddServer(this, tempServer = new ServerNBTStorage(StatCollector.translateToLocal("selectServer.defaultName"), "")));
  210. }
  211. else if (par1GuiButton.id == 7)
  212. {
  213. editClicked = true;
  214. ServerNBTStorage servernbtstorage = (ServerNBTStorage)serverList.get(selectedServer);
  215. mc.displayGuiScreen(new GuiScreenAddServer(this, tempServer = new ServerNBTStorage(servernbtstorage.name, servernbtstorage.host)));
  216. }
  217. else if (par1GuiButton.id == 0)
  218. {
  219. mc.displayGuiScreen(parentScreen);
  220. }
  221. else if (par1GuiButton.id == 8)
  222. {
  223. mc.displayGuiScreen(new GuiMultiplayer(parentScreen));
  224. }
  225. else
  226. {
  227. serverSlotContainer.actionPerformed(par1GuiButton);
  228. }
  229. }
  230.  
  231. /**
  232. * Deletes the selected world.
  233. */
  234. public void deleteWorld(boolean par1, int par2)
  235. {
  236. if (deleteClicked)
  237. {
  238. deleteClicked = false;
  239.  
  240. if (par1)
  241. {
  242. serverList.remove(par2);
  243. saveServerList();
  244. }
  245.  
  246. mc.displayGuiScreen(this);
  247. }
  248. else if (directClicked)
  249. {
  250. directClicked = false;
  251.  
  252. if (par1)
  253. {
  254. joinServer(tempServer);
  255. }
  256. else
  257. {
  258. mc.displayGuiScreen(this);
  259. }
  260. }
  261. else if (addClicked)
  262. {
  263. addClicked = false;
  264.  
  265. if (par1)
  266. {
  267. serverList.add(tempServer);
  268. saveServerList();
  269. }
  270.  
  271. mc.displayGuiScreen(this);
  272. }
  273. else if (editClicked)
  274. {
  275. editClicked = false;
  276.  
  277. if (par1)
  278. {
  279. ServerNBTStorage servernbtstorage = (ServerNBTStorage)serverList.get(selectedServer);
  280. servernbtstorage.name = tempServer.name;
  281. servernbtstorage.host = tempServer.host;
  282. saveServerList();
  283. }
  284.  
  285. mc.displayGuiScreen(this);
  286. }
  287. }
  288.  
  289. private static int parseIntWithDefault(String par1Str, int par2)
  290. {
  291. try
  292. {
  293. return Integer.parseInt(par1Str.trim());
  294. }
  295. catch (Exception exception)
  296. {
  297. return par2;
  298. }
  299. }
  300.  
  301. /**
  302. * Fired when a key is typed. This is the equivalent of KeyListener.keyTyped(KeyEvent e).
  303. */
  304. protected void keyTyped(char par1, int par2)
  305. {
  306. if (par1 == '\r')
  307. {
  308. actionPerformed((GuiButton)controlList.get(2));
  309. }
  310. }
  311.  
  312. /**
  313. * Called when the mouse is clicked.
  314. */
  315. protected void mouseClicked(int par1, int par2, int par3)
  316. {
  317. super.mouseClicked(par1, par2, par3);
  318. }
  319.  
  320. /**
  321. * Draws the screen and all the components in it.
  322. */
  323. public void drawScreen(int par1, int par2, float par3)
  324. {
  325. lagTooltip = null;
  326. StringTranslate stringtranslate = StringTranslate.getInstance();
  327. drawDefaultBackground();
  328. serverSlotContainer.drawScreen(par1, par2, par3);
  329. drawCenteredString(fontRenderer, stringtranslate.translateKey("multiplayer.title"), width / 2, 20, 0xffffff);
  330. super.drawScreen(par1, par2, par3);
  331.  
  332. if (lagTooltip != null)
  333. {
  334. func_35325_a(lagTooltip, par1, par2);
  335. }
  336. }
  337.  
  338. /**
  339. * Join server by slot index
  340. */
  341. static void joinServer(int par1)
  342. {
  343. joinServer((ServerNBTStorage)serverList.get(par1));
  344. }
  345.  
  346. /**
  347. * Join server by ServerNBTStorage
  348. */
  349. private static void joinServer(ServerNBTStorage par1ServerNBTStorage)
  350. {
  351. String s = par1ServerNBTStorage.host;
  352. String as[] = s.split(":");
  353.  
  354. if (s.startsWith("["))
  355. {
  356. int i = s.indexOf("]");
  357.  
  358. if (i > 0)
  359. {
  360. String s1 = s.substring(1, i);
  361. String s2 = s.substring(i + 1).trim();
  362.  
  363. if (s2.startsWith(":") && s2.length() > 0)
  364. {
  365. s2 = s2.substring(1);
  366. as = new String[2];
  367. as[0] = s1;
  368. as[1] = s2;
  369. }
  370. else
  371. {
  372. as = new String[1];
  373. as[0] = s1;
  374. }
  375. }
  376. }
  377.  
  378. if (as.length > 2)
  379. {
  380. as = new String[1];
  381. as[0] = s;
  382. }
  383.  
  384. mc.displayGuiScreen(new GuiConnecting(mc, as[0], as.length > 1 ? parseIntWithDefault(as[1], 25565) : 25565));
  385. }
  386.  
  387. /**
  388. * Poll server for MOTD, lag, and player count/max
  389. */
  390. private void pollServer(ServerNBTStorage par1ServerNBTStorage) throws IOException
  391. {
  392. String s = par1ServerNBTStorage.host;
  393. String as[] = s.split(":");
  394.  
  395. if (s.startsWith("["))
  396. {
  397. int i = s.indexOf("]");
  398.  
  399. if (i > 0)
  400. {
  401. String s2 = s.substring(1, i);
  402. String s3 = s.substring(i + 1).trim();
  403.  
  404. if (s3.startsWith(":") && s3.length() > 0)
  405. {
  406. s3 = s3.substring(1);
  407. as = new String[2];
  408. as[0] = s2;
  409. as[1] = s3;
  410. }
  411. else
  412. {
  413. as = new String[1];
  414. as[0] = s2;
  415. }
  416. }
  417. }
  418.  
  419. if (as.length > 2)
  420. {
  421. as = new String[1];
  422. as[0] = s;
  423. }
  424.  
  425. String s1 = as[0];
  426. int j = as.length > 1 ? parseIntWithDefault(as[1], 25565) : 25565;
  427. Socket socket = null;
  428. DataInputStream datainputstream = null;
  429. DataOutputStream dataoutputstream = null;
  430.  
  431. try
  432. {
  433. socket = new Socket();
  434. socket.setSoTimeout(3000);
  435. socket.setTcpNoDelay(true);
  436. socket.setTrafficClass(18);
  437. socket.connect(new InetSocketAddress(s1, j), 3000);
  438. datainputstream = new DataInputStream(socket.getInputStream());
  439. dataoutputstream = new DataOutputStream(socket.getOutputStream());
  440. dataoutputstream.write(254);
  441.  
  442. if (datainputstream.read() != 255)
  443. {
  444. throw new IOException("Bad message");
  445. }
  446.  
  447. String s4 = Packet.readString(datainputstream, 256);
  448. char ac[] = s4.toCharArray();
  449.  
  450. for (int k = 0; k < ac.length; k++)
  451. {
  452. if (ac[k] != '\247' && ChatAllowedCharacters.allowedCharacters.indexOf(ac[k]) < 0)
  453. {
  454. ac[k] = '?';
  455. }
  456. }
  457.  
  458. s4 = new String(ac);
  459. String as1[] = s4.split("\247");
  460. s4 = as1[0];
  461. int l = -1;
  462. int i1 = -1;
  463.  
  464. try
  465. {
  466. l = Integer.parseInt(as1[1]);
  467. i1 = Integer.parseInt(as1[2]);
  468. }
  469. catch (Exception exception) { }
  470.  
  471. par1ServerNBTStorage.motd = (new StringBuilder()).append("\2477").append(s4).toString();
  472.  
  473. if (l >= 0 && i1 > 0)
  474. {
  475. par1ServerNBTStorage.playerCount = (new StringBuilder()).append("\2477").append(l).append("\2478/\2477").append(i1).toString();
  476. }
  477. else
  478. {
  479. par1ServerNBTStorage.playerCount = "\2478???";
  480. }
  481. }
  482. finally
  483. {
  484. try
  485. {
  486. if (datainputstream != null)
  487. {
  488. datainputstream.close();
  489. }
  490. }
  491. catch (Throwable throwable) { }
  492.  
  493. try
  494. {
  495. if (dataoutputstream != null)
  496. {
  497. dataoutputstream.close();
  498. }
  499. }
  500. catch (Throwable throwable1) { }
  501.  
  502. try
  503. {
  504. if (socket != null)
  505. {
  506. socket.close();
  507. }
  508. }
  509. catch (Throwable throwable2) { }
  510. }
  511. }
  512.  
  513. protected void func_35325_a(String par1Str, int par2, int par3)
  514. {
  515. if (par1Str == null)
  516. {
  517. return;
  518. }
  519. else
  520. {
  521. int i = par2 + 12;
  522. int j = par3 - 12;
  523. int k = fontRenderer.getStringWidth(par1Str);
  524. drawGradientRect(i - 3, j - 3, i + k + 3, j + 8 + 3, 0xc0000000, 0xc0000000);
  525. fontRenderer.func_50103_a(par1Str, i, j, -1);
  526. return;
  527. }
  528. }
  529.  
  530. /**
  531. * Return the List of ServerNBTStorage objects
  532. */
  533. static List getServerList(GuiMultiplayer par0GuiMultiplayer)
  534. {
  535. return par0GuiMultiplayer.serverList;
  536. }
  537.  
  538. /**
  539. * Set index of the currently selected server
  540. */
  541. static int setSelectedServer(GuiMultiplayer par0GuiMultiplayer, int par1)
  542. {
  543. return par0GuiMultiplayer.selectedServer = par1;
  544. }
  545.  
  546. /**
  547. * Return index of the currently selected server
  548. */
  549. static int getSelectedServer(GuiMultiplayer par0GuiMultiplayer)
  550. {
  551. return par0GuiMultiplayer.selectedServer;
  552. }
  553.  
  554. /**
  555. * Return buttonSelect GuiButton
  556. */
  557. static GuiButton getButtonSelect(GuiMultiplayer par0GuiMultiplayer)
  558. {
  559. return par0GuiMultiplayer.buttonSelect;
  560. }
  561.  
  562. /**
  563. * Return buttonEdit GuiButton
  564. */
  565. static GuiButton getButtonEdit(GuiMultiplayer par0GuiMultiplayer)
  566. {
  567. return par0GuiMultiplayer.buttonEdit;
  568. }
  569.  
  570. /**
  571. * Return buttonDelete GuiButton
  572. */
  573. static GuiButton getButtonDelete(GuiMultiplayer par0GuiMultiplayer)
  574. {
  575. return par0GuiMultiplayer.buttonDelete;
  576. }
  577.  
  578. /**
  579. * Join server by slot index (called on double click from GuiSlotServer)
  580. */
  581. static void joinServer(GuiMultiplayer par0GuiMultiplayer, int par1)
  582. {
  583. par0GuiMultiplayer.joinServer(par1);
  584. }
  585.  
  586. /**
  587. * Get lock object for use with synchronized()
  588. */
  589. static Object getLock()
  590. {
  591. return lock;
  592. }
  593.  
  594. /**
  595. * Return number of outstanding ThreadPollServers threads
  596. */
  597. static int getThreadsPending()
  598. {
  599. return threadsPending;
  600. }
  601.  
  602. /**
  603. * Increment number of outstanding ThreadPollServers threads by 1
  604. */
  605. static int incrementThreadsPending()
  606. {
  607. return threadsPending++;
  608. }
  609.  
  610. /**
  611. * Poll server for MOTD, lag, and player count/max
  612. */
  613. static void pollServer(GuiMultiplayer par0GuiMultiplayer, ServerNBTStorage par1ServerNBTStorage) throws IOException
  614. {
  615. par0GuiMultiplayer.pollServer(par1ServerNBTStorage);
  616. }
  617.  
  618. /**
  619. * Decrement number of outstanding ThreadPollServers threads by 1
  620. */
  621. static int decrementThreadsPending()
  622. {
  623. return threadsPending--;
  624. }
  625.  
  626. /**
  627. * Sets a GUI's lag tooltip text.
  628. */
  629. static String setTooltipText(GuiMultiplayer par0GuiMultiplayer, String par1Str)
  630. {
  631. return par0GuiMultiplayer.lagTooltip = par1Str;
  632. }
  633. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement