错误“由:java.lang.StackOverflowError引起”正在调试我的Minecraft Java插件

时间:2019-07-03 22:08:08

标签: java serialization plugins deserialization minecraft

好吧,我正在努力让这个坏男孩: https://cdn.discordapp.com/attachments/545206963185647637/596098096853942272/javaw_E4cv3bfjGE.png

变成一个项目:

https://cdn.discordapp.com/attachments/545206963185647637/596098385040113673/javaw_sorHX2JM5s.png

这样做,市民将消失并变成物品。该项目是一个ItemStack,然后对其进行序列化(下面的查看类),然后将其添加到.json文件中,用户的UUID将附加到序列化的ItemStack上。然后,当用户右键单击该项目时,我可以对该项目进行反序列化,并检查其是否与任何经过反序列化处理的项目相对应。

项目控制器(只是一个用于初始化它的类)

public class ItemController {

    public ItemController(){

    }

    public void giveItem(Player player, NPC npc){
        ItemStack item = ItemBuilder.buildItem(Material.NETHER_STAR,
                ChatUtil.colorize("&6&lGuard Skull"),
                1,
                ChatUtil.colorize(
                        "&6&lID: &e" + npc.getId() + "\n" +
                        "&6&lOwned by:&e " + player.getName() + "\n" +
                        "&6&lName: &e" + npc.getFullName()
                )
        );
        item.setDurability((short) npc.getId());
        player.getInventory().addItem(item);
        HandlerManager.getSerializer().addItem(player, item);
        HandlerManager.getSerializer().disinit();
        HandlerManager.getSerializer().init();
        GuardNPC.getInstance().getItemLoggerConfig().save();
    }
}
public class ItemList {
    @Getter Player player;

    public ItemList(Player player){
        this.player = player;
    }

    @Getter private HashMap<Player, String> serialzedItems;
    @Getter private HashMap<Player, ItemStack> items;
    @Getter private HashMap<Player, ItemStack> allitems;

    public ItemList serialize() {
        if (serialzedItems == null)
            serialzedItems = new HashMap<>();
        items.entrySet().forEach(e -> {
            serialzedItems.put(e.getKey(), ItemSerializer.itemStackToBase64(e.getValue()));
        });
        return this;
    }

    public ItemList deserialize() {
        if (items == null)
            items = new HashMap<>();
        if (serialzedItems != null && !serialzedItems.isEmpty()) {
            serialzedItems.entrySet().forEach(entry -> items.put(entry.getKey(), ItemSerializer.itemStackFromBase64(entry.getValue())));
        }
        return this;
    }

    public void addItem(Player player, ItemStack item){
        if (items == null)
            items = new HashMap<>();
        if (serialzedItems == null)
            serialzedItems = new HashMap<>();
        items.put(player, item);
        serialzedItems.put(player, ItemSerializer.itemStackToBase64(item));
    }
    public void removeItem(Player player, ItemStack item){
        items.remove(item, player);
        serialzedItems.remove(player, ItemSerializer.itemStackToBase64(item));
    }

}


ItemList的处理程序

public class Serializer extends Handler{

    public Serializer() {
        super(false);
    }
    private List<ItemList> items;
    public void init(){
        items = GsonUtil.loadObject(new TypeToken<List<ItemList>>() {
        }, GuardNPC.getInstance().getItemLoggerConfig().getFile());
        if (items == null)
            items = new ArrayList<>();
        items.forEach(itemList -> itemList.deserialize());
    }
    public void disinit() {
        items.forEach(itemList -> itemList.serialize());
        GsonUtil.saveObject(items, GuardNPC.getInstance().getItemLoggerConfig().getFile());
    }

    public ItemList addItem(Player player, ItemStack item) {
        ItemList list;
        if (getItemList(player) == null){
            list = new ItemList(player);
        } else {
            list = getItemList(player);
        }
        list.addItem(player, item);
        items.add(list);

        return list;
    }
    public ItemList getItemList(Player player) {
        Optional<ItemList> o = items.stream().filter(itemList -> itemList.getPlayer().equals(player)).findAny();
        return o.isPresent() ? o.get() : null;
    }

}

GSon序列化实用程序:

public class GsonUtil {

    private static Gson gson = new GsonBuilder().setPrettyPrinting().create();

    public static void saveObject(Object object, File file) {
        try {
            Writer writer = new FileWriter(file);
            writer.write(gson.toJson(object));
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static <T> T loadObject(TypeToken<T> token, File file) {
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            return gson.fromJson(br, token.getType());
        } catch (IOException e) {
        }
        return null;
    }
}

点击NPC的事件


public class EventNPCPlayerRightClick implements Listener{

    @EventHandler
    public void onClick(NPCRightClickEvent e){
        Player player = e.getClicker();
        NPC npc = e.getNPC();
        int id = npc.getId();
        UUID uuid = player.getUniqueId();
        if(GuardNPC.getInstance().getNpcController().isOwner(player, npc)){
            npc.despawn();
            GuardNPC.getInstance().getItemController().giveItem(player, npc);
            return;
        } else if(GuardNPC.getInstance().getDebuggedPlayers().contains(player)){
            GuardNPC.getInstance().getNpcController().addNPC(npc, player);
            npc.despawn();
            GuardNPC.getInstance().getItemController().giveItem(player, npc);
            return;
        }


    }
}

错误:https://pastebin.com/M2TWv9NF

0 个答案:

没有答案