· 9 years ago · Aug 13, 2017, 01:18 PM
1package fr.futurixel.zenprotect;
2
3import java.sql.Connection;
4import java.sql.DriverManager;
5import java.sql.PreparedStatement;
6import java.sql.ResultSet;
7import java.sql.SQLException;
8import java.sql.Timestamp;
9import java.util.ArrayList;
10import java.util.Date;
11import java.util.List;
12
13import org.bukkit.Location;
14import org.bukkit.entity.Player;
15
16import fr.futurixel.zenprotect.locations.ZenClaimRequest;
17import fr.futurixel.zenprotect.utils.LocUtils;
18
19public class ZenDatabase {
20
21 private Connection connection;
22 private String urlbase;
23 private String host;
24 private String database;
25 private String user;
26 private String pass;
27
28 public ZenDatabase(String urlbase, String host, String database, String user, String pass) {
29 this.urlbase = urlbase;
30 this.host = host;
31 this.database = database;
32 this.user = user;
33 this.pass = pass;
34 }
35
36 public void connection() {
37 if (!isConnected()) {
38 try {
39 connection = DriverManager.getConnection(urlbase + host + "/" + database + "?autoReconnect=true", user, pass);
40 System.out.println("[ZenCore-SqlUtils] Succefuly connected with MySql Database");
41 } catch (SQLException e) {
42 e.printStackTrace();
43 }
44 }
45 createTable();
46 }
47
48 public void disconnect() {
49 if (isConnected()) {
50 try {
51 connection.close();
52 System.out.println("[ZenCore-SqlUtils] Succefuly disconnected with MySql Database");
53 } catch (SQLException e) {
54 e.printStackTrace();
55 }
56 }
57 }
58
59 public boolean isConnected() {
60 return connection != null;
61 }
62
63 public void createTable(){
64 try {
65 PreparedStatement q = connection.prepareStatement(
66 "CREATE TABLE IF NOT EXISTS requests ("
67 + "id INT(11) NOT NULL AUTO_INCREMENT, "
68 + "pseudo VARCHAR(255) NOT NULL, "
69 + "loc1 VARCHAR(255) NOT NULL,"
70 + "loc2 VARCHAR(255) NOT NULL,"
71 + "date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,"
72 + "PRIMARY KEY (id))");
73 q.execute();
74 q.close();
75 } catch (SQLException e) {
76 e.printStackTrace();
77 }
78 }
79
80 public void createRequest(Player player, Location loc1, Location loc2) {
81
82 if (hasRequest(player)) {
83 player.sendMessage("Vous avez dejà fait une requete");
84 return;
85 }
86
87 try {
88 PreparedStatement q = connection.prepareStatement("INSERT INTO requests(pseudo,loc1,loc2) VALUES (?,?,?)");
89 q.setString(1, player.getName());
90 q.setString(2, LocUtils.parse(loc1));
91 q.setString(3, LocUtils.parse(loc2));
92 q.execute();
93 q.close();
94 } catch (SQLException e) {
95 e.printStackTrace();
96 }
97
98 player.sendMessage("Une demande a été envoyé !");
99 }
100
101 public void removeRequest(int id) {
102
103 try {
104 PreparedStatement q = connection.prepareStatement("DELETE FROM requests WHERE id = ?");
105 q.setInt(1, id);
106 q.execute();
107 q.close();
108
109 } catch (SQLException e) {
110 e.printStackTrace();
111 }
112
113 }
114
115 public boolean hasRequest(Player player) {
116
117 try {
118 PreparedStatement q = connection.prepareStatement("SELECT pseudo FROM requests WHERE pseudo = ?");
119 q.setString(1, player.getName());
120 ResultSet resultat = q.executeQuery();
121 boolean hasAccount = resultat.next();
122 q.close();
123 return hasAccount;
124 } catch (SQLException e) {
125 e.printStackTrace();
126 }
127
128 return false;
129 }
130
131 public ZenClaimRequest getRequest(int id) {
132
133 try {
134 PreparedStatement q = connection.prepareStatement("SELECT * FROM requests WHERE id = ?");
135 q.setInt(1, id);
136
137 ResultSet resultat = q.executeQuery();
138
139 while(resultat.next()){
140 String player = resultat.getString("pseudo");
141 Location loc1 = LocUtils.unparse(resultat.getString("loc1"));
142 Location loc2 = LocUtils.unparse(resultat.getString("loc2"));
143 Timestamp date = resultat.getTimestamp("date");
144 return new ZenClaimRequest(player, id, loc1, loc2, date, loc2);
145 }
146
147 q.close();
148
149 } catch (SQLException e) {
150 e.printStackTrace();
151 }
152
153 return new ZenClaimRequest("Aucun", 0, null, null, new Timestamp(new Date().getTime()), null);
154
155 }
156
157 public List<ZenClaimRequest> getRequests() {
158
159 List<ZenClaimRequest> list = new ArrayList<>();
160
161 try {
162 PreparedStatement q = connection.prepareStatement("SELECT * FROM requests ORDER BY id DESC LIMIT 10");
163 ResultSet resultat = q.executeQuery();
164
165 while(resultat.next()){
166 int id = resultat.getInt("id");
167 String player = resultat.getString("pseudo");
168 Location loc1 = LocUtils.unparse(resultat.getString("loc1"));
169 Location loc2 = LocUtils.unparse(resultat.getString("loc2"));
170 Timestamp date = resultat.getTimestamp("date");
171 list.add(new ZenClaimRequest(player, id, loc1, loc2, date, loc2));
172 }
173
174 q.close();
175
176 } catch (SQLException e) {
177 e.printStackTrace();
178 }
179
180 return list;
181
182 }
183
184 public void clearRequests() {
185
186 try {
187 PreparedStatement q = connection.prepareStatement("ALTER TABLE requests AUTO_INCREMENT = 1");
188 q.execute();
189 } catch (SQLException e) {
190 e.printStackTrace();
191 }
192
193 }
194
195}