Skip to content

Commit e7acbbe

Browse files
committed
Update UserManager.java
1 parent cbbacd3 commit e7acbbe

File tree

4 files changed

+48
-10
lines changed

4 files changed

+48
-10
lines changed

src/main/java/com/mengcraft/playersql/PluginMain.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public Player getPlayer(UUID uuid) {
7070
}
7171

7272
public void logException(Exception e) {
73-
getLogger().log(Level.WARNING, e.getMessage(), e);
73+
getLogger().log(Level.WARNING, e.toString(), e);
7474
}
7575

7676
public void logMessage(String s) {

src/main/java/com/mengcraft/playersql/UserManager.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.mengcraft.playersql.lib.ExpUtil;
44
import com.mengcraft.playersql.lib.ItemUtil;
5+
import com.mengcraft.playersql.lib.JSONUtil;
56
import com.mengcraft.playersql.task.DailySaveTask;
67
import org.bukkit.Material;
78
import org.bukkit.entity.Player;
@@ -10,9 +11,13 @@
1011
import org.bukkit.potion.PotionEffectType;
1112
import org.bukkit.scheduler.BukkitTask;
1213
import org.json.simple.JSONArray;
13-
import org.json.simple.JSONValue;
1414

15-
import java.util.*;
15+
import java.util.ArrayList;
16+
import java.util.Collection;
17+
import java.util.List;
18+
import java.util.Map;
19+
import java.util.Queue;
20+
import java.util.UUID;
1621
import java.util.concurrent.ConcurrentHashMap;
1722
import java.util.concurrent.ConcurrentLinkedQueue;
1823

@@ -167,7 +172,11 @@ public void unlockUser(UUID uuid, boolean scheduled) {
167172
*/
168173
public void pendFetched() {
169174
while (!this.fetched.isEmpty()) {
170-
pend(this.fetched.poll());
175+
try {
176+
pend(this.fetched.poll());
177+
} catch (Exception e) {
178+
main.logException(e);
179+
}
171180
}
172181
}
173182

@@ -215,7 +224,7 @@ private void pend(User polled, Player player) {
215224

216225
@SuppressWarnings("unchecked")
217226
private List<PotionEffect> toEffect(String data) {
218-
List<List> parsed = (List) JSONValue.parse(data);
227+
List<List> parsed = JSONUtil.parseArray(data, JSONUtil.EMPTY_ARRAY);
219228
List<PotionEffect> output = new ArrayList<>(parsed.size());
220229
for (List<Number> entry : parsed) {
221230
output.add(new PotionEffect(PotionEffectType.getById(entry.get(0).intValue()), entry.get(1).intValue(), entry.get(2).intValue()));
@@ -225,7 +234,7 @@ private List<PotionEffect> toEffect(String data) {
225234

226235
@SuppressWarnings("unchecked")
227236
private ItemStack[] toStack(String data) {
228-
List<String> parsed = (List) JSONValue.parse(data);
237+
List<String> parsed = JSONUtil.parseArray(data, JSONUtil.EMPTY_ARRAY);
229238
List<ItemStack> output = new ArrayList<>(parsed.size());
230239
for (String s : parsed)
231240
if (s == null) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.mengcraft.playersql.lib;
2+
3+
import org.json.simple.JSONArray;
4+
import org.json.simple.JSONObject;
5+
import org.json.simple.JSONValue;
6+
7+
/**
8+
* Created on 16-1-19.
9+
*/
10+
public final class JSONUtil {
11+
12+
public static JSONObject parseObject(String in, JSONObject normal) {
13+
Object parsed = JSONValue.parse(in);
14+
if (parsed instanceof JSONObject) {
15+
return (JSONObject) parsed;
16+
}
17+
return normal;
18+
}
19+
20+
public static JSONArray parseArray(String in, JSONArray normal) {
21+
Object parsed = JSONValue.parse(in);
22+
if (parsed instanceof JSONArray) {
23+
return ((JSONArray) parsed);
24+
}
25+
return normal;
26+
}
27+
28+
public static final JSONArray EMPTY_ARRAY = new JSONArray();
29+
30+
}

src/main/java/com/mengcraft/playersql/task/FetchUserTask.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.mengcraft.playersql.Config;
44
import com.mengcraft.playersql.EventExecutor;
5-
import com.mengcraft.playersql.PluginException;
65
import com.mengcraft.playersql.User;
76

87
import java.util.UUID;
@@ -31,18 +30,18 @@ public synchronized void run() {
3130
this.executor.getUserManager().unlockUser(this.uuid, true);
3231
} else if (user.isLocked() && this.retryCount++ < 8) {
3332
if (Config.DEBUG) {
34-
this.executor.getMain().logException(new PluginException("Fetch " + this.uuid + " retry " + this.retryCount + '.'));
33+
this.executor.getMain().logMessage("Load user " + uuid + " retry at " + retryCount + '.');
3534
}
3635
} else {
3736
if (Config.DEBUG) {
38-
this.executor.getMain().logMessage("Load user " + this.uuid + " done. Scheduling store data.");
37+
this.executor.getMain().logMessage("Load user " + uuid + " done. Scheduling store data.");
3938
}
4039
this.executor.getUserManager().cacheUser(this.uuid, user);
4140
this.executor.getUserManager().addFetched(user);
4241
this.executor.cancelTask(this.taskId);
4342
this.executor.getUserManager().saveUser(user, true);
4443
if (Config.DEBUG) {
45-
this.executor.getMain().logMessage("Lock user " + this.uuid + " on database done.");
44+
this.executor.getMain().logMessage("Lock user " + uuid + " done.");
4645
}
4746
}
4847
}

0 commit comments

Comments
 (0)