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

问题描述 投票:0回答:1

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

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;
}
java minecraft bukkit
1个回答
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);
    }
}

这样做会保留项目的位置,还会添加一些错误检查。

© www.soinside.com 2019 - 2024. All rights reserved.