Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 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. // The player exists in database.
  42. group_loaded = result.getInt("group");
  43. permissions_loaded = result.getString("permissions");
  44. prefix_loaded = result.getString("prefix");
  45.  
  46. // Check if name mismatch, update player name
  47. String playerName_loaded = result.getString("name");
  48. String playerName = p.getName();
  49. if(!playerName_loaded.equals(playerName))
  50. {
  51. s = conn.prepareStatement("UPDATE Players SET `name`=? WHERE `uuid`=?;");
  52. s.setString(1, p.getName());
  53. s.setString(2, p.getUniqueId().toString());
  54. s.execute();
  55. }
  56. }
  57. else
  58. {
  59. // The player might exist in database but has no UUID yet.
  60. s = conn.prepareStatement("SELECT * FROM Players WHERE `name`=?");
  61. s.setString(1, p.getName());
  62. s.execute();
  63. result = s.getResultSet();
  64. if (result.next())
  65. {
  66. // Player exists in database but has no UUID. Lets enter it.
  67. s = conn.prepareStatement("UPDATE Players SET `uuid`=? WHERE `name`=?;");
  68. s.setString(1, p.getUniqueId().toString());
  69. s.setString(2, p.getName());
  70. s.execute();
  71. // UUID has been entered into player. Lets continue.
  72. group_loaded = result.getInt("group");
  73. permissions_loaded = result.getString("permissions");
  74. prefix_loaded = result.getString("prefix");
  75. s.close();
  76. }
  77. else
  78. {
  79. // Player does not exist in database. Create a new player.
  80. s.close();
  81. s = conn.prepareStatement("INSERT INTO Players SET `uuid`=?, `name`=?;");
  82. s.setString(1, p.getUniqueId().toString());
  83. s.setString(2, p.getName());
  84. s.execute();
  85. s.close();
  86. }
  87. }
  88. s.close();
  89. }
  90. catch (SQLException ex)
  91. {
  92. ex.printStackTrace();
  93. }
  94. }
  95.  
  96. public static int getCoins(Player p)
  97. {
  98. PreparedStatement s;
  99. try
  100. {
  101. s = PermissionManager.conn.prepareStatement("SELECT * FROM Players WHERE `uuid`=?");
  102. s.setString(1, p.getUniqueId().toString());
  103. s.execute();
  104. ResultSet result = s.getResultSet();
  105. if(result.next())
  106. {
  107. return result.getInt("coins");
  108. }
  109. else
  110. Bukkit.getLogger().severe("Could not get coins of an online player! Player does not exist in database!");
  111. }
  112. catch (SQLException e)
  113. {
  114. e.printStackTrace();
  115. }
  116. return 0;
  117. }
  118.  
  119. public static int getChips(Player p)
  120. {
  121. PreparedStatement s;
  122. try
  123. {
  124. s = PermissionManager.conn.prepareStatement("SELECT * FROM Players WHERE `uuid`=?");
  125. s.setString(1, p.getUniqueId().toString());
  126. s.execute();
  127. ResultSet result = s.getResultSet();
  128. if(result.next())
  129. {
  130. return result.getInt("chips");
  131. }
  132. else
  133. Bukkit.getLogger().severe("Could not get coins of an online player! Player does not exist in database!");
  134. }
  135. catch (SQLException e)
  136. {
  137. e.printStackTrace();
  138. }
  139. return 0;
  140. }
  141.  
  142. public static void setCoins(Player p, int i)
  143. {
  144. PreparedStatement s;
  145. try
  146. {
  147. s = PermissionManager.conn.prepareStatement("UPDATE Players SET `coins`=? WHERE `uuid`=?");
  148. s.setInt(1, i);
  149. s.setString(2, p.getUniqueId().toString());
  150. s.execute();
  151. }
  152. catch (SQLException e)
  153. {
  154. e.printStackTrace();
  155. }
  156. }
  157.  
  158. public static void setChips(Player p, int i)
  159. {
  160. PreparedStatement s;
  161. try
  162. {
  163. s = PermissionManager.conn.prepareStatement("UPDATE Players SET `chips`=? WHERE `uuid`=?");
  164. s.setInt(1, i);
  165. s.setString(2, p.getUniqueId().toString());
  166. s.execute();
  167. }
  168. catch (SQLException e)
  169. {
  170. e.printStackTrace();
  171. }
  172. }
  173.  
  174. public static void removeCoins(Player p, int amount)
  175. {
  176. setCoins(p, (getCoins(p) - amount));
  177. }
  178.  
  179. public static void addCoins(Player p, int amount)
  180. {
  181. setCoins(p, (getCoins(p) + amount));
  182. }
  183.  
  184. public static void removeChips(Player p, int i)
  185. {
  186. int bal = getChips(p);
  187. bal = bal - i;
  188. setChips(p, bal);
  189. }
  190.  
  191. public static void addChips(Player p, int i)
  192. {
  193. int bal = getChips(p);
  194. bal = bal + i;
  195. setChips(p, bal);
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement