This commit is contained in:
YuTian 2024-07-21 23:45:03 +08:00
parent ffe3514de8
commit 6894e9e776
7 changed files with 5258 additions and 5054 deletions

12
pom.xml
View File

@ -88,18 +88,18 @@
<artifactId>HikariCP</artifactId> <artifactId>HikariCP</artifactId>
<version>4.0.3</version> <version>4.0.3</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.3.230</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc --> <!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
<dependency> <dependency>
<groupId>org.xerial</groupId> <groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId> <artifactId>sqlite-jdbc</artifactId>
<version>3.46.0.0</version> <version>3.46.0.0</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>5.1.3</version>
</dependency>
</dependencies> </dependencies>

View File

@ -0,0 +1,9 @@
package com.io.yutian.aulib.redis;
public class RedisCacheHelper {
public static void updateCache(String key, String value) {
}
}

View File

@ -0,0 +1,89 @@
package com.io.yutian.aulib.redis;
import com.io.yutian.aulib.util.FileUtil;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.io.File;
import java.time.Duration;
import java.util.Set;
import java.util.concurrent.FutureTask;
public class RedisIO {
private JedisPool jedisPool;
public void init(Plugin plugin) {
File file = FileUtil.getFile(plugin, "", "redis.yml");
if (!file.exists()) {
plugin.saveResource("redis.yml", false);
}
FileConfiguration configuration = YamlConfiguration.loadConfiguration(file);
String redisServer = configuration.getString("redis-server", "localhost");
int redisPort = configuration.getInt("redis-port", 6379);
String redisPassword = configuration.getString("redis-password");
if (redisPassword != null && (redisPassword.isEmpty() || redisPassword.equals("none"))) {
redisPassword = null;
}
try {
String finalRedisPassword = redisPassword;
FutureTask<JedisPool> task = new FutureTask<>(() -> {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(1024);
config.setMaxWait(Duration.ofMillis(10000));
return new JedisPool(config, redisServer, redisPort, 0, finalRedisPassword);
});
Bukkit.getServer().getScheduler().runTaskAsynchronously(plugin, task);
jedisPool = task.get();
} catch (Exception e) {
e.printStackTrace();
}
}
public void close() {
if (jedisPool != null && !jedisPool.isClosed()) {
jedisPool.close();
jedisPool.destroy();
}
}
public JedisPool getJedisPool() {
return jedisPool;
}
public Set<String> getKeys(String arg) {
try (Jedis resource = jedisPool.getResource()) {
return resource.keys(arg);
}
}
public void remove(String key) {
try (Jedis resource = jedisPool.getResource()) {
resource.del(key);
}
}
public void remove(String key, String field) {
try (Jedis resource = jedisPool.getResource()) {
resource.hdel(key, field);
}
}
public boolean has(String key) {
try (Jedis resource = jedisPool.getResource()) {
return resource.exists(key);
}
}
public boolean has(String key, String field) {
try (Jedis resource = jedisPool.getResource()) {
return resource.hexists(key, field);
}
}
}

View File

@ -0,0 +1,106 @@
package com.io.yutian.aulib.util;
import com.github.luben.zstd.Zstd;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveOutputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;
import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Objects;
public class CompressUtils {
public static String zstdString(String unzip) {
byte[] compress = Zstd.compress(unzip.getBytes());
return Base64.encodeBase64String(compress);
}
public static String unZstdString(String zip) {
byte[] decode = Base64.decodeBase64(zip);
Long size = Zstd.decompressedSize(decode);
byte[] decompress = Zstd.decompress(decode, size.intValue());
return new String(decompress);
}
public static void zip(File file, File file1) {
try {
OutputStream outputStream = new FileOutputStream(file1);
zip(file, outputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void zip(File file, OutputStream outputStream) throws IOException {
try (BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
ArchiveOutputStream out = new ZipArchiveOutputStream(bufferedOutputStream)) {
Path start = Paths.get(file.toURI());
Files.walkFileTree(start, new SimpleFileVisitor<>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
ArchiveEntry entry = new ZipArchiveEntry(dir.toFile(), start.relativize(dir).toString());
out.putArchiveEntry(entry);
out.closeArchiveEntry();
return super.preVisitDirectory(dir, attrs);
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
try (InputStream input = new FileInputStream(file.toFile())) {
ArchiveEntry entry = new ZipArchiveEntry(file.toFile(), start.relativize(file).toString());
out.putArchiveEntry(entry);
IOUtils.copy(input, out);
out.closeArchiveEntry();
}
return super.visitFile(file, attrs);
}
});
}
}
public static void unzip(File file, String destDir) {
try {
try (InputStream inputStream = new FileInputStream(file);) {
unzip(inputStream, destDir);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void unzip(InputStream inputStream, String destDir) {
try {
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); ArchiveInputStream in = new ZipArchiveInputStream(bufferedInputStream)) {
ArchiveEntry entry;
while (Objects.nonNull(entry = in.getNextEntry())) {
File file = Paths.get(destDir, entry.getName()).toFile();
if (in.canReadEntryData(entry)) {
if (entry.isDirectory()) {
if (!file.exists()) {
file.mkdirs();
}
} else {
try (OutputStream out = new FileOutputStream(file)) {
IOUtils.copy(in, out);
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -1,5 +1,5 @@
name: AuLib name: AuLib
main: com.io.yutian.aulib.AuLib main: com.io.yutian.aulib.AuLib
version: 1.8 version: 1.9
api-version: 1.18 api-version: 1.18
author: SuperYuTian author: SuperYuTian

View File

File diff suppressed because it is too large Load Diff