Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. public static Connection conn;
  2.  
  3. public static String host = "jdbc:mysql://localhost:3306/TableName";
  4. public static String username = "root";
  5. public static String password = "examplepassword";
  6.  
  7.  
  8. //MAKE SURE THIS IS IN YOUR ONENABLE!!
  9. public static void setupConnection(){
  10. try
  11. {
  12. Class.forName("com.mysql.jdbc.Driver");
  13. conn = DriverManager.getConnection(host, username, password);
  14. Bukkit.getLogger().info("Permissions connected to MySQL");
  15. LoadGroups();
  16. }
  17. catch (Exception ex)
  18. {
  19. ex.printStackTrace();
  20. }
  21. }
  22.  
  23. //MAKE SURE THIS RUNS EVERY TIME A PLAYER LOGS IN
  24. private static void LoadPlayer(Player p)
  25. {
  26. /**
  27. * Loads player data from MySQL, removes old data
  28. */
  29. try
  30. {
  31. int group_loaded = 1;
  32. String permissions_loaded = "";
  33. String prefix_loaded = "";
  34.  
  35. PreparedStatement s = conn.prepareStatement("SELECT * FROM Players WHERE `uuid`=?");
  36. s.setString(1, p.getUniqueId().toString());
  37. s.execute();
  38. ResultSet result = s.getResultSet();
  39. if (result.next())
  40. {
  41. String playerName_loaded = result.getString("name");
  42. String playerName = p.getName();
  43. if(!playerName_loaded.equals(playerName))
  44. {
  45. s = conn.prepareStatement("UPDATE Players SET `name`=? WHERE `uuid`=?;");
  46. s.setString(1, p.getName());
  47. s.setString(2, p.getUniqueId().toString());
  48. s.execute();
  49. }
  50. }
  51. else
  52. {
  53. // The player might exist in database but has no UUID yet.
  54. s = conn.prepareStatement("SELECT * FROM Players WHERE `name`=?");
  55. s.setString(1, p.getName());
  56. s.execute();
  57. result = s.getResultSet();
  58. if (result.next())
  59. {
  60. // Player exists in database but has no UUID. Lets enter it.
  61. s = conn.prepareStatement("UPDATE Players SET `uuid`=? WHERE `name`=?;");
  62. s.setString(1, p.getUniqueId().toString());
  63. s.setString(2, p.getName());
  64. s.execute();
  65. // UUID has been entered into player. Lets continue.
  66. group_loaded = result.getInt("group");
  67. permissions_loaded = result.getString("permissions");
  68. prefix_loaded = result.getString("prefix");
  69. s.close();
  70. }
  71. else
  72. {
  73. // Player does not exist in database. Create a new player.
  74. s.close();
  75. s = conn.prepareStatement("INSERT INTO Players SET `uuid`=?, `name`=?, 'coins'=0;");
  76. s.setString(1, p.getUniqueId().toString());
  77. s.setString(2, p.getName());
  78. s.execute();
  79. s.close();
  80. }
  81. }
  82. s.close();
  83. }
  84. catch (SQLException ex)
  85. {
  86. ex.printStackTrace();
  87. }
  88. }
  89.  
  90. public static int getCoins(Player p)
  91. {
  92. PreparedStatement s;
  93. try
  94. {
  95. s = PermissionManager.conn.prepareStatement("SELECT * FROM Players WHERE `uuid`=?");
  96. s.setString(1, p.getUniqueId().toString());
  97. s.execute();
  98. ResultSet result = s.getResultSet();
  99. if(result.next())
  100. {
  101. return result.getInt("coins");
  102. }
  103. else
  104. Bukkit.getLogger().severe("Could not get coins of an online player! Player does not exist in database!");
  105. }
  106. catch (SQLException e)
  107. {
  108. e.printStackTrace();
  109. }
  110. return 0;
  111. }
  112.  
  113. public static int getChips(Player p)
  114. {
  115. PreparedStatement s;
  116. try
  117. {
  118. s = PermissionManager.conn.prepareStatement("SELECT * FROM Players WHERE `uuid`=?");
  119. s.setString(1, p.getUniqueId().toString());
  120. s.execute();
  121. ResultSet result = s.getResultSet();
  122. if(result.next())
  123. {
  124. return result.getInt("chips");
  125. }
  126. else
  127. Bukkit.getLogger().severe("Could not get coins of an online player! Player does not exist in database!");
  128. }
  129. catch (SQLException e)
  130. {
  131. e.printStackTrace();
  132. }
  133. return 0;
  134. }
  135.  
  136. public static void setCoins(Player p, int i)
  137. {
  138. PreparedStatement s;
  139. try
  140. {
  141. s = PermissionManager.conn.prepareStatement("UPDATE Players SET `coins`=? WHERE `uuid`=?");
  142. s.setInt(1, i);
  143. s.setString(2, p.getUniqueId().toString());
  144. s.execute();
  145. }
  146. catch (SQLException e)
  147. {
  148. e.printStackTrace();
  149. }
  150. }
  151.  
  152. public static void setChips(Player p, int i)
  153. {
  154. PreparedStatement s;
  155. try
  156. {
  157. s = PermissionManager.conn.prepareStatement("UPDATE Players SET `chips`=? WHERE `uuid`=?");
  158. s.setInt(1, i);
  159. s.setString(2, p.getUniqueId().toString());
  160. s.execute();
  161. }
  162. catch (SQLException e)
  163. {
  164. e.printStackTrace();
  165. }
  166. }
  167.  
  168. public static void removeCoins(Player p, int amount)
  169. {
  170. setCoins(p, (getCoins(p) - amount));
  171. }
  172.  
  173. public static void addCoins(Player p, int amount)
  174. {
  175. setCoins(p, (getCoins(p) + amount));
  176. }
  177.  
  178. public static void removeChips(Player p, int i)
  179. {
  180. int bal = getChips(p);
  181. bal = bal - i;
  182. setChips(p, bal);
  183. }
  184.  
  185. public static void addChips(Player p, int i)
  186. {
  187. int bal = getChips(p);
  188. bal = bal + i;
  189. setChips(p, bal);
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement