perf: 优化代码
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
package cn.hamster3.mc.plugin.core.common.api;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import net.kyori.adventure.platform.AudienceProvider;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public abstract class CoreAPI {
|
||||
@@ -25,4 +28,24 @@ public abstract class CoreAPI {
|
||||
public Connection getConnection() throws SQLException {
|
||||
return getDataSource().getConnection();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return GSON 工具
|
||||
*/
|
||||
public abstract Gson getGson();
|
||||
|
||||
/**
|
||||
* @return GSON 工具,会使用格式化输出、且解析中包含null参数
|
||||
*/
|
||||
public abstract Gson getHumanGson();
|
||||
|
||||
/**
|
||||
* @return 异步线程池
|
||||
*/
|
||||
public abstract ExecutorService getExecutorService();
|
||||
|
||||
/**
|
||||
* @return 调度器线程池
|
||||
*/
|
||||
public abstract ScheduledExecutorService getScheduledExecutorService();
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
package cn.hamster3.mc.plugin.core.common.thread;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.common.util.CoreUtils;
|
||||
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
|
||||
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -23,7 +23,8 @@ public abstract class CountdownThread implements Runnable {
|
||||
}
|
||||
|
||||
public void start(long initialDelay) {
|
||||
future = CoreUtils.SCHEDULED_EXECUTOR.scheduleWithFixedDelay(this, initialDelay, interval, TimeUnit.MILLISECONDS);
|
||||
future = CoreAPI.getInstance().getScheduledExecutorService()
|
||||
.scheduleWithFixedDelay(this, initialDelay, interval, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -1,15 +1,8 @@
|
||||
package cn.hamster3.mc.plugin.core.common.util;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
|
||||
import cn.hamster3.mc.plugin.core.common.thread.NamedThreadFactory;
|
||||
import cn.hamster3.mc.plugin.core.common.util.serializer.ComponentTypeAdapter;
|
||||
import cn.hamster3.mc.plugin.core.common.util.serializer.MessageTypeAdapter;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
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;
|
||||
@@ -18,40 +11,11 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public final class CoreUtils {
|
||||
/**
|
||||
* 异步线程
|
||||
*/
|
||||
public static final ExecutorService WORKER_EXECUTOR = Executors.newCachedThreadPool(new NamedThreadFactory("HamsterCore - Executor"));
|
||||
/**
|
||||
* 调度器线程
|
||||
*/
|
||||
public static final ScheduledExecutorService SCHEDULED_EXECUTOR = Executors
|
||||
.newScheduledThreadPool(1, new NamedThreadFactory("HamsterCore - Scheduler"));
|
||||
/**
|
||||
* GSON 工具
|
||||
*/
|
||||
public static Gson GSON = new GsonBuilder()
|
||||
.registerTypeAdapter(Component.class, ComponentTypeAdapter.INSTANCE)
|
||||
.registerTypeAdapter(DisplayMessage.class, MessageTypeAdapter.INSTANCE)
|
||||
.create();
|
||||
/**
|
||||
* GSON 工具,会使用格式化输出、且解析中包含null参数
|
||||
*/
|
||||
public static Gson GSON_HUMAN = new GsonBuilder()
|
||||
.registerTypeAdapter(Component.class, ComponentTypeAdapter.INSTANCE)
|
||||
.registerTypeAdapter(DisplayMessage.class, MessageTypeAdapter.INSTANCE)
|
||||
.serializeNulls()
|
||||
.setPrettyPrinting()
|
||||
.create();
|
||||
|
||||
private CoreUtils() {
|
||||
}
|
||||
|
||||
|
@@ -1,36 +0,0 @@
|
||||
package cn.hamster3.mc.plugin.core.common.util;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Objects;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class Pair<K, V> implements Serializable {
|
||||
private final K key;
|
||||
private final V value;
|
||||
|
||||
public Pair(K key, V value) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public K getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public V getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Pair<?, ?> pair = (Pair<?, ?>) o;
|
||||
return Objects.equals(key, pair.key) && Objects.equals(value, pair.value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(key, value);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user