perf(hamster-core-bukkit): 优化代码

This commit is contained in:
2023-06-05 17:21:18 +08:00
parent 32833879eb
commit 973cfa6896
3 changed files with 24 additions and 40 deletions

View File

@@ -1,3 +1,6 @@
plugins {
id 'com.github.johnrengelman.shadow' version '7.1.2'
}
setArchivesBaseName("HamsterCore-Bukkit") setArchivesBaseName("HamsterCore-Bukkit")
evaluationDependsOn(':hamster-core-common') evaluationDependsOn(':hamster-core-common')
@@ -10,20 +13,17 @@ dependencies {
api project(":hamster-core-common") transitive false api project(":hamster-core-common") transitive false
shade project(":hamster-core-common") transitive false shade project(":hamster-core-common") transitive false
implementation "de.tr7zw:item-nbt-api:2.11.2"
//noinspection VulnerableLibrariesLocal //noinspection VulnerableLibrariesLocal
compileOnly 'org.spigotmc:spigot-api:1.19.4-R0.1-SNAPSHOT' exclude group: "com.google.code.gson" 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 'net.milkbowl.vault:VaultAPI:1.7' transitive false
compileOnly 'org.black_ixx:playerpoints:2.1.3' 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 // https://mvnrepository.com/artifact/net.kyori/adventure-platform-bukkit
api "net.kyori:adventure-platform-bukkit:${adventure_version}" api "net.kyori:adventure-platform-bukkit:${adventure_version}"
//noinspection VulnerableLibrariesLocal
oldJar "net.kyori:adventure-platform-bukkit:${adventure_version}" oldJar "net.kyori:adventure-platform-bukkit:${adventure_version}"
// https://mvnrepository.com/artifact/net.kyori/adventure-text-minimessage // https://mvnrepository.com/artifact/net.kyori/adventure-text-minimessage
api "net.kyori:adventure-text-minimessage:${adventure_serializer_version}" api "net.kyori:adventure-text-minimessage:${adventure_serializer_version}"
@@ -52,18 +52,17 @@ processResources {
} }
} }
tasks.compileJava.dependsOn(":hamster-core-common:build") tasks.compileJava.dependsOn(":hamster-core-common:build")
tasks.register("shadowJar", Jar) { shadowJar {
dependsOn("jar") //noinspection GrDeprecatedAPIUsage
from([ classifier = ""
tasks.jar.outputs.files.collect { dependencies {
it.isDirectory() ? it : zipTree(it) exclude(dependency {
}, it.moduleGroup != 'cn.hamster3.mc.plugin.core' && it.moduleGroup != 'de.tr7zw'
configurations.shade.collect { })
it.isDirectory() ? it : zipTree(it)
} }
]) 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) destinationDir(getRootProject().buildDir)
} }
tasks.build.dependsOn(shadowJar) tasks.build.dependsOn(shadowJar)

View File

@@ -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.bukkit.listener.CallbackListener;
import cn.hamster3.mc.plugin.core.common.data.DisplayMessage; import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
import com.comphenix.protocol.utility.StreamSerializer;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import de.tr7zw.changeme.nbtapi.NBTContainer;
import de.tr7zw.changeme.nbtapi.NBTItem;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
@@ -236,36 +237,20 @@ public final class CoreBukkitUtils {
@NotNull @NotNull
public static String serializeItemStack(@NotNull ItemStack stack) { public static String serializeItemStack(@NotNull ItemStack stack) {
if (!Bukkit.getPluginManager().isPluginEnabled("ProtocolLib")) { NBTContainer container = NBTItem.convertItemtoNBT(stack);
throw new RuntimeException("ProtocolLib 前置插件未启用, 无法序列化物品!"); return container.toString();
}
try {
return StreamSerializer.getDefault().serializeItemStack(stack);
} catch (IOException e) {
throw new RuntimeException("序列化物品时出现了一个异常!", e);
}
} }
@NotNull @NotNull
public static ItemStack deserializeItemStack(@NotNull String s) { public static ItemStack deserializeItemStack(@NotNull String s) {
if (!Bukkit.getPluginManager().isPluginEnabled("ProtocolLib")) { return NBTItem.convertNBTtoItem(new NBTContainer(s));
throw new RuntimeException("ProtocolLib 前置插件未启用, 无法反序列化物品!");
}
try {
return StreamSerializer.getDefault().deserializeItemStack(s);
} catch (IOException e) {
throw new RuntimeException("反序列化物品时出现了一个异常!", e);
}
} }
@NotNull @NotNull
public static ItemStack deserializeItemStack(@NotNull String s, @NotNull ItemStack defaultValue) { public static ItemStack deserializeItemStack(@NotNull String s, @NotNull ItemStack defaultValue) {
if (!Bukkit.getPluginManager().isPluginEnabled("ProtocolLib")) {
return defaultValue;
}
try { try {
return StreamSerializer.getDefault().deserializeItemStack(s); return NBTItem.convertNBTtoItem(new NBTContainer(s));
} catch (IOException e) { } catch (Exception e) {
return defaultValue; return defaultValue;
} }
} }

View File

@@ -12,7 +12,7 @@ load: STARTUP
softdepend: softdepend:
- Vault - Vault
- PlayerPoints - PlayerPoints
- ProtocolLib - NBTAPI
loadbefore: loadbefore:
- HamsterAPI - HamsterAPI