InventorySerializer.java 文件源码

java
阅读 31 收藏 0 点赞 0 评论 0

项目:RPGInventory 作者:
static void savePlayer(PlayerWrapper playerWrapper, Path file) throws IOException {
    List<NbtCompound> slotList = new ArrayList<>();
    try (DataOutputStream dataOutput = new DataOutputStream(new GZIPOutputStream(Files.newOutputStream(file)))) {
        for (Slot slot : SlotManager.instance().getSlots()) {
            if (slot.getSlotType() == Slot.SlotType.ARMOR) {
                continue;
            }

            List<NbtCompound> itemList = new ArrayList<>();
            List<Integer> slotIds = slot.getSlotIds();
            Inventory inventory = playerWrapper.getInventory();
            for (int i = 0; i < slotIds.size(); i++) {
                int slotId = slotIds.get(i);
                ItemStack itemStack = inventory.getItem(slotId);
                if (!ItemUtils.isEmpty(itemStack) && !slot.isCup(itemStack)) {
                    itemList.add(ItemUtils.itemStackToNBT(itemStack, i + ""));
                }
            }

            if (itemList.size() > 0 || playerWrapper.isBuyedSlot(slot.getName())) {
                NbtCompound slotNbt = NbtFactory.ofCompound(slot.getName());
                slotNbt.put("type", slot.getSlotType().name());
                if (playerWrapper.isBuyedSlot(slot.getName())) {
                    slotNbt.put("buyed", "true");
                }
                slotNbt.put(NbtFactory.ofCompound("items", itemList));
                slotList.add(slotNbt);
            }
        }

        NbtCompound playerNbt = NbtFactory.ofCompound("Inventory");
        playerNbt.put(NbtFactory.ofCompound("slots", slotList));
        playerNbt.put("buyed-slots", playerWrapper.getBuyedGenericSlots());

        NbtBinarySerializer.DEFAULT.serialize(playerNbt, dataOutput);
    }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号