feat: 添加更多工具方法
This commit is contained in:
@@ -8,7 +8,6 @@ import org.bukkit.Bukkit;
|
|||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.potion.PotionEffect;
|
import org.bukkit.potion.PotionEffect;
|
||||||
import org.bukkit.potion.PotionEffectType;
|
import org.bukkit.potion.PotionEffectType;
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -21,11 +20,11 @@ public final class BukkitSerializeUtils {
|
|||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public static String serializeItemStack(@Nullable ItemStack stack) {
|
public static String serializeItemStack(@Nullable ItemStack stack) {
|
||||||
if (!Bukkit.getPluginManager().isPluginEnabled("ProtocolLib")) {
|
if (BukkitUtils.isEmptyItemStack(stack)) {
|
||||||
HamsterCorePlugin.getInstance().getLogger().warning("ProtocolLib 前置插件未启用, 无法序列化物品!");
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (BukkitUtils.isEmptyItemStack(stack)) {
|
if (!Bukkit.getPluginManager().isPluginEnabled("ProtocolLib")) {
|
||||||
|
HamsterCorePlugin.getInstance().getLogger().warning("ProtocolLib 前置插件未启用, 无法序列化物品!");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
@@ -37,28 +36,27 @@ public final class BukkitSerializeUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public static ItemStack deserializeItemStack(@NotNull JsonElement element) {
|
public static ItemStack deserializeItemStack(@Nullable String s) {
|
||||||
|
if (s == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
if (!Bukkit.getPluginManager().isPluginEnabled("ProtocolLib")) {
|
if (!Bukkit.getPluginManager().isPluginEnabled("ProtocolLib")) {
|
||||||
HamsterCorePlugin.getInstance().getLogger().warning("ProtocolLib 前置插件未启用, 无法反序列化物品!");
|
HamsterCorePlugin.getInstance().getLogger().warning("ProtocolLib 前置插件未启用, 无法反序列化物品!");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
if (element.isJsonNull()) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
String s = element.getAsString();
|
|
||||||
if (s == null || s.length() < 1) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return StreamSerializer.getDefault().deserializeItemStack(s);
|
return StreamSerializer.getDefault().deserializeItemStack(s);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
HamsterCorePlugin.getInstance().getLogger().log(Level.WARNING, "反序列化物品 " + element + " 时出错!", e);
|
HamsterCorePlugin.getInstance().getLogger().log(Level.WARNING, "反序列化物品 " + s + " 时出错!", e);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public static JsonObject serializePotionEffect(@NotNull PotionEffect effect) {
|
public static JsonObject serializePotionEffect(@Nullable PotionEffect effect) {
|
||||||
|
if (effect == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
JsonObject object = new JsonObject();
|
JsonObject object = new JsonObject();
|
||||||
object.addProperty("type", effect.getType().getName());
|
object.addProperty("type", effect.getType().getName());
|
||||||
@@ -72,8 +70,8 @@ public final class BukkitSerializeUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public static PotionEffect deserializePotionEffect(@NotNull JsonElement element) {
|
public static PotionEffect deserializePotionEffect(@Nullable JsonElement element) {
|
||||||
if (!element.isJsonObject()) {
|
if (element == null || !element.isJsonObject()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
JsonObject effectObject = element.getAsJsonObject();
|
JsonObject effectObject = element.getAsJsonObject();
|
||||||
|
Reference in New Issue
Block a user