· 6 years ago · Sep 10, 2019, 12:04 AM
1package info.nickgs.machines.database.DAO;
2
3import info.nickgs.machines.MachinesPlugin;
4import info.nickgs.machines.objects.others.Booster;
5import info.nickgs.machines.utils.StringUtils;
6import org.bukkit.Bukkit;
7
8import java.sql.*;
9import java.util.HashMap;
10
11public class BoosterDAO {
12
13 public static void setupTable() {
14 try {
15 Statement stmt = MachinesPlugin.getConnectionFactory().getConnection().createStatement();
16 String sql = "CREATE TABLE IF NOT EXISTS booster " +
17 "(owner VARCHAR PRIMARY KEY ," +
18 " end BIGINT NOT NULL," +
19 " multiplier INT NOT NULL);";
20 stmt.executeUpdate(sql);
21 stmt.close();
22 } catch (SQLException e) {
23 e.printStackTrace();
24 }
25 }
26
27 public static void silentInsertBooster(String owner, Booster booster) {
28 try {
29 String sql = "INSERT INTO booster (owner, end, multiplier) " +
30 "VALUES (?, ?, ?);";
31 PreparedStatement stmt = MachinesPlugin.getConnectionFactory().getConnection().prepareStatement(sql);
32 stmt.setString(1, owner);
33 stmt.setLong(2, booster.getEnd());
34 stmt.setInt(3, booster.getMultiplier());
35
36 stmt.executeUpdate();
37
38 stmt.getGeneratedKeys().next();
39 stmt.close();
40 } catch (SQLException ignored) {}
41 }
42
43 public static void insertBooster(String owner, Booster booster) {
44 try {
45 String sql = "INSERT INTO booster (owner, end, multiplier) " +
46 "VALUES (?, ?, ?);";
47 PreparedStatement stmt = MachinesPlugin.getConnectionFactory().getConnection().prepareStatement(sql);
48 stmt.setString(1, owner);
49 stmt.setLong(2, booster.getEnd());
50 stmt.setInt(3, booster.getMultiplier());
51
52 stmt.executeUpdate();
53
54 stmt.getGeneratedKeys().next();
55 stmt.close();
56 } catch (SQLException e) {
57 e.printStackTrace();
58 }
59 }
60
61 public static void removeBooster(String owner) {
62 try {
63 String sql = "DELETE FROM booster WHERE owner=?;";
64 PreparedStatement stmt = MachinesPlugin.getConnectionFactory().getConnection().prepareStatement(sql);
65 stmt.setString(1, owner);
66
67 stmt.executeUpdate();
68
69 stmt.close();
70 } catch (SQLException e) {
71 e.printStackTrace();
72 }
73 }
74
75 public static void updateBooster(String owner, Booster booster) {
76 try {
77 String sql = "UPDATE booster SET end = ?, multiplier = ? WHERE owner=?;";
78 PreparedStatement stmt = MachinesPlugin.getConnectionFactory().getConnection().prepareStatement(sql);
79 stmt.setLong(1, booster.getEnd());
80 stmt.setInt(2, booster.getMultiplier());
81 stmt.setString(3, owner);
82
83 stmt.executeUpdate();
84
85 stmt.close();
86 } catch (SQLException e) {
87 e.printStackTrace();
88 }
89 }
90
91 public static HashMap<String, Booster> getBoosters() {
92 HashMap<String, Booster> boosters = new HashMap<>();
93 try {
94 Connection connection = MachinesPlugin.getConnectionFactory().getConnection();
95
96 Statement stmt = connection.createStatement();
97 ResultSet resultSet = stmt.executeQuery("SELECT Count(*) AS count FROM booster;");
98 resultSet.next();
99 Bukkit.broadcastMessage(StringUtils.convertColors("&6[Machines] &aReiniciando carregamento dos boosters..."));
100 Bukkit.broadcastMessage(StringUtils.convertColors("&6[Machines] &a0.0%"));
101 int total = resultSet.getInt("count");
102 for(int i = 0; i < (total / 50000) + 1; i ++) {
103 boosters.putAll(getBoostersInRange(i * 50000, (i+1) * 50000));
104 Bukkit.broadcastMessage(StringUtils.convertColors("&6[Machines] &a" + (((i + 1) / (((total / 50000) + 1) * 1.0))*100) + "%"));
105 }
106 Bukkit.broadcastMessage(StringUtils.convertColors("&6[Machines] &aCarregamento terminado! (" + boosters.size() + " boosters carregados)"));
107 } catch (SQLException e) {
108 e.printStackTrace();
109 }
110 return boosters;
111 }
112
113 private static HashMap<String, Booster> getBoostersInRange(int start, int end) {
114 HashMap<String, Booster> boosters = new HashMap<>();
115 try {
116 Connection connection = MachinesPlugin.getConnectionFactory().getConnection();
117
118 PreparedStatement stmt = connection.prepareStatement("SELECT * FROM booster LIMIT ?, ?;");
119 stmt.setInt(1, start);
120 stmt.setInt(2, end);
121
122 ResultSet resultSet = stmt.executeQuery();
123 while(resultSet.next()) {
124 boosters.put(resultSet.getString("owner"), new Booster(resultSet.getInt("multiplier"), resultSet.getLong("end")));
125 }
126 } catch (SQLException e) {
127 e.printStackTrace();
128 }
129 return boosters;
130 }
131
132}