perf: 优化代码
This commit is contained in:
@@ -10,22 +10,13 @@ import cn.hamster3.mc.plugin.core.bukkit.listener.CallbackListener;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.listener.DebugListener;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.page.handler.PageHandler;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.page.listener.PageListener;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.util.serializer.ItemStackAdapter;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.util.serializer.PotionEffectAdapter;
|
||||
import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
|
||||
import cn.hamster3.mc.plugin.core.common.util.CoreUtils;
|
||||
import cn.hamster3.mc.plugin.core.common.util.serializer.ComponentTypeAdapter;
|
||||
import cn.hamster3.mc.plugin.core.common.util.serializer.MessageTypeAdapter;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
|
||||
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.InventoryView;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.potion.PotionEffect;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@@ -47,12 +38,6 @@ public class HamsterCorePlugin extends JavaPlugin {
|
||||
saveDefaultConfig();
|
||||
reloadConfig();
|
||||
logger.info("已读取配置文件.");
|
||||
CoreUtils.GSON = new GsonBuilder()
|
||||
.registerTypeAdapter(Component.class, ComponentTypeAdapter.INSTANCE)
|
||||
.registerTypeAdapter(DisplayMessage.class, MessageTypeAdapter.INSTANCE)
|
||||
.registerTypeAdapter(ItemStack.class, ItemStackAdapter.INSTANCE)
|
||||
.registerTypeAdapter(PotionEffect.class, PotionEffectAdapter.INSTANCE)
|
||||
.create();
|
||||
CoreBukkitAPI.init();
|
||||
logger.info("已初始化 CoreBukkitAPI.");
|
||||
CoreMessage.init(getLogger(), getConfig().getConfigurationSection("messages"));
|
||||
@@ -93,10 +78,10 @@ public class HamsterCorePlugin extends JavaPlugin {
|
||||
logger.info("仓鼠核心正在关闭...");
|
||||
CoreBukkitAPI.getInstance().getDataSource().close();
|
||||
logger.info("已关闭数据库连接池.");
|
||||
CoreUtils.WORKER_EXECUTOR.shutdownNow();
|
||||
logger.info("已暂停 WORKER_EXECUTOR.");
|
||||
CoreUtils.SCHEDULED_EXECUTOR.shutdownNow();
|
||||
logger.info("已暂停 SCHEDULED_EXECUTOR.");
|
||||
CoreAPI.getInstance().getExecutorService().shutdownNow();
|
||||
logger.info("已关闭线程池.");
|
||||
CoreAPI.getInstance().getScheduledExecutorService().shutdownNow();
|
||||
logger.info("已关闭调度器.");
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
InventoryView view = player.getOpenInventory();
|
||||
Inventory inventory = view.getTopInventory();
|
||||
@@ -105,6 +90,7 @@ public class HamsterCorePlugin extends JavaPlugin {
|
||||
}
|
||||
player.closeInventory();
|
||||
}
|
||||
logger.info("已关闭所有玩家的界面.");
|
||||
long time = System.currentTimeMillis() - start;
|
||||
logger.info("仓鼠核心已关闭,总计耗时 " + time + " ms.");
|
||||
}
|
||||
|
@@ -1,17 +1,36 @@
|
||||
package cn.hamster3.mc.plugin.core.bukkit.api;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.bukkit.HamsterCorePlugin;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.util.serializer.ItemStackAdapter;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.util.serializer.PotionEffectAdapter;
|
||||
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
|
||||
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.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.potion.PotionEffect;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public final class CoreBukkitAPI extends CoreAPI {
|
||||
private final Gson gson;
|
||||
private final Gson humanGson;
|
||||
private final HikariDataSource datasource;
|
||||
private final ExecutorService executorService;
|
||||
private final ScheduledExecutorService scheduledExecutorService;
|
||||
|
||||
private CoreBukkitAPI() {
|
||||
HamsterCorePlugin plugin = HamsterCorePlugin.getInstance();
|
||||
@@ -38,6 +57,24 @@ public final class CoreBukkitAPI extends CoreAPI {
|
||||
hikariConfig.setValidationTimeout(datasourceConfig.getLong("validation-timeout", 5000));
|
||||
hikariConfig.setPoolName("HamsterCore-Pool");
|
||||
datasource = new HikariDataSource(hikariConfig);
|
||||
|
||||
gson = new GsonBuilder()
|
||||
.registerTypeAdapter(Component.class, ComponentTypeAdapter.INSTANCE)
|
||||
.registerTypeAdapter(DisplayMessage.class, MessageTypeAdapter.INSTANCE)
|
||||
.registerTypeAdapter(ItemStack.class, ItemStackAdapter.INSTANCE)
|
||||
.registerTypeAdapter(PotionEffect.class, PotionEffectAdapter.INSTANCE)
|
||||
.create();
|
||||
humanGson = new GsonBuilder()
|
||||
.registerTypeAdapter(Component.class, ComponentTypeAdapter.INSTANCE)
|
||||
.registerTypeAdapter(DisplayMessage.class, MessageTypeAdapter.INSTANCE)
|
||||
.registerTypeAdapter(ItemStack.class, ItemStackAdapter.INSTANCE)
|
||||
.registerTypeAdapter(PotionEffect.class, PotionEffectAdapter.INSTANCE)
|
||||
.serializeNulls()
|
||||
.setPrettyPrinting()
|
||||
.create();
|
||||
|
||||
executorService = Executors.newCachedThreadPool(new NamedThreadFactory("HamsterCore - Executor"));
|
||||
scheduledExecutorService = Executors.newScheduledThreadPool(1, new NamedThreadFactory("HamsterCore - Scheduler"));
|
||||
}
|
||||
|
||||
public static CoreBukkitAPI getInstance() {
|
||||
@@ -60,4 +97,24 @@ public final class CoreBukkitAPI extends CoreAPI {
|
||||
public @NotNull HikariDataSource getDataSource() {
|
||||
return datasource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Gson getGson() {
|
||||
return gson;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Gson getHumanGson() {
|
||||
return humanGson;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExecutorService getExecutorService() {
|
||||
return executorService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScheduledExecutorService getScheduledExecutorService() {
|
||||
return scheduledExecutorService;
|
||||
}
|
||||
}
|
||||
|
@@ -1,7 +1,7 @@
|
||||
package cn.hamster3.mc.plugin.core.bungee;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.bungee.api.CoreBungeeAPI;
|
||||
import cn.hamster3.mc.plugin.core.common.util.CoreUtils;
|
||||
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
|
||||
import net.kyori.adventure.platform.bungeecord.BungeeAudiences;
|
||||
import net.md_5.bungee.api.plugin.Plugin;
|
||||
|
||||
@@ -45,10 +45,10 @@ public class HamsterCorePlugin extends Plugin {
|
||||
logger.info("仓鼠核心正在关闭...");
|
||||
CoreBungeeAPI.getInstance().getDataSource().close();
|
||||
logger.info("已关闭数据库连接池.");
|
||||
CoreUtils.WORKER_EXECUTOR.shutdownNow();
|
||||
logger.info("已暂停 WORKER_EXECUTOR.");
|
||||
CoreUtils.SCHEDULED_EXECUTOR.shutdownNow();
|
||||
logger.info("已暂停 SCHEDULED_EXECUTOR.");
|
||||
CoreAPI.getInstance().getExecutorService().shutdownNow();
|
||||
logger.info("已关闭线程池.");
|
||||
CoreAPI.getInstance().getScheduledExecutorService().shutdownNow();
|
||||
logger.info("已关闭调度器.");
|
||||
long time = System.currentTimeMillis() - start;
|
||||
logger.info("仓鼠核心已关闭,总计耗时 " + time + " ms.");
|
||||
}
|
||||
|
@@ -3,15 +3,30 @@ package cn.hamster3.mc.plugin.core.bungee.api;
|
||||
import cn.hamster3.mc.plugin.core.bungee.HamsterCorePlugin;
|
||||
import cn.hamster3.mc.plugin.core.bungee.util.CoreBungeeCordUtils;
|
||||
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
|
||||
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.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import net.kyori.adventure.platform.AudienceProvider;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.md_5.bungee.config.Configuration;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public final class CoreBungeeAPI extends CoreAPI {
|
||||
private final Gson gson;
|
||||
private final Gson humanGson;
|
||||
private final HikariDataSource datasource;
|
||||
private final ExecutorService executorService;
|
||||
private final ScheduledExecutorService scheduledExecutorService;
|
||||
|
||||
private CoreBungeeAPI() {
|
||||
HamsterCorePlugin plugin = HamsterCorePlugin.getInstance();
|
||||
@@ -38,6 +53,20 @@ public final class CoreBungeeAPI extends CoreAPI {
|
||||
hikariConfig.setValidationTimeout(datasourceConfig.getLong("validation-timeout", 5000));
|
||||
hikariConfig.setPoolName("HamsterCore-Pool");
|
||||
datasource = new HikariDataSource(hikariConfig);
|
||||
|
||||
gson = new GsonBuilder()
|
||||
.registerTypeAdapter(Component.class, ComponentTypeAdapter.INSTANCE)
|
||||
.registerTypeAdapter(DisplayMessage.class, MessageTypeAdapter.INSTANCE)
|
||||
.create();
|
||||
humanGson = new GsonBuilder()
|
||||
.registerTypeAdapter(Component.class, ComponentTypeAdapter.INSTANCE)
|
||||
.registerTypeAdapter(DisplayMessage.class, MessageTypeAdapter.INSTANCE)
|
||||
.serializeNulls()
|
||||
.setPrettyPrinting()
|
||||
.create();
|
||||
|
||||
executorService = Executors.newCachedThreadPool(new NamedThreadFactory("HamsterCore - Executor"));
|
||||
scheduledExecutorService = Executors.newScheduledThreadPool(1, new NamedThreadFactory("HamsterCore - Scheduler"));
|
||||
}
|
||||
|
||||
public static CoreBungeeAPI getInstance() {
|
||||
@@ -60,4 +89,24 @@ public final class CoreBungeeAPI extends CoreAPI {
|
||||
public @NotNull HikariDataSource getDataSource() {
|
||||
return datasource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Gson getGson() {
|
||||
return gson;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Gson getHumanGson() {
|
||||
return humanGson;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExecutorService getExecutorService() {
|
||||
return executorService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScheduledExecutorService getScheduledExecutorService() {
|
||||
return scheduledExecutorService;
|
||||
}
|
||||
}
|
||||
|
@@ -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