feat: 初版完成

初版完成
This commit is contained in:
2022-10-23 05:41:01 +08:00
parent b0e4920e63
commit bcd0410fe1
37 changed files with 1453 additions and 174 deletions

View File

@@ -13,7 +13,7 @@ dependencies {
// https://mvnrepository.com/artifact/net.kyori/adventure-text-minimessage
api 'net.kyori:adventure-text-minimessage:4.11.0'
// https://mvnrepository.com/artifact/net.kyori/adventure-platform-api
implementation 'net.kyori:adventure-platform-api:4.1.2'
api 'net.kyori:adventure-platform-api:4.1.2'
// https://mvnrepository.com/artifact/net.kyori/adventure-text-serializer-gson
api 'net.kyori:adventure-text-serializer-gson:4.11.0'

View File

@@ -0,0 +1,36 @@
package cn.hamster3.mc.plugin.core.common.api;
import net.kyori.adventure.platform.AudienceProvider;
import org.jetbrains.annotations.NotNull;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
@SuppressWarnings("unused")
public abstract class CoreAPI {
protected static CoreAPI instance;
public static CoreAPI getInstance() {
return instance;
}
@NotNull
public abstract AudienceProvider getAudienceProvider();
@NotNull
public abstract DataSource getDataSource();
@NotNull
public Connection getConnection() throws SQLException {
return getDataSource().getConnection();
}
public void reportError(@NotNull String apiKey, @NotNull String projectID, @NotNull Throwable exception) {
}
public void reportFile(@NotNull String apiKey, @NotNull String projectID, @NotNull String filename, byte @NotNull [] bytes) {
}
}

View File

@@ -1,30 +0,0 @@
package cn.hamster3.mc.plugin.core.common.api;
import net.kyori.adventure.platform.AudienceProvider;
import org.jetbrains.annotations.NotNull;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
@SuppressWarnings("unused")
public abstract class CoreCommonAPI {
protected static CoreCommonAPI instance;
public static CoreCommonAPI getInstance() {
return instance;
}
@NotNull
public abstract AudienceProvider getAudienceProvider();
@NotNull
public abstract DataSource getDataSource();
@NotNull
public abstract Connection getConnection() throws SQLException;
public abstract void reportError(@NotNull String apiKey, @NotNull String projectID, @NotNull Throwable exception);
public abstract void reportFile(@NotNull String apiKey, @NotNull String projectID, @NotNull String filename, byte @NotNull [] bytes);
}

View File

@@ -5,9 +5,16 @@ import com.google.gson.*;
import org.jetbrains.annotations.NotNull;
import java.lang.reflect.Type;
import java.util.UUID;
import java.util.concurrent.*;
@SuppressWarnings("unused")
public interface CoreConstantObjects {
/**
* Minecraft 默认指定的空 UUID
*/
UUID NIL_UUID = new UUID(0L, 0L);
/**
* GSON 工具
*/
@@ -28,14 +35,14 @@ public interface CoreConstantObjects {
*/
JsonParser JSON_PARSER = new JsonParser();
/**
* 调度器线程
*/
ScheduledExecutorService SCHEDULED_EXECUTOR = Executors.newScheduledThreadPool(1, new APIThreadFactory("HamsterCore - Scheduler"));
/**
* 异步线程
*/
ExecutorService WORKER_EXECUTOR = new ThreadPoolExecutor(1, Integer.MAX_VALUE, 60, TimeUnit.MINUTES, new SynchronousQueue<>(), new APIThreadFactory("HamsterCore - Executor"));
/**
* 调度器线程
*/
ScheduledExecutorService SCHEDULED_EXECUTOR = Executors.newScheduledThreadPool(1, new APIThreadFactory("HamsterCore - Scheduler"));
class APIThreadFactory implements ThreadFactory {
private final String name;

View File

@@ -6,15 +6,19 @@ import java.util.Collections;
import java.util.Stack;
/**
* 算数表达式求值
* 算数表达式求值工具
* <p>
* 传入算数表达式,将返回一个浮点值结果
* <p>
* 如果计算过程错误将返回一个NaN
* <p>
* 我也忘了这个类是哪里抄来的
* 反正它运行起来比直接用JavaScript引擎计算要快很多
* <p>
* 反正它运行起来比直接用 JavaScript 引擎计算要快很多
*/
@SuppressWarnings("unused")
public class Calculator {
public final class Calculator {
public static final Calculator INSTANCE = new Calculator();
// 默认除法运算精度
private static final int DEF_DIV_SCALE = 16;

View File

@@ -2,6 +2,9 @@ package cn.hamster3.mc.plugin.core.common.util;
@SuppressWarnings("unused")
public final class CaseUtils {
private CaseUtils() {
}
@SuppressWarnings("unchecked")
public static <T> T caseObject(Object o) {
return (T) o;

View File

@@ -0,0 +1,182 @@
package cn.hamster3.mc.plugin.core.common.util;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@SuppressWarnings("unused")
public final class CommonUtils {
private CommonUtils() {
}
public static void zipCompressionFolder(@NotNull File folder, @NotNull File zipFile) throws IOException {
ZipOutputStream stream = new ZipOutputStream(Files.newOutputStream(zipFile.toPath()));
putFileToZipStream(stream, "", folder);
stream.close();
}
public static void putFileToZipStream(@NotNull ZipOutputStream stream, @NotNull String path, @NotNull File file) throws IOException {
if (file.isDirectory()) {
File[] files = file.listFiles();
if (files == null) {
throw new IOException();
}
for (File subFile : files) {
putFileToZipStream(stream, path + file.getName() + "/", subFile);
}
return;
}
ZipEntry entry = new ZipEntry(path + file.getName());
stream.putNextEntry(entry);
stream.write(Files.readAllBytes(file.toPath()));
stream.closeEntry();
}
/**
* 替换颜色代码
*
* @param string 要替换的字符串
* @return 替换后的字符串
*/
@Nullable
public static String replaceColorCode(@Nullable String string) {
if (string == null) return null;
return string.replace("&", "§");
}
/**
* 替换颜色代码
* <p>
* 添加这个方法是因为 ConfigurationSection 中的 getString 方法有 @Nullable 注解
* <p>
* 导致 idea 会弹出某些警告,让人非常不爽
*
* @param string 要替换的字符串
* @param defaultValue 若 string 为空则使用该字符串
* @return 替换后的字符串
*/
@NotNull
public static String replaceColorCode(@Nullable String string, @NotNull String defaultValue) {
if (string == null) {
return replaceColorCode(defaultValue);
}
return replaceColorCode(string);
}
/**
* 替换颜色代码
*
* @param strings 要替换的字符串
* @return 替换后的字符串
*/
@NotNull
public static ArrayList<String> replaceColorCode(@Nullable Iterable<String> strings) {
ArrayList<String> list = new ArrayList<>();
if (strings == null) return list;
for (String s : strings) {
list.add(replaceColorCode(s));
}
return list;
}
/**
* 替换颜色代码
*
* @param strings 要替换的字符串
* @return 替换后的字符串
*/
@NotNull
public static ArrayList<String> replaceColorCode(@Nullable String[] strings) {
ArrayList<String> list = new ArrayList<>();
if (strings == null) return list;
for (String s : strings) {
list.add(replaceColorCode(s));
}
return list;
}
@NotNull
public static String[] replaceStringList(@NotNull String[] strings, @NotNull String key, @NotNull String value) {
for (int i = 0; i < strings.length; i++) {
strings[i] = strings[i].replace(key, value);
}
return strings;
}
@NotNull
public static List<String> replaceStringList(@NotNull Iterable<String> strings, @NotNull String key, @NotNull String value) {
ArrayList<String> list = new ArrayList<>();
for (String s : strings) {
list.add(s.replace(key, value));
}
return list;
}
@NotNull
public static List<String> replaceStringList(@NotNull List<String> strings, @NotNull String key, @NotNull String value) {
strings.replaceAll(s -> s.replace(key, value));
return strings;
}
public static boolean startsWithIgnoreCase(@NotNull String string, @NotNull String prefix) {
return string.regionMatches(true, 0, prefix, 0, prefix.length());
}
public static boolean endsWithIgnoreCase(@NotNull String string, @NotNull String suffix) {
int strOffset = string.length() - suffix.length();
return string.regionMatches(true, strOffset, suffix, 0, suffix.length());
}
@NotNull
public static ArrayList<String> startsWith(@NotNull Iterable<String> strings, @NotNull String with) {
ArrayList<String> list = new ArrayList<>();
for (String string : strings) {
if (string.startsWith(with)) {
list.add(string);
}
}
return list;
}
@NotNull
public static ArrayList<String> endsWith(@NotNull Iterable<String> strings, @NotNull String with) {
ArrayList<String> list = new ArrayList<>();
for (String string : strings) {
if (string.endsWith(with)) {
list.add(string);
}
}
return list;
}
@NotNull
public static ArrayList<String> startsWithIgnoreCase(@NotNull Iterable<String> strings, @NotNull String start) {
ArrayList<String> list = new ArrayList<>();
for (String string : strings) {
if (startsWithIgnoreCase(string, start)) {
list.add(string);
}
}
return list;
}
@NotNull
public static ArrayList<String> endsWithIgnoreCase(@NotNull Iterable<String> strings, @NotNull String end) {
ArrayList<String> list = new ArrayList<>();
for (String string : strings) {
if (endsWithIgnoreCase(string, end)) {
list.add(string);
}
}
return list;
}
}

View File

@@ -1,35 +0,0 @@
package cn.hamster3.mc.plugin.core.common.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@SuppressWarnings("unused")
public class FileUtils {
@SuppressWarnings("IOStreamConstructor")
public static void zipCompressionFolder(File folder, File zipFile) throws IOException {
ZipOutputStream stream = new ZipOutputStream(new FileOutputStream(zipFile));
putFileToZipStream(stream, "", folder);
stream.close();
}
public static void putFileToZipStream(ZipOutputStream stream, String path, File file) throws IOException {
if (file.isDirectory()) {
File[] files = file.listFiles();
if (files == null) {
throw new IOException();
}
for (File subFile : files) {
putFileToZipStream(stream, path + file.getName() + "/", subFile);
}
return;
}
ZipEntry entry = new ZipEntry(path + file.getName());
stream.putNextEntry(entry);
stream.write(Files.readAllBytes(file.toPath()));
stream.closeEntry();
}
}

View File

@@ -11,7 +11,19 @@ import org.jetbrains.annotations.NotNull;
import java.time.Duration;
public class SerializeUtils {
/**
* 序列化相关工具
*/
public final class SerializeUtils {
private SerializeUtils() {
}
/**
* 将 adventure 中的 title 对象序列化为 json
*
* @param title -
* @return -
*/
@NotNull
public static JsonObject serializeTitle(@NotNull Title title) {
JsonObject object = new JsonObject();
@@ -24,6 +36,12 @@ public class SerializeUtils {
return object;
}
/**
* 将 json 反序列化为 adventure 中的 title
*
* @param object -
* @return -
*/
@NotNull
public static Title deserializeTitle(@NotNull JsonObject object) {
if (object.has("times")) {