· 6 years ago · Jun 19, 2019, 09:46 AM
1package soulfight.ams;
2
3import java.sql.PreparedStatement;
4import java.sql.ResultSet;
5import java.sql.SQLException;
6import java.util.HashMap;
7import java.util.UUID;
8
9import org.bukkit.Bukkit;
10
11import soulfight.main.Main;
12
13public class AMSManager {
14
15 public static final String amsPrefix = Main.prefix;
16 public static String getAMSCache;
17 public HashMap<UUID, PlayerAMS> amsCache;
18
19 public AMSManager() {
20 this.amsCache = new HashMap<UUID, PlayerAMS>();
21 Main.getSQLManager().executeUpdate(
22 "CREATE TABLE IF NOT EXISTS DB_AMS (UUID VARCHAR(100), Spawner LONG, UNIQUE KEY (UUID));");
23 loadMoneyForOnlines();
24 }
25
26 public void boot() {
27 int i = 0;
28 System.out.println("{SQL} LOADING AMS DATA ...");
29 try {
30 PreparedStatement stCheck = Main.getSQLManager().getConnection()
31 .prepareStatement("SELECT ALL * FROM DB_AMS");
32 ResultSet rsCheck = Main.getSQLManager().executeQuery(stCheck);
33 while(rsCheck.next()) {
34 this.addPlayerToCache(UUID.fromString(rsCheck.getString("UUID")));
35 i++;
36 }
37 System.out.println("{SQL} FINISHED");
38 System.out.println("{SQL} LOADED " + i + " UUID'S INTO THE CACHE");
39 } catch (SQLException e) {
40 e.printStackTrace();
41 }
42 }
43
44 public void shutdown() {
45 System.out.println("{SQL} SAVING AMS ...");
46 amsCache.keySet().forEach(UUID -> {
47 PlayerAMS ams = (PlayerAMS) this.amsCache.get(UUID);
48 ams.saveData();
49 this.removePlayerFromCache(UUID);
50 });
51 System.out.println("{SQL} DONE");
52 }
53
54 public void saveOnlineAsync() {
55 amsCache.keySet().forEach(UUID -> {
56 PlayerAMS ams = (PlayerAMS) this.amsCache.get(UUID);
57 ams.saveDataAsync();
58 });
59 }
60
61 public void loadMoneyForOnlines() {
62 Bukkit.getOnlinePlayers().forEach(all ->{
63 addPlayerToCache(all.getUniqueId());
64 });
65 }
66
67 public void addPlayerToCache(UUID uuid) {
68 if (this.amsCache.containsKey(uuid)) {
69 return;
70 }
71 this.amsCache.put(uuid, new PlayerAMS(uuid));
72 }
73
74 public void removePlayerFromCache(UUID uuid) {
75 if (!this.amsCache.containsKey(uuid)) {
76 return;
77 }
78 PlayerAMS ams = (PlayerAMS) this.amsCache.get(uuid);
79 ams.saveDataAsync();
80 ams.removeFromUpdater();
81 this.amsCache.remove(ams);
82 }
83
84 public PlayerAMS getPlayerAMS(UUID uuid) {
85 if (!this.amsCache.containsKey(uuid)) {
86 addPlayerToCache(uuid);
87 }
88 return (PlayerAMS) this.amsCache.get(uuid);
89 }
90
91 public HashMap<UUID, PlayerAMS> getAMSCache() {
92 return this.amsCache;
93 }
94}