· 9 years ago · Oct 18, 2016, 07:56 PM
1package net.aerion.moderationsystem.commandsgeneral.ban;
2
3import java.sql.PreparedStatement;
4import java.util.function.Consumer;
5
6import net.aerion.api.sql.DatabaseConnection;
7import net.aerion.api.sql.SQLDatabase;
8import net.aerion.api.sql.abstracts.AbstractTable;
9import net.aerion.api.sql.util.DatabaseUtils;
10import net.aerion.api.sql.util.SQLResult;
11import net.aerion.api.sqlservice.Identifier;
12import net.aerion.api.sqlservice.SQLServiceExecutor;
13import net.aerion.api.sqlservice.ServiceResult;
14import net.aerion.moderationsystem.Main;
15import net.aerion.moderationsystem.SQLManagerInstance;
16
17public class BanManager extends AbstractTable {
18
19 public BanManager(String uuid) {
20 super("AerionModerationsSystem-Bans");
21 }
22
23 // UUID, banTime (BIGINT), unbanTime, reason TEXT, id
24
25 @Override
26 protected void executeCreateQuery(DatabaseConnection con) {
27 con.update(
28 "CREATE TABLE IF NOT EXISTS AerionModerationsSystem-Bans (PRIMARY KEY(id), uuid VARCHAR(36),banTime BIGINT, unbanTime BIGINT, reason TEXT, id INT AUTO_INCREMENT)");
29 }
30
31 private static void containsEntry(String uuid, Consumer<Boolean> action) {
32 SQLServiceExecutor executor = SQLManagerInstance.getServive().executor();
33
34 executor.executeSelectAction(Identifier.player(Main.getManager(), uuid, "containsEntry"), new Consumer<SQLDatabase>() {
35
36 @Override
37 public void accept(SQLDatabase database) {
38 DatabaseConnection con = database.getConnection();
39 PreparedStatement stmt = con.prepare("SELECT COUNT(*) AS total FROM AerionModerationsSystem-Bans WHERE uuid = ?");
40 DatabaseUtils.setString(stmt, 1, uuid);
41 SQLResult result = con.query(stmt);
42
43 if (result.next()) {
44 if (result.current("total", long.class) >= 1) {
45 action.accept(true);
46 } else {
47 action.accept(false);
48 }
49 } else {
50 action.accept(null);
51 }
52
53 result.close();
54
55 }
56 }, new Consumer<ServiceResult>() {
57 @Override
58 public void accept(ServiceResult result) {
59 if (result == ServiceResult.INTERACTION_FAILURE) {
60 action.accept(null);
61 }
62 }
63 });
64 }
65
66 public static void addEntry(String uuid, long unbanTime, String reason, Consumer<Boolean> action) {
67
68 SQLServiceExecutor executor = SQLManagerInstance.getServive().executor();
69
70 executor.executeInsertAction(Identifier.player(Main.getManager(), uuid, "addEntry"), new Consumer<SQLDatabase>() {
71
72 @Override
73 public void accept(SQLDatabase database) {
74 DatabaseConnection con = database.getConnection();
75 PreparedStatement stmt = con.prepare("INSERT INTO AerionModerationsSystem-Bans (uuid, banTime, unbanTime, reason) VALUES (?,?,?)");
76 DatabaseUtils.setString(stmt, 1, uuid);
77 DatabaseUtils.setLong(stmt, 2, System.currentTimeMillis());
78 DatabaseUtils.setLong(stmt, 3, unbanTime);
79 DatabaseUtils.setString(stmt, 4, reason);
80 con.update(stmt);
81
82 }
83 }, new Consumer<ServiceResult>() {
84
85 @Override
86 public void accept(ServiceResult result) {
87 if (result == ServiceResult.INTERACTION_SUCCESS) {
88 action.accept(true);
89 } else if (result == ServiceResult.INTERACTION_FAILURE) {
90 action.accept(false);
91 }
92
93 }
94 });
95
96 }
97
98 private static void isTimeOver(String uuid, Consumer<Boolean> action) {
99 BanManager.getUnbanTime(uuid, new Consumer<Long>() {
100
101 @Override
102 public void accept(Long t) {
103
104 if(t == null) {
105 action.accept(null);
106 }
107
108 if(System.currentTimeMillis() >= t) {
109 action.accept(true);
110 } else {
111 action.accept(false);
112 }
113 }
114 });
115 }
116
117 public static void isBanned(String uuid, Consumer<Boolean> action) {
118 BanManager.containsEntry(uuid, new Consumer<Boolean>() {
119
120 @Override
121 public void accept(Boolean t) {
122 if (t == null) {
123 return;
124 }
125
126 if (t) {
127
128 BanManager.isTimeOver(uuid, new Consumer<Boolean>() {
129
130 @Override
131 public void accept(Boolean t) {
132 if(t == null) {
133 return;
134 }
135 if(t) {
136 action.accept(true);
137 } else {
138 action.accept(false);
139 }
140 }
141 });
142
143
144 } else {
145 action.accept(false);
146 }
147 }
148 });
149 }
150
151 public static void getReason(String uuid, Consumer<String> action) {
152 SQLServiceExecutor service = SQLManagerInstance.getServive().executor();
153
154 service.executeSelectAction(Identifier.player(Main.getManager(), uuid, "selectReason"), new Consumer<SQLDatabase>() {
155
156 @Override
157 public void accept(SQLDatabase database) {
158 DatabaseConnection con = database.getConnection();
159 PreparedStatement stmt = con.prepare("SELECT reason FROM AerionModerationsSystem-Bans ORDER BY id DESC LIMIT 1 WHERE uuid=?");
160 DatabaseUtils.setString(stmt, 1, uuid);
161 SQLResult result = con.query(stmt);
162
163 if (result.next()) {
164 action.accept(result.current("reason", String.class));
165 } else {
166 action.accept(null);
167 }
168 result.close();
169 }
170 }, new Consumer<ServiceResult>() {
171 @Override
172 public void accept(ServiceResult result) {
173 if (result == ServiceResult.INTERACTION_FAILURE) {
174 action.accept(null);
175 }
176
177 }
178 });
179 }
180
181 public static void getUnbanTime(String uuid, Consumer<Long> action) {
182 SQLServiceExecutor service = SQLManagerInstance.getServive().executor();
183
184 service.executeSelectAction(Identifier.player(Main.getManager(), uuid, "selectTime"), new Consumer<SQLDatabase>() {
185
186 @Override
187 public void accept(SQLDatabase database) {
188 DatabaseConnection con = database.getConnection();
189 PreparedStatement stmt = con.prepare("SELECT unbanTime FROM AerionModerationsSystem-Bans ORDER BY id DESC LIMIT 1 WHERE uuid=?");
190 DatabaseUtils.setString(stmt, 1, uuid);
191 SQLResult result = con.query(stmt);
192
193 if (result.next()) {
194 action.accept(result.current("unbanTime", Long.class));
195 } else {
196 action.accept(null);
197 }
198 result.close();
199 }
200 }, new Consumer<ServiceResult>() {
201
202 @Override
203 public void accept(ServiceResult result) {
204 if (result == ServiceResult.INTERACTION_FAILURE) {
205 action.accept(null);
206 }
207
208 }
209 });
210 }
211
212}