init: 项目初始化
项目初始化
This commit is contained in:
29
hamster-core-common/build.gradle
Normal file
29
hamster-core-common/build.gradle
Normal file
@@ -0,0 +1,29 @@
|
||||
setArchivesBaseName("HamsterCore-Common")
|
||||
|
||||
dependencies {
|
||||
// https://mvnrepository.com/artifact/com.google.code.gson/gson
|
||||
//noinspection GradlePackageUpdate
|
||||
compileOnly 'com.google.code.gson:gson:2.8.0'
|
||||
|
||||
// https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp
|
||||
api 'com.squareup.okhttp3:okhttp:4.10.0'
|
||||
|
||||
// https://mvnrepository.com/artifact/net.kyori/adventure-api
|
||||
api 'net.kyori:adventure-api:4.11.0'
|
||||
// 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'
|
||||
|
||||
// https://mvnrepository.com/artifact/net.kyori/adventure-text-serializer-gson
|
||||
api 'net.kyori:adventure-text-serializer-gson:4.11.0'
|
||||
// https://mvnrepository.com/artifact/net.kyori/adventure-text-serializer-legacy
|
||||
api 'net.kyori:adventure-text-serializer-legacy:4.11.0'
|
||||
|
||||
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
|
||||
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0'
|
||||
}
|
||||
|
||||
test {
|
||||
useJUnitPlatform()
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
package cn.hamster3.core.common.api;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public abstract class CommonAPI {
|
||||
protected static CommonAPI instance;
|
||||
|
||||
public static CommonAPI getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void reportError(String apiKey, String projectID, Throwable exception) {
|
||||
|
||||
}
|
||||
|
||||
public void reportFile(String apiKey, String projectID, String filename) {
|
||||
}
|
||||
}
|
@@ -0,0 +1,68 @@
|
||||
package cn.hamster3.core.common.constant;
|
||||
|
||||
import cn.hamster3.core.common.data.Message;
|
||||
import com.google.gson.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
public interface ConstantObjects {
|
||||
/**
|
||||
* GSON 工具
|
||||
*/
|
||||
Gson GSON = new GsonBuilder()
|
||||
.registerTypeAdapter(Message.class, MessageTypeAdapter.INSTANCE)
|
||||
.create();
|
||||
|
||||
/**
|
||||
* GSON 工具,会使用格式化输出、且解析中包含null参数
|
||||
*/
|
||||
Gson GSON_HUMAN = new GsonBuilder()
|
||||
.registerTypeAdapter(Message.class, MessageTypeAdapter.INSTANCE)
|
||||
.serializeNulls()
|
||||
.setPrettyPrinting()
|
||||
.create();
|
||||
|
||||
/**
|
||||
* 调度器线程
|
||||
*/
|
||||
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"));
|
||||
|
||||
class APIThreadFactory implements ThreadFactory {
|
||||
private final String name;
|
||||
private int threadID;
|
||||
|
||||
public APIThreadFactory(String name) {
|
||||
this.name = name;
|
||||
threadID = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Thread newThread(@NotNull Runnable runnable) {
|
||||
threadID++;
|
||||
return new Thread(runnable, name + "#" + threadID);
|
||||
}
|
||||
}
|
||||
|
||||
class MessageTypeAdapter implements JsonSerializer<Message>, JsonDeserializer<Message> {
|
||||
public static final MessageTypeAdapter INSTANCE = new MessageTypeAdapter();
|
||||
|
||||
private MessageTypeAdapter() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Message deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||
return new Message().json(json);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(Message src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
return src.saveToJson();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,220 @@
|
||||
package cn.hamster3.core.common.data;
|
||||
|
||||
import cn.hamster3.core.common.constant.ConstantObjects;
|
||||
import cn.hamster3.core.common.util.SerializeUtils;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.sound.Sound;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextReplacementConfig;
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
import net.kyori.adventure.title.Title;
|
||||
import net.kyori.adventure.util.Ticks;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class Message {
|
||||
private Component message;
|
||||
private Component actionbar;
|
||||
private Title title;
|
||||
/**
|
||||
* <a href="https://minecraft.fandom.com/zh/wiki/Sounds.json">点击查看 minecraft 所有声音资源</a>
|
||||
*/
|
||||
private Sound sound;
|
||||
|
||||
public Message() {
|
||||
}
|
||||
|
||||
public void show(@NotNull Audience audience) {
|
||||
if (message != null) {
|
||||
audience.sendMessage(message);
|
||||
}
|
||||
if (actionbar != null) {
|
||||
audience.sendActionBar(actionbar);
|
||||
}
|
||||
if (title != null) {
|
||||
audience.showTitle(title);
|
||||
}
|
||||
if (sound != null) {
|
||||
audience.playSound(sound);
|
||||
}
|
||||
}
|
||||
|
||||
public void show(@NotNull Audience audience, TextReplacementConfig replacement) {
|
||||
if (message != null) {
|
||||
audience.sendMessage(message.compact().replaceText(replacement));
|
||||
}
|
||||
if (actionbar != null) {
|
||||
audience.sendActionBar(actionbar.replaceText(replacement));
|
||||
}
|
||||
if (title != null) {
|
||||
audience.showTitle(Title.title(
|
||||
title.title().replaceText(replacement),
|
||||
title.subtitle().replaceText(replacement),
|
||||
title.times()
|
||||
));
|
||||
}
|
||||
if (sound != null) {
|
||||
audience.playSound(sound);
|
||||
}
|
||||
}
|
||||
|
||||
public JsonObject saveToJson() {
|
||||
JsonObject object = new JsonObject();
|
||||
if (message != null) {
|
||||
object.add("message", GsonComponentSerializer.gson().serializeToTree(message.compact()));
|
||||
}
|
||||
if (actionbar != null) {
|
||||
object.add("actionbar", GsonComponentSerializer.gson().serializeToTree(actionbar.compact()));
|
||||
}
|
||||
if (title != null) {
|
||||
object.add("title", SerializeUtils.serializeTitle(title));
|
||||
}
|
||||
if (sound != null) {
|
||||
object.add("sound", SerializeUtils.serializeSound(sound));
|
||||
}
|
||||
return object;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Message message(@NotNull String message) {
|
||||
this.message = Component.text(message);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Message message(@NotNull Component message) {
|
||||
this.message = message;
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Message actionbar(@NotNull String message) {
|
||||
this.actionbar = Component.text(message);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Message actionbar(@NotNull Component message) {
|
||||
this.actionbar = message;
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Message title(@NotNull String title, @NotNull String subtitle) {
|
||||
this.title = Title.title(Component.text(title), Component.text(subtitle));
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Message title(@NotNull String title, @NotNull String subtitle, int fadeIn, int stay, int fadeOut) {
|
||||
this.title = Title.title(
|
||||
Component.text(title),
|
||||
Component.text(subtitle),
|
||||
Title.Times.times(
|
||||
Ticks.duration(fadeIn),
|
||||
Ticks.duration(stay),
|
||||
Ticks.duration(fadeOut)
|
||||
)
|
||||
);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Message title(@NotNull Component title, @NotNull Component subtitle) {
|
||||
this.title = Title.title(title, subtitle);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Message title(@NotNull Component title, @NotNull Component subtitle, int fadeIn, int stay, int fadeOut) {
|
||||
this.title = Title.title(
|
||||
title,
|
||||
subtitle,
|
||||
Title.Times.times(
|
||||
Ticks.duration(fadeIn),
|
||||
Ticks.duration(stay),
|
||||
Ticks.duration(fadeOut)
|
||||
)
|
||||
);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Message title(@Nullable Title title) {
|
||||
this.title = title;
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@SuppressWarnings("PatternValidation")
|
||||
public Message sound(@NotNull String sound) {
|
||||
this.sound = Sound.sound(Key.key(sound), Sound.Source.MASTER, 1, 1);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@SuppressWarnings("PatternValidation")
|
||||
public Message sound(@NotNull String namespace, @NotNull String path) {
|
||||
this.sound = Sound.sound(Key.key(namespace, path), Sound.Source.MASTER, 1, 1);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@SuppressWarnings("PatternValidation")
|
||||
public Message sound(@NotNull String sound, float volume, float pitch) {
|
||||
this.sound = Sound.sound(Key.key(sound), Sound.Source.MASTER, volume, pitch);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@SuppressWarnings("PatternValidation")
|
||||
public Message sound(@NotNull String namespace, @NotNull String value, float volume, float pitch) {
|
||||
this.sound = Sound.sound(Key.key(namespace, value), Sound.Source.MASTER, volume, pitch);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@SuppressWarnings("PatternValidation")
|
||||
public Message sound(@NotNull String namespace, @NotNull String value, @NotNull Sound.Source source, float volume, float pitch) {
|
||||
this.sound = Sound.sound(Key.key(namespace, value), source, volume, pitch);
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Message sound(@Nullable Sound sound) {
|
||||
this.sound = sound;
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@SuppressWarnings("UnusedReturnValue")
|
||||
public Message json(@NotNull JsonElement element) {
|
||||
if (!element.isJsonObject()) {
|
||||
message = Component.text(element.toString());
|
||||
return this;
|
||||
}
|
||||
JsonObject object = element.getAsJsonObject();
|
||||
if (object.has("message")) {
|
||||
message = GsonComponentSerializer.gson().deserializeFromTree(object.get("message"));
|
||||
}
|
||||
if (object.has("actionbar")) {
|
||||
actionbar = GsonComponentSerializer.gson().deserializeFromTree(object.get("actionbar"));
|
||||
}
|
||||
if (object.has("title")) {
|
||||
title = SerializeUtils.deserializeTitle(object.getAsJsonObject("title"));
|
||||
}
|
||||
if (object.has("sound")) {
|
||||
sound = SerializeUtils.deserializeSound(object.getAsJsonObject("sound"));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return ConstantObjects.GSON.toJson(this);
|
||||
}
|
||||
}
|
@@ -0,0 +1,61 @@
|
||||
package cn.hamster3.core.common.thread;
|
||||
|
||||
import cn.hamster3.core.common.constant.ConstantObjects;
|
||||
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public abstract class CountdownThread implements Runnable {
|
||||
private final long interval;
|
||||
private final long ticks;
|
||||
protected int tick;
|
||||
private ScheduledFuture<?> future;
|
||||
|
||||
public CountdownThread(long interval, long ticks) {
|
||||
this.interval = interval;
|
||||
this.ticks = ticks;
|
||||
tick = 0;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
start(interval);
|
||||
}
|
||||
|
||||
public void start(long initialDelay) {
|
||||
future = ConstantObjects.SCHEDULED_EXECUTOR.scheduleWithFixedDelay(this, initialDelay, interval, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (tick == ticks) {
|
||||
try {
|
||||
onFinish();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
cancel();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
onTick(tick);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
tick++;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计时器运行一个 tick
|
||||
*
|
||||
* @param tick 时间刻度,以 0 开始,到 interval-1 结束
|
||||
*/
|
||||
protected void onTick(int tick) {
|
||||
}
|
||||
|
||||
public void cancel() {
|
||||
future.cancel(false);
|
||||
}
|
||||
|
||||
protected abstract void onFinish();
|
||||
}
|
@@ -0,0 +1,204 @@
|
||||
package cn.hamster3.core.common.util;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.Collections;
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
* 算数表达式求值
|
||||
* 传入算数表达式,将返回一个浮点值结果
|
||||
* 如果计算过程错误,将返回一个NaN
|
||||
* <p>
|
||||
* 我也忘了这个类是哪里抄来的
|
||||
* 反正它运行起来比直接用JavaScript引擎计算要快很多
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public class Calculator {
|
||||
// 默认除法运算精度
|
||||
private static final int DEF_DIV_SCALE = 16;
|
||||
|
||||
private final Stack<String> postfixStack = new Stack<>();// 后缀式栈
|
||||
private final Stack<Character> opStack = new Stack<>();// 运算符栈
|
||||
private final int[] operaPriority = new int[]{0, 3, 2, 1, -1, 1, 0, 2};// 运用运算符ASCII码-40做索引的运算符优先级
|
||||
|
||||
private Calculator() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 按照给定的表达式计算
|
||||
*
|
||||
* @param expression 要计算的表达式例如:5+12*(3+5)/7
|
||||
* @return 计算结果
|
||||
*/
|
||||
public double calculate(String expression) {
|
||||
Stack<String> resultStack = new Stack<>();
|
||||
prepare(expression);
|
||||
Collections.reverse(postfixStack);// 将后缀式栈反转
|
||||
String firstValue, secondValue, currentValue;// 参与计算的第一个值,第二个值和算术运算符
|
||||
while (!postfixStack.isEmpty()) {
|
||||
currentValue = postfixStack.pop();
|
||||
if (!isOperator(currentValue.charAt(0))) {// 如果不是运算符则存入操作数栈中
|
||||
currentValue = currentValue.replace("~", "-");
|
||||
resultStack.push(currentValue);
|
||||
} else {// 如果是运算符则从操作数栈中取两个值和该数值一起参与运算
|
||||
secondValue = resultStack.pop();
|
||||
firstValue = resultStack.pop();
|
||||
|
||||
// 将负数标记符改为负号
|
||||
firstValue = firstValue.replace("~", "-");
|
||||
secondValue = secondValue.replace("~", "-");
|
||||
|
||||
String tempResult = calculate(firstValue, secondValue, currentValue.charAt(0));
|
||||
resultStack.push(tempResult);
|
||||
}
|
||||
}
|
||||
return Double.parseDouble(resultStack.pop());
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据准备阶段将表达式转换成为后缀式栈
|
||||
*
|
||||
* @param expression 表达式
|
||||
*/
|
||||
private void prepare(String expression) {
|
||||
opStack.push(',');// 运算符放入栈底元素逗号,此符号优先级最低
|
||||
char[] arr = expression.toCharArray();
|
||||
int currentIndex = 0;// 当前字符的位置
|
||||
int count = 0;// 上次算术运算符到本次算术运算符的字符的长度便于或者之间的数值
|
||||
char currentOp, peekOp;// 当前操作符和栈顶操作符
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
currentOp = arr[i];
|
||||
if (isOperator(currentOp)) {// 如果当前字符是运算符
|
||||
if (count > 0) {
|
||||
postfixStack.push(new String(arr, currentIndex, count));// 取两个运算符之间的数字
|
||||
}
|
||||
peekOp = opStack.peek();
|
||||
if (currentOp == ')') {// 遇到反括号则将运算符栈中的元素移除到后缀式栈中直到遇到左括号
|
||||
while (opStack.peek() != '(') {
|
||||
postfixStack.push(String.valueOf(opStack.pop()));
|
||||
}
|
||||
opStack.pop();
|
||||
} else {
|
||||
while (currentOp != '(' && peekOp != ',' && compare(currentOp, peekOp)) {
|
||||
postfixStack.push(String.valueOf(opStack.pop()));
|
||||
peekOp = opStack.peek();
|
||||
}
|
||||
opStack.push(currentOp);
|
||||
}
|
||||
count = 0;
|
||||
currentIndex = i + 1;
|
||||
} else {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
if (count > 1 || (count == 1 && !isOperator(arr[currentIndex]))) {// 最后一个字符不是括号或者其他运算符的则加入后缀式栈中
|
||||
postfixStack.push(new String(arr, currentIndex, count));
|
||||
}
|
||||
|
||||
while (opStack.peek() != ',') {
|
||||
postfixStack.push(String.valueOf(opStack.pop()));// 将操作符栈中的剩余的元素添加到后缀式栈中
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否为算术符号
|
||||
*
|
||||
* @param c 要判断的字符
|
||||
* @return 判断结果
|
||||
*/
|
||||
private boolean isOperator(char c) {
|
||||
return c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')';
|
||||
}
|
||||
|
||||
/**
|
||||
* 利用ASCII码-40做下标去算术符号优先级
|
||||
*
|
||||
* @param cur 字符1
|
||||
* @param peek 字符2
|
||||
* @return 如果是peek优先级高于cur,返回true,默认都是peek优先级要低
|
||||
*/
|
||||
private boolean compare(char cur, char peek) {
|
||||
return operaPriority[(peek) - 40] >= operaPriority[(cur) - 40];
|
||||
}
|
||||
|
||||
/**
|
||||
* 按照给定的算术运算符做计算
|
||||
*
|
||||
* @param firstValue 第一个值
|
||||
* @param secondValue 第二个值
|
||||
* @param currentOp 算数运算符
|
||||
* @return 运算结果
|
||||
*/
|
||||
private String calculate(String firstValue, String secondValue, char currentOp) {
|
||||
String result = "";
|
||||
switch (currentOp) {
|
||||
case '+':
|
||||
result = String.valueOf(add(firstValue, secondValue));
|
||||
break;
|
||||
case '-':
|
||||
result = String.valueOf(sub(firstValue, secondValue));
|
||||
break;
|
||||
case '*':
|
||||
result = String.valueOf(mul(firstValue, secondValue));
|
||||
break;
|
||||
case '/':
|
||||
result = String.valueOf(div(firstValue, secondValue));
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 提供精确的加法运算。
|
||||
*
|
||||
* @param v1 被加数
|
||||
* @param v2 加数
|
||||
* @return 两个参数的和
|
||||
*/
|
||||
private double add(String v1, String v2) {
|
||||
BigDecimal b1 = new BigDecimal(v1);
|
||||
BigDecimal b2 = new BigDecimal(v2);
|
||||
return b1.add(b2).doubleValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* 提供精确的减法运算。
|
||||
*
|
||||
* @param v1 被减数
|
||||
* @param v2 减数
|
||||
* @return 两个参数的差
|
||||
*/
|
||||
private double sub(String v1, String v2) {
|
||||
BigDecimal b1 = new BigDecimal(v1);
|
||||
BigDecimal b2 = new BigDecimal(v2);
|
||||
return b1.subtract(b2).doubleValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* 提供精确的乘法运算。
|
||||
*
|
||||
* @param v1 被乘数
|
||||
* @param v2 乘数
|
||||
* @return 两个参数的积
|
||||
*/
|
||||
private double mul(String v1, String v2) {
|
||||
BigDecimal b1 = new BigDecimal(v1);
|
||||
BigDecimal b2 = new BigDecimal(v2);
|
||||
return b1.multiply(b2).doubleValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* 提供(相对)精确的除法运算,当发生除不尽的情况时,精确到 小数点以后10位,以后的数字四舍五入。
|
||||
*
|
||||
* @param v1 被除数
|
||||
* @param v2 除数
|
||||
* @return 两个参数的商
|
||||
*/
|
||||
private double div(String v1, String v2) {
|
||||
BigDecimal b1 = new BigDecimal(v1);
|
||||
BigDecimal b2 = new BigDecimal(v2);
|
||||
return b1.divide(b2, DEF_DIV_SCALE, RoundingMode.HALF_UP).doubleValue();
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,9 @@
|
||||
package cn.hamster3.core.common.util;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public final class CaseUtils {
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T caseObject(Object o) {
|
||||
return (T) o;
|
||||
}
|
||||
}
|
@@ -0,0 +1,35 @@
|
||||
package cn.hamster3.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();
|
||||
}
|
||||
}
|
@@ -0,0 +1,81 @@
|
||||
package cn.hamster3.core.common.util;
|
||||
|
||||
import cn.hamster3.core.common.constant.ConstantObjects;
|
||||
import com.google.gson.JsonObject;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.sound.Sound;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
import net.kyori.adventure.title.Title;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
public class SerializeUtils {
|
||||
@NotNull
|
||||
public static JsonObject serializeTitle(@NotNull Title title) {
|
||||
JsonObject object = new JsonObject();
|
||||
object.add("title", GsonComponentSerializer.gson().serializeToTree(title.title()));
|
||||
object.add("subtitle", GsonComponentSerializer.gson().serializeToTree(title.subtitle()));
|
||||
Title.Times times = title.times();
|
||||
if (times != null) {
|
||||
object.add("times", serializeTitleTimes(times));
|
||||
}
|
||||
return object;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Title deserializeTitle(@NotNull JsonObject object) {
|
||||
if (object.has("times")) {
|
||||
return Title.title(
|
||||
ConstantObjects.GSON.fromJson(object.get("title"), Component.class),
|
||||
ConstantObjects.GSON.fromJson(object.get("subtitle"), Component.class),
|
||||
deserializeTitleTimes(object.getAsJsonObject("times"))
|
||||
);
|
||||
} else {
|
||||
return Title.title(
|
||||
GsonComponentSerializer.gson().deserializeFromTree(object.get("title")),
|
||||
GsonComponentSerializer.gson().deserializeFromTree(object.get("subtitle"))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static JsonObject serializeTitleTimes(@NotNull Title.Times times) {
|
||||
JsonObject object = new JsonObject();
|
||||
object.addProperty("fadeIn", times.fadeIn().toMillis());
|
||||
object.addProperty("stay", times.stay().toMillis());
|
||||
object.addProperty("fadeOut", times.fadeOut().toMillis());
|
||||
return object;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Title.Times deserializeTitleTimes(@NotNull JsonObject object) {
|
||||
return Title.Times.times(
|
||||
Duration.ofMillis(object.get("fadeIn").getAsLong()),
|
||||
Duration.ofMillis(object.get("stay").getAsLong()),
|
||||
Duration.ofMillis(object.get("fadeOut").getAsLong())
|
||||
);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static JsonObject serializeSound(@NotNull Sound sound) {
|
||||
JsonObject object = new JsonObject();
|
||||
object.addProperty("key", sound.name().asString());
|
||||
object.addProperty("source", sound.source().name());
|
||||
object.addProperty("volume", sound.volume());
|
||||
object.addProperty("pitch", sound.pitch());
|
||||
return object;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@SuppressWarnings("PatternValidation")
|
||||
public static Sound deserializeSound(@NotNull JsonObject object) {
|
||||
return Sound.sound(
|
||||
Key.key(object.get("key").getAsString()),
|
||||
Sound.Source.valueOf(object.get("source").getAsString()),
|
||||
object.get("volume").getAsFloat(),
|
||||
object.get("pitch").getAsFloat()
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user