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);
|
||||
}
|
||||
|
||||
|
@@ -5,45 +5,36 @@ import com.google.gson.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public interface CoreConstantObjects {
|
||||
public abstract class CoreConstantObjects {
|
||||
/**
|
||||
* Minecraft 默认指定的空 UUID
|
||||
* 异步线程
|
||||
*/
|
||||
UUID NIL_UUID = new UUID(0L, 0L);
|
||||
|
||||
public static final ExecutorService WORKER_EXECUTOR = Executors.newCachedThreadPool(new NamedThreadFactory("HamsterCore - Executor"));
|
||||
/**
|
||||
* 调度器线程
|
||||
*/
|
||||
public static final ScheduledExecutorService SCHEDULED_EXECUTOR = Executors
|
||||
.newScheduledThreadPool(1, new NamedThreadFactory("HamsterCore - Scheduler"));
|
||||
/**
|
||||
* GSON 工具
|
||||
*/
|
||||
Gson GSON = new GsonBuilder()
|
||||
public static Gson GSON = new GsonBuilder()
|
||||
.registerTypeAdapter(DisplayMessage.class, MessageTypeAdapter.INSTANCE)
|
||||
.create();
|
||||
|
||||
/**
|
||||
* GSON 工具,会使用格式化输出、且解析中包含null参数
|
||||
*/
|
||||
Gson GSON_HUMAN = new GsonBuilder()
|
||||
public static Gson GSON_HUMAN = new GsonBuilder()
|
||||
.registerTypeAdapter(DisplayMessage.class, MessageTypeAdapter.INSTANCE)
|
||||
.serializeNulls()
|
||||
.setPrettyPrinting()
|
||||
.create();
|
||||
|
||||
/**
|
||||
* 异步线程
|
||||
*/
|
||||
ExecutorService WORKER_EXECUTOR = Executors.newCachedThreadPool(new NamedThreadFactory("HamsterCore - Executor"));
|
||||
/**
|
||||
* 调度器线程
|
||||
*/
|
||||
ScheduledExecutorService SCHEDULED_EXECUTOR = Executors
|
||||
.newScheduledThreadPool(1, new NamedThreadFactory("HamsterCore - Scheduler"));
|
||||
|
||||
}
|
||||
|
||||
class NamedThreadFactory implements ThreadFactory {
|
||||
|
Reference in New Issue
Block a user