feat: 添加更多工具方法
This commit is contained in:
@@ -30,8 +30,8 @@ subprojects {
|
|||||||
}
|
}
|
||||||
|
|
||||||
java {
|
java {
|
||||||
sourceCompatibility = JavaVersion.VERSION_1_8
|
sourceCompatibility = JavaVersion.VERSION_17
|
||||||
targetCompatibility = JavaVersion.VERSION_1_8
|
targetCompatibility = JavaVersion.VERSION_17
|
||||||
// withJavadocJar()
|
// withJavadocJar()
|
||||||
withSourcesJar()
|
withSourcesJar()
|
||||||
}
|
}
|
||||||
|
@@ -9,6 +9,7 @@ dependencies {
|
|||||||
|
|
||||||
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/net.kyori/adventure-platform-bukkit
|
// https://mvnrepository.com/artifact/net.kyori/adventure-platform-bukkit
|
||||||
api 'net.kyori:adventure-platform-bukkit:4.1.2'
|
api 'net.kyori:adventure-platform-bukkit:4.1.2'
|
||||||
|
@@ -1,6 +1,9 @@
|
|||||||
package cn.hamster3.mc.plugin.core.bukkit.util;
|
package cn.hamster3.mc.plugin.core.bukkit.util;
|
||||||
|
|
||||||
|
import cn.hamster3.mc.plugin.core.bukkit.HamsterCorePlugin;
|
||||||
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.JsonElement;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.OfflinePlayer;
|
import org.bukkit.OfflinePlayer;
|
||||||
@@ -12,8 +15,11 @@ import org.bukkit.inventory.ItemStack;
|
|||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
import org.bukkit.inventory.meta.SkullMeta;
|
import org.bukkit.inventory.meta.SkullMeta;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
public final class BukkitUtils {
|
public final class BukkitUtils {
|
||||||
@@ -30,6 +36,7 @@ public final class BukkitUtils {
|
|||||||
return Bukkit.getServer().getClass().getName().split("\\.")[3];
|
return Bukkit.getServer().getClass().getName().split("\\.")[3];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
@NotNull
|
@NotNull
|
||||||
public static Package getNMSPackage() {
|
public static Package getNMSPackage() {
|
||||||
String nmsVersion = getNMSVersion();
|
String nmsVersion = getNMSVersion();
|
||||||
@@ -206,4 +213,40 @@ public final class BukkitUtils {
|
|||||||
}
|
}
|
||||||
return displayMessage;
|
return displayMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String serializeItemStack(@Nullable ItemStack stack) {
|
||||||
|
if (!Bukkit.getPluginManager().isPluginEnabled("ProtocolLib")) {
|
||||||
|
HamsterCorePlugin.getInstance().getLogger().warning("ProtocolLib 前置插件未启用, 无法序列化物品!");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (isEmptyItemStack(stack)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return StreamSerializer.getDefault().serializeItemStack(stack);
|
||||||
|
} catch (IOException e) {
|
||||||
|
HamsterCorePlugin.getInstance().getLogger().log(Level.WARNING, "序列化物品 " + stack + " 时出错!", e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ItemStack deserializeItemStack(@NotNull JsonElement element) {
|
||||||
|
if (!Bukkit.getPluginManager().isPluginEnabled("ProtocolLib")) {
|
||||||
|
HamsterCorePlugin.getInstance().getLogger().warning("ProtocolLib 前置插件未启用, 无法反序列化物品!");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (element.isJsonNull()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
String s = element.getAsString();
|
||||||
|
if (s == null || s.length() < 1) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return StreamSerializer.getDefault().deserializeItemStack(s);
|
||||||
|
} catch (Exception e) {
|
||||||
|
HamsterCorePlugin.getInstance().getLogger().log(Level.WARNING, "反序列化物品 " + element + " 时出错!", e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -12,6 +12,7 @@ load: STARTUP
|
|||||||
softdepend:
|
softdepend:
|
||||||
- Vault
|
- Vault
|
||||||
- PlayerPoints
|
- PlayerPoints
|
||||||
|
- ProtocolLib
|
||||||
|
|
||||||
loadbefore:
|
loadbefore:
|
||||||
- HamsterAPI
|
- HamsterAPI
|
||||||
|
Reference in New Issue
Block a user