· 6 years ago · Aug 05, 2019, 01:24 AM
1package fr.Rgld_.Fraud.Storage;
2
3import com.google.common.collect.Lists;
4import com.google.gson.GsonBuilder;
5import fr.Rgld_.Fraud.Fraud;
6import org.bukkit.entity.Player;
7
8import java.io.File;
9import java.io.IOException;
10import java.net.InetSocketAddress;
11import java.sql.*;
12import java.text.MessageFormat;
13import java.util.HashMap;
14import java.util.List;
15
16@SuppressWarnings("ALL")
17public class Datas {
18
19 private final Fraud fraud;
20 private final File jsonFile;
21 private final File file;
22 private final String TABLE_NAME_ips = "ips";
23 private final String TABLE_NAME_history = "history";
24 private final String TABLE_NAME_pref = "preferences";
25
26 public Datas(Fraud fraud) throws Throwable {
27 this.fraud = fraud;
28 jsonFile = new File(fraud.getDataFolder(), "data.json");
29 if (jsonFile.exists()) {
30 jsonFile.delete();
31 fraud.getConsole().sm("§eThe data file system has been changed. The file \"data.json\" has been deleted and replaced by a \"data.sqlite\". Sorry for the incovenient.");
32 }
33 file = new File(fraud.getDataFolder(), "data.sqlite");
34 createConnectionTable();
35 createHistoryTable();
36 createPreferenceTable();
37 }
38
39 private void createConnectionTable() {
40 try (Connection connection = connect()) {
41 String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME_ips + "(id integer PRIMARY KEY,pseudo text NOT NULL,ip text NOT NULL, actual boolean);";
42 connection.createStatement().execute(sql);
43 } catch (SQLException | ClassNotFoundException e) {
44 e.printStackTrace();
45 }
46 }
47
48 private void createPreferenceTable() {
49 try (Connection connection = connect()) {
50 String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME_pref + "(id integer PRIMARY KEY,key text NOT NULL,value text NOT NULL);";
51 connection.createStatement().execute(sql);
52 } catch (SQLException | ClassNotFoundException e) {
53 e.printStackTrace();
54 }
55 }
56 private void createHistoryTable() {
57 try (Connection connection = connect()) {
58 String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME_history + "(id integer PRIMARY KEY,pseudo text NOT NULL,ip text NOT NULL);";
59 connection.createStatement().execute(sql);
60 } catch (SQLException | ClassNotFoundException e) {
61 e.printStackTrace();
62 }
63 }
64
65 private Connection connect() throws SQLException, ClassNotFoundException {
66 File dataFolder = fraud.getDataFolder();
67 if (!file.exists())
68 try {
69 file.createNewFile();
70 } catch (IOException e) {
71 e.printStackTrace();
72 }
73
74 Class.forName("org.sqlite.JDBC");
75 return DriverManager.getConnection("jdbc:sqlite:" + file.getAbsolutePath().replace("\\", File.separator));
76 }
77
78 public File getFile() {
79 return file;
80 }
81
82
83 public void putPlayer(Player p) {
84 if (p.hasPermission("fraud.bypass.ip")) return;
85 try (Connection connection = connect()) {
86 String sql;
87 if (isEverRegisteredOnIps(p)) {
88 sql = MessageFormat.format("UPDATE `{0}` SET ip = ? WHERE pseudo = ?", TABLE_NAME_ips);
89 PreparedStatement psst = connection.prepareStatement(sql);
90 psst.setString(1, getAddress(p.getAddress()));
91 psst.setString(2, p.getName());
92 psst.executeUpdate();
93 } else {
94 sql = MessageFormat.format("INSERT INTO {0}(pseudo,ip) VALUES(?,?);", TABLE_NAME_ips);
95 PreparedStatement psst = connection.prepareStatement(sql);
96 psst.setString(1, p.getName());
97 psst.setString(2, getAddress(p.getAddress()));
98 psst.executeUpdate();
99 }
100 if (!isEverRegisteredOnHistory(p)) {
101 sql = MessageFormat.format("INSERT INTO {0}(pseudo,ip) VALUES(?,?);", TABLE_NAME_history);
102 PreparedStatement psst = connection.prepareStatement(sql);
103 psst.setString(1, p.getName());
104 psst.setString(2, getAddress(p.getAddress()));
105 psst.executeUpdate();
106 }
107
108 } catch (SQLException | ClassNotFoundException e) {
109 e.printStackTrace();
110 }
111 }
112
113 public boolean isEverRegisteredOnIps(Player p) {
114 try (Connection connection = connect()) {
115 String sql = MessageFormat.format("SELECT ip FROM `{0}` WHERE pseudo = ?", TABLE_NAME_ips);
116 PreparedStatement psst = connection.prepareStatement(sql);
117 psst.setString(1, p.getName());
118 return !psst.executeQuery().wasNull();
119 } catch (SQLException | ClassNotFoundException e) {
120 e.printStackTrace();
121 }
122 return false;
123 }
124
125 public boolean isEverRegisteredOnHistory(Player p) {
126 try (Connection connection = connect()) {
127 String sql = MessageFormat.format("SELECT ip FROM `{0}` WHERE pseudo = ? AND ip = ?", TABLE_NAME_history);
128 PreparedStatement psst = connection.prepareStatement(sql);
129 psst.setString(1, p.getName());
130 psst.setString(2, getAddress(p.getAddress()));
131 ResultSet rs = psst.executeQuery();
132 System.out.println("[DEBUG Fraud] " + new GsonBuilder().create().toJson(rs));
133 return !rs.wasNull();
134 } catch (SQLException | ClassNotFoundException e) {
135 e.printStackTrace();
136 }
137 return false;
138 }
139
140 public HashMap<String, String> getAllDatasFromIps() {
141 String sql = MessageFormat.format("SELECT * FROM `{0}`", TABLE_NAME_ips);
142 HashMap<String, String> map = new HashMap<>();
143 try (Connection connection = connect()) {
144 ResultSet rs = connection.prepareStatement(sql).executeQuery();
145 try {
146 while (rs.next()) {
147 map.put(rs.getString("pseudo"), rs.getString("ip"));
148 }
149 } catch (SQLException ignored) {
150 }
151 } catch (SQLException | ClassNotFoundException e) {
152 e.printStackTrace();
153 }
154 return map;
155 }
156
157 public HashMap<String, String> getAllDatasFromHistory() {
158 String sql = MessageFormat.format("SELECT * FROM `{0}`", TABLE_NAME_history);
159 HashMap<String, String> map = new HashMap<>();
160 try (Connection connection = connect()) {
161 ResultSet rs = connection.prepareStatement(sql).executeQuery();
162 try {
163 while (rs.next()) {
164 map.put(rs.getString("pseudo"), rs.getString("ip"));
165 }
166 } catch (SQLException ignored) {
167 }
168 } catch (SQLException | ClassNotFoundException e) {
169 e.printStackTrace();
170 }
171 return map;
172 }
173
174 public HashMap<String, String> getAllDatasFromPreferences() {
175 String sql = MessageFormat.format("SELECT * FROM `{0}`", TABLE_NAME_pref);
176 HashMap<String, String> map = new HashMap<>();
177 try (Connection connection = connect()) {
178 ResultSet rs = connection.prepareStatement(sql).executeQuery();
179 try {
180 while (rs.next()) {
181 map.put(rs.getString("key"), rs.getString("value"));
182 }
183 } catch (SQLException ignored) {
184 }
185 } catch (SQLException | ClassNotFoundException e) {
186 e.printStackTrace();
187 }
188 return map;
189 }
190
191 public List<String> getListByPseudo(String pseudo) {
192 String ipFromPseudo = null;
193
194 try (Connection connection = connect()) {
195 String sql_getIp = MessageFormat.format("SELECT ip FROM `{0}` WHERE pseudo = ?", TABLE_NAME_ips);
196 PreparedStatement psst_getIp = connection.prepareStatement(sql_getIp);
197 psst_getIp.setString(1, pseudo);
198 ResultSet rs_getIp = psst_getIp.executeQuery();
199 while (rs_getIp.next()) {
200 ipFromPseudo = rs_getIp.getString("ip");
201 }
202 if (ipFromPseudo == null) {
203 return Lists.newArrayList();
204 }
205 String sql_getPseudos = MessageFormat.format("SELECT pseudo FROM `{0}` WHERE ip = ?", TABLE_NAME_ips);
206 PreparedStatement psst_getPseudos = connection.prepareStatement(sql_getPseudos);
207 psst_getPseudos.setString(1, ipFromPseudo);
208 ResultSet rs = psst_getPseudos.executeQuery();
209 List<String> pseudos = Lists.newArrayList();
210 while (rs.next()) {
211 pseudos.add(rs.getString("pseudo"));
212 }
213 return pseudos;
214 } catch (SQLException | ClassNotFoundException e) {
215 e.printStackTrace();
216 }
217 return Lists.newArrayList();
218 }
219
220 public List<String> getHistoryByPlayer(Player player){
221 try (Connection connection = connect()) {
222 String sql = MessageFormat.format("SELECT ip FROM `{0}` WHERE pseudo = ?", TABLE_NAME_history);
223 PreparedStatement psst = connection.prepareStatement(sql);
224 psst.setString(1, player.getName());
225 ResultSet rs = psst.executeQuery();
226 List<String> stringList = Lists.newArrayList();
227 while(rs.next()){
228 stringList.add(rs.getString("ip"));
229 }
230 return stringList;
231 } catch (SQLException | ClassNotFoundException e) {
232 e.printStackTrace();
233 }
234 return Lists.newArrayList();
235 }
236
237 public List<String> getListByPlayer(Player player) {
238 return getListByAddress(getAddress(player.getAddress()));
239 }
240
241 public List<String> getListByAddress(String address) {
242 try (Connection connection = connect()) {
243 String sql = MessageFormat.format("SELECT pseudo FROM `{0}` WHERE ip = ?", TABLE_NAME_ips);
244 PreparedStatement psst = connection.prepareStatement(sql);
245 psst.setString(1, address);
246 ResultSet rs = psst.executeQuery();
247 List<String> pseudos = Lists.newArrayList();
248 while (rs.next()) {
249 pseudos.add(rs.getString("pseudo"));
250 }
251 return pseudos;
252 } catch (SQLException | ClassNotFoundException e) {
253 e.printStackTrace();
254 }
255 return Lists.newArrayList();
256 }
257
258 private String getAddress(InetSocketAddress address) {
259 return address.toString().split(":")[0].substring(1);
260 }
261
262}