· 6 years ago · Aug 11, 2019, 08:30 PM
1package me.devleo.niferpvp.APIs;
2
3import java.sql.ResultSet;
4import java.sql.SQLException;
5import java.sql.Statement;
6import java.util.Calendar;
7import java.util.UUID;
8
9import org.bukkit.entity.Player;
10import org.bukkit.event.EventHandler;
11import org.bukkit.event.Listener;
12import org.bukkit.event.player.PlayerJoinEvent;
13
14import me.devleo.niferpvp.Main;
15import me.devleo.niferpvp.Eventos.Board;
16import me.devleo.niferpvp.MySQL.MySQL;
17
18public class KitManager implements Listener {
19
20 private static Player jogador;
21 private static MySQL db;
22 public Main plugin;
23
24 public KitManager(Main plugin2) {
25 this.plugin = plugin2;
26 }
27
28 public static boolean KitManager(Player p, String kit) {
29 jogador = p;
30 Board.setarScore(p);
31 if (kit.equals("noob")) {
32 if (!getData(p, "NoobDiario").equals("--/--/-- --:--")) {
33 p.sendMessage("§cAguarde até " + getData(p, "NoobDiario") + " para recolher este kit novamente.");
34 return true;
35 }
36 Calendar c = Calendar.getInstance();
37 int dia = c.get(Calendar.DAY_OF_MONTH);
38 int mes = c.get(Calendar.MONTH) + 1;
39 int ano = c.get(Calendar.YEAR);
40 int hora = c.get(Calendar.HOUR_OF_DAY);
41 int minuto = c.get(Calendar.MINUTE) + 5;
42 if(minuto > 59){
43 minuto = 0;
44 hora = hora + 1;
45 if(hora > 23){
46 hora = 1;
47 dia = dia + 1;
48 if(dia > 29){
49 dia = 1;
50 mes = mes + 1;
51 }
52 }
53 }
54 String data = dia + "/" + mes + "/" + ano + " " + hora + ":" + minuto;
55 setData(p, "NoobDiario", data);
56 return false;
57 }
58 if (kit.equals("key")) {
59 if (!getData(p, "Key").equals("--/--/-- --:--")) {
60 p.sendMessage("§cAguarde até " + getData(p, "Key") + " para recolher este kit novamente.");
61 return true;
62 }
63 Calendar c = Calendar.getInstance();
64 int dia = c.get(Calendar.DAY_OF_MONTH) + 7;
65 int mes = c.get(Calendar.MONTH) + 1;
66 if (dia > 29) {
67 dia = 1;
68 mes = mes + 1;
69 }
70 int ano = c.get(Calendar.YEAR);
71 int hora = c.get(Calendar.HOUR_OF_DAY);
72 int minuto = c.get(Calendar.MINUTE);
73 String data = dia + "/" + mes + "/" + ano + " " + hora + ":" + minuto;
74 setData(p, "Key", data);
75 return false;
76 }
77 if (kit.equals("vipdiario")) {
78 if (!getData(p, "VIPDiario").equals("--/--/-- --:--")) {
79 p.sendMessage("§cAguarde até " + getData(p, "VIPDiario") + " para recolher este kit novamente.");
80 return true;
81 }
82 Calendar c = Calendar.getInstance();
83 int dia = c.get(Calendar.DAY_OF_MONTH) + 1;
84 int mes = c.get(Calendar.MONTH) + 1;
85 if (dia > 29) {
86 dia = 1;
87 mes = mes + 1;
88 }
89 int ano = c.get(Calendar.YEAR);
90 int hora = c.get(Calendar.HOUR_OF_DAY);
91 int minuto = c.get(Calendar.MINUTE);
92 String data = dia + "/" + mes + "/" + ano + " " + hora + ":" + minuto;
93 setData(p, "VIPDiario", data);
94 return false;
95 }
96 if (kit.equals("vipsemanal")) {
97 if (!getData(p, "VIPSemanal").equals("--/--/-- --:--")) {
98 p.sendMessage("§cAguarde até " + getData(p, "VIPSemanal") + " para recolher este kit novamente.");
99 return true;
100 }
101 Calendar c = Calendar.getInstance();
102 int dia = c.get(Calendar.DAY_OF_MONTH) + 7;
103 int mes = c.get(Calendar.MONTH) + 1;
104 if (dia > 29) {
105 dia = 1;
106 mes = mes + 1;
107 }
108 int ano = c.get(Calendar.YEAR);
109 int hora = c.get(Calendar.HOUR_OF_DAY);
110 int minuto = c.get(Calendar.MINUTE);
111 String data = dia + "/" + mes + "/" + ano + " " + hora + ":" + minuto;
112 setData(p, "VIPSemanal", data);
113 return false;
114 }
115 if (kit.equals("vipmensal")) {
116 if (!getData(p, "VIPMensal").equals("--/--/-- --:--")) {
117 p.sendMessage("§cAguarde até " + getData(p, "VIPMensal") + " para recolher este kit novamente.");
118 return true;
119 }
120 Calendar c = Calendar.getInstance();
121 int dia = c.get(Calendar.DAY_OF_MONTH);
122 int mes = c.get(Calendar.MONTH) + 2;
123 int ano = c.get(Calendar.YEAR);
124 int hora = c.get(Calendar.HOUR_OF_DAY);
125 int minuto = c.get(Calendar.MINUTE);
126 String data = dia + "/" + mes + "/" + ano + " " + hora + ":" + minuto;
127 setData(p, "VIPMensal", data);
128 return false;
129 }
130 if (kit.equals("pedra")) {
131 if (!getData(p, "Pedra").equals("--/--/-- --:--")) {
132 p.sendMessage("§cAguarde até " + getData(p, "Pedra") + " para recolher este kit novamente.");
133 return true;
134 }
135 Calendar c = Calendar.getInstance();
136 int dia = c.get(Calendar.DAY_OF_MONTH) + 7;
137 int mes = c.get(Calendar.MONTH) + 1;
138 if (dia > 29) {
139 dia = 1;
140 mes = mes + 1;
141 }
142 int ano = c.get(Calendar.YEAR);
143 int hora = c.get(Calendar.HOUR_OF_DAY);
144 int minuto = c.get(Calendar.MINUTE);
145 String data = dia + "/" + mes + "/" + ano + " " + hora + ":" + minuto;
146 setData(p, "Pedra", data);
147 return false;
148 }
149 return false;
150 }
151
152 public void iniciarDatabase() throws SQLException {
153 db = new MySQL(this.plugin, Main.plugin.getConfig().getString("mysql.host-name"),
154 Main.plugin.getConfig().getString("mysql.porta"), Main.plugin.getConfig().getString("mysql.database"),
155 Main.plugin.getConfig().getString("mysql.usuario"), Main.plugin.getConfig().getString("mysql.senha"));
156 db.openConnection();
157 Statement statement = db.getConnection().createStatement();
158 statement.executeUpdate(
159 "CREATE TABLE IF NOT EXISTS `kits` (`UUID` varchar(255), `NoobDiario` varchar(255), `Key` varchar(255), `VIPDiario` varchar(255), `VIPSemanal` varchar(255), `VIPMensal` varchar(255), `Pedra` varchar(255))");
160
161 this.plugin.database = true;
162 }
163
164 public static void criarDados(UUID uuid) throws SQLException {
165 if (!db.checkConnection()) {
166 db.openConnection();
167 }
168 Statement s = db.getConnection().createStatement();
169 ResultSet rs = s.executeQuery("SELECT * FROM kits WHERE `UUID`='" + uuid + "';");
170 if (rs.next()) {
171 return;
172 }
173 s.executeUpdate(
174 "INSERT INTO kits (`UUID`, `NoobDiario`, `Key`, `VIPDiario`, `VIPSemanal`, `VIPMensal`, `Pedra`) VALUES ('"
175 + uuid
176 + "', '--/--/-- --:--', '--/--/-- --:--', '--/--/-- --:--', '--/--/-- --:--', '--/--/-- --:--', '--/--/-- --:--');");
177 }
178
179 public static String getData(Player p, String kit) {
180 if (!db.checkConnection()) {
181 db.openConnection();
182 }
183 Statement s;
184 try {
185 s = db.getConnection().createStatement();
186 ResultSet rs = s.executeQuery("SELECT * FROM kits WHERE `UUID`='" + p.getUniqueId() + "';");
187 if (!rs.next()) {
188 return "--/--/-- --:--";
189 }
190 String retorno = rs.getString(kit);
191 return retorno;
192 } catch (SQLException e) {
193 Main.plugin.getLogger().severe("Nao foi possivel carregar os dados do jogador: " + p.getName());
194 }
195 return "--/--/-- --:--";
196 }
197
198 public static void setData(Player p, String kit, String data) {
199 if (!db.checkConnection()) {
200 db.openConnection();
201 }
202 Statement s;
203 try {
204 s = db.getConnection().createStatement();
205 s.executeUpdate(
206 "UPDATE kits SET `" + kit + "`='" + data + "' WHERE `UUID`='" + p.getUniqueId().toString() + "';");
207 } catch (SQLException e) {
208 Main.plugin.getLogger().severe("Nao foi possivel atualizar os dados do jogador: " + p.getName());
209 }
210 }
211
212 public static void checar(Player p, String file) {
213 Calendar c = Calendar.getInstance();
214 int diac = c.get(Calendar.DAY_OF_MONTH);
215 int mesc = c.get(Calendar.MONTH) + 1;
216 int horac = c.get(Calendar.HOUR_OF_DAY);
217 int minutoc = c.get(Calendar.MINUTE);
218 if (file.equals("NoobDiario")) {
219 if (!getData(p, "NoobDiario").equals("--/--/-- --:--")) {
220 String data = getData(p, "NoobDiario");
221 String dias[] = data.split(" ");
222
223 // args[0] - data
224 String dia[] = dias[0].split("/");
225 String diad = dia[0];
226
227 // args[1] - hora
228 String horas[] = dias[1].split(":");
229 String hora = horas[0];
230 String minuto = horas[1];
231 if (diac >= Integer.valueOf(diad)) {
232 if ((horac >= Integer.valueOf(hora))
233 || (horac == Integer.valueOf(hora)) && (minutoc >= Integer.valueOf(minuto))) {
234 setData(p, "NoobDiario", "--/--/-- --:--");
235 }
236 }
237 }
238 }
239 if (file.equals("Key")) {
240 if (!getData(p, "Key").equals("--/--/-- --:--")) {
241 String data = getData(p, "Key");
242 String dias[] = data.split(" ");
243
244 // args[0] - data
245 String dia[] = dias[0].split("/");
246 String diad = dia[0];
247
248 // args[1] - hora
249 String horas[] = dias[1].split(":");
250 String hora = horas[0];
251 String minuto = horas[1];
252 if (diac >= Integer.valueOf(diad)) {
253 if ((horac >= Integer.valueOf(hora))
254 || (horac == Integer.valueOf(hora)) && (minutoc >= Integer.valueOf(minuto))) {
255 setData(p, "Key", "--/--/-- --:--");
256 }
257 }
258 }
259 }
260 if (file.equals("VIPDiario")) {
261 if (!getData(p, "VIPDiario").equals("--/--/-- --:--")) {
262 String data = getData(p, "VIPDiario");
263 String dias[] = data.split(" ");
264
265 // args[0] - data
266 String dia[] = dias[0].split("/");
267 String diad = dia[0];
268
269 // args[1] - hora
270 String horas[] = dias[1].split(":");
271 String hora = horas[0];
272 String minuto = horas[1];
273 if (diac >= Integer.valueOf(diad)) {
274 if ((horac >= Integer.valueOf(hora))
275 || (horac == Integer.valueOf(hora)) && (minutoc >= Integer.valueOf(minuto))) {
276 setData(p, "Key", "--/--/-- --:--");
277 }
278 }
279 }
280 }
281 if (file.equals("VIPSemanal")) {
282 if (!getData(p, "VIPSemanal").equals("--/--/-- --:--")) {
283 String data = getData(p, "VIPSemanal");
284 String dias[] = data.split(" ");
285
286 // args[0] - data
287 String dia[] = dias[0].split("/");
288 String diad = dia[0];
289
290 // args[1] - hora
291 String horas[] = dias[1].split(":");
292 String hora = horas[0];
293 String minuto = horas[1];
294 if (diac >= Integer.valueOf(diad)) {
295 if ((horac >= Integer.valueOf(hora))
296 || (horac == Integer.valueOf(hora)) && (minutoc >= Integer.valueOf(minuto))) {
297 setData(p, "VIPSemanal", "--/--/-- --:--");
298 }
299 }
300 }
301 }
302 if (file.equals("VIPMensal")) {
303 if (!getData(p, "VIPMensal").equals("--/--/-- --:--")) {
304 String data = getData(p, "VIPMensal");
305 String dias[] = data.split(" ");
306
307 // args[0] - data
308 String dia[] = dias[0].split("/");
309 String mes = dia[1];
310
311 // args[1] - hora
312 String horas[] = dias[1].split(":");
313 String hora = horas[0];
314 String minuto = horas[1];
315 if (mesc >= Integer.valueOf(mes)) {
316 if (horac >= Integer.valueOf(hora)) {
317 if (minutoc >= Integer.valueOf(minuto)) {
318 setData(p, "Key", "--/--/-- --:--");
319 }
320 }
321 }
322 }
323 if (file.equals("Pedra")) {
324 if (!getData(p, "Pedra").equals("--/--/-- --:--")) {
325 String data = getData(p, "Pedra");
326 String dias[] = data.split(" ");
327
328 // args[0] - data
329 String dia[] = dias[0].split("/");
330 String diad = dia[0];
331
332 // args[1] - hora
333 String horas[] = dias[1].split(":");
334 String hora = horas[0];
335 String minuto = horas[1];
336 if (diac >= Integer.valueOf(diad)) {
337 if ((horac >= Integer.valueOf(hora))
338 || (horac == Integer.valueOf(hora)) && (minutoc >= Integer.valueOf(minuto))) {
339 setData(p, "Pedra", "--/--/-- --:--");
340 }
341 }
342 }
343 }
344 }
345 }
346
347 @EventHandler
348 private void entrar(PlayerJoinEvent e) {
349 Player p = e.getPlayer();
350 checar(p, "NoobDiario");
351 checar(p, "Key");
352 checar(p, "VIPDiario");
353 checar(p, "VIPMensal");
354 checar(p, "Pedra");
355 }
356}