Straapix

RankSQL

Feb 19th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. package com.rankup.fr;
  2.  
  3. import java.sql.Connection ;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8.  
  9. import org.bukkit.entity.Player;
  10.  
  11. public class RankSQL {
  12.  
  13. private String url_base, host, name, username, password, table;
  14. private Connection connection;
  15.  
  16. public RankSQL(String url_base, String host, String name, String username, String password, String table) {
  17. this.url_base = url_base;
  18. this.host = host;
  19. this.name = name;
  20. this.username = username;
  21. this.password = password;
  22. this.table = table;
  23. }
  24.  
  25. public void connection() {
  26. if (!isConnected()) {
  27. try {
  28. connection = DriverManager.getConnection(url_base + host + "/" + name, username, password);
  29. } catch (SQLException e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. }
  34.  
  35. public void deconnection() {
  36. if (isConnected()) {
  37. try {
  38. connection.close();
  39. } catch (SQLException e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. }
  44.  
  45. private boolean isConnected() {
  46. try {
  47. if ((connection == null) || (connection.isClosed()) || (!connection.isValid(5))) {/*si sa ne marche pas remettre 5 a la place de 20*/
  48. return false;
  49. } else {
  50. return true;
  51. }
  52. } catch (SQLException e) {
  53. e.printStackTrace();
  54. }
  55. return false;
  56. }
  57.  
  58. private Connection getConnection() {
  59. return connection;
  60. }
  61.  
  62. public void createPlayerIfNotExist(Player player) {
  63. try {
  64. PreparedStatement sts = getConnection().prepareStatement("SELECT rank FROM " + table + " WHERE uuid = ?");
  65. sts.setString(1, player.getUniqueId().toString());
  66. ResultSet rs = sts.executeQuery();
  67. if (!rs.next()) {
  68. sts.close();
  69. sts = getConnection().prepareStatement("INSERT INTO " + table + " (uuid, rank) VALUES (?, ?)");
  70. sts.setString(1, player.getUniqueId().toString());
  71. sts.setInt(2, 10);
  72. sts.executeUpdate();
  73. sts.close();
  74. }
  75. } catch (SQLException e) {
  76. e.printStackTrace();
  77. }
  78. }
  79.  
  80. public Rank getRank(String uuid) {
  81. Rank rank = null;
  82. try {
  83. PreparedStatement sts = getConnection().prepareStatement("SELECT rank FROM " + table + " WHERE uuid = ?, ?");
  84. sts.setString(1, uuid);
  85. ResultSet rs = sts.executeQuery();
  86. if (!rs.next()) {
  87. return null;
  88. }
  89. rank = Rank.getFromPower(rs.getInt("rank"));
  90. sts.close();
  91. } catch (SQLException e) {
  92. e.printStackTrace();
  93. }
  94. return rank;
  95. }
  96.  
  97. public void suppPermission(Player p, String permission){
  98. try {
  99. PreparedStatement sts = getConnection().prepareStatement("DELETE FROM "+table+" WHERE " + "uuid = ? " + "and permissions = ?");
  100. sts.setString(1, p.getUniqueId().toString());
  101. sts.setString(2, permission);
  102. sts.executeUpdate();
  103. sts.close();
  104. } catch (SQLException e) {
  105. e.printStackTrace();
  106. }
  107. }
  108.  
  109. public void suppPermissionsTime(){
  110. try {
  111. PreparedStatement sts = getConnection().prepareStatement("DELETE FROM "+table+" WHERE time <= NOW()");
  112. sts.executeUpdate();
  113. sts.close();
  114. }catch (SQLException e) {
  115. e.printStackTrace();
  116. }
  117. }
  118.  
  119. public boolean hasPermission(Player p, String permission){
  120. suppPermissionsTime();
  121. try {
  122. PreparedStatement sts = getConnection().prepareStatement("SELECT permissions FROM " + table + " WHERE uuid = ? AND time >= NOW()");
  123. sts.setString(1, p.getUniqueId().toString());
  124. ResultSet rs = sts.executeQuery();
  125. while(rs.next()){
  126. if(permission.equalsIgnoreCase(rs.getString("permissions"))){
  127. return true;
  128. }
  129. }
  130. sts.close();
  131. } catch (SQLException e) {
  132. e.printStackTrace();
  133. }
  134. return false;
  135. }
  136.  
  137. public void addPermissionWithTimer(Player player, String permission, Integer temps){
  138. try {
  139. PreparedStatement sts = getConnection().prepareStatement("INSERT INTO "+table+" (uuid, rank, permissions, time) VALUES (?, ?, ?, DATE_ADD(NOW(), INTERVAL " + temps + " MINUTE))");
  140. sts.setString(1, player.getUniqueId().toString());
  141. sts.setInt(2, getRank(player.getUniqueId().toString()).getPower());
  142. sts.setString(3, permission);
  143. sts.executeUpdate();
  144. sts.close();
  145. } catch (SQLException e) {
  146. e.printStackTrace();
  147. }
  148. }
  149.  
  150. public void addPermission(Player p, String permission){
  151. try {
  152. PreparedStatement sts = getConnection().prepareStatement("INSERT INTO "+table+" (uuid, rank, permissions) VALUES (?, ?, ?)");
  153. sts.setString(1, p.getUniqueId().toString());
  154. sts.setInt(2, getRank(p.getUniqueId().toString()).getPower());
  155. sts.setString(3, permission);
  156. sts.executeUpdate();
  157. sts.close();
  158. } catch (SQLException e) {
  159. e.printStackTrace();
  160. }
  161. }
  162.  
  163. public void setRank(Player p, Rank r){
  164. try {
  165. PreparedStatement sts = getConnection().prepareStatement("UPDATE "+table+" SET rank = ? WHERE uuid = ?");
  166. sts.setInt(1, r.getPower());
  167. sts.setString(2, p.getUniqueId().toString());
  168. sts.executeUpdate();
  169. sts.close();
  170. } catch (SQLException e) {
  171. e.printStackTrace();
  172. }
  173. }
  174. }
Add Comment
Please, Sign In to add comment