Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package soulfight.ams;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.HashMap;
- import java.util.UUID;
- import org.bukkit.Bukkit;
- import soulfight.main.Main;
- public class AMSManager {
- public static final String amsPrefix = Main.prefix;
- public static String getAMSCache;
- public HashMap<UUID, PlayerAMS> amsCache;
- public AMSManager() {
- this.amsCache = new HashMap<UUID, PlayerAMS>();
- Main.getSQLManager().executeUpdate(
- "CREATE TABLE IF NOT EXISTS DB_AMS (UUID VARCHAR(100), Spawner LONG, UNIQUE KEY (UUID));");
- loadMoneyForOnlines();
- }
- public void boot() {
- int i = 0;
- System.out.println("{SQL} LOADING AMS DATA ...");
- try {
- PreparedStatement stCheck = Main.getSQLManager().getConnection()
- .prepareStatement("SELECT ALL * FROM DB_AMS");
- ResultSet rsCheck = Main.getSQLManager().executeQuery(stCheck);
- while(rsCheck.next()) {
- this.addPlayerToCache(UUID.fromString(rsCheck.getString("UUID")));
- i++;
- }
- System.out.println("{SQL} FINISHED");
- System.out.println("{SQL} LOADED " + i + " UUID'S INTO THE CACHE");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void shutdown() {
- System.out.println("{SQL} SAVING AMS ...");
- amsCache.keySet().forEach(UUID -> {
- PlayerAMS ams = (PlayerAMS) this.amsCache.get(UUID);
- ams.saveData();
- this.removePlayerFromCache(UUID);
- });
- System.out.println("{SQL} DONE");
- }
- public void saveOnlineAsync() {
- amsCache.keySet().forEach(UUID -> {
- PlayerAMS ams = (PlayerAMS) this.amsCache.get(UUID);
- ams.saveDataAsync();
- });
- }
- public void loadMoneyForOnlines() {
- Bukkit.getOnlinePlayers().forEach(all ->{
- addPlayerToCache(all.getUniqueId());
- });
- }
- public void addPlayerToCache(UUID uuid) {
- if (this.amsCache.containsKey(uuid)) {
- return;
- }
- this.amsCache.put(uuid, new PlayerAMS(uuid));
- }
- public void removePlayerFromCache(UUID uuid) {
- if (!this.amsCache.containsKey(uuid)) {
- return;
- }
- PlayerAMS ams = (PlayerAMS) this.amsCache.get(uuid);
- ams.saveDataAsync();
- ams.removeFromUpdater();
- this.amsCache.remove(ams);
- }
- public PlayerAMS getPlayerAMS(UUID uuid) {
- if (!this.amsCache.containsKey(uuid)) {
- addPlayerToCache(uuid);
- }
- return (PlayerAMS) this.amsCache.get(uuid);
- }
- public HashMap<UUID, PlayerAMS> getAMSCache() {
- return this.amsCache;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement