· 8 years ago · Dec 04, 2017, 12:46 PM
1package com.wesley.funvex.sql.mySql.bdd.bdds;
2
3import com.wesley.funvex.sql.main.Main;
4import com.wesley.funvex.sql.mySql.Sql;
5import com.wesley.funvex.sql.utils.RanksList;
6import org.bukkit.Bukkit;
7import org.bukkit.command.ConsoleCommandSender;
8import org.bukkit.entity.Player;
9
10import java.sql.*;
11import java.util.UUID;
12
13/**
14 * Created by :
15 * @author sokoon
16 * @version 2.0.0
17 * @since 1.0.O
18 */
19public class SqlUtilisateursBase {
20
21 private final Connection connection = new Sql().getConnection();
22
23 public void Create() {
24 try {
25 PreparedStatement q = connection.prepareStatement("CREATE TABLE IF NOT EXISTS SqlUtilisateursBase(id INT PRIMARY KEY AUTO_INCREMENT NOT NULL, uuid VARCHAR(255) UNIQUE NOT NULL, playername VARCHAR(255) NOT NULL, gold DOUBLE NOT NULL, diamond DOUBLE NOT NULL, rankpower INT NOT NULL, rankends VARCHAR(255), banned BOOLEAN, banreason VARCHAR(255), banend VARCHAR(255), muted BOOLEAN, mutereason VARCHAR NOT NULL, muteend VARCHAR(255), created DATE NOT NULL, ip VARCHAR(255) NOT NULL)");
26 q.execute();
27 q.close();
28 System.out.println("(SqlUtilisateursBase) Create >> SUCCESS");
29 } catch (SQLException e) {
30 System.out.println("(SqlUtilisateursBase) Create >> ERROR");
31 e.printStackTrace();
32 }
33 }
34
35 public void AccountCreate(UUID uuid) {
36 if (!this.HasAccount(uuid)) {
37 try {
38 PreparedStatement q = connection.prepareStatement("INSERT INTO SqlUtilisateursBase(uuid, playername, gold, diamond, rankpower, rankend, banned, banreason, banend, muted, mutereason, muteend, created, ip) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
39 q.setString(1, uuid.toString());
40 q.setString(2, Bukkit.getServer().getPlayer(uuid).getName());
41 q.setDouble(3, 0.0D);
42 q.setDouble(4, 0.0D);
43 q.setInt(5, 0);
44 q.setString(6, "-1");
45 q.setBoolean(7, false);
46 q.setString(8, null);
47 q.setString(9, null);
48 q.setBoolean(10, false);
49 q.setString(11, null);
50 q.setString(12, null);
51 q.setDate(13, new Date(System.currentTimeMillis()));
52 q.setString(14, Bukkit.getServer().getPlayer(uuid).getAddress().getHostString());
53 q.execute();
54 q.close();
55 System.out.println("(SqlUtilisateursBase) AccountCreate >> SUCCESS");
56 } catch (SQLException e) {
57 System.out.println("(SqlUtilisateursBase) AccountCreate >> ERROR");
58 e.printStackTrace();
59 }
60 }
61 }
62
63 public boolean HasAccount(UUID uuid) {
64 try {
65 PreparedStatement q = connection.prepareStatement("SELECT uuid FROM SqlUtilisateursBase WHERE uuid = ?");
66 q.setString(1, uuid.toString());
67 ResultSet rs = q.executeQuery();
68 boolean hasAccount = rs.next();
69 q.close();
70 return hasAccount;
71 } catch (SQLException e) {
72 System.out.println("(SqlUtilisateursBase) HasAccount >> ERROR");
73 e.printStackTrace();
74 }
75 return false;
76 }
77
78 public UUID GetUUID(String pn) {
79 try {
80 PreparedStatement q = connection.prepareStatement("SELECT uuid FROM SqlUtilisateursBase WHERE name = ?");
81 q.setString(1, pn);
82 ResultSet rs = q.executeQuery();
83 UUID uuid = null;
84 while (rs.next()) {
85 uuid = UUID.fromString(rs.getString("uuid"));
86 }
87 q.close();
88 return uuid;
89 } catch (SQLException e) {
90 System.out.println("(SqlUtilisateursBase) GetUUID >> ERROR");
91 e.printStackTrace();
92 }
93 return null;
94 }
95
96 public String GetPlayerName(UUID uuid) {
97 if (this.HasAccount(uuid)) {
98 try {
99 PreparedStatement q = connection.prepareStatement("SELECT name FROM SqlUtilisateursBase WHERE uuid = ?");
100 q.setString(1, uuid.toString());
101 ResultSet rs = q.executeQuery();
102 String name = this.cs("&4ERROR");
103 while (rs.next()) {
104 name = rs.getString("playername");
105 }
106 q.close();
107 return name;
108 } catch (SQLException e) {
109 System.out.println("(SqlUtilisateursBase) GetPlayerName >> ERROR");
110 e.printStackTrace();
111 }
112 }
113 return this.cs("&4ERROR");
114 }
115
116 public void SetPlayersName(UUID uuid) {
117 if (this.HasAccount(uuid)) {
118 try {
119 PreparedStatement q = connection.prepareStatement("UPDATE SqlUtilisateursBase SET playername = ? WHERE uuid = ?");
120 q.setString(1, Bukkit.getServer().getPlayer(uuid).getName());
121 q.setString(2, uuid.toString());
122 q.executeUpdate();
123 q.close();
124 } catch (SQLException e) {
125 System.out.println("(SqlUtilisateursBase) SetPlayersName >> ERRED");
126 e.printStackTrace();
127 }
128 }
129 }
130
131 public RanksList GetRank(UUID uuid) {
132 if (this.HasAccount(uuid)) {
133 try {
134 PreparedStatement q = connection.prepareStatement("SELECT rankpower FROM SqlUtilisateursBase WHERE uuid = ?");
135 q.setString(1, uuid.toString());
136 ResultSet rs = q.executeQuery();
137 int power = 0;
138 while (rs.next()) {
139 power = rs.getInt("rankpower");
140 }
141 q.close();
142 return RanksList.powerToRank(power);
143 } catch (SQLException e) {
144 System.out.println("(SqlUtilisateursBase) GetRank >> ERROR");
145 e.printStackTrace();
146 }
147 }
148 return RanksList.JOUEUR;
149 }
150
151 public Long GetRankEnd(UUID uuid) {
152 if (this.HasAccount(uuid)) {
153 try {
154 PreparedStatement q = connection.prepareStatement("SELECT rankend FROM SqlUtilisateursBase WHERE uuid = ?");
155 q.setString(1, uuid.toString());
156 ResultSet rs = q.executeQuery();
157 Long end = -2L;
158 while (rs.next()) {
159 end = rs.getLong("rankend");
160 }
161 q.close();
162 return end;
163 } catch (SQLException e) {
164 System.out.println("(SqlUtilisateursBase) GetEnd >> ERROR");
165 e.printStackTrace();
166 }
167 }
168 return -2L;
169 }
170
171 public void SetRank(UUID uuid, int power, long time, ConsoleCommandSender sender) {
172 if (this.HasAccount(uuid)) {
173 if (sender instanceof Player) {
174 if (uuid == UUID.fromString(((Player) sender).getUniqueId().toString())) {
175 sender.sendMessage(this.cs("&7(&cRankSystem&7) &8» &cVous ne pouvez pas changer votre propre 'rank'."));
176 return;
177 }
178 }
179 if (power < 0) {
180 sender.sendMessage(this.cs("&7(&cRankSystem&7) &8» &cVous ne pouvez pas définir &f'Inférieurement à 0'"));
181 return;
182 }
183 if (power > (RanksList.ranks.size() - 1)) {
184 sender.sendMessage(this.cs("&7(&cRankSystem&7) &8» &cVous ne pouvez pas définir &f'Supérieurement à " + (RanksList.ranks.size() - 1) + "'"));
185 return;
186 }
187 RanksList rl = RanksList.powerToRank(power);
188 long e = 0L;
189 if (time == -1L) {
190 e = time;
191 } else if (time > 0L) {
192 long current = System.currentTimeMillis();
193 long millis = time * 1000L;
194 e = current + millis;
195 }else if (time < -1L) {
196 sender.sendMessage(this.cs("&7(&cRankSystem&7) &8» &cVous ne pouvez pas définir le temps &f'Inférieurement à 0'"));
197 return;
198 }
199 try {
200 PreparedStatement q = connection.prepareStatement("UPDATE SqlUtilisateursBase SET rankpower = ?, powerend = ? WHERE uuid = ?");
201 q.setInt(1, rl.getPower());
202 q.setLong(2, e);
203 q.setString(3, uuid.toString());
204 q.executeUpdate();
205 q.close();
206 if (Bukkit.getServer().getPlayer(uuid) != null) {
207 if (Bukkit.getPlayer(uuid).isOnline()) {
208 Player ta = Bukkit.getServer().getPlayer(uuid);
209 ta.sendMessage(this.cs("&7&m--+-------------------------+---"));
210 ta.sendMessage(this.cs(" "));
211 ta.sendMessage(this.cs("&cRankSystem &8» &7Votre grade a été modifié."));
212 ta.sendMessage(this.cs(" "));
213 ta.sendMessage(this.cs(" "));
214 ta.sendMessage(this.cs("&7&nInformation :"));
215 ta.sendMessage(this.cs(" "));
216 ta.sendMessage(this.cs("&7oRank &8» " + rl.getName()));
217 ta.sendMessage(this.cs("&7&oTemps &8» &e" + this.GetRemainingTimeRank(uuid)));
218 ta.sendMessage(this.cs(" "));
219 ta.sendMessage(this.cs(" "));
220 ta.sendMessage(this.cs("&c&nINFO : &7&oPour effectué le changement, déconnecte-toi."));
221 ta.sendMessage(this.cs(" "));
222 ta.sendMessage(this.cs("&7&m--+-------------------------+---"));
223 }
224 }
225 sender.sendMessage(this.cs("&7(&cRankSystem&7) &8» &cVous avez définie le rank &f'" + rl.getName() + "' &7(&4" + rl.getPower() + "&7)&f' à &f'" + this.GetPlayerName(uuid) + "'"));
226 } catch (SQLException ex) {
227 System.out.println("(SqlUtilisateursBase) SetRank >> ERROR");
228 ex.printStackTrace();
229 }
230 }
231 }
232
233 public void ResetRank(UUID uuid, ConsoleCommandSender sender) {
234 if (this.HasAccount(uuid)) {
235 try {
236 PreparedStatement q = connection.prepareStatement("UPDATE SqlUtilisateursBase SET rankpower = ?, end = ? WHERE uuid = ?");
237 q.setInt(1, 0);
238 q.setString(2, "-1");
239 q.setString(3, uuid.toString());
240 q.executeUpdate();
241 q.close();
242 if (Bukkit.getServer().getPlayer(uuid) != null) {
243 if (Bukkit.getServer().getPlayer(uuid).isOnline()) {
244 Player t = Bukkit.getServer().getPlayer(uuid);
245 t.kickPlayer(this.cs(
246 "&7&m------&8» &7(&cRankSystem&7) &8«&7&m------&r"
247 + "\n \n"
248 + "&cVous avez été exclus du serveur.."
249 + "\n \n"
250 + "&7Raison &8» &f'" + sender.getName() + "' &cvient de vous réinitialiser votre rank !"
251 + "\n \n"
252 + "&7&m------&8»&7--------------&8«&7------"));
253 }
254 }
255 sender.sendMessage(this.cs("&7(&cRankSystem&7) &8» &cVous venez de réinitialiser le rank de &f'" + this.GetPlayerName(uuid) + "'"));
256 System.out.println("(SqlUtilisateursBase) ResetRank >> SUCCESS");
257 } catch (SQLException e) {
258 System.out.println("(SqlUtilisateursBase) ResetRank >> ERROR");
259 e.printStackTrace();
260 }
261 }
262 }
263
264 public boolean GetBan(UUID uuid) {
265 if (this.HasAccount(uuid)) {
266 try {
267 PreparedStatement q = connection.prepareStatement("SELECT banned FROM SqlUtilisateursBase WHERE uuid = ?");
268 q.setString(1, uuid.toString());
269 ResultSet rs = q.executeQuery();
270 boolean banned = false;
271 while (rs.next()) {
272 banned = rs.getBoolean("banned");
273 }
274 q.close();
275 return banned;
276 } catch (SQLException e) {
277 System.out.println("(SqlUtilisateursBase) GetBan >> ERROR");
278 e.printStackTrace();
279 }
280 }
281 return false;
282 }
283
284 public String GetBanReason(UUID uuid) {
285 if (this.HasAccount(uuid)) {
286 try {
287 PreparedStatement q = connection.prepareStatement("SELECT banreason FROM SqlUtilisateursBase WHERE uuid = ?");
288 q.setString(1, uuid.toString());
289 ResultSet rs = q.executeQuery();
290 String reason = this.cs("&4ERROR");
291 while (rs.next()) {
292 reason = rs.getString("banreason");
293 }
294 q.close();
295 return reason;
296 } catch (SQLException e) {
297 System.out.println("(SqlUtilisateursBase) GetBanReason >> ERROR");
298 }
299 }
300 return this.cs("&4ERROR");
301 }
302
303 public Long GetBanEnd(UUID uuid) {
304 if (this.HasAccount(uuid)) {
305 try {
306 PreparedStatement q = connection.prepareStatement("SELECT banend FROM SqlUtilisateursBase WHERE uuid = ?");
307 q.setString(1, uuid.toString());
308 ResultSet rs = q.executeQuery();
309 Long end = -2L;
310 while (rs.next()) {
311 end = rs.getLong("banend");
312 }
313 q.close();
314 return end;
315 } catch (SQLException e) {
316 System.out.println("(SqlUtilisateursBase) GetBanEnd >> ERROR");
317 e.printStackTrace();
318 }
319 }
320 return -2L;
321 }
322
323 public boolean HasBan(UUID uuid) {
324 if (this.HasAccount(uuid)) {
325 boolean b = this.GetBan(uuid);
326 if (b) {
327 return true;
328 }
329 if (!b) {
330 return false;
331 }
332 }
333 return false;
334 }
335
336 public void Ban(UUID uuid, String reason, long time, ConsoleCommandSender sender) {
337 if(this.HasAccount(uuid)) {
338 if(!this.HasBan(uuid)) {
339 long e = 0L;
340 if (time == -1L) {
341 e = time;
342 } else if (time > 0L) {
343 long current = System.currentTimeMillis();
344 long millis = time * 1000L;
345 e = current + millis;
346 }else if (time < -1L) {
347 sender.sendMessage(this.cs("&7(&cBanSystem&7) &8» &cVous ne pouvez pas définir le temps &f'Inférieurement à 0'"));
348 return;
349 }
350 try {
351 PreparedStatement q = connection.prepareStatement("UPDATE SqlUtilisateursBase SET banned = ?, banreason = ?, banend = ? WHERE uuid = ?");
352 q.setBoolean(1, true);
353 q.setString(2, reason);
354 q.setLong(3, e);
355 q.setString(4, uuid.toString());
356 q.executeUpdate();
357 q.close();
358 Bukkit.getServer().broadcastMessage(this.cs("&7(&cBanSystem&7) &8» &f'" + this.GetPlayerName(uuid) + "' &cvient d'être banni pour &e'" + reason + "' &cpendant &a'" + this.GetRemainingTimeBan(uuid) + "'"));
359 if(Bukkit.getServer().getPlayer(uuid) != null) {
360 if(Bukkit.getServer().getPlayer(uuid).isOnline()) {
361 Player t = Bukkit.getServer().getPlayer(uuid);
362 t.kickPlayer(this.cs(
363 "&7&m------&8» &7(&cBanSystem&7) &8«&7&m------&r"
364 + "\n \n"
365 + "&cVous avez été banni du serveur.."
366 + "\n \n"
367 + "&7Raison &8» &e'" + reason + "&e'"
368 + "\n "
369 + "&7Temps &8» &a'" + this.GetRemainingTimeBan(uuid) + "'"
370 + "\n \n"
371 + "&d&oPour contester cette sanction, merci de passer par le TeamSpeak &f'" + Main.getTeamSpeak() + "'"
372 + "\n \n"
373 + "&7&m------&8»&7--------------&8«&7------"));
374 }
375 }
376 } catch (SQLException ex) {
377 System.out.println("(SqlUtilisateursBase) Ban >> ERROR");
378 ex.printStackTrace();
379 }
380 }else {
381 sender.sendMessage(this.cs("&7(&cBanSystem&7) &8» &cCe membre est déjà banni."));
382 }
383 }
384 }
385
386 public Date GetCreated(UUID uuid) {
387 if (this.HasAccount(uuid)) {
388 try {
389 PreparedStatement q = connection.prepareStatement("SELECT created FROM SqlUtilisateursBase WHERE uuid = ?");
390 q.setString(1, uuid.toString());
391 ResultSet rs = q.executeQuery();
392 Date date = null;
393 while (rs.next()) {
394 date = Date.valueOf(rs.getString("created"));
395 }
396 q.close();
397 return date;
398 } catch (SQLException e) {
399 System.out.println("(SqlUtilisateursBase) GetDate >> ERROR");
400 e.printStackTrace();
401 }
402 }
403 return null;
404 }
405
406 public String GetIP(UUID uuid) {
407 if (this.HasAccount(uuid)) {
408 try {
409 PreparedStatement q = connection.prepareStatement("SELECT ip FROM SqlUtilisateursBase WHERE uuid = ?");
410 q.setString(1, uuid.toString());
411 ResultSet rs = q.executeQuery();
412 String ip = this.cs("&4ERROR");
413 while (rs.next()) {
414 ip = rs.getString("ip");
415 }
416 q.close();
417 return ip;
418 } catch (SQLException e) {
419 System.out.println("(SqlUtilisateursBase) GetIP >> ERROR");
420 e.printStackTrace();
421 }
422 }
423 return this.cs("&4ERROR");
424 }
425
426 public void SetIP(UUID uuid) {
427 if (this.HasAccount(uuid)) {
428 String ip;
429 if (this.GetRank(uuid).getPower() >= RanksList.PARTENAIRE.getPower()) {
430 ip = "Hidden";
431 } else {
432 ip = Bukkit.getServer().getPlayer(uuid).getAddress().getHostString();
433 }
434 try {
435 PreparedStatement q = connection.prepareStatement("UPDATE SqlUtilisateursBase SET ip = ? WHERE uuid = ?");
436 q.setString(1, ip);
437 q.setString(2, uuid.toString());
438 q.executeUpdate();
439 q.close();
440 } catch (SQLException e) {
441 System.out.println("(SqlUtilisateursBase) SetIP >> ERROR");
442 e.printStackTrace();
443 }
444 }
445 }
446
447 public String GetRemainingTimeRank(UUID uuid) {
448 if (this.HasAccount(uuid)) {
449 long e = this.GetRankEnd(uuid);
450 if (e == -2L) {
451 return this.cs("&4ERROR");
452 } else if (e == -1L) {
453 return this.cs("&cPermanent");
454 }
455 long c = System.currentTimeMillis();
456 long m = e - c;
457 String t = this.RemainingTime(m);
458 return t;
459 }
460 return this.cs("&4ERROR");
461 }
462
463 public String GetRemainingTimeBan(UUID uuid) {
464 if (this.HasAccount(uuid)) {
465 long e = this.GetRankEnd(uuid);
466 if (e == -2L) {
467 return this.cs("&4ERROR");
468 } else if (e == -1L) {
469 return this.cs("&cPermanent");
470 }
471 long c = System.currentTimeMillis();
472 long m = e - c;
473 String t = this.RemainingTime(m);
474 return t;
475 }
476 return this.cs("&4ERROR");
477 }
478
479 private String RemainingTime(long m) {
480 int s = 0, mi = 0, h = 0, d = 0, mo = 0;
481 while (m > 1000L) {
482 m -= 1000L;
483 s++;
484 }
485 while (s > 60) {
486 s -= 60;
487 mi++;
488 }
489 while (mi > 60) {
490 mi -= 60;
491 h++;
492 }
493 while (h > 24) {
494 h -= 24;
495 d++;
496 }
497 while (d > 29) {
498 d -= 29;
499 mo++;
500 }
501 StringBuilder r = new StringBuilder();
502 if (mo != 0) {
503 r.append(mo).append(" Mois ");
504 }
505 if (d != 0) {
506 r.append(d).append(" Jour(s) ");
507 }
508 if (h != 0) {
509 r.append(h).append(" Heure(s) ");
510 }
511 if (mi != 0) {
512 r.append(mi).append(" Minute(s) ");
513 }
514 if (s != 0) {
515 r.append(s).append(" Seconde(s)");
516 }
517 if (r.toString().isEmpty()) {
518 return "1 Seconde(s)";
519 }
520 return r.toString();
521 }
522
523 private String cs(String msg) {
524 return msg.replace("&", "§");
525 }
526}