@@ -55,20 +55,23 @@ public void setup() {
55
55
* @return the interim data
56
56
*/
57
57
public InterimData load (String name , String uuid , String ip ) {
58
- Set <Punishment > punishments = new HashSet <>();
59
- Set <Punishment > history = new HashSet <>();
58
+ Set <Punishment > punishments = new HashSet <>();
59
+ Set <Punishment > history = new HashSet <>();
60
60
try (ResultSet resultsPunishments = DatabaseManager .get ().executeResultStatement (SQLQuery .SELECT_USER_PUNISHMENTS_WITH_IP , uuid , ip ); ResultSet resultsHistory = DatabaseManager .get ().executeResultStatement (SQLQuery .SELECT_USER_PUNISHMENTS_HISTORY_WITH_IP , uuid , ip )) {
61
-
62
- while (resultsPunishments .next ()) {
61
+ if (resultsHistory == null || resultsPunishments == null )
62
+ return null ;
63
+
64
+ while (resultsPunishments .next ()) {
63
65
punishments .add (getPunishmentFromResultSet (resultsPunishments ));
64
- }
66
+ }
65
67
while (resultsHistory .next ()) {
66
68
history .add (getPunishmentFromResultSet (resultsHistory ));
67
69
}
68
-
70
+
69
71
} catch (SQLException ex ) {
70
72
universal .log ("An error has occurred loading the punishments from the database." );
71
73
universal .debugSqlException (ex );
74
+ return null ;
72
75
}
73
76
return new InterimData (uuid , name , ip , punishments , history );
74
77
}
@@ -116,7 +119,7 @@ public List<Punishment> getPunishments(String target, PunishmentType put, boolea
116
119
List <Punishment > ptList = new ArrayList <>();
117
120
118
121
if (isCached (target )) {
119
- for (Iterator <Punishment > iterator = (current ? punishments : history ).iterator (); iterator .hasNext ();) {
122
+ for (Iterator <Punishment > iterator = (current ? punishments : history ).iterator (); iterator .hasNext (); ) {
120
123
Punishment pt = iterator .next ();
121
124
if ((put == null || put == pt .getType ().getBasic ()) && pt .getUuid ().equals (target )) {
122
125
if (!current || !pt .isExpired ()) {
@@ -176,19 +179,26 @@ public List<Punishment> getPunishments(SQLQuery sqlQuery, Object... parameters)
176
179
* @return the punishment
177
180
*/
178
181
public Punishment getPunishment (int id ) {
179
- ResultSet rs = DatabaseManager .get ().executeResultStatement (SQLQuery .SELECT_PUNISHMENT_BY_ID , id );
180
- Punishment pt = null ;
181
- try {
182
+ final Optional <Punishment > cachedPunishment = getLoadedPunishments (false ).stream ()
183
+ .filter (punishment -> punishment .getId () == id ).findAny ();
184
+
185
+ if (cachedPunishment .isPresent ())
186
+ return cachedPunishment .get ();
187
+
188
+
189
+ try (ResultSet rs = DatabaseManager .get ().executeResultStatement (SQLQuery .SELECT_PUNISHMENT_BY_ID , id )) {
182
190
if (rs .next ()) {
183
- pt = getPunishmentFromResultSet (rs );
191
+ final Punishment punishment = getPunishmentFromResultSet (rs );
192
+ if (!punishment .isExpired ())
193
+ return punishment ;
184
194
}
185
- rs .close ();
186
195
} catch (SQLException ex ) {
187
196
universal .log ("An error has occurred getting a punishment by his id." );
188
197
universal .debug ("Punishment id: '" + id + "'" );
189
198
universal .debugSqlException (ex );
190
199
}
191
- return pt == null || pt .isExpired () ? null : pt ;
200
+
201
+ return null ;
192
202
}
193
203
194
204
/**
@@ -200,7 +210,7 @@ public Punishment getPunishment(int id) {
200
210
public Punishment getWarn (int id ) {
201
211
Punishment punishment = getPunishment (id );
202
212
203
- if (punishment == null )
213
+ if (punishment == null )
204
214
return null ;
205
215
206
216
return punishment .getType ().getBasic () == PunishmentType .WARNING ? punishment : null ;
@@ -292,14 +302,14 @@ public int getCalculationLevel(String uuid, String layout) {
292
302
if (isCached (uuid )) {
293
303
return (int ) history .stream ().filter (pt -> pt .getUuid ().equals (uuid ) && layout .equalsIgnoreCase (pt .getCalculation ())).count ();
294
304
}
295
-
305
+
296
306
int i = 0 ;
297
307
try (ResultSet resultSet = DatabaseManager .get ().executeResultStatement (SQLQuery .SELECT_USER_PUNISHMENTS_HISTORY_BY_CALCULATION , uuid , layout )) {
298
-
308
+
299
309
while (resultSet .next ()) {
300
- i ++;
310
+ i ++;
301
311
}
302
-
312
+
303
313
} catch (SQLException ex ) {
304
314
universal .log ("An error has occurred getting the level for the layout '" + layout + "' for '" + uuid + "'" );
305
315
universal .debugSqlException (ex );
0 commit comments