如何反序列表播放器的广告资源?

时间:2014-09-14 22:46:33

标签: java minecraft bukkit

这是我的序列化方法。我将如何在以后进行反序列化/加载?

invs是我的inventories.yml FileConfiguration变量。

public void action(Player p){
    PlayerInventory i = p.getInventory();
    int slot = 0;
    for(ItemStack item : i){
        Map<String, Object> itemS = item.serialize();
        if(Main.invs.get(p.getName() + ".inventory.slot." + slot) == null){
            Main.invs.createSection(p.getName()+ ".inventory.slot." + slot);
        }
        Main.invs.set(p.getName() + ".inventory.slot." + slot, itemS);
        slot = slot + 1;
    }
    slot = 0;
}

1 个答案:

答案 0 :(得分:1)

试试这个:

public PlayerInventory deserializeInventory(Player p) {
    PlayerInventory inv = p.getInventory();
    for(int slot = 0; slot < 36 /*Size of inventory */; slot++){
        //Removes any existing item from the inventory.
        inv.clear(slot);

        Object itemTemp = Main.invs.get(p.getName() + ".inventory.slot." + slot);
        if (itemTemp == null) { //Skip null values.
            continue;
        }
        if (!(itemTemp instanceof ItemStack)) {
            //Might want to do an error message, but for now just ignore this.
            continue;
        }
        ItemStack item = (ItemStack) itemTemp;
        inv.setItem(slot, item);
    }
    return inv;
}

作为旁注,我强烈建议您将序列化方法更改为:

public void action(Player p){
    PlayerInventory i = p.getInventory();
    for(int slot = 0; slot < 36 /*Size of inventory */; slot++){
        ItemStack item = i.getItem(slot);
        if (item == null || item.getType() == Material.AIR) { //Do nothing.
            continue;
        }
        Map<String, Object> itemS = item.serialize();
        if(Main.invs.get(p.getName() + ".inventory.slot." + slot) == null){
            Main.invs.createSection(p.getName()+ ".inventory.slot." + slot);
        }
        Main.invs.set(p.getName() + ".inventory.slot." + slot, itemS);
    }
}

执行此操作将保留项目的位置,并添加一些错误检查。