28 oct. 2016, 22:48

J’ai un petit soucis : j’ai créer un item de type sac pour stocker des items mais il sauvegarde pas les items 😕

public class InventoryItemBag implements IInventory
{
public ItemStack[] inventorySlots;
public int inventoryStackLimit = 64;
/** Use only a multiple of 9 (27, 54, 81, …) */
public int inventorySize = 54;

public InventoryItemBag(ItemStack container, int size)
{
this.inventorySize = size;
this.inventorySlots = new ItemStack;

if (!container.hasTagCompound())
{
container.setTagCompound(new NBTTagCompound());
}

this.readFromNBT(container.getTagCompound());
}

public void readFromNBT(NBTTagCompound comp)
{
NBTTagList nbtlist = comp.getTagList("Inventory", Constants.NBT.TAG_COMPOUND);

for (int i = 0; i < nbtlist.tagCount(); i++)
{
NBTTagCompound comp1 = nbtlist.getCompoundTagAt(i);
int slot = comp1.getInteger("Slot");
this.inventorySlots[slot] = ItemStack.loadItemStackFromNBT(comp1);
}
}

public void writeToNBT(NBTTagCompound comp)
{
NBTTagList nbtlist = new NBTTagList();

for (int i = 0; i < this.inventorySize; i++)
{
if (this.inventorySlots != null)
{
NBTTagCompound comp1 = new NBTTagCompound();
comp1.setInteger("Slot", i);
this.inventorySlots.writeToNBT(comp1);
nbtlist.appendTag(comp1);
}
}
comp.setTag("Inventory", nbtlist);
}

@Override
public int getSizeInventory()
{
return this.inventorySize;
}

@Nullable
@Override
public ItemStack getStackInSlot(int index)
{
return this.inventorySlots[index];
}

@Nullable
@Override
public ItemStack decrStackSize(int index, int count)
{
return null;
}

@Nullable
@Override
public ItemStack removeStackFromSlot(int index)
{
return null;
}

@Override
public void setInventorySlotContents(int index, @Nullable ItemStack stack)
{

}

@Override
public int getInventoryStackLimit()
{
return this.inventoryStackLimit;
}

@Override
public void markDirty()
{

}

@Override
public boolean isUseableByPlayer(EntityPlayer player)
{
return true;
}

@Override
public void openInventory(EntityPlayer player)
{

}

@Override
public void closeInventory(EntityPlayer player)
{

}

@Override
public boolean isItemValidForSlot(int index, ItemStack stack)
{
return !(stack.getItem() instanceof ItemBag);
}

@Override
public int getField(int id)
{
return 0;
}

@Override
public void setField(int id, int value)
{

}

@Override
public int getFieldCount()
{
return 0;
}

@Override
public void clear()
{

}

@Override
public String getName()
{
return "item.container.bag.gui";
}

@Override
public boolean hasCustomName()
{
return false;
}

@Override
public ITextComponent getDisplayName()
{
return null;
}