Changed usermap to be synchronized
This commit is contained in:
parent
ed46f6b790
commit
5ea05c7869
|
|
@ -18,8 +18,8 @@ import java.util.stream.Collectors;
|
||||||
public class EconUser {
|
public class EconUser {
|
||||||
|
|
||||||
private static Object2ObjectOpenHashMap<UUID, EconUser> users = new Object2ObjectOpenHashMap<>();
|
private static Object2ObjectOpenHashMap<UUID, EconUser> users = new Object2ObjectOpenHashMap<>();
|
||||||
private final static Queue<EconUser> addQueue = new LinkedBlockingQueue<>();
|
// private final static Queue<EconUser> addQueue = new LinkedBlockingQueue<>();
|
||||||
private final static Queue<EconUser> removeQueue = new LinkedBlockingQueue<>();
|
// private final static Queue<EconUser> removeQueue = new LinkedBlockingQueue<>();
|
||||||
|
|
||||||
private final UUID uuid;
|
private final UUID uuid;
|
||||||
private final Object2ObjectOpenHashMap<String, Integer> pointsMap;
|
private final Object2ObjectOpenHashMap<String, Integer> pointsMap;
|
||||||
|
|
@ -27,12 +27,27 @@ public class EconUser {
|
||||||
public EconUser(UUID uuid, Object2ObjectOpenHashMap<String, Integer> points) {
|
public EconUser(UUID uuid, Object2ObjectOpenHashMap<String, Integer> points) {
|
||||||
this.uuid = uuid;
|
this.uuid = uuid;
|
||||||
this.pointsMap = points;
|
this.pointsMap = points;
|
||||||
addQueue.offer(this);
|
addUser(uuid, this);
|
||||||
updateUsers();
|
|
||||||
if (Config.DEBUG)
|
if (Config.DEBUG)
|
||||||
Logger.info("Created EconUser for: %", uuid.toString());
|
Logger.info("Created EconUser for: %", uuid.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private synchronized static void addUser(UUID uuid, EconUser econUser) {
|
||||||
|
users.put(uuid, econUser);
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized static EconUser getUser(UUID uuid) {
|
||||||
|
return users.getOrDefault(uuid, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private synchronized static boolean containsUser(UUID uuid) {
|
||||||
|
return users.containsKey(uuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
private synchronized static void removeUserFromMap(UUID uuid) {
|
||||||
|
users.remove(uuid);
|
||||||
|
}
|
||||||
|
|
||||||
public static void removeQueriedUser(UUID uuid) {
|
public static void removeQueriedUser(UUID uuid) {
|
||||||
queriedUsers.remove(uuid);
|
queriedUsers.remove(uuid);
|
||||||
}
|
}
|
||||||
|
|
@ -112,14 +127,9 @@ public class EconUser {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
//Can return null
|
|
||||||
public static EconUser getUser(UUID uuid) {
|
|
||||||
return (users.get(uuid));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static HashSet<UUID> queriedUsers = new HashSet<>();
|
private static HashSet<UUID> queriedUsers = new HashSet<>();
|
||||||
public static void tryLoadUser(UUID uuid) {
|
public static void tryLoadUser(UUID uuid) {
|
||||||
if (queriedUsers.contains(uuid) || users.containsKey(uuid))
|
if (queriedUsers.contains(uuid) || containsUser(uuid))
|
||||||
return;
|
return;
|
||||||
queriedUsers.add(uuid);
|
queriedUsers.add(uuid);
|
||||||
new BukkitRunnable() {
|
new BukkitRunnable() {
|
||||||
|
|
@ -139,34 +149,14 @@ public class EconUser {
|
||||||
queriedUsers.remove(uuid);
|
queriedUsers.remove(uuid);
|
||||||
if (Config.DEBUG)
|
if (Config.DEBUG)
|
||||||
Logger.info("Unloading EconUser %", uuid.toString());
|
Logger.info("Unloading EconUser %", uuid.toString());
|
||||||
EconUser user = users.get(uuid);
|
// EconUser user = getUser(uuid);
|
||||||
if (user == null)
|
// if (user == null)
|
||||||
return;
|
// return;
|
||||||
removeQueue.offer(user);
|
removeUserFromMap(uuid);
|
||||||
updateUsers();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void updateUsers() {
|
|
||||||
if (addQueue.isEmpty() && removeQueue.isEmpty())
|
|
||||||
return;
|
|
||||||
Object2ObjectOpenHashMap<UUID, EconUser> tmp = new Object2ObjectOpenHashMap<>(users);
|
|
||||||
while (true) {
|
|
||||||
EconUser user = addQueue.poll();
|
|
||||||
if (user == null)
|
|
||||||
break;
|
|
||||||
tmp.put(user.getUuid(), user);
|
|
||||||
}
|
|
||||||
while (true) {
|
|
||||||
EconUser user = addQueue.poll();
|
|
||||||
if (user == null)
|
|
||||||
break;
|
|
||||||
tmp.remove(user.getUuid());
|
|
||||||
}
|
|
||||||
users = tmp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Unmodifiable
|
@Unmodifiable
|
||||||
public static List<EconUser> getEconUsers() {
|
public synchronized static List<EconUser> getEconUsers() {
|
||||||
return Collections.unmodifiableList(users.values().stream().toList());
|
return Collections.unmodifiableList(users.values().stream().toList());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user