Skip to content

Commit

Permalink
Closed #26
Browse files Browse the repository at this point in the history
  • Loading branch information
caoli5288 committed Jan 25, 2016
1 parent 005e7b3 commit 80b4107
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 33 deletions.
14 changes: 8 additions & 6 deletions src/main/java/com/mengcraft/playersql/EventExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
});
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/mengcraft/playersql/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ public boolean isLocked() {
}

public void setLocked(boolean locked) {
synchronized (this) {
this.locked = locked;
}
this.locked = locked;
}

public Timestamp getLastUpdate() {
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/mengcraft/playersql/UserManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}
}

Expand Down Expand Up @@ -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;
}
Expand All @@ -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));
Expand Down Expand Up @@ -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();
}
Expand Down
16 changes: 5 additions & 11 deletions src/main/java/com/mengcraft/playersql/lib/JSONUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
19 changes: 9 additions & 10 deletions src/main/java/com/mengcraft/playersql/task/FetchUserTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 80b4107

Please sign in to comment.