Skip to content

Commit ea3c887

Browse files
committed
Some fixes
1 parent 26b5efc commit ea3c887

File tree

4 files changed

+71
-44
lines changed

4 files changed

+71
-44
lines changed

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

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
*/
2222
public class EventExecutor implements Listener {
2323

24-
private final Map<UUID, BukkitTask> taskMap = new ConcurrentHashMap<>();
25-
2624
private UserManager userManager;
2725
private PluginMain main;
2826

@@ -43,10 +41,12 @@ public void handle(PlayerJoinEvent event) {
4341

4442
@EventHandler
4543
public void handle(PlayerQuitEvent event) {
46-
if (!this.userManager.isUserLocked(event.getPlayer().getUniqueId())) {
47-
User user = this.userManager.getUser(event.getPlayer().getUniqueId());
48-
this.taskMap.remove(event.getPlayer().getUniqueId()).cancel();
44+
UUID uuid = event.getPlayer().getUniqueId();
45+
if (!this.userManager.isUserLocked(uuid)) {
46+
User user = this.userManager.getUser(uuid);
47+
this.userManager.cancelTask(uuid);
4948
this.userManager.syncUser(user);
49+
this.userManager.cacheUser(uuid);
5050
this.main.runTaskAsynchronously(() -> {
5151
this.userManager.saveUser(user, false);
5252
});
@@ -106,10 +106,6 @@ public void handle(PlayerCommandPreprocessEvent event) {
106106
}
107107
}
108108

109-
public void cancelTask(int i) {
110-
this.main.getServer().getScheduler().cancelTask(i);
111-
}
112-
113109
public void setUserManager(UserManager userManager) {
114110
this.userManager = userManager;
115111
}
@@ -126,24 +122,8 @@ public PluginMain getMain() {
126122
return this.main;
127123
}
128124

129-
public void createTask(UUID uuid) {
130-
if (Config.DEBUG) {
131-
this.main.logMessage("Scheduling daily save task for user " + uuid + '.');
132-
}
133-
DailySaveTask saveTask = new DailySaveTask();
134-
BukkitTask task = this.main.runTaskTimer(saveTask, 6000);
135-
synchronized (saveTask) {
136-
saveTask.setUuid(uuid);
137-
saveTask.setExecutor(this);
138-
saveTask.setTaskId(task.getTaskId());
139-
}
140-
BukkitTask old = this.taskMap.put(uuid, task);
141-
if (old != null) {
142-
if (Config.DEBUG) {
143-
this.main.logException(new PluginException("Already schedule task for user " + uuid + '!'));
144-
}
145-
old.cancel();
146-
}
125+
public void cancelTask(int taskId) {
126+
userManager.cancelTask(taskId);
147127
}
148128

149129
}

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

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
import com.mengcraft.playersql.lib.ExpUtil;
44
import com.mengcraft.playersql.lib.ItemUtil;
5+
import com.mengcraft.playersql.task.DailySaveTask;
56
import org.bukkit.Material;
67
import org.bukkit.entity.Player;
78
import org.bukkit.inventory.ItemStack;
89
import org.bukkit.potion.PotionEffect;
910
import org.bukkit.potion.PotionEffectType;
11+
import org.bukkit.scheduler.BukkitTask;
1012
import org.json.simple.JSONArray;
1113
import org.json.simple.JSONValue;
1214

@@ -22,6 +24,7 @@ public final class UserManager {
2224
public static final UserManager INSTANCE = new UserManager();
2325
public static final ItemStack AIR = new ItemStack(Material.AIR);
2426

27+
private final Map<UUID, BukkitTask> taskMap;
2528
private final List<UUID> locked;
2629
private final Map<UUID, User> userMap;
2730
private final Queue<User> fetched;
@@ -31,6 +34,7 @@ public final class UserManager {
3134
private ExpUtil expUtil;
3235

3336
private UserManager() {
37+
this.taskMap = new ConcurrentHashMap<>();
3438
this.locked = new ArrayList<>();
3539
this.userMap = new ConcurrentHashMap<>();
3640
this.fetched = new ConcurrentLinkedQueue<>();
@@ -67,7 +71,11 @@ public void cacheUser(UUID uuid) {
6771
}
6872

6973
public void cacheUser(UUID uuid, User user) {
70-
this.userMap.put(uuid, user);
74+
if (user == null) {
75+
userMap.remove(uuid);
76+
} else {
77+
userMap.put(uuid, user);
78+
}
7179
}
7280

7381
public void saveUser(UUID uuid, boolean lock) {
@@ -96,7 +104,7 @@ public void saveUser(User user, boolean lock) {
96104
}
97105

98106
public void syncUser(User user) {
99-
Player player = this.main.getPlayer(user.getUuid());
107+
Player player = main.getPlayer(user.getUuid());
100108
synchronized (user) {
101109
if (Config.SYN_HEALTH) {
102110
user.setHealth(player.getHealth());
@@ -131,6 +139,9 @@ public void lockUser(UUID uuid) {
131139
}
132140

133141
public void unlockUser(UUID uuid, boolean b) {
142+
if (Config.DEBUG) {
143+
main.logMessage("Unlock user task on thread " + Thread.currentThread().getName() + '.');
144+
}
134145
if (b) {
135146
this.main.runTask(() -> unlockUser(uuid, false));
136147
} else {
@@ -188,7 +199,8 @@ private void pend(User polled, Player player) {
188199
player.getEnderChest().setContents(toStack(polled.getChest()));
189200
}
190201
}
191-
unlockUser(player.getUniqueId(), true);
202+
createTask(player.getUniqueId());
203+
unlockUser(player.getUniqueId(), false);
192204
}
193205

194206
@SuppressWarnings("unchecked")
@@ -242,6 +254,39 @@ private String toString(Collection<PotionEffect> effects) {
242254
return array.toString();
243255
}
244256

257+
public void cancelTask(int i) {
258+
this.main.getServer().getScheduler().cancelTask(i);
259+
}
260+
261+
public void cancelTask(UUID uuid) {
262+
BukkitTask task = taskMap.remove(uuid);
263+
if (task != null) {
264+
task.cancel();
265+
} else if (Config.DEBUG) {
266+
this.main.logMessage("No task can be canceled for " + uuid + '!');
267+
}
268+
}
269+
270+
public void createTask(UUID uuid) {
271+
if (Config.DEBUG) {
272+
this.main.logMessage("Scheduling daily save task for user " + uuid + '.');
273+
}
274+
DailySaveTask saveTask = new DailySaveTask();
275+
BukkitTask task = this.main.runTaskTimer(saveTask, 6000);
276+
synchronized (saveTask) {
277+
saveTask.setUuid(uuid);
278+
saveTask.setUserManager(this);
279+
saveTask.setTaskId(task.getTaskId());
280+
}
281+
BukkitTask old = this.taskMap.put(uuid, task);
282+
if (old != null) {
283+
if (Config.DEBUG) {
284+
this.main.logMessage("Already schedule task for user " + uuid + '!');
285+
}
286+
old.cancel();
287+
}
288+
}
289+
245290
public void setItemUtil(ItemUtil itemUtil) {
246291
this.itemUtil = itemUtil;
247292
}
@@ -254,4 +299,8 @@ public void setMain(PluginMain main) {
254299
this.main = main;
255300
}
256301

302+
public PluginMain getMain() {
303+
return main;
304+
}
305+
257306
}
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.mengcraft.playersql.task;
22

33
import com.mengcraft.playersql.Config;
4-
import com.mengcraft.playersql.EventExecutor;
54
import com.mengcraft.playersql.PluginException;
65
import com.mengcraft.playersql.User;
6+
import com.mengcraft.playersql.UserManager;
77

88
import java.util.UUID;
99

@@ -12,40 +12,40 @@
1212
*/
1313
public class DailySaveTask implements Runnable {
1414

15-
private EventExecutor executor;
15+
private UserManager userManager;
1616
private UUID uuid;
1717

1818
private int taskId;
1919
private int saveCount;
2020

2121
@Override
2222
public synchronized void run() {
23-
User user = this.executor.getUserManager().getUser(this.uuid);
23+
User user = userManager.getUser(this.uuid);
2424
if (user == null) {
2525
if (Config.DEBUG) {
26-
this.executor.getMain().logException(new PluginException("User " + this.uuid + " not cached!"));
26+
userManager.getMain().logException(new PluginException("User " + this.uuid + " not cached!"));
2727
}
28-
this.executor.cancelTask(this.taskId);
28+
userManager.cancelTask(this.taskId);
2929
} else {
3030
this.saveCount++;
3131
if (Config.DEBUG) {
32-
this.executor.getMain().logMessage("Save user " + this.uuid + " count " + this.saveCount + '.');
32+
userManager.getMain().logMessage("Save user " + this.uuid + " count " + this.saveCount + '.');
3333
}
34-
this.executor.getUserManager().syncUser(user);
35-
this.executor.getMain().runTaskAsynchronously(() -> this.executor.getUserManager().saveUser(user, true));
34+
userManager.syncUser(user);
35+
userManager.getMain().runTaskAsynchronously(() -> userManager.saveUser(user, true));
3636
}
3737
}
3838

3939
public void setTaskId(int taskId) {
4040
this.taskId = taskId;
4141
}
4242

43-
public void setExecutor(EventExecutor executor) {
44-
this.executor = executor;
45-
}
46-
4743
public void setUuid(UUID uuid) {
4844
this.uuid = uuid;
4945
}
5046

47+
public void setUserManager(UserManager userManager) {
48+
this.userManager = userManager;
49+
}
50+
5151
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public synchronized void run() {
2828
}
2929
this.executor.getUserManager().cacheUser(this.uuid);
3030
this.executor.cancelTask(this.taskId);
31-
this.executor.createTask(this.uuid);
3231
this.executor.getUserManager().unlockUser(this.uuid, true);
3332
} else if (user.isLocked() && this.retryCount++ < 8) {
3433
if (Config.DEBUG) {
@@ -41,7 +40,6 @@ public synchronized void run() {
4140
this.executor.getUserManager().cacheUser(this.uuid, user);
4241
this.executor.getUserManager().addFetched(user);
4342
this.executor.cancelTask(this.taskId);
44-
this.executor.createTask(this.uuid);
4543
this.executor.getUserManager().saveUser(user, true);
4644
if (Config.DEBUG) {
4745
this.executor.getMain().logMessage("Lock user " + this.uuid + " on database done.");

0 commit comments

Comments
 (0)