Advertisement
Guest User

Untitled

a guest
Feb 1st, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. package de.pi.bungeeinfo.freunde;
  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.ArrayList;
  9.  
  10. import com.google.gson.Gson;
  11.  
  12. import de.pi.bungeeinfo.perms.RechteVerteiler;
  13. import net.md_5.bungee.api.connection.ProxiedPlayer;
  14.  
  15. public class FreundeDatenbank {
  16.  
  17. private static String host = "**********";
  18. private static String port = "3306";
  19. private static String user = "playinfinity";
  20. private static String password = "**********";
  21. private static String database = "db";
  22.  
  23. static Connection conn = null;
  24.  
  25. public static Statement getStat() {
  26. try {
  27. if (conn == null || conn.isClosed()) {
  28. Class.forName("com.mysql.jdbc.Driver");
  29. conn = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, user,
  30. password);
  31. }
  32. return conn.createStatement();
  33. } catch (Exception e) {
  34. System.out.println("KEINE VERBINDUNG");
  35. }
  36. return null;
  37. }
  38.  
  39. static Gson g = new Gson();
  40.  
  41. public static boolean hasFreundAsync(String s1, String s2) {
  42. ArrayList<String> all = getFreundeCanBeNullAsync(s1);
  43.  
  44. if(all == null) return false;
  45.  
  46. for(String s : all) {
  47. if(s.equalsIgnoreCase(s2)) {
  48. return true;
  49. }
  50. }
  51.  
  52.  
  53. //if(all.contains(s2)) return true;
  54. return false;
  55. }
  56.  
  57. public static boolean canHasMoreFreunde(ProxiedPlayer p) {
  58. if(p.hasPermission("system.admin")) return true;
  59.  
  60. ArrayList<String> all = getFreundeCanBeNullAsync(p.getName());
  61.  
  62. if(all == null) return true;
  63. if(all.size() <= 500 && p.hasPermission("system.premium")) return true;
  64.  
  65.  
  66. if(all.size() <= 15) return true;
  67.  
  68. return false;
  69.  
  70. }
  71. public static boolean canHasMoreFreunde(String n) {
  72. ArrayList<String> all = getFreundeCanBeNullAsync(n);
  73.  
  74. if(all == null) return true;
  75. if(all.size() <= 15) return true;
  76.  
  77. int r = RechteVerteiler.getRechteAusDBRang(RechteVerteiler.getRang(n));
  78.  
  79. if(r >= 4) return true;
  80.  
  81. if(all.size() <= 500 && r >= 1) return true;
  82.  
  83. return false;
  84.  
  85. }
  86.  
  87. public static void addFreundAsync(String user, String news) {
  88. ArrayList<String> all = getFreundeCanBeNullAsync(user);
  89. if(all == null) all = new ArrayList<String>();
  90.  
  91. if(all.contains(news) == false) {
  92. all.add(news);
  93. setFreundeAsyncNow(user, all);
  94. }
  95.  
  96. }
  97.  
  98. public static void removeFreundBitteZweiAsync(String user, String removes) {
  99. ArrayList<String> all = getFreundeCanBeNullAsync(user);
  100. if(all == null) return;
  101.  
  102. all.remove(removes);
  103. setFreundeAsyncNow(user, all);
  104. }
  105.  
  106. public static void checkForInsertAsync(String user) {
  107. try {
  108. ResultSet r = getStat().executeQuery("SELECT * FROM Freunde WHERE username LIKE '" + user + "' LIMIT 1");
  109. if(!r.next()) {
  110. insertAsync(user);
  111. }
  112. } catch (SQLException e) {
  113. e.printStackTrace();
  114. }
  115. }
  116.  
  117. private static void insertAsync(String user) {
  118. try {
  119. getStat().execute("INSERT INTO `Freunde` (`id`, `username`, `freunde`) VALUES (NULL, '" + user + "', 'none');");
  120. } catch (SQLException e) {
  121. e.printStackTrace();
  122. }
  123. }
  124.  
  125. public static void setFreundeAsyncNow(String user, ArrayList<String> freunde) {
  126.  
  127. StatusManager.namefreundelistp.put(user, freunde);
  128.  
  129. String all = g.toJson(freunde);
  130.  
  131. try {
  132. getStat().execute("UPDATE `Freunde` SET `freunde` = '" + all + "' WHERE `Freunde`.`username` LIKE '" + user + "';");
  133. } catch (SQLException e) {
  134. e.printStackTrace();
  135. }
  136. //upload :)
  137. }
  138.  
  139. public static ArrayList<String> getFreundeCanBeNullAsync(String user) {
  140. try {
  141. ResultSet r = getStat().executeQuery("SELECT * FROM Freunde WHERE username LIKE '" + user + "' LIMIT 1");
  142.  
  143. if(r.next()) {
  144.  
  145. String all = r.getString("freunde");
  146.  
  147. if(all == null) return null;
  148. if(all.equalsIgnoreCase("none")) return null;
  149.  
  150. ArrayList<String> listt = g.fromJson(all, ArrayList.class);
  151.  
  152. ArrayList<String> list = new ArrayList<>();
  153.  
  154. for(String i : listt) {
  155. if(list.contains(i) == false) list.add(i);
  156. }
  157.  
  158.  
  159. StatusManager.namefreundelistp.put(user, list);
  160.  
  161. return list;
  162. } else {
  163. return null;
  164. }
  165.  
  166. } catch (Exception e) {
  167. return null;
  168. }
  169. }
  170.  
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement