1
mirror of https://github.com/CarmJos/EasyPlugin.git synced 2024-09-19 19:25:45 +00:00

feat(user): 提供便捷的用户数据库管理类

This commit is contained in:
Carm Jos 2023-03-12 00:26:52 +08:00
parent 85b7179867
commit fa4a97d60d
17 changed files with 356 additions and 14 deletions

View File

@ -6,7 +6,7 @@
<parent>
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-parent</artifactId>
<version>1.5.3</version>
<version>1.5.4</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<properties>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.3</version>
<version>1.5.4</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.3</version>
<version>1.5.4</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.3</version>
<version>1.5.4</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -6,7 +6,7 @@
<parent>
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-parent</artifactId>
<version>1.5.3</version>
<version>1.5.4</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<properties>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.3</version>
<version>1.5.4</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

64
base/user/pom.xml Normal file
View File

@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-parent</artifactId>
<version>1.5.4</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<properties>
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties>
<artifactId>easyplugin-user</artifactId>
<developers>
<developer>
<id>CarmJos</id>
<name>Carm Jos</name>
<email>carm@carm.cc</email>
<url>https://www.carm.cc</url>
</developer>
</developers>
<licenses>
<license>
<name>The MIT License</name>
<url>https://opensource.org/licenses/MIT</url>
</license>
</licenses>
<issueManagement>
<system>GitHub Issues</system>
<url>https://github.com/CarmJos/EasyPlugin/issues</url>
</issueManagement>
<ciManagement>
<system>GitHub Actions</system>
<url>https://github.com/CarmJos/EasyPlugin/actions/workflows/maven.yml</url>
</ciManagement>
<dependencies>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>easyplugin-main</artifactId>
<version>${project.parent.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,32 @@
package cc.carm.lib.easyplugin.user;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
public abstract class UserData<K> {
protected final @NotNull K key;
protected UserData(@NotNull K key) {
this.key = key;
}
public @NotNull K getKey() {
return key;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UserData<?> userData = (UserData<?>) o;
return key.equals(userData.key);
}
@Override
public int hashCode() {
return Objects.hash(key);
}
}

View File

@ -0,0 +1,245 @@
package cc.carm.lib.easyplugin.user;
import cc.carm.lib.easyplugin.EasyPlugin;
import com.google.common.collect.ImmutableSet;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.logging.Logger;
import java.util.stream.Collectors;
public abstract class UserDataManager<K, U extends UserData<K>> {
protected final @NotNull EasyPlugin plugin;
protected final @NotNull ExecutorService executor;
protected final @NotNull Map<K, U> dataCache;
public UserDataManager(@NotNull EasyPlugin plugin) {
this(plugin, Executors.newCachedThreadPool((r) -> {
Thread t = new Thread(r);
t.setDaemon(true);
t.setName(plugin.getName() + "-UserManager");
return t;
}), new ConcurrentHashMap<>());
}
public UserDataManager(@NotNull EasyPlugin plugin, @NotNull ExecutorService executor) {
this(plugin, executor, new ConcurrentHashMap<>());
}
public UserDataManager(@NotNull EasyPlugin plugin, @NotNull ExecutorService executor, @NotNull Map<K, U> cacheMap) {
this.plugin = plugin;
this.executor = executor;
this.dataCache = cacheMap;
}
public void shutdown() {
this.executor.shutdown();
}
protected @NotNull EasyPlugin getPlugin() {
return plugin;
}
protected @NotNull Logger getLogger() {
return getPlugin().getLogger();
}
public String serializeKey(@NotNull K key) {
return key.toString();
}
public abstract @NotNull U empty(@NotNull K key);
protected abstract @Nullable U loadData(@NotNull K key) throws Exception;
protected abstract void saveData(@NotNull U data) throws Exception;
public @NotNull CompletableFuture<U> load(@NotNull K key) {
return load(key, false);
}
public @NotNull CompletableFuture<U> load(@NotNull K key, boolean cache) {
return load(key, () -> cache);
}
public @NotNull Map<K, U> getDataCache() {
return dataCache;
}
@Unmodifiable
public @NotNull Set<U> list() {
return ImmutableSet.copyOf(getDataCache().values());
}
public @NotNull U get(@NotNull K key) {
return Optional.ofNullable(getNullable(key)).orElseThrow(() -> new NullPointerException("User " + key + " not found."));
}
public @Nullable U getNullable(@NotNull K key) {
return getDataCache().get(key);
}
public @NotNull Optional<@Nullable U> getOptional(@NotNull K key) {
return Optional.ofNullable(getNullable(key));
}
public @NotNull CompletableFuture<U> load(@NotNull K key, @NotNull Supplier<Boolean> cacheCondition) {
return CompletableFuture.supplyAsync(() -> {
String identifier = serializeKey(key);
try {
long s1 = System.currentTimeMillis();
getPlugin().debug("开始加载用户 " + identifier + " 的数据...");
U data = loadData(key);
if (data == null) {
getPlugin().debug("数据库内不存在用户 " + identifier + " 的数据,视作新档案。");
return empty(key);
} else {
getPlugin().debug("加载用户 " + identifier + " 的数据完成,耗时 " + (System.currentTimeMillis() - s1) + " ms.");
return data;
}
} catch (Exception ex) {
getPlugin().error("加载用户 " + serializeKey(key) + " 数据失败,请检查相关配置!");
ex.printStackTrace();
return empty(key);
}
}, executor).thenApply((data) -> {
if (cacheCondition.get()) dataCache.put(key, data);
return data;
});
}
public @NotNull CompletableFuture<Boolean> save(@NotNull U user) {
return CompletableFuture.supplyAsync(() -> {
String identifier = serializeKey(user.getKey());
try {
long s1 = System.currentTimeMillis();
getPlugin().debug("开始保存用户 " + identifier + " 的数据...");
saveData(user);
getPlugin().debug("保存用户 " + identifier + " 的数据完成,耗时 " + (System.currentTimeMillis() - s1) + " ms.");
return true;
} catch (Exception ex) {
getPlugin().error("保存用户 " + identifier + " 数据失败,请检查相关配置!");
ex.printStackTrace();
return false;
}
}, executor);
}
public @NotNull CompletableFuture<Boolean> unload(@NotNull K key) {
return unload(key, true);
}
public @NotNull CompletableFuture<Boolean> unload(@NotNull K key, boolean save) {
U data = getNullable(key);
if (data == null) return CompletableFuture.completedFuture(false);
if (save) {
return save(data).thenApply(result -> {
this.dataCache.remove(key);
return result;
});
} else {
this.dataCache.remove(key);
return CompletableFuture.completedFuture(true);
}
}
public @NotNull CompletableFuture<Boolean> modify(@NotNull K key, @NotNull Consumer<U> consumer) {
U cached = getNullable(key);
if (cached != null) {
return CompletableFuture.supplyAsync(() -> {
consumer.accept(cached);
return true;
}, executor);
} else {
return load(key, false).thenApply((data) -> {
consumer.accept(data);
return data;
}).thenCompose(this::save);
}
}
public <V> @NotNull CompletableFuture<V> peek(@NotNull K key, @NotNull Function<U, V> function) {
U cached = getNullable(key);
if (cached != null) {
return CompletableFuture.supplyAsync(() -> function.apply(cached), executor);
} else {
return load(key, false).thenApply(function);
}
}
public @NotNull CompletableFuture<Map<K, U>> loadOnline(@NotNull Function<Player, ? extends K> function) {
return loadGroup(Bukkit.getOnlinePlayers(), function, OfflinePlayer::isOnline);
}
public <T> @NotNull CompletableFuture<Map<K, U>> loadGroup(@NotNull Collection<? extends T> users,
@NotNull Function<? super T, ? extends K> function,
@NotNull Predicate<T> cacheCondition) {
CompletableFuture<Map<K, U>> task = CompletableFuture.completedFuture(new ConcurrentHashMap<>());
if (users.isEmpty()) return task;
Map<K, T> usersMap = users.stream().collect(Collectors.toMap(function, Function.identity()));
for (Map.Entry<K, T> entry : usersMap.entrySet()) {
K key = entry.getKey();
T user = entry.getValue();
task = task.thenCombine(
load(key, () -> cacheCondition.test(user)),
(map, result) -> {
map.put(key, result);
return map;
}
);
}
return task.thenApply(Collections::unmodifiableMap);
}
public @NotNull CompletableFuture<Map<K, U>> loadGroup(@NotNull Collection<K> allKeys,
@NotNull Predicate<K> cacheCondition) {
return loadGroup(allKeys, Function.identity(), cacheCondition);
}
public @NotNull CompletableFuture<Map<K, U>> loadGroup(@NotNull Collection<K> allKeys) {
return loadGroup(allKeys, (v) -> false);
}
public @NotNull CompletableFuture<Integer> saveAll() {
CompletableFuture<Integer> future = CompletableFuture.completedFuture(0);
if (getDataCache().isEmpty()) return future;
for (U value : getDataCache().values()) {
future = future.thenCombine(save(value), (before, result) -> before + (result ? 1 : 0));
}
return future;
}
public @NotNull CompletableFuture<Integer> unloadAll() {
return saveAll().thenApply(result -> {
getDataCache().clear();
return result;
});
}
}

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.3</version>
<version>1.5.4</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.3</version>
<version>1.5.4</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.3</version>
<version>1.5.4</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.3</version>
<version>1.5.4</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.3</version>
<version>1.5.4</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.3</version>
<version>1.5.4</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.5.3</version>
<version>1.5.4</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -15,7 +15,7 @@
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-parent</artifactId>
<packaging>pom</packaging>
<version>1.5.3</version>
<version>1.5.4</version>
<modules>
<module>base/color</module>
<module>base/utils</module>
@ -25,6 +25,7 @@
<module>base/gui</module>
<module>base/command</module>
<module>base/storage</module>
<module>base/user</module>
<module>extension/papi</module>
<module>extension/vault</module>