feat: 为 GSON 工具增加 ItemStack 序列化功能
This commit is contained in:
@@ -9,11 +9,15 @@ import cn.hamster3.mc.plugin.core.bukkit.hook.VaultAPI;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.listener.CallbackListener;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.listener.DebugListener;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.page.listener.PageListener;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.util.BukkitSerializeUtils;
|
||||
import cn.hamster3.mc.plugin.core.common.constant.CoreConstantObjects;
|
||||
import com.google.gson.*;
|
||||
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class HamsterCorePlugin extends JavaPlugin {
|
||||
@@ -38,6 +42,9 @@ public class HamsterCorePlugin extends JavaPlugin {
|
||||
saveDefaultConfig();
|
||||
reloadConfig();
|
||||
logger.info("已读取配置文件.");
|
||||
CoreConstantObjects.GSON = CoreConstantObjects.GSON.newBuilder()
|
||||
.registerTypeAdapter(ItemStack.class, ItemStackAdapter.INSTANCE)
|
||||
.create();
|
||||
CoreBukkitAPI.init();
|
||||
logger.info("已初始化 CoreBukkitAPI.");
|
||||
CoreMessage.init(this);
|
||||
@@ -86,3 +93,27 @@ public class HamsterCorePlugin extends JavaPlugin {
|
||||
return audienceProvider;
|
||||
}
|
||||
}
|
||||
|
||||
class ItemStackAdapter implements JsonSerializer<ItemStack>, JsonDeserializer<ItemStack> {
|
||||
public static final ItemStackAdapter INSTANCE = new ItemStackAdapter();
|
||||
|
||||
private ItemStackAdapter() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||
if (json.isJsonNull()) {
|
||||
return null;
|
||||
}
|
||||
return BukkitSerializeUtils.deserializeItemStack(json.getAsString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(ItemStack src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
String s = BukkitSerializeUtils.serializeItemStack(src);
|
||||
if (s == null) {
|
||||
return JsonNull.INSTANCE;
|
||||
}
|
||||
return new JsonPrimitive(s);
|
||||
}
|
||||
}
|
@@ -169,8 +169,7 @@ public class ButtonGroup {
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof ButtonGroup)) return false;
|
||||
ButtonGroup that = (ButtonGroup) o;
|
||||
if (!(o instanceof ButtonGroup that)) return false;
|
||||
return name.equals(that.name);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user