Skip to content

Commit

Permalink
feat(user): Support UserKey upsert.
Browse files Browse the repository at this point in the history
  • Loading branch information
CarmJos committed Mar 4, 2024
1 parent 6f4793b commit f7e599f
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.artformgames</groupId>
<artifactId>artcore-parent</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>
</parent>
<properties>
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
Expand Down
11 changes: 11 additions & 0 deletions api/src/main/java/com/artformgames/core/ArtCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -56,6 +60,13 @@ public static User getUser(@NotNull Player player) {
return getUser(player).getHandler(handler);
}

public static <T extends UserHandler> @Nullable T getHandler(@NotNull UUID playerUUID,
@NotNull Class<T> handler) {
return Optional.ofNullable(getUserManager().get(playerUUID))
.map(user -> user.getNullableHandler(handler))
.orElse(null);
}

public static BungeeChannelApi getBungeeAPI() {
return plugin().getBungeeAPI();
}
Expand Down
6 changes: 0 additions & 6 deletions api/src/main/java/com/artformgames/core/user/UserKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ public boolean isInstance(KeyType type, Object param) {
};
}

public Map<String, String> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.artformgames</groupId>
<artifactId>artcore-parent</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>
</parent>
<properties>
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ public Map<UUID, BukkitUser> 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;
Expand All @@ -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();
});
}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
</properties>
<groupId>com.artformgames</groupId>
<artifactId>artcore-parent</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>

<name>ArtCore</name>
<description>
Expand Down

0 comments on commit f7e599f

Please sign in to comment.