From 973cfa68960b5a1b5673fb113d35fb94154b5353 Mon Sep 17 00:00:00 2001 From: MiniDay <372403923@qq.com> Date: Mon, 5 Jun 2023 17:21:18 +0800 Subject: [PATCH] =?UTF-8?q?perf(hamster-core-bukkit):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hamster-core-bukkit/build.gradle | 33 +++++++++---------- .../core/bukkit/util/CoreBukkitUtils.java | 29 ++++------------ .../src/main/resources/plugin.yml | 2 +- 3 files changed, 24 insertions(+), 40 deletions(-) diff --git a/hamster-core-bukkit/build.gradle b/hamster-core-bukkit/build.gradle index 327c526..659b702 100644 --- a/hamster-core-bukkit/build.gradle +++ b/hamster-core-bukkit/build.gradle @@ -1,3 +1,6 @@ +plugins { + id 'com.github.johnrengelman.shadow' version '7.1.2' +} setArchivesBaseName("HamsterCore-Bukkit") evaluationDependsOn(':hamster-core-common') @@ -10,20 +13,17 @@ dependencies { api project(":hamster-core-common") transitive false shade project(":hamster-core-common") transitive false + implementation "de.tr7zw:item-nbt-api:2.11.2" + //noinspection VulnerableLibrariesLocal compileOnly 'org.spigotmc:spigot-api:1.19.4-R0.1-SNAPSHOT' exclude group: "com.google.code.gson" compileOnly 'net.milkbowl.vault:VaultAPI:1.7' transitive false compileOnly 'org.black_ixx:playerpoints:2.1.3' transitive false - compileOnly "com.comphenix.protocol:ProtocolLib-API:4.4.0" transitive false - - // 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 api "net.kyori:adventure-platform-bukkit:${adventure_version}" + //noinspection VulnerableLibrariesLocal oldJar "net.kyori:adventure-platform-bukkit:${adventure_version}" // https://mvnrepository.com/artifact/net.kyori/adventure-text-minimessage api "net.kyori:adventure-text-minimessage:${adventure_serializer_version}" @@ -52,18 +52,17 @@ processResources { } } - tasks.compileJava.dependsOn(":hamster-core-common:build") -tasks.register("shadowJar", Jar) { - dependsOn("jar") - from([ - tasks.jar.outputs.files.collect { - it.isDirectory() ? it : zipTree(it) - }, - configurations.shade.collect { - it.isDirectory() ? it : zipTree(it) - } - ]) +shadowJar { + //noinspection GrDeprecatedAPIUsage + classifier = "" + dependencies { + exclude(dependency { + it.moduleGroup != 'cn.hamster3.mc.plugin.core' && it.moduleGroup != 'de.tr7zw' + }) + } + relocate 'de.tr7zw.changeme.nbtapi', 'cn.hamster3.mc.plugin.core.bukkit.nbtapi' + relocate 'de.tr7zw.annotations', 'cn.hamster3.mc.plugin.core.bukkit.nbtapi.annotations' destinationDir(getRootProject().buildDir) } tasks.build.dependsOn(shadowJar) diff --git a/hamster-core-bukkit/src/main/java/cn/hamster3/mc/plugin/core/bukkit/util/CoreBukkitUtils.java b/hamster-core-bukkit/src/main/java/cn/hamster3/mc/plugin/core/bukkit/util/CoreBukkitUtils.java index 188a462..0c9d23d 100644 --- a/hamster-core-bukkit/src/main/java/cn/hamster3/mc/plugin/core/bukkit/util/CoreBukkitUtils.java +++ b/hamster-core-bukkit/src/main/java/cn/hamster3/mc/plugin/core/bukkit/util/CoreBukkitUtils.java @@ -2,8 +2,9 @@ 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.JsonObject; +import de.tr7zw.changeme.nbtapi.NBTContainer; +import de.tr7zw.changeme.nbtapi.NBTItem; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.OfflinePlayer; @@ -236,36 +237,20 @@ public final class CoreBukkitUtils { @NotNull public static String serializeItemStack(@NotNull ItemStack stack) { - if (!Bukkit.getPluginManager().isPluginEnabled("ProtocolLib")) { - throw new RuntimeException("ProtocolLib 前置插件未启用, 无法序列化物品!"); - } - try { - return StreamSerializer.getDefault().serializeItemStack(stack); - } catch (IOException e) { - throw new RuntimeException("序列化物品时出现了一个异常!", e); - } + NBTContainer container = NBTItem.convertItemtoNBT(stack); + return container.toString(); } @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); - } + return NBTItem.convertNBTtoItem(new NBTContainer(s)); } @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 NBTItem.convertNBTtoItem(new NBTContainer(s)); + } catch (Exception e) { return defaultValue; } } diff --git a/hamster-core-bukkit/src/main/resources/plugin.yml b/hamster-core-bukkit/src/main/resources/plugin.yml index c1af32c..9844ef6 100644 --- a/hamster-core-bukkit/src/main/resources/plugin.yml +++ b/hamster-core-bukkit/src/main/resources/plugin.yml @@ -12,7 +12,7 @@ load: STARTUP softdepend: - Vault - PlayerPoints - - ProtocolLib + - NBTAPI loadbefore: - HamsterAPI