PlayerData#set, PlayerData#setBoolean and PlayerData#setDate now return a CompletableFuture for consecutive writes to lp DB

This commit is contained in:
dimaguy 2024-10-13 17:18:17 +01:00
parent 99099a345a
commit a327b7a867

View file

@ -13,6 +13,7 @@ import org.jetbrains.annotations.Nullable;
import java.time.Instant; import java.time.Instant;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.*; import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class PlayerData { public class PlayerData {
@ -76,9 +77,10 @@ public class PlayerData {
nodes = rawNodes.stream().collect(Collectors.toMap(MetaNode::getMetaKey, MetaNode::getMetaValue)); nodes = rawNodes.stream().collect(Collectors.toMap(MetaNode::getMetaKey, MetaNode::getMetaValue));
} }
public void set(String key, @Nullable String value) { @SuppressWarnings("UnusedReturnValue")
public CompletableFuture<Void> set(String key, @Nullable String value) {
var node = meta(key, value).build(); var node = meta(key, value).build();
luckPerms().getUserManager().modifyUser(uuid, user -> { return luckPerms().getUserManager().modifyUser(uuid, user -> {
user.data().clear(NodeType.META.predicate(mn -> mn.getMetaKey().equals(key))); user.data().clear(NodeType.META.predicate(mn -> mn.getMetaKey().equals(key)));
user.data().add(node); user.data().add(node);
refreshNodes(); refreshNodes();
@ -94,9 +96,9 @@ public class PlayerData {
public @Nullable MetaNode getNode(String key) { public @Nullable MetaNode getNode(String key) {
return rawNodes.stream().filter(rawNode -> rawNode.getMetaKey().equals(key)).findFirst().orElse(null); return rawNodes.stream().filter(rawNode -> rawNode.getMetaKey().equals(key)).findFirst().orElse(null);
} }
@SuppressWarnings("UnusedReturnValue")
public void setBoolean(String key, boolean value) { public CompletableFuture<Void> setBoolean(String key, boolean value) {
set(key, Boolean.toString(value)); return set(key, Boolean.toString(value));
} }
public boolean getBoolean(String key) { public boolean getBoolean(String key) {
@ -110,10 +112,10 @@ public class PlayerData {
return defaultValue; return defaultValue;
return Boolean.parseBoolean(nodes.get(nodePrefix + "." + key)); return Boolean.parseBoolean(nodes.get(nodePrefix + "." + key));
} }
@SuppressWarnings("UnusedReturnValue")
public void setDate(String key, Date date) { public CompletableFuture<Void> setDate(String key, Date date) {
var dateString = DateTimeFormatter.ISO_INSTANT.format(date.toInstant()); var dateString = DateTimeFormatter.ISO_INSTANT.format(date.toInstant());
set(key, dateString); return set(key, dateString);
} }
public Date getDate(String key) { public Date getDate(String key) {
@ -123,9 +125,9 @@ public class PlayerData {
var ta = DateTimeFormatter.ISO_INSTANT.parse(dateString); var ta = DateTimeFormatter.ISO_INSTANT.parse(dateString);
return Date.from(Instant.from(ta)); return Date.from(Instant.from(ta));
} }
@SuppressWarnings("UnusedReturnValue")
public void delete(String key) { public CompletableFuture<Void> delete(String key) {
luckPerms().getUserManager().modifyUser(uuid, user -> { return luckPerms().getUserManager().modifyUser(uuid, user -> {
user.data().clear(NodeType.META.predicate(mn -> mn.getMetaKey().equals(nodePrefix + "." + key))); user.data().clear(NodeType.META.predicate(mn -> mn.getMetaKey().equals(nodePrefix + "." + key)));
}); });
} }