Added functions to sync user points, remove users, and remove user points
This commit is contained in:
parent
0af3bcb843
commit
7e5123d7fe
|
|
@ -5,6 +5,7 @@ import com.alttd.database.Queries;
|
||||||
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
|
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
|
||||||
import org.bukkit.scheduler.BukkitRunnable;
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
|
@ -34,16 +35,49 @@ public class EconUser {
|
||||||
pointsMap.put(villagerType, points);
|
pointsMap.put(villagerType, points);
|
||||||
else
|
else
|
||||||
pointsMap.put(villagerType, Objects.requireNonNullElse(pointsMap.get(villagerType), 0) + points);
|
pointsMap.put(villagerType, Objects.requireNonNullElse(pointsMap.get(villagerType), 0) + points);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void syncPoints() {
|
||||||
new BukkitRunnable() {
|
new BukkitRunnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
Queries.updateUserPoints(uuid, villagerType, pointsMap.get(villagerType));
|
Queries.updateUserPoints(uuid, pointsMap);
|
||||||
}
|
}
|
||||||
}.runTaskAsynchronously(VillagerUI.getInstance());
|
}.runTaskAsynchronously(VillagerUI.getInstance());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void removePoints(int remove) {
|
||||||
|
pointsMap.forEach((villagerType, points) -> {
|
||||||
|
if (points == 0)
|
||||||
|
return;
|
||||||
|
if (points > 0)
|
||||||
|
if (points < remove)
|
||||||
|
points = 0;
|
||||||
|
else
|
||||||
|
points -= remove;
|
||||||
|
else
|
||||||
|
if (-points < remove)
|
||||||
|
points = 0;
|
||||||
|
else
|
||||||
|
points += remove;
|
||||||
|
pointsMap.put(villagerType, points);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public static EconUser getUser(UUID uuid) {
|
public static EconUser getUser(UUID uuid) {
|
||||||
EconUser user = users.get(uuid);
|
EconUser user = users.get(uuid);
|
||||||
return (user == null ? Queries.getEconUser(uuid) : user);
|
if (user == null) {
|
||||||
|
user = Queries.getEconUser(uuid);
|
||||||
|
EconUser.users.put(uuid, user);
|
||||||
|
}
|
||||||
|
return (user);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void removeUser(UUID uuid) {
|
||||||
|
users.remove(uuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void syncAllPoints() {
|
||||||
|
Collections.unmodifiableMap(users).values().forEach(EconUser::syncPoints);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user