feat: code
This commit is contained in:
96
common/src/main/java/online/mineroo/common/cache/UserInfoCache.java
vendored
Normal file
96
common/src/main/java/online/mineroo/common/cache/UserInfoCache.java
vendored
Normal file
@@ -0,0 +1,96 @@
|
||||
package online.mineroo.common.cache;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import online.mineroo.common.request.UserRequest.SimpleUserInfoResponse;
|
||||
|
||||
public class UserInfoCache {
|
||||
private ConcurrentHashMap<String, UUID> uuidMap = new ConcurrentHashMap<>();
|
||||
private ConcurrentHashMap<UUID, SimpleUserCacheInfo> simpleUserInfo = new ConcurrentHashMap<>();
|
||||
|
||||
public UserInfoCache() {}
|
||||
|
||||
public UUID insert(UUID uuid, SimpleUserInfoResponse simpleUserInfoResponse) {
|
||||
this.simpleUserInfo.put(uuid, new SimpleUserCacheInfo(simpleUserInfoResponse));
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public UUID insert(String name, SimpleUserInfoResponse simpleUserInfoResponse) {
|
||||
if (this.uuidMap.containsKey(name.toLowerCase())) {
|
||||
UUID uuid = this.uuidMap.get(name.toLowerCase());
|
||||
this.simpleUserInfo.put(uuid, new SimpleUserCacheInfo(simpleUserInfoResponse));
|
||||
return uuid;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public SimpleUserInfoResponse get(UUID uuid) {
|
||||
SimpleUserCacheInfo info = this.simpleUserInfo.get(uuid);
|
||||
|
||||
if (info == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// LocalDateTime expireTime = info.getTime().plusHours(24);
|
||||
// if (LocalDateTime.now().isAfter(expireTime)) {
|
||||
// return null;
|
||||
// }
|
||||
|
||||
return info.getData();
|
||||
}
|
||||
|
||||
public SimpleUserInfoResponse get(String name) {
|
||||
if (name == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
UUID uuid = this.uuidMap.get(name.toLowerCase());
|
||||
if (uuid == null) {
|
||||
return null;
|
||||
}
|
||||
return this.get(uuid);
|
||||
}
|
||||
|
||||
public void remove(UUID uuid) {
|
||||
if (uuid == null) {
|
||||
return;
|
||||
}
|
||||
this.simpleUserInfo.remove(uuid);
|
||||
this.uuidMap.values().removeIf(val -> val.equals(uuid));
|
||||
}
|
||||
|
||||
public void updateNameMapping(String name, UUID uuid) {
|
||||
if (name != null && uuid != null) {
|
||||
this.uuidMap.put(name.toLowerCase(), uuid);
|
||||
}
|
||||
}
|
||||
|
||||
public void cleanup() {
|
||||
// LocalDateTime now = LocalDateTime.now();
|
||||
//
|
||||
// simpleUserInfo.entrySet().removeIf(entry -> {
|
||||
// return now.isAfter(entry.getValue().getTime().plusHours(24));
|
||||
// });
|
||||
//
|
||||
// uuidMap.entrySet().removeIf(entry -> !simpleUserInfo.containsKey(entry.getValue()));
|
||||
}
|
||||
|
||||
public static class SimpleUserCacheInfo {
|
||||
private SimpleUserInfoResponse data;
|
||||
private LocalDateTime time;
|
||||
|
||||
public SimpleUserCacheInfo(SimpleUserInfoResponse data) {
|
||||
this.data = data;
|
||||
this.time = LocalDateTime.now();
|
||||
}
|
||||
|
||||
public SimpleUserInfoResponse getData() {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
public LocalDateTime getTime() {
|
||||
return this.time;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,30 @@ public class UserRequest {
|
||||
this.networkService = networkService;
|
||||
}
|
||||
|
||||
public static class SimpleUserInfoResponse implements Serializable {
|
||||
@SerializedName("username") private String username;
|
||||
@SerializedName("nickname") private String nickname;
|
||||
@SerializedName("avatar") private String avatar;
|
||||
@SerializedName("user_id") private int userId;
|
||||
|
||||
// -- Getters
|
||||
public String getUsername() {
|
||||
return this.username;
|
||||
}
|
||||
|
||||
public String getNickname() {
|
||||
return this.nickname;
|
||||
}
|
||||
|
||||
public String getAvatar() {
|
||||
return this.avatar;
|
||||
}
|
||||
|
||||
public int getUserId() {
|
||||
return this.userId;
|
||||
}
|
||||
}
|
||||
|
||||
public class PlayerBindStatusResponse implements Serializable {
|
||||
@SerializedName("status") private PlayerBindStatusEnum status;
|
||||
|
||||
@@ -244,4 +268,31 @@ public class UserRequest {
|
||||
return createPlayerBindErrorResponse("Network Error: " + e.getMessage());
|
||||
});
|
||||
}
|
||||
|
||||
public CompletableFuture<SimpleUserInfoResponse> getUserInfo(UUID uuid) {
|
||||
String path = "/server/user/info";
|
||||
String simpleUuid = uuid.toString().replace("-", "");
|
||||
|
||||
Map<String, String> params = new HashMap<>();
|
||||
|
||||
params.put("uuid", simpleUuid);
|
||||
|
||||
return networkService.getData(path, params)
|
||||
.thenApply(response -> {
|
||||
Gson gson = new Gson();
|
||||
if (response.getStatusCode() == 200) {
|
||||
try {
|
||||
return gson.fromJson(response.getBody(), SimpleUserInfoResponse.class);
|
||||
} catch (Exception e) {
|
||||
logger.warn("Failed to parse UserInfo JSON: " + e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
})
|
||||
.exceptionally(e -> {
|
||||
logger.error("Mineroo Bind: Network exception", e);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
package online.mineroo.paper;
|
||||
|
||||
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
|
||||
import java.util.HashMap;
|
||||
import java.util.UUID;
|
||||
import online.mineroo.common.HttpNetworkService;
|
||||
import online.mineroo.common.MessageManager;
|
||||
import online.mineroo.common.NetworkServiceInterface;
|
||||
import online.mineroo.common.cache.UserInfoCache;
|
||||
import online.mineroo.common.request.RequestClient;
|
||||
import online.mineroo.paper.commands.MainCommand;
|
||||
import online.mineroo.paper.expansions.MinerooUserExpansion;
|
||||
import online.mineroo.paper.listeners.BindListener;
|
||||
import online.mineroo.paper.listeners.PlayerBindListener;
|
||||
import online.mineroo.paper.structure.PlayerInfo;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
@@ -20,7 +20,7 @@ public class MinerooCore extends JavaPlugin implements Listener {
|
||||
private RequestClient requestClient;
|
||||
private Config config;
|
||||
|
||||
private HashMap<UUID, PlayerInfo> playerCache = new HashMap<>();
|
||||
private UserInfoCache userInfoCache;
|
||||
|
||||
private BindListener bindListener;
|
||||
|
||||
@@ -42,6 +42,13 @@ public class MinerooCore extends JavaPlugin implements Listener {
|
||||
messageManager = new MessageManager();
|
||||
|
||||
getServer().getPluginManager().registerEvents(this, this);
|
||||
|
||||
this.userInfoCache = new UserInfoCache();
|
||||
|
||||
// regsiter Placeholder
|
||||
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
|
||||
new MinerooUserExpansion(this).register();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -90,4 +97,8 @@ public class MinerooCore extends JavaPlugin implements Listener {
|
||||
public RequestClient getRequestClient() {
|
||||
return requestClient;
|
||||
}
|
||||
|
||||
public UserInfoCache getUserInfoCache() {
|
||||
return this.userInfoCache;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package online.mineroo.paper.expansions;
|
||||
|
||||
import java.util.UUID;
|
||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||
import online.mineroo.common.request.UserRequest.SimpleUserInfoResponse;
|
||||
import online.mineroo.paper.MinerooCore;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -22,7 +23,7 @@ public class MinerooUserExpansion extends PlaceholderExpansion {
|
||||
@Override
|
||||
@NotNull
|
||||
public String getIdentifier() {
|
||||
return "example";
|
||||
return "mineroo";
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -44,6 +45,8 @@ public class MinerooUserExpansion extends PlaceholderExpansion {
|
||||
UUID uuid = player.getUniqueId();
|
||||
|
||||
if (params.equals("name")) {
|
||||
SimpleUserInfoResponse info = plugin.getUserInfoCache().get(uuid);
|
||||
return info.getNickname();
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
@@ -51,7 +51,7 @@ public class PlayerBindListener implements Listener {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
Config config = this.plugin.getConfigObject();
|
||||
MessageManager messageManager = this.plugin.getMessageManager();
|
||||
@@ -94,10 +94,23 @@ public class PlayerBindListener implements Listener {
|
||||
}, 40L);
|
||||
}
|
||||
|
||||
private void onBindSuccess(Player player) {
|
||||
UUID uuid = player.getUniqueId();
|
||||
|
||||
restrictedPlayers.remove(uuid);
|
||||
|
||||
plugin.getRequestClient().user().getUserInfo(uuid).thenAccept(response -> {
|
||||
if (response != null) {
|
||||
plugin.getUserInfoCache().insert(uuid, response);
|
||||
plugin.getUserInfoCache().updateNameMapping(player.getName(), uuid);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void handleBindStatus(Player player, PlayerBindStatusEnum status, String message) {
|
||||
if (status == PlayerBindStatusEnum.BOUND) {
|
||||
// Player is bound, release restriction
|
||||
restrictedPlayers.remove(player.getUniqueId());
|
||||
onBindSuccess(player);
|
||||
return;
|
||||
} else if (status == PlayerBindStatusEnum.NOT_BOUND) {
|
||||
Dialog dialog = RegistryAccess.registryAccess()
|
||||
@@ -127,7 +140,7 @@ public class PlayerBindListener implements Listener {
|
||||
}
|
||||
|
||||
if (bindResponse.getStatus() == PlayerBindEnum.BOUND) {
|
||||
restrictedPlayers.remove(uniqueId);
|
||||
onBindSuccess(player);
|
||||
player.sendMessage(this.plugin.getMessageManager().get("info.bind.player.success"));
|
||||
} else if (bindResponse.getStatus() == PlayerBindEnum.PENDING) {
|
||||
player.closeDialog();
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
package online.mineroo.paper.structure;
|
||||
|
||||
public class PlayerInfo {
|
||||
private String minerooUsername;
|
||||
|
||||
public PlayerInfo(String minerooUsername) {
|
||||
this.minerooUsername = minerooUsername;
|
||||
}
|
||||
|
||||
public String getMinerooUsername() {
|
||||
return this.minerooUsername;
|
||||
}
|
||||
}
|
||||
@@ -4,3 +4,9 @@ main: "online.mineroo.paper.MinerooCore"
|
||||
description: Paper Test Plugin
|
||||
api-version: "1.21.10"
|
||||
bootstrapper: "online.mineroo.paper.MinerooCoreBootstrap"
|
||||
|
||||
dependencies:
|
||||
server:
|
||||
PlaceholderAPI:
|
||||
load: BEFORE
|
||||
required: false
|
||||
|
||||
Reference in New Issue
Block a user