feat: 添加更多工具方法

This commit is contained in:
2022-11-04 00:25:30 +08:00
parent 7d96ce0e41
commit a78b0709c6
2 changed files with 93 additions and 43 deletions

View File

@@ -1,9 +1,6 @@
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;
@@ -15,11 +12,8 @@ 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 {
@@ -137,7 +131,7 @@ public final class BukkitUtils {
* @return 是否为空 * @return 是否为空
*/ */
public static boolean isEmptyItemStack(ItemStack stack) { public static boolean isEmptyItemStack(ItemStack stack) {
return stack == null || stack.getType() == Material.AIR || stack.getAmount() < 1; return stack == null || stack.getAmount() < 1 || stack.getType() == Material.AIR;
} }
/** /**
@@ -213,40 +207,4 @@ 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;
}
}
} }

View File

@@ -0,0 +1,92 @@
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 SerializeUtils {
private SerializeUtils() {
}
@Nullable
public static String serializeItemStack(@Nullable ItemStack stack) {
if (!Bukkit.getPluginManager().isPluginEnabled("ProtocolLib")) {
HamsterCorePlugin.getInstance().getLogger().warning("ProtocolLib 前置插件未启用, 无法序列化物品!");
return null;
}
if (BukkitUtils.isEmptyItemStack(stack)) {
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(@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;
}
}
@Nullable
public static JsonObject serializePotionEffect(@NotNull PotionEffect effect) {
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(@NotNull JsonElement element) {
if (!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;
}
}
}