From f7e599fe992d7b5bffed055bb6bbb2eb18f0032c Mon Sep 17 00:00:00 2001 From: Carm Date: Mon, 4 Mar 2024 14:13:17 +0800 Subject: [PATCH] feat(user): Support UserKey upsert. --- api/pom.xml | 2 +- .../main/java/com/artformgames/core/ArtCore.java | 11 +++++++++++ .../java/com/artformgames/core/user/UserKey.java | 6 ------ .../core/user/manager/UserKeyManager.java | 1 + plugin/pom.xml | 2 +- .../core/user/BukkitUserManager.java | 16 +++++++++++----- pom.xml | 2 +- 7 files changed, 26 insertions(+), 14 deletions(-) diff --git a/api/pom.xml b/api/pom.xml index b84a3bf..9f01c02 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -6,7 +6,7 @@ com.artformgames artcore-parent - 1.1.0 + 1.1.1 ${project.jdk.version} diff --git a/api/src/main/java/com/artformgames/core/ArtCore.java b/api/src/main/java/com/artformgames/core/ArtCore.java index 5cac826..38ba5c0 100644 --- a/api/src/main/java/com/artformgames/core/ArtCore.java +++ b/api/src/main/java/com/artformgames/core/ArtCore.java @@ -15,6 +15,10 @@ import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Optional; +import java.util.UUID; public class ArtCore { @@ -56,6 +60,13 @@ public static User getUser(@NotNull Player player) { return getUser(player).getHandler(handler); } + public static @Nullable T getHandler(@NotNull UUID playerUUID, + @NotNull Class handler) { + return Optional.ofNullable(getUserManager().get(playerUUID)) + .map(user -> user.getNullableHandler(handler)) + .orElse(null); + } + public static BungeeChannelApi getBungeeAPI() { return plugin().getBungeeAPI(); } diff --git a/api/src/main/java/com/artformgames/core/user/UserKey.java b/api/src/main/java/com/artformgames/core/user/UserKey.java index 1e379f5..e9e157d 100644 --- a/api/src/main/java/com/artformgames/core/user/UserKey.java +++ b/api/src/main/java/com/artformgames/core/user/UserKey.java @@ -32,12 +32,6 @@ public boolean isInstance(KeyType type, Object param) { }; } - public Map toMap() { - String jsonValue = GSON.toJson(this); - return Arrays.stream(KeyType.values()) - .collect(Collectors.toMap(this::getValue, value -> jsonValue, (a, b) -> b)); - } - @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/api/src/main/java/com/artformgames/core/user/manager/UserKeyManager.java b/api/src/main/java/com/artformgames/core/user/manager/UserKeyManager.java index ed780cd..37e186a 100644 --- a/api/src/main/java/com/artformgames/core/user/manager/UserKeyManager.java +++ b/api/src/main/java/com/artformgames/core/user/manager/UserKeyManager.java @@ -9,6 +9,7 @@ public interface UserKeyManager { + UserKey upsertKey(@NotNull UUID userUUID, @NotNull String username) throws Exception; @Nullable UserKey getKey(UserKey.KeyType type, Object param); diff --git a/plugin/pom.xml b/plugin/pom.xml index 073710d..55a045a 100644 --- a/plugin/pom.xml +++ b/plugin/pom.xml @@ -6,7 +6,7 @@ com.artformgames artcore-parent - 1.1.0 + 1.1.1 ${project.jdk.version} diff --git a/plugin/src/main/java/com/artformgames/core/user/BukkitUserManager.java b/plugin/src/main/java/com/artformgames/core/user/BukkitUserManager.java index 3a2e2b2..2e25d71 100644 --- a/plugin/src/main/java/com/artformgames/core/user/BukkitUserManager.java +++ b/plugin/src/main/java/com/artformgames/core/user/BukkitUserManager.java @@ -131,6 +131,12 @@ public Map getUsersMap() { return users; } + public UserKey upsertKey(@NotNull UUID uuid, @NotNull String username) throws Exception { + Long id = upsertID(uuid, username); + if (id == null || id <= 0) throw new Exception("Unable to get " + username + "'s uid."); + return new UserKey(id, uuid, username); + } + public Long upsertID(@NotNull UUID uuid, @NotNull String username) throws Exception { long id = -1; String cachedName = null; @@ -152,7 +158,7 @@ public Long upsertID(@NotNull UUID uuid, @NotNull String username) throws Except .addColumnValue(UserKey.KeyType.NAME.getColumnName(), username) .addCondition("id", id) .build().execute((e, a) -> { - getLogger().severe("更新用户 " + username + " 的用户名失败!"); + getLogger().severe("Failed to update " + username + "'s username!"); e.printStackTrace(); }); } @@ -163,16 +169,16 @@ public Long upsertID(@NotNull UUID uuid, @NotNull String username) throws Except .setColumnNames(UserKey.KeyType.UUID.getColumnName(), UserKey.KeyType.NAME.getColumnName()) .setParams(uuid, username).returnGeneratedKey(Long.class).execute(); } catch (SQLException e) { - getLogger().severe("创建新用户 " + username + " 失败!"); + getLogger().severe("Failed to create " + username + "'s account!"); return null; } } } public BukkitUser createUser(@NotNull UUID userUUID, @NotNull String username) throws Exception { - Long id = upsertID(userUUID, username); - if (id == null || id <= 0) throw new Exception("无法获取用户 " + username + " 的UID!"); - return new BukkitUser(this, new UserKey(id, userUUID, username)); + UserKey key = upsertKey(userUUID, username); + if (key == null) throw new Exception("Unable to fetch " + username + "'s key."); + return new BukkitUser(this, key); } @NotNull diff --git a/pom.xml b/pom.xml index f65f1e4..751f7d5 100644 --- a/pom.xml +++ b/pom.xml @@ -30,7 +30,7 @@ com.artformgames artcore-parent - 1.1.0 + 1.1.1 ArtCore