Advertisement
Guest User

Untitled

a guest
Dec 9th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. package com.arlania;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. import com.arlania.world.entity.impl.player.Player;
  10.  
  11. /**
  12. * Using this class:
  13. * To call this class, it's best to make a new thread. You can do it below like so:
  14. * new Thread(new Donation(player)).start();
  15. */
  16. public class Donation implements Runnable {
  17.  
  18. public static final String HOST = ""; // website ip address
  19. public static final String USER = "";
  20. public static final String PASS = "";
  21. public static final String DATABASE = "";
  22. private Player player;
  23. private Connection conn;
  24. private Statement stmt;
  25.  
  26. /**
  27. * The constructor
  28. * @param player
  29. */
  30. public Donation(Player player) {
  31. this.player = player;
  32. }
  33.  
  34. @Override
  35. public void run() {
  36. try {
  37. if (!connect(HOST, DATABASE, USER, PASS)) {
  38. return;
  39. }
  40.  
  41. String name = player.getUsername().replace("_", " ");
  42. ResultSet rs = executeQuery("SELECT * FROM payments WHERE player_name='"+name+"' AND status='Completed' AND claimed=0");
  43.  
  44. while (rs.next()) {
  45. int item_number = rs.getInt("item_number");
  46. double paid = rs.getDouble("amount");
  47. int quantity = rs.getInt("quantity");
  48.  
  49. switch (item_number) {// add products according to their ID in the ACP
  50.  
  51. case 0:
  52. player.sendMessage("Your donation was not found please Contact Jake!");
  53. break;
  54. case 18: // $10 Scroll
  55. //Save users current Donator Rank
  56. player.setRights(PlayerRights.PLAYER); //Set rights to regular player
  57. if (paid == 10 * quantity) {
  58. player.getInventory().add(15355, 1 * quantity);
  59. }
  60. //Give user back previous Donator Rank
  61. player.getPacketSender().sendRights(); //Updates player rank
  62. break;
  63. case 19: // $5 Scroll
  64. if (paid == 5 * quantity) {
  65. player.getInventory().add(15356, 1 * quantity);
  66. }
  67. break;
  68. case 20: // $25 Scroll
  69. if (paid == 25 * quantity) {
  70. player.getInventory().add(15359, 1 * quantity);
  71. }
  72. break;
  73. case 21: // $50 Scroll
  74. if (paid == 50 * quantity) {
  75. player.getInventory().add(15358, 1 * quantity);
  76. }
  77. break;
  78. }
  79.  
  80. rs.updateInt("claimed", 1); // do not delete otherwise they can reclaim!
  81. rs.updateRow();
  82. }
  83.  
  84. destroy();
  85. } catch (Exception e) {
  86. e.printStackTrace();
  87. }
  88. }
  89.  
  90. /**
  91. *
  92. * @param host the host ip address or url
  93. * @param database the name of the database
  94. * @param user the user attached to the database
  95. * @param pass the users password
  96. * @return true if connected
  97. */
  98. public boolean connect(String host, String database, String user, String pass) {
  99. try {
  100. this.conn = DriverManager.getConnection("jdbc:mysql://"+host+":3306/"+database, user, pass);
  101. return true;
  102. } catch (SQLException e) {
  103. System.out.println("Failing connecting to database!");
  104. return false;
  105. }
  106. }
  107.  
  108. /**
  109. * Disconnects from the MySQL server and destroy the connection
  110. * and statement instances
  111. */
  112. public void destroy() {
  113. try {
  114. conn.close();
  115. conn = null;
  116. if (stmt != null) {
  117. stmt.close();
  118. stmt = null;
  119. }
  120. } catch(Exception e) {
  121. e.printStackTrace();
  122. }
  123. }
  124.  
  125. /**
  126. * Executes an update query on the database
  127. * @param query
  128. * @see {@link Statement#executeUpdate}
  129. */
  130. public int executeUpdate(String query) {
  131. try {
  132. this.stmt = this.conn.createStatement(1005, 1008);
  133. int results = stmt.executeUpdate(query);
  134. return results;
  135. } catch (SQLException ex) {
  136. ex.printStackTrace();
  137. }
  138. return -1;
  139. }
  140.  
  141. /**
  142. * Executres a query on the database
  143. * @param query
  144. * @see {@link Statement#executeQuery(String)}
  145. * @return the results, never null
  146. */
  147. public ResultSet executeQuery(String query) {
  148. try {
  149. this.stmt = this.conn.createStatement(1005, 1008);
  150. ResultSet results = stmt.executeQuery(query);
  151. return results;
  152. } catch (SQLException ex) {
  153. ex.printStackTrace();
  154. }
  155. return null;
  156. }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement