Advertisement
Guest User

Untitled

a guest
Sep 17th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.35 KB | None | 0 0
  1. Time to add highscores to 562.
  2. This is server sided maybe i make another website sided
  3.  
  4. Go to src -> com -> rs2hd and add: Hiscores.java
  5. Hiscores.java
  6. Code: (Click Here to Select All)
  7. package com.rs2hd;
  8.  
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.sql.Statement;
  14.  
  15. import com.rs2hd.model.PlayerDetails;
  16. import com.rs2hd.model.Skills;
  17. import com.rs2hd.model.Player;
  18.  
  19. public class Hiscores {
  20.  
  21. public static Connection con = null;
  22. public static Statement stmt;
  23. public static boolean connectionMade;
  24.  
  25. public static void createConnection() {
  26. try {
  27. Class.forName("com.mysql.jdbc.Driver").newInstance();
  28. String IP="localhost:yourport";
  29. String DB="highscores";
  30. String User="mysqlusername";
  31. String Pass="mysqlpassword";
  32. con = DriverManager.getConnection("jdbc:mysql://"+IP+"/"+DB, User, Pass);
  33. stmt = con.createStatement();
  34. System.out.println("Connection to SQL database successful!");
  35. } catch (Exception e) {
  36. System.out.println("Connection to SQL database failed");
  37. e.printStackTrace();
  38. }
  39. }
  40.  
  41.  
  42. public static ResultSet query(String s) throws SQLException {
  43. try {
  44. if (s.toLowerCase().startsWith("select")) {
  45. ResultSet rs = stmt.executeQuery(s);
  46. return rs;
  47. } else {
  48. stmt.executeUpdate(s);
  49. }
  50. return null;
  51. } catch (Exception e) {
  52. destroyConnection();
  53. }
  54. return null;
  55. }
  56.  
  57. public static void destroyConnection() {
  58. try {
  59. stmt.close();
  60. con.close();
  61. } catch (Exception e) {
  62. }
  63. }
  64.  
  65. public static boolean saveHighScore(Player player) {
  66. try {
  67. Skills skills = player.getSkills();
  68. int[] overall = getOverall(player);
  69. query("DELETE FROM `skills` WHERE playerName = '"+player.getDisplayName()+"';");
  70. query("DELETE FROM `skillsoverall` WHERE playerName = '"+player.getDisplayName()+"';");
  71. if(player.getRights() == 2) {
  72. return true;
  73. }
  74. query("INSERT INTO `skills` (`playerName`,`Attacklvl`,`Attackxp`,`Defencelvl`,`Defencexp`,`Strengthlvl`,`Strengthxp`,`Hitpointslvl`,`Hitpointsxp`,`Rangelvl`,`Rangexp`,`Prayerlvl`,`Prayerxp`,`Magiclvl`,`Magicxp`,`Cookinglvl`,`Cookingxp`,`Woodcuttinglvl`,`Woodcuttingxp`,`Fletchinglvl`,`Fletchingxp`,`Fishinglvl`,`Fishingxp`,`Firemakinglvl`,`Firemakingxp`,`Craftinglvl`,`Craftingxp`,`Smithinglvl`,`Smithingxp`,`Mininglvl`,`Miningxp`,`Herblorelvl`,`Herblorexp`,`Agilitylvl`,`Agilityxp`,`Thievinglvl`,`Thievingxp`,`Slayerlvl`,`Slayerxp`,`Farminglvl`,`Farmingxp`,`Runecraftlvl`,`Runecraftxp`, `Hunterlvl`, `Hunterxp`, `Constructionlvl`, `Constructionxp`, `Summoninglvl`, `Summoningxp`) VALUES ('"+player.getDisplayName()+"',"+skills.getLevel(0)+","+skills.getXp(0)+","+skills.getLevel(1)+","+skills.getXp(1)+","+skills.getLevel(2)+","+skills.getXp(2)+","+skills.getLevel(3)+","+skills.getXp(3)+","+skills.getLevel(4)+","+skills.getXp(4)+","+skills.getLevel(5)+","+skills.getXp(5)+","+skills.getLevel(6)+","+skills.getXp(6)+","+skills.getLevel(7)+","+skills.getXp(7)+","+skills.getLevel(8)+","+skills.getXp(8)+","+skills.getLevel(9)+","+skills.getXp(9)+","+skills.getLevel(10)+","+skills.getXp(10)+","+skills.getLevel(11)+","+skills.getXp(11)+","+skills.getLevel(12)+","+skills.getXp(12)+","+skills.getLevel(13)+","+skills.getXp(13)+","+skills.getLevel(14)+","+skills.getXp(14)+","+skills.getLevel(15)+","+skills.getXp(15)+","+skills.getLevel(16)+","+skills.getXp(16)+","+skills.getLevel(17)+","+skills.getXp(17)+","+skills.getLevel(18)+","+skills.getXp(18)+","+skills.getLevel(19)+","+skills.getXp(19)+","+skills.getLevel(20)+","+skills.getXp(20)+"," + skills.getLevel(21)+"," + skills.getXp(21) + "," +skills.getLevel(22) + "," + skills.getXp(22) + "," + skills.getLevel(23) + "," + skills.getXp(23)+");");
  75. query("INSERT INTO `skillsoverall` (`playerName`,`lvl`,`xp`) VALUES ('"+player.getDisplayName()+"'," + overall[0] +"," + overall[1] +");");
  76. //query("INSERT INTO `skillsoverall` (`playerName`,`lvl`,`xp`) VALUES ('"+player.getDisplayName()+"',"+(skills.getLevelForXp(0) + skills.getLevelForXp(1) + skills.getLevelForXp(2) + skills.getLevelForXp(3) + skills.getLevelForXp(4) + skills.getLevelForXp(5) + skills.getLevelForXp(6) + skills.getLevelForXp(7) + skills.getLevelForXp(8) + skills.getLevelForXp(9) + skills.getLevelForXp(10) + skills.getLevelForXp(11) + skills.getLevelForXp(12) + skills.getLevelForXp(13) + skills.getLevelForXp(14) + skills.getLevelForXp(15) + skills.getLevelForXp(16) + skills.getLevelForXp(17) + skills.getLevelForXp(18) + skills.getLevelForXp(19) + skills.getLevelForXp(20))+","+((skills.getXp(0)) + (skills.getXp(1)) + (skills.getXp(2)) + (skills.getXp(3)) + (skills.getXp(4)) + (skills.getXp(5)) + (skills.getXp(6)) + (skills.getXp(7)) + (skills.getXp(8)) + (skills.getXp(9)) + (skills.getXp(10)) + (skills.getXp(11)) + (skills.getXp(12)) + (skills.getXp(13)) + (skills.getXp(14)) + (skills.getXp(15)) + (skills.getXp(16)) + (skills.getXp(17)) + (skills.getXp(18)) + (skills.getXp(19)) + (skills.getXp(20)))+");");
  77. } catch (Exception e) {
  78. return false;
  79. }
  80. return true;
  81. }
  82.  
  83. public static void restore(Player player) throws SQLException {
  84. createConnection();
  85. Statement statement = con.createStatement();
  86. String query = "SELECT * FROM skills WHERE playerName = '" + player.getDisplayName() + "'";
  87. ResultSet results = statement.executeQuery(query);
  88. if (results.next()) {
  89. for(int i = 0; i < Skills.SKILL_COUNT; i++) {
  90. //int level = (int) results.getDouble(Skills.SKILL_NAME[i] +"lvl");
  91. //int xp = (int) results.getDouble(Skills.SKILL_NAME[i] +"xp");
  92. }
  93. }
  94. destroyConnection();
  95. }
  96.  
  97. public static int[] getOverall(Player player) {
  98. int totalLevel = 0;
  99. int totalXp = 0;
  100. for(int i = 0; i < Skills.SKILL_COUNT; i++) {
  101. totalLevel += player.getSkills().getLevelForXp(i);
  102. }
  103. for(int i = 0; i < Skills.SKILL_COUNT; i++) {
  104. totalXp += player.getSkills().getXp(i);
  105. }
  106. return new int[] {totalLevel, totalXp};
  107. }
  108. }
  109.  
  110. Edit:
  111. String IP="localhost:yourport";
  112. String DB="highscores";
  113. String User="root";
  114. String Pass="mysql password";
  115.  
  116. Only add that port if you host it online else: localhost or 127.0.0.1
  117. For starts: you need to edit that in hiscores.java
  118.  
  119. Go to src -> com -> rs2hd -> model -> world.java
  120. Add these imports:
  121. Code: (Click Here to Select All)
  122. import java.util.concurrent.ExecutorService;
  123. import java.util.concurrent.Executors;
  124. import java.util.concurrent.TimeUnit;
  125. import java.util.LinkedList;
  126.  
  127. Search for:
  128. Code: (Click Here to Select All)
  129. private Configuration configuration;
  130.  
  131. under that method:
  132. Code: (Click Here to Select All)
  133. /**
  134. * The configuration.
  135. */
  136. private Configuration configuration;
  137.  
  138. add:
  139.  
  140. Code: (Click Here to Select All)
  141. /**
  142. * Highscores executor
  143. */
  144. private ExecutorService hiscoreExcutor = Executors.newSingleThreadExecutor();
  145.  
  146. Search for:
  147. Code: (Click Here to Select All)
  148. public void unregister(Player p)
  149.  
  150. Under that method add:
  151.  
  152. Code: (Click Here to Select All)
  153. public void saveHighscores(final Player p) {
  154. hiscoreExcutor.submit(new Runnable() {
  155. @Override
  156. public void run() {
  157. try {
  158. Hiscores.createConnection();
  159. Hiscores.saveHighScore(p);
  160. } catch(Exception e) {
  161. e.printStackTrace();
  162. }
  163. }
  164. });
  165. }
  166.  
  167.  
  168. If i forget something then say please.
  169.  
  170. btw:
  171. Credits:
  172. Tutorial: 100% to me,
  173. hiscores.java 100% to noszscape,
  174. world.java 70% to noszscape and 30% to me.
  175.  
  176.  
  177.  
  178.  
  179. My first Tut?!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement