perf: 重写部分代码
This commit is contained in:
@@ -10,7 +10,8 @@ dependencies {
|
||||
api(project(":hamster-core-common")) { transitive = false }
|
||||
shade(project(":hamster-core-common")) { transitive = false }
|
||||
|
||||
compileOnly('org.spigotmc:spigot-api:1.19.2-R0.1-SNAPSHOT') {
|
||||
//noinspection VulnerableLibrariesLocal
|
||||
compileOnly('org.spigotmc:spigot-api:1.19.4-R0.1-SNAPSHOT') {
|
||||
exclude group: "com.google.code.gson"
|
||||
}
|
||||
|
||||
@@ -21,6 +22,7 @@ dependencies {
|
||||
|
||||
// https://mvnrepository.com/artifact/com.google.code.gson/gson
|
||||
//noinspection GradlePackageUpdate
|
||||
//noinspection VulnerableLibrariesLocal
|
||||
compileOnly 'com.google.code.gson:gson:2.8.0'
|
||||
|
||||
// https://mvnrepository.com/artifact/net.kyori/adventure-platform-bukkit
|
||||
@@ -61,6 +63,7 @@ tasks.build.dependsOn(shadowJar)
|
||||
|
||||
tasks.register("oldJar", Jar) {
|
||||
dependsOn("jar")
|
||||
//noinspection GrDeprecatedAPIUsage
|
||||
setClassifier("Old")
|
||||
from([
|
||||
tasks.jar.outputs.files.collect {
|
||||
|
@@ -10,10 +10,11 @@ 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.ItemStackAdapter;
|
||||
import cn.hamster3.mc.plugin.core.common.constant.CoreConstantObjects;
|
||||
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.misc.MessageTypeAdapter;
|
||||
import cn.hamster3.mc.plugin.core.common.util.serializer.MessageTypeAdapter;
|
||||
import cn.hamster3.mc.plugin.core.common.util.CoreUtils;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
||||
import org.bukkit.Bukkit;
|
||||
@@ -22,6 +23,7 @@ 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;
|
||||
|
||||
@@ -43,9 +45,10 @@ public class HamsterCorePlugin extends JavaPlugin {
|
||||
saveDefaultConfig();
|
||||
reloadConfig();
|
||||
logger.info("已读取配置文件.");
|
||||
CoreConstantObjects.GSON = new GsonBuilder()
|
||||
CoreUtils.GSON = new GsonBuilder()
|
||||
.registerTypeAdapter(DisplayMessage.class, MessageTypeAdapter.INSTANCE)
|
||||
.registerTypeAdapter(ItemStack.class, ItemStackAdapter.INSTANCE)
|
||||
.registerTypeAdapter(PotionEffect.class, PotionEffectAdapter.INSTANCE)
|
||||
.create();
|
||||
CoreBukkitAPI.init();
|
||||
logger.info("已初始化 CoreBukkitAPI.");
|
||||
@@ -83,9 +86,9 @@ public class HamsterCorePlugin extends JavaPlugin {
|
||||
Logger logger = getLogger();
|
||||
long start = System.currentTimeMillis();
|
||||
logger.info("仓鼠核心正在关闭...");
|
||||
CoreConstantObjects.WORKER_EXECUTOR.shutdownNow();
|
||||
CoreUtils.WORKER_EXECUTOR.shutdownNow();
|
||||
logger.info("已暂停 WORKER_EXECUTOR.");
|
||||
CoreConstantObjects.SCHEDULED_EXECUTOR.shutdownNow();
|
||||
CoreUtils.SCHEDULED_EXECUTOR.shutdownNow();
|
||||
logger.info("已暂停 SCHEDULED_EXECUTOR.");
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
InventoryView view = player.getOpenInventory();
|
||||
|
@@ -3,7 +3,6 @@ package cn.hamster3.mc.plugin.core.bukkit.command.lore;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.HamsterCorePlugin;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.command.ParentCommand;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.command.lore.sub.*;
|
||||
import cn.hamster3.mc.plugin.core.common.util.CoreUtils;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
@@ -30,7 +29,9 @@ public final class ParentLoreCommand extends ParentCommand {
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
|
||||
CoreUtils.replaceColorCode(args);
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
args[i] = args[i].replace("&", "§");
|
||||
}
|
||||
return super.onCommand(sender, command, label, args);
|
||||
}
|
||||
}
|
||||
|
@@ -47,8 +47,12 @@ public enum CoreMessage {
|
||||
return;
|
||||
}
|
||||
for (CoreMessage value : values()) {
|
||||
ConfigurationSection section = config.getConfigurationSection(value.name());
|
||||
if (section == null) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
value.message = BukkitUtils.getDisplayMessage(config.getConfigurationSection(value.name()));
|
||||
value.message = BukkitUtils.getDisplayMessage(section);
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().warning("加载消息设置 " + value.name() + " 时遇到了一个异常: ");
|
||||
e.printStackTrace();
|
||||
@@ -65,16 +69,6 @@ public enum CoreMessage {
|
||||
message.show(audience);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public void show(CommandSender sender, TextReplacementConfig replacement) {
|
||||
if (message == null) {
|
||||
sender.sendMessage(name());
|
||||
return;
|
||||
}
|
||||
Audience audience = CoreBukkitAPI.getInstance().getAudienceProvider().sender(sender);
|
||||
message.show(audience, replacement);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public void show(CommandSender sender, TextReplacementConfig... replacement) {
|
||||
if (message == null) {
|
||||
|
@@ -1,26 +0,0 @@
|
||||
package cn.hamster3.mc.plugin.core.bukkit.util;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.bukkit.listener.CallbackListener;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public final class BukkitCallbackUtils {
|
||||
private BukkitCallbackUtils() {
|
||||
}
|
||||
|
||||
public static CompletableFuture<String> getPlayerChat(HumanEntity player) {
|
||||
return CallbackListener.CHATS.computeIfAbsent(player.getUniqueId(), o -> new CompletableFuture<>());
|
||||
}
|
||||
|
||||
public static CompletableFuture<Block> getPlayerClickedBlock(HumanEntity player) {
|
||||
return CallbackListener.BLOCKS.computeIfAbsent(player.getUniqueId(), o -> new CompletableFuture<>());
|
||||
}
|
||||
|
||||
public static CompletableFuture<Entity> getPlayerClickedEntity(HumanEntity player) {
|
||||
return CallbackListener.ENTITIES.computeIfAbsent(player.getUniqueId(), o -> new CompletableFuture<>());
|
||||
}
|
||||
}
|
@@ -1,108 +0,0 @@
|
||||
package cn.hamster3.mc.plugin.core.bukkit.util;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.bukkit.HamsterCorePlugin;
|
||||
import com.comphenix.protocol.utility.StreamSerializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.potion.PotionEffect;
|
||||
import org.bukkit.potion.PotionEffectType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Level;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public final class BukkitSerializeUtils {
|
||||
private BukkitSerializeUtils() {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String serializeItemStack(@Nullable ItemStack stack) {
|
||||
if (BukkitUtils.isEmptyItemStack(stack)) {
|
||||
return null;
|
||||
}
|
||||
if (!Bukkit.getPluginManager().isPluginEnabled("ProtocolLib")) {
|
||||
HamsterCorePlugin.getInstance().getLogger().warning("ProtocolLib 前置插件未启用, 无法序列化物品!");
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return StreamSerializer.getDefault().serializeItemStack(stack);
|
||||
} catch (IOException e) {
|
||||
HamsterCorePlugin.getInstance().getLogger().log(Level.WARNING, "序列化物品 " + stack + " 时出错!", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static ItemStack deserializeItemStack(@Nullable String s) {
|
||||
if (s == null) {
|
||||
return null;
|
||||
}
|
||||
if (!Bukkit.getPluginManager().isPluginEnabled("ProtocolLib")) {
|
||||
HamsterCorePlugin.getInstance().getLogger().warning("ProtocolLib 前置插件未启用, 无法反序列化物品!");
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return StreamSerializer.getDefault().deserializeItemStack(s);
|
||||
} catch (Exception e) {
|
||||
HamsterCorePlugin.getInstance().getLogger().log(Level.WARNING, "反序列化物品 " + s + " 时出错!", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static ItemStack deserializeItemStack(@Nullable String s, @NotNull ItemStack defaultValue) {
|
||||
if (s == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
if (!Bukkit.getPluginManager().isPluginEnabled("ProtocolLib")) {
|
||||
HamsterCorePlugin.getInstance().getLogger().warning("ProtocolLib 前置插件未启用, 无法反序列化物品!");
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return StreamSerializer.getDefault().deserializeItemStack(s);
|
||||
} catch (Exception e) {
|
||||
HamsterCorePlugin.getInstance().getLogger().log(Level.WARNING, "反序列化物品 " + s + " 时出错!", e);
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static JsonObject serializePotionEffect(@Nullable PotionEffect effect) {
|
||||
if (effect == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
JsonObject object = new JsonObject();
|
||||
object.addProperty("type", effect.getType().getName());
|
||||
object.addProperty("duration", effect.getDuration());
|
||||
object.addProperty("amplifier", effect.getAmplifier());
|
||||
return object;
|
||||
} catch (Exception e) {
|
||||
HamsterCorePlugin.getInstance().getLogger().log(Level.WARNING, "序列化药水效果 " + effect + " 时出错!", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static PotionEffect deserializePotionEffect(@Nullable JsonElement element) {
|
||||
if (element == null || !element.isJsonObject()) {
|
||||
return null;
|
||||
}
|
||||
JsonObject effectObject = element.getAsJsonObject();
|
||||
try {
|
||||
//noinspection ConstantConditions
|
||||
return new PotionEffect(
|
||||
PotionEffectType.getByName(effectObject.get("type").getAsString()),
|
||||
effectObject.get("duration").getAsInt(),
|
||||
effectObject.get("amplifier").getAsInt()
|
||||
);
|
||||
} catch (Exception e) {
|
||||
HamsterCorePlugin.getInstance().getLogger().log(Level.WARNING, "反序列化药水效果 " + element + " 时出错!", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,17 +1,25 @@
|
||||
package cn.hamster3.mc.plugin.core.bukkit.util;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.bukkit.listener.CallbackListener;
|
||||
import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
|
||||
import com.comphenix.protocol.utility.StreamSerializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.inventory.meta.SkullMeta;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.potion.PotionEffect;
|
||||
import org.bukkit.potion.PotionEffectType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -22,6 +30,7 @@ import java.nio.file.Files;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public final class BukkitUtils {
|
||||
@@ -174,10 +183,8 @@ public final class BukkitUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static DisplayMessage getDisplayMessage(ConfigurationSection config) {
|
||||
if (config == null) {
|
||||
return null;
|
||||
}
|
||||
@NotNull
|
||||
public static DisplayMessage getDisplayMessage(@NotNull ConfigurationSection config) {
|
||||
DisplayMessage displayMessage = new DisplayMessage();
|
||||
String message = config.getString("message");
|
||||
if (message != null) {
|
||||
@@ -185,14 +192,14 @@ public final class BukkitUtils {
|
||||
}
|
||||
String actionbar = config.getString("actionbar");
|
||||
if (actionbar != null) {
|
||||
displayMessage.setActionBar(actionbar);
|
||||
displayMessage.setActionbar(actionbar);
|
||||
}
|
||||
String title = config.getString("title");
|
||||
String subtitle = config.getString("subtitle");
|
||||
if (title != null || subtitle != null) {
|
||||
displayMessage.setTitle(
|
||||
title == null ? "" : title,
|
||||
subtitle == null ? "" : subtitle,
|
||||
title,
|
||||
subtitle,
|
||||
config.getInt("fade-in", 10),
|
||||
config.getInt("stay", 70),
|
||||
config.getInt("fade-out", 20)
|
||||
@@ -227,4 +234,75 @@ public final class BukkitUtils {
|
||||
}
|
||||
return YamlConfiguration.loadConfiguration(file);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static String serializeItemStack(@Nullable ItemStack stack) {
|
||||
if (!Bukkit.getPluginManager().isPluginEnabled("ProtocolLib")) {
|
||||
throw new RuntimeException("ProtocolLib 前置插件未启用, 无法序列化物品!");
|
||||
}
|
||||
try {
|
||||
return StreamSerializer.getDefault().serializeItemStack(stack);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("序列化物品时出现了一个异常!", e);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static ItemStack deserializeItemStack(@NotNull String s) {
|
||||
if (!Bukkit.getPluginManager().isPluginEnabled("ProtocolLib")) {
|
||||
throw new RuntimeException("ProtocolLib 前置插件未启用, 无法反序列化物品!");
|
||||
}
|
||||
try {
|
||||
return StreamSerializer.getDefault().deserializeItemStack(s);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("反序列化物品时出现了一个异常!", e);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static ItemStack deserializeItemStack(@NotNull String s, @NotNull ItemStack defaultValue) {
|
||||
if (!Bukkit.getPluginManager().isPluginEnabled("ProtocolLib")) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return StreamSerializer.getDefault().deserializeItemStack(s);
|
||||
} catch (IOException e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static JsonObject serializePotionEffect(@NotNull PotionEffect effect) {
|
||||
JsonObject object = new JsonObject();
|
||||
object.addProperty("type", effect.getType().getName());
|
||||
object.addProperty("duration", effect.getDuration());
|
||||
object.addProperty("amplifier", effect.getAmplifier());
|
||||
return object;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static PotionEffect deserializePotionEffect(@NotNull JsonElement element) {
|
||||
JsonObject effectObject = element.getAsJsonObject();
|
||||
//noinspection ConstantConditions
|
||||
return new PotionEffect(
|
||||
PotionEffectType.getByName(effectObject.get("type").getAsString()),
|
||||
effectObject.get("duration").getAsInt(),
|
||||
effectObject.get("amplifier").getAsInt()
|
||||
);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CompletableFuture<String> getPlayerChat(HumanEntity player) {
|
||||
return CallbackListener.CHATS.computeIfAbsent(player.getUniqueId(), o -> new CompletableFuture<>());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CompletableFuture<Block> getPlayerClickedBlock(HumanEntity player) {
|
||||
return CallbackListener.BLOCKS.computeIfAbsent(player.getUniqueId(), o -> new CompletableFuture<>());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CompletableFuture<Entity> getPlayerClickedEntity(HumanEntity player) {
|
||||
return CallbackListener.ENTITIES.computeIfAbsent(player.getUniqueId(), o -> new CompletableFuture<>());
|
||||
}
|
||||
}
|
||||
|
@@ -1,11 +1,12 @@
|
||||
package cn.hamster3.mc.plugin.core.bukkit.util;
|
||||
package cn.hamster3.mc.plugin.core.bukkit.util.serializer;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.bukkit.util.BukkitUtils;
|
||||
import com.google.gson.*;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class ItemStackAdapter implements JsonSerializer<ItemStack>, JsonDeserializer<ItemStack> {
|
||||
public final class ItemStackAdapter implements JsonSerializer<ItemStack>, JsonDeserializer<ItemStack> {
|
||||
public static final ItemStackAdapter INSTANCE = new ItemStackAdapter();
|
||||
|
||||
private ItemStackAdapter() {
|
||||
@@ -16,15 +17,15 @@ public class ItemStackAdapter implements JsonSerializer<ItemStack>, JsonDeserial
|
||||
if (json.isJsonNull()) {
|
||||
return null;
|
||||
}
|
||||
return BukkitSerializeUtils.deserializeItemStack(json.getAsString());
|
||||
return BukkitUtils.deserializeItemStack(json.getAsString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(ItemStack src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
String s = BukkitSerializeUtils.serializeItemStack(src);
|
||||
if (s == null) {
|
||||
if (src == null) {
|
||||
return JsonNull.INSTANCE;
|
||||
}
|
||||
String s = BukkitUtils.serializeItemStack(src);
|
||||
return new JsonPrimitive(s);
|
||||
}
|
||||
}
|
@@ -0,0 +1,30 @@
|
||||
package cn.hamster3.mc.plugin.core.bukkit.util.serializer;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.bukkit.util.BukkitUtils;
|
||||
import com.google.gson.*;
|
||||
import org.bukkit.potion.PotionEffect;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public final class PotionEffectAdapter implements JsonSerializer<PotionEffect>, JsonDeserializer<PotionEffect> {
|
||||
public static final PotionEffectAdapter INSTANCE = new PotionEffectAdapter();
|
||||
|
||||
private PotionEffectAdapter() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public PotionEffect deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||
if (json.isJsonNull()) {
|
||||
return null;
|
||||
}
|
||||
return BukkitUtils.deserializePotionEffect(json);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(PotionEffect src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
if (src == null) {
|
||||
return JsonNull.INSTANCE;
|
||||
}
|
||||
return BukkitUtils.serializePotionEffect(src);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user