2014-09-14 2 views
0

다음은 내 직렬화 코드입니다. 나중에 다시로드하려면 어떻게해야합니까?플레이어의 인벤토리를 비 시리얼 화하는 방법

  • invs은 my inventories.ymlFileConfiguration입니다.
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

이 시도 :의 위치를 ​​유지합니다

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); 
    } 
} 

이렇게 :

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; 
} 

는 측면 참고로, 난 강력이에 직렬화 방법을 변경하는 것이 좋습니다 항목을 검사하고 몇 가지 오류 검사 항목을 추가합니다.

+0

감사합니다. 지금 당장이 작업을 수행하고 있습니다. – ffxhand

+0

나는 실제로 || (OR 비교) 부분이 효과가 있다고 생각하십니까? http://pastebin.com/UjZkLeHK – ffxhand

+1

정상적으로 작동하며 수정하려면 게시물을 수정하고 있습니다. 내가 어떻게 망쳤는지 전혀 모르겠다. 부분이지만 분명히 실수입니다. – Pokechu22

관련 문제