Java 如何反序列化玩家';什么是库存?

Java 如何反序列化玩家';什么是库存?,java,minecraft,bukkit,Java,Minecraft,Bukkit,这是我的序列化方法。我以后如何进行反序列化/加载 invs是我的库存.yml文件配置变量 公开作废操作(玩家p){ PlayerInventory i=p.getInventory(); int槽=0; 对于(ItemStack项:i){ 映射项=项。序列化(); if(Main.invs.get(p.getName()+”.inventory.slot.“+slot)==null){ Main.invs.createSection(p.getName()+”.inventory.slot.“+

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

invs
是我的
库存.yml
文件配置
变量

公开作废操作(玩家p){
PlayerInventory i=p.getInventory();
int槽=0;
对于(ItemStack项:i){
映射项=项。序列化();
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);
槽=槽+1;
}
槽=0;
}
试试这个:

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 Player库存反序列化库存(Player p){
PlayerInventory inv=p.getInventory();
对于(int slot=0;slot<36/*库存大小*/;slot++){
//从库存中删除任何现有项目。
库存清除(插槽);
objectitemtemp=Main.invs.get(p.getName()+”.inventory.slot.+slot);
如果(itemTemp==null){//跳过null值。
继续;
}
如果(!(itemTemp instanceof ItemStack)){
//可能需要发出错误消息,但现在请忽略此操作。
继续;
}
ItemStack item=(ItemStack)itemTemp;
库存设置项目(插槽、项目);
}
退货库存;
}
作为补充说明,我强烈建议将序列化方法更改为:

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);
    }
}
公开作废操作(玩家p){
PlayerInventory i=p.getInventory();
对于(int slot=0;slot<36/*库存大小*/;slot++){
ItemStack item=i.getItem(插槽);
如果(item==null | | item.getType()==Material.AIR){//什么也不做。
继续;
}
映射项=项。序列化();
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);
}
}

这样做将保留项目的位置,并添加一些错误检查内容。

我实际上在| |(或比较)部分遇到错误,您认为这样做可行吗?这应该行得通,我正在编辑我的帖子来修复它。我不知道我是怎么搞砸了那部分,但这显然是个错误。
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);
    }
}