From 80b4107053bf441bcbc5bbe2434ccc71fb3949e2 Mon Sep 17 00:00:00 2001 From: caoli5288 Date: Mon, 25 Jan 2016 14:11:03 +0800 Subject: [PATCH] Closed #26 --- .../mengcraft/playersql/EventExecutor.java | 14 ++++++++------ .../java/com/mengcraft/playersql/User.java | 4 +--- .../com/mengcraft/playersql/UserManager.java | 10 +++++++--- .../com/mengcraft/playersql/lib/JSONUtil.java | 16 +++++----------- .../playersql/task/FetchUserTask.java | 19 +++++++++---------- 5 files changed, 30 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/mengcraft/playersql/EventExecutor.java b/src/main/java/com/mengcraft/playersql/EventExecutor.java index e07e732..5463b4b 100644 --- a/src/main/java/com/mengcraft/playersql/EventExecutor.java +++ b/src/main/java/com/mengcraft/playersql/EventExecutor.java @@ -26,7 +26,11 @@ public class EventExecutor implements Listener { @EventHandler public void handle(PlayerLoginEvent event) { - this.userManager.lockUser(event.getPlayer().getUniqueId()); + UUID uuid = event.getPlayer().getUniqueId(); + if (Config.DEBUG) { + main.logMessage("Lock user " + uuid + " done!"); + } + this.userManager.lockUser(uuid); } @EventHandler @@ -44,12 +48,10 @@ public void handle(PlayerQuitEvent event) { UUID uuid = event.getPlayer().getUniqueId(); if (userManager.isUserNotLocked(uuid)) { userManager.cancelTask(uuid); - userManager.cacheUser(uuid); - - User user = userManager.getUser(uuid); - userManager.syncUser(user, true); + userManager.syncUser(uuid, true); main.runTaskAsynchronously(() -> { - userManager.saveUser(user, false); + userManager.saveUser(uuid, false); + userManager.cacheUser(uuid, null); }); } } diff --git a/src/main/java/com/mengcraft/playersql/User.java b/src/main/java/com/mengcraft/playersql/User.java index 5c38db3..8c0abdc 100644 --- a/src/main/java/com/mengcraft/playersql/User.java +++ b/src/main/java/com/mengcraft/playersql/User.java @@ -118,9 +118,7 @@ public boolean isLocked() { } public void setLocked(boolean locked) { - synchronized (this) { - this.locked = locked; - } + this.locked = locked; } public Timestamp getLastUpdate() { diff --git a/src/main/java/com/mengcraft/playersql/UserManager.java b/src/main/java/com/mengcraft/playersql/UserManager.java index 2def2bc..3e0dbb2 100644 --- a/src/main/java/com/mengcraft/playersql/UserManager.java +++ b/src/main/java/com/mengcraft/playersql/UserManager.java @@ -104,7 +104,7 @@ public void saveUser(User user, boolean lock) { this.main.getDatabase().save(user); } if (Config.DEBUG) { - this.main.logMessage("Save user " + user.getUuid() + " done!"); + this.main.logMessage("Save user data " + user.getUuid() + " done!"); } } @@ -141,6 +141,10 @@ public void syncUser(User user, boolean closedInventory) { } } + public void syncUser(UUID uuid, boolean closedInventory) { + syncUser(userMap.get(uuid), closedInventory); + } + public boolean isUserLocked(UUID uuid) { return this.locked.indexOf(uuid) != -1; } @@ -155,7 +159,7 @@ public void lockUser(UUID uuid) { public void unlockUser(UUID uuid, boolean scheduled) { if (Config.DEBUG) { - main.logMessage("Unlock user task on thread " + Thread.currentThread().getName() + '.'); + main.logMessage("Unlock user task on " + Thread.currentThread().getName() + '.'); } if (scheduled) { this.main.runTask(() -> unlockUser(uuid, false)); @@ -300,7 +304,7 @@ public void createTask(UUID uuid) { BukkitTask old = this.taskMap.put(uuid, task); if (old != null) { if (Config.DEBUG) { - this.main.logMessage("Already schedule task for user " + uuid + '!'); + this.main.logMessage("Already scheduled task for user " + uuid + '!'); } old.cancel(); } diff --git a/src/main/java/com/mengcraft/playersql/lib/JSONUtil.java b/src/main/java/com/mengcraft/playersql/lib/JSONUtil.java index 10860e8..0db42d1 100644 --- a/src/main/java/com/mengcraft/playersql/lib/JSONUtil.java +++ b/src/main/java/com/mengcraft/playersql/lib/JSONUtil.java @@ -9,18 +9,12 @@ */ public final class JSONUtil { - public static JSONObject parseObject(String in, JSONObject normal) { - Object parsed = JSONValue.parse(in); - if (parsed instanceof JSONObject) { - return (JSONObject) parsed; - } - return normal; - } - public static JSONArray parseArray(String in, JSONArray normal) { - Object parsed = JSONValue.parse(in); - if (parsed instanceof JSONArray) { - return ((JSONArray) parsed); + if (in != null) { + Object parsed = JSONValue.parse(in); + if (parsed instanceof JSONArray) { + return ((JSONArray) parsed); + } } return normal; } diff --git a/src/main/java/com/mengcraft/playersql/task/FetchUserTask.java b/src/main/java/com/mengcraft/playersql/task/FetchUserTask.java index 51ee393..a88c51e 100644 --- a/src/main/java/com/mengcraft/playersql/task/FetchUserTask.java +++ b/src/main/java/com/mengcraft/playersql/task/FetchUserTask.java @@ -21,28 +21,27 @@ public class FetchUserTask implements Runnable { public synchronized void run() { User user = this.executor.getUserManager().fetchUser(this.uuid); if (user == null) { - if (Config.DEBUG) { - this.executor.getMain().logMessage("Fresh user " + this.uuid + '.'); - } this.executor.getUserManager().cacheUser(this.uuid); this.executor.getUserManager().saveUser(this.uuid, true); - this.executor.cancelTask(this.taskId); + this.executor.getUserManager().createTask(this.uuid); this.executor.getUserManager().unlockUser(this.uuid, true); - } else if (user.isLocked() && this.retryCount++ < 8) { if (Config.DEBUG) { - this.executor.getMain().logMessage("Load user " + uuid + " retry at " + retryCount + '.'); + this.executor.getMain().logMessage("User data " + this.uuid + " not found!"); } - } else { + this.executor.cancelTask(this.taskId); + } else if (user.isLocked() && this.retryCount++ < 8) { if (Config.DEBUG) { - this.executor.getMain().logMessage("Load user " + uuid + " done. Scheduling store data."); + this.executor.getMain().logMessage("Load user data " + uuid + " fail " + retryCount + '.'); } + } else { this.executor.getUserManager().cacheUser(this.uuid, user); this.executor.getUserManager().addFetched(user); - this.executor.cancelTask(this.taskId); this.executor.getUserManager().saveUser(user, true); if (Config.DEBUG) { - this.executor.getMain().logMessage("Lock user " + uuid + " done."); + this.executor.getMain().logMessage("Load user data " + uuid + " done."); + this.executor.getMain().logMessage("Lock user data " + uuid + " done."); } + this.executor.cancelTask(this.taskId); } }