Advertisement
Guest User

asf

a guest
Jul 3rd, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. package net.arrav.sql.database.util;
  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. import java.util.Collections;
  9. import java.util.EnumSet;
  10. import java.util.Set;
  11.  
  12. import net.arrav.model.entity.player.Player;
  13.  
  14. public class DonationConnector {
  15.  
  16. /**
  17. * The player that we are giving the points to.
  18. */
  19. private Player player;
  20.  
  21. private enum DonatorRanks {
  22. DONATOR(7, 10, 50, "@red@Donator", 1),
  23. SUPER_DONATOR(6, 50, 250, "@blu@Super Donator", 2),
  24. EXTREME_DONATOR(5, 250, 1000, "@gre@Extreme Donator", 3);
  25. private final int rank, min, max, drank;
  26. private final String string;
  27.  
  28. private DonatorRanks(int rank, int min, int max, String string, int drank) {
  29. this.rank = rank;
  30. this.min = min;
  31. this.max = max;
  32. this.string = string;
  33. this.drank = drank;
  34. }
  35.  
  36. public static Set<DonatorRanks> RANKS = Collections.unmodifiableSet(EnumSet.allOf(DonatorRanks.class));
  37.  
  38. public int getRank() {
  39. return rank;
  40. }
  41.  
  42. public int getMin() {
  43. return min;
  44. }
  45.  
  46. public int getMax() {
  47. return max;
  48. }
  49.  
  50. public String getString() {
  51. return string;
  52. }
  53. // keyboard shortcut alt + shift + s
  54.  
  55. public Object getDRank() {
  56. return drank;
  57. }
  58. }
  59.  
  60. /**
  61. * The amount of donation points we are gonna give the player.
  62. */
  63. private int amount;
  64.  
  65. public static boolean connected = false;
  66. private static Connection connection;
  67. private static long lasConnection = System.currentTimeMillis();
  68.  
  69. static {
  70. createConnection();
  71. }
  72.  
  73. public static void createConnection() {
  74. try {
  75. Class.forName("com.mysql.jdbc.Driver").newInstance();
  76.  
  77. connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/DBNAME", "USERNAME", "PASSWORD");
  78. connected = true;
  79. System.out.println("[SQL] Donation Connected.");
  80. } catch (Exception e) {
  81. e.printStackTrace();
  82. connected = false;
  83. System.out.println("[SQL] Donation error Connecting.");
  84. }
  85.  
  86. }
  87.  
  88. public static void destroyConnection() {
  89. try {
  90. if (connection != null)
  91. connection.close();
  92. } catch (SQLException e) {
  93. e.printStackTrace();
  94. }
  95. }
  96.  
  97. public static void claimPayment(final Player p, final String name) {
  98. try {
  99. if (System.currentTimeMillis() - lasConnection > 10000) {
  100. destroyConnection();
  101. createConnection();
  102. lasConnection = System.currentTimeMillis();
  103. }
  104. Statement s = connection.createStatement();
  105. String name2 = name.replaceAll(" ", "_");
  106. String query = "SELECT * FROM paid_donations WHERE username = '" + name2 + "' AND claimed = '0'";
  107. ResultSet rs = s.executeQuery(query);
  108. boolean claimed = false;
  109. while (rs.next()) {
  110. int prod = Integer.parseInt(rs.getString("product_id"));
  111. double price = Double.parseDouble(rs.getString("price"));
  112. if (prod == 1 && price == 10.00) {
  113. p.donatorPoints += 100;
  114. p.incrementAmountDonated(10);
  115. p.getActionSender().sendMessage("You have received 100 Donation Points from Donating.");
  116. claimed = true;
  117. } else if (prod == 2 && price == 25.00) {
  118. p.donatorPoints += 250;
  119. p.incrementAmountDonated(25);
  120. p.getActionSender().sendMessage("You have received 250 Donation Points from Donating.");
  121. claimed = true;
  122. } else if (prod == 3 && price == 45.00) {
  123. p.donatorPoints += 500;
  124. p.incrementAmountDonated(45);
  125. p.getActionSender().sendMessage("You have received 500 Donation Points from Donating.");
  126. claimed = true;
  127. } else if (prod == 4 && price == 85.00) {
  128. p.donatorPoints += 1000;
  129. p.incrementAmountDonated(85);
  130. p.getActionSender().sendMessage("You have received 1,000 Donation Points from Donating.");
  131. claimed = true;
  132. } else if (prod == 5 && price == 165.00) {
  133. p.donatorPoints += 2000;
  134. p.incrementAmountDonated(165);
  135. p.getActionSender().sendMessage("You have received 2,000 Donation Points from Donating.");
  136. claimed = true;
  137. } else if (prod == 6 && price == 420.00) {
  138. p.donatorPoints += 5000;
  139. p.incrementAmountDonated(420);
  140. p.getActionSender().sendMessage("You have received 5,000 Donation Points from Donating.");
  141. claimed = true;
  142. }
  143. }
  144.  
  145. boolean isStaff = (p.getPrivileges() == 1 || p.getPrivileges() == 2 || p.getPrivileges() == 3);
  146. if (!isStaff) {
  147. for (DonatorRanks rank : DonatorRanks.RANKS) {
  148. if (p.amountDonated >= rank.getMin() && p.amountDonated < rank.getMax()) {
  149. if (p.getPrivileges() != rank.getRank()) {
  150. p.setPrivileges(rank.getRank());
  151. //p.donatorRights(rank.getDRank());
  152. p.getActionSender()
  153. .sendMessage("Your account has been upgraded to : " + rank.getString() + ".");
  154. p.getActionSender().sendWindowsRemoval();
  155. break;
  156. }
  157. }
  158.  
  159. }
  160. }
  161.  
  162. if (claimed) {
  163. s.execute("UPDATE `paid_donations` SET `claimed` = '1' WHERE `username` = '" + name2 + "';");
  164. } else
  165. p.getActionSender().sendMessage("Sorry, you have not donated today. Do ::donate.");
  166. } catch (Exception e) {
  167. e.printStackTrace();
  168. p.getActionSender().sendMessage("[MySQL-Donation-Error] Report to Owner / Developer.");
  169. }
  170. }
  171.  
  172. /**
  173. * @return the amount
  174. */
  175. public int getAmount() {
  176. return amount;
  177. }
  178.  
  179. /**
  180. *
  181. * @return The player
  182. */
  183. public Player getPlayer() {
  184. return player;
  185. }
  186.  
  187. /**
  188. * @param amount
  189. * the amount to set
  190. */
  191. public void setAmount(int amount) {
  192. this.amount = amount;
  193. }
  194.  
  195. /**
  196. *
  197. * @param player
  198. * The player
  199. */
  200. public void setPlayer(Player player) {
  201. this.player = player;
  202. }
  203.  
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement