Skip to content

Commit 80b4107

Browse files
committed
Closed #26
1 parent 005e7b3 commit 80b4107

File tree

5 files changed

+30
-33
lines changed

5 files changed

+30
-33
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ public class EventExecutor implements Listener {
2626

2727
@EventHandler
2828
public void handle(PlayerLoginEvent event) {
29-
this.userManager.lockUser(event.getPlayer().getUniqueId());
29+
UUID uuid = event.getPlayer().getUniqueId();
30+
if (Config.DEBUG) {
31+
main.logMessage("Lock user " + uuid + " done!");
32+
}
33+
this.userManager.lockUser(uuid);
3034
}
3135

3236
@EventHandler
@@ -44,12 +48,10 @@ public void handle(PlayerQuitEvent event) {
4448
UUID uuid = event.getPlayer().getUniqueId();
4549
if (userManager.isUserNotLocked(uuid)) {
4650
userManager.cancelTask(uuid);
47-
userManager.cacheUser(uuid);
48-
49-
User user = userManager.getUser(uuid);
50-
userManager.syncUser(user, true);
51+
userManager.syncUser(uuid, true);
5152
main.runTaskAsynchronously(() -> {
52-
userManager.saveUser(user, false);
53+
userManager.saveUser(uuid, false);
54+
userManager.cacheUser(uuid, null);
5355
});
5456
}
5557
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@ public boolean isLocked() {
118118
}
119119

120120
public void setLocked(boolean locked) {
121-
synchronized (this) {
122-
this.locked = locked;
123-
}
121+
this.locked = locked;
124122
}
125123

126124
public Timestamp getLastUpdate() {

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public void saveUser(User user, boolean lock) {
104104
this.main.getDatabase().save(user);
105105
}
106106
if (Config.DEBUG) {
107-
this.main.logMessage("Save user " + user.getUuid() + " done!");
107+
this.main.logMessage("Save user data " + user.getUuid() + " done!");
108108
}
109109
}
110110

@@ -141,6 +141,10 @@ public void syncUser(User user, boolean closedInventory) {
141141
}
142142
}
143143

144+
public void syncUser(UUID uuid, boolean closedInventory) {
145+
syncUser(userMap.get(uuid), closedInventory);
146+
}
147+
144148
public boolean isUserLocked(UUID uuid) {
145149
return this.locked.indexOf(uuid) != -1;
146150
}
@@ -155,7 +159,7 @@ public void lockUser(UUID uuid) {
155159

156160
public void unlockUser(UUID uuid, boolean scheduled) {
157161
if (Config.DEBUG) {
158-
main.logMessage("Unlock user task on thread " + Thread.currentThread().getName() + '.');
162+
main.logMessage("Unlock user task on " + Thread.currentThread().getName() + '.');
159163
}
160164
if (scheduled) {
161165
this.main.runTask(() -> unlockUser(uuid, false));
@@ -300,7 +304,7 @@ public void createTask(UUID uuid) {
300304
BukkitTask old = this.taskMap.put(uuid, task);
301305
if (old != null) {
302306
if (Config.DEBUG) {
303-
this.main.logMessage("Already schedule task for user " + uuid + '!');
307+
this.main.logMessage("Already scheduled task for user " + uuid + '!');
304308
}
305309
old.cancel();
306310
}

src/main/java/com/mengcraft/playersql/lib/JSONUtil.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,12 @@
99
*/
1010
public final class JSONUtil {
1111

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-
2012
public static JSONArray parseArray(String in, JSONArray normal) {
21-
Object parsed = JSONValue.parse(in);
22-
if (parsed instanceof JSONArray) {
23-
return ((JSONArray) parsed);
13+
if (in != null) {
14+
Object parsed = JSONValue.parse(in);
15+
if (parsed instanceof JSONArray) {
16+
return ((JSONArray) parsed);
17+
}
2418
}
2519
return normal;
2620
}

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,27 @@ public class FetchUserTask implements Runnable {
2121
public synchronized void run() {
2222
User user = this.executor.getUserManager().fetchUser(this.uuid);
2323
if (user == null) {
24-
if (Config.DEBUG) {
25-
this.executor.getMain().logMessage("Fresh user " + this.uuid + '.');
26-
}
2724
this.executor.getUserManager().cacheUser(this.uuid);
2825
this.executor.getUserManager().saveUser(this.uuid, true);
29-
this.executor.cancelTask(this.taskId);
26+
this.executor.getUserManager().createTask(this.uuid);
3027
this.executor.getUserManager().unlockUser(this.uuid, true);
31-
} else if (user.isLocked() && this.retryCount++ < 8) {
3228
if (Config.DEBUG) {
33-
this.executor.getMain().logMessage("Load user " + uuid + " retry at " + retryCount + '.');
29+
this.executor.getMain().logMessage("User data " + this.uuid + " not found!");
3430
}
35-
} else {
31+
this.executor.cancelTask(this.taskId);
32+
} else if (user.isLocked() && this.retryCount++ < 8) {
3633
if (Config.DEBUG) {
37-
this.executor.getMain().logMessage("Load user " + uuid + " done. Scheduling store data.");
34+
this.executor.getMain().logMessage("Load user data " + uuid + " fail " + retryCount + '.');
3835
}
36+
} else {
3937
this.executor.getUserManager().cacheUser(this.uuid, user);
4038
this.executor.getUserManager().addFetched(user);
41-
this.executor.cancelTask(this.taskId);
4239
this.executor.getUserManager().saveUser(user, true);
4340
if (Config.DEBUG) {
44-
this.executor.getMain().logMessage("Lock user " + uuid + " done.");
41+
this.executor.getMain().logMessage("Load user data " + uuid + " done.");
42+
this.executor.getMain().logMessage("Lock user data " + uuid + " done.");
4543
}
44+
this.executor.cancelTask(this.taskId);
4645
}
4746
}
4847

0 commit comments

Comments
 (0)