Gui/Container/Update Item Tick
-
ok merci je vois et pour le writeToNBT en gros quand j’ouvre le gui je peut y déposer les items il y reste, mais des que je le ferme il 'y sont plus
J’ai modif ma class IInventory
import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.FurnaceRecipes; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.tileentity.TileEntityFurnace; import net.minecraft.util.ActionResult; import net.minecraft.util.EnumActionResult; import net.minecraft.util.EnumHand; import net.minecraft.util.text.ITextComponent; import net.minecraft.world.World; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; import net.spyman.utils.SpyUtil; import javax.annotation.Nullable; public class ItemPortableFurnace extends Item implements IInventory { private ItemStack[] inventorySlots = new ItemStack[3]; public int workingTime = 0; public int workingTimeNeeded = 200; public int workingTimeNeededDefault = 200; public int burnTime = 0; public int burnTimeTotal = 0; public int inventorySize = inventorySlots.length; public int fuelSlot = 1; public int outputSlot = 2; public int inputSlot = 0; public ItemPortableFurnace() { this.setMaxStackSize(1); } @Override public ActionResult <itemstack>onItemRightClick(ItemStack itemStack, World world, EntityPlayer player, EnumHand hand) { player.openGui(SpyUtil.INSTANCE, 1, world, (int) player.posX, (int) player.posY, (int) player.posZ); return new ActionResult(EnumActionResult.PASS, itemStack); } public void readFromNBT(NBTTagCompound compound) { NBTTagList nbttaglist = compound.getTagList("SpyUtilsItems", 10); this.inventorySlots = new ItemStack[this.getSizeInventory()]; for(int i = 0; i < nbttaglist.tagCount(); ++i) { NBTTagCompound nbtTagCompound1 = nbttaglist.getCompoundTagAt(i); int j = nbtTagCompound1.getByte("Slot") & 255; if(j >= 0 && j < this.inventorySlots.length) { this.inventorySlots[j] = ItemStack.loadItemStackFromNBT(nbtTagCompound1); } } this.workingTime = compound.getShort("workingTime"); this.burnTime = compound.getShort("burnTime"); this.burnTimeTotal = compound.getShort("burnTimeTotal"); } public void writeToNBT(NBTTagCompound compound) { NBTTagList nbttaglist = new NBTTagList(); for(int i = 0; i < this.inventorySlots.length; ++i) { if(this.inventorySlots* != null) { NBTTagCompound nbtTagCompound1 = new NBTTagCompound(); nbtTagCompound1.setByte("Slot", (byte)i); this.inventorySlots*.writeToNBT(nbtTagCompound1); nbttaglist.appendTag(nbtTagCompound1); } } compound.setTag("SpyUtilsItems", nbttaglist); compound.setShort("workingTime", (short)this.workingTime); compound.setShort("burnTime", (short)this.burnTime); compound.setShort("burnTimeTotal", (short)this.burnTimeTotal); } @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 amount) { if(this.inventorySlots[index] != null) { ItemStack itemstack; if(this.inventorySlots[index].stackSize <= amount) { itemstack = this.inventorySlots[index]; this.inventorySlots[index] = null; this.markDirty(); return itemstack; } else { itemstack = this.inventorySlots[index].splitStack(amount); if(this.inventorySlots[index].stackSize == 0) { this.inventorySlots[index] = null; } this.markDirty(); return itemstack; } } else { return null; } } @Override public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) { if(this.burnTime > 0) { burnTime–; } if(this.canSmelt()) { if(this.burnTime <= 0) { int time = TileEntityFurnace.getItemBurnTime(inventorySlots[1]); if (time > 0) { this.decrStackSize(1, 1); this.burnTimeTotal = time; this.burnTime = time; } } if(burnTime > 0) { workingTime++; } } if (this.workingTime >= this.workingTimeNeeded) { this.smeltItem(); this.workingTime = 0; } if (!this.canSmelt() || burnTime <= 0) { this.workingTime = 0; } } @Nullable @Override public ItemStack removeStackFromSlot(int index) { return null; } @Override public void setInventorySlotContents(int index, @Nullable ItemStack stack) { this.inventorySlots[index] = stack; } @Override public int getInventoryStackLimit() { return 64; } @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 ItemPortableFurnace); } public boolean isBurning() { return this.workingTime > 0; } protected boolean canSmelt() { if(this.inventorySlots[this.inputSlot] == null) { return false; } else { ItemStack itemstack = FurnaceRecipes.instance().getSmeltingResult(inventorySlots[this.inputSlot]); if (itemstack == null) { return false; } if (this.inventorySlots[this.outputSlot] == null) { return true; } if (!this.inventorySlots[this.outputSlot].isItemEqual(itemstack)) { return false; } int result = inventorySlots[this.outputSlot].stackSize + itemstack.stackSize; return result <= getInventoryStackLimit() && result <= this.inventorySlots[2].getMaxStackSize(); } } public void smeltItem() { if (this.canSmelt()) { ItemStack itemstack = FurnaceRecipes.instance().getSmeltingResult(inventorySlots[this.inputSlot]); if (this.inventorySlots[this.outputSlot] == null) { this.inventorySlots[this.outputSlot] = itemstack.copy(); } else if (this.inventorySlots[this.outputSlot].getItem() == itemstack.getItem()) { this.inventorySlots[this.outputSlot].stackSize += itemstack.stackSize; } this.decrStackSize(0, 1); } } @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.portable_furnace.gui"; } @Override public boolean hasCustomName() { return false; } @Override public ITextComponent getDisplayName() { return null; } @SideOnly(Side.CLIENT) public int getSmeltProcess() { return this.workingTime * 24 / this.workingTimeNeeded; } @SideOnly(Side.CLIENT) public int getBurnTime() { return this.burnTime * 14 / this.burnTimeTotal; } } ```</itemstack> -
Je viens de remarque mais c’est normal que ça fonctionne pas ! Tout ce qui est en rapport avec l’inventaire de l’item doit aller dans une autre class car ça dépend de l’ItemStack et non de l’Item. Il faut que tu fasse une class pour ton IInventory et que quand on ouvre le gui, ça créé une nouvelle instance de cet inventaire à partir des NBT de l’itemStack et quand tu ferme l’inventaire, ça sauvegarde tout dans les NBTTag. Ps : tout est dans le tuto pour créer un backPack.
-
J’essayé les deux mais ça ne change pas grand chose
import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.FurnaceRecipes; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.tileentity.TileEntityFurnace; import net.minecraft.util.text.ITextComponent; import net.minecraft.world.World; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; import net.spyman.utils.common.items.ItemPortableFurnace; import javax.annotation.Nullable; public class InventoryItemPortableFurnace implements IInventory { private ItemStack[] inventorySlots = new ItemStack[3]; public int workingTime = 0; public int workingTimeNeeded = 200; public int workingTimeNeededDefault = 200; public int burnTime = 0; public int burnTimeTotal = 0; public int inventorySize = inventorySlots.length; public int fuelSlot = 1; public int outputSlot = 2; public int inputSlot = 0; public InventoryItemPortableFurnace(ItemStack itemStack) { if (!itemStack.hasTagCompound()) { itemStack.setTagCompound(new NBTTagCompound()); } this.readFromNBT(itemStack.getTagCompound()); } public void readFromNBT(NBTTagCompound compound) { NBTTagList nbttaglist = compound.getTagList("SpyUtilsItems", 10); this.inventorySlots = new ItemStack[this.getSizeInventory()]; for(int i = 0; i < nbttaglist.tagCount(); ++i) { NBTTagCompound nbtTagCompound1 = nbttaglist.getCompoundTagAt(i); int j = nbtTagCompound1.getByte("Slot") & 255; if(j >= 0 && j < this.inventorySlots.length) { this.inventorySlots[j] = ItemStack.loadItemStackFromNBT(nbtTagCompound1); } } this.workingTime = compound.getShort("workingTime"); this.burnTime = compound.getShort("burnTime"); this.burnTimeTotal = compound.getShort("burnTimeTotal"); } public void writeToNBT(NBTTagCompound compound) { System.out.println("WRITE TO NBT CALLED"); NBTTagList nbttaglist = new NBTTagList(); for(int i = 0; i < this.inventorySlots.length; ++i) { if(this.inventorySlots* != null) { NBTTagCompound nbtTagCompound1 = new NBTTagCompound(); nbtTagCompound1.setByte("Slot", (byte)i); this.inventorySlots*.writeToNBT(nbtTagCompound1); nbttaglist.appendTag(nbtTagCompound1); } } compound.setTag("SpyUtilsItems", nbttaglist); compound.setShort("workingTime", (short)this.workingTime); compound.setShort("burnTime", (short)this.burnTime); compound.setShort("burnTimeTotal", (short)this.burnTimeTotal); } @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 amount) { if(this.inventorySlots[index] != null) { ItemStack itemstack; if(this.inventorySlots[index].stackSize <= amount) { itemstack = this.inventorySlots[index]; this.inventorySlots[index] = null; this.markDirty(); return itemstack; } else { itemstack = this.inventorySlots[index].splitStack(amount); if(this.inventorySlots[index].stackSize == 0) { this.inventorySlots[index] = null; } this.markDirty(); return itemstack; } } else { return null; } } /** * Used to update item. * @param stack * @param worldIn * @param entityIn * @param itemSlot * @param isSelected */ public void updateItem(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) { if(this.burnTime > 0) { burnTime–; } if(this.canSmelt()) { if(this.burnTime <= 0) { int time = TileEntityFurnace.getItemBurnTime(inventorySlots[1]); if (time > 0) { this.decrStackSize(1, 1); this.burnTimeTotal = time; this.burnTime = time; } } if(burnTime > 0) { workingTime++; } } if (this.workingTime >= this.workingTimeNeeded) { this.smeltItem(); this.workingTime = 0; } if (!this.canSmelt() || burnTime <= 0) { this.workingTime = 0; } } @Nullable @Override public ItemStack removeStackFromSlot(int index) { return null; } @Override public void setInventorySlotContents(int index, @Nullable ItemStack stack) { this.inventorySlots[index] = stack; } @Override public int getInventoryStackLimit() { return 64; } @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 ItemPortableFurnace); } public boolean isBurning() { return this.workingTime > 0; } protected boolean canSmelt() { if(this.inventorySlots[this.inputSlot] == null) { return false; } else { ItemStack itemstack = FurnaceRecipes.instance().getSmeltingResult(inventorySlots[this.inputSlot]); if (itemstack == null) { return false; } if (this.inventorySlots[this.outputSlot] == null) { return true; } if (!this.inventorySlots[this.outputSlot].isItemEqual(itemstack)) { return false; } int result = inventorySlots[this.outputSlot].stackSize + itemstack.stackSize; return result <= getInventoryStackLimit() && result <= this.inventorySlots[2].getMaxStackSize(); } } public void smeltItem() { if (this.canSmelt()) { ItemStack itemstack = FurnaceRecipes.instance().getSmeltingResult(inventorySlots[this.inputSlot]); if (this.inventorySlots[this.outputSlot] == null) { this.inventorySlots[this.outputSlot] = itemstack.copy(); } else if (this.inventorySlots[this.outputSlot].getItem() == itemstack.getItem()) { this.inventorySlots[this.outputSlot].stackSize += itemstack.stackSize; } this.decrStackSize(0, 1); } } @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.portable_furnace.gui"; } @Override public boolean hasCustomName() { return false; } @Override public ITextComponent getDisplayName() { return null; } @SideOnly(Side.CLIENT) public int getSmeltProcess() { return this.workingTime * 24 / this.workingTimeNeeded; } @SideOnly(Side.CLIENT) public int getBurnTime() { return this.burnTime * 14 / this.burnTimeTotal; } } -
Est-ce que le read et le write sont appelés ?
-
Le read oui mais pas le write
où faut-il l’appeler déjà ?
normalement si je mais IInventory dans la class de l’item ce ne pause pas probleme? -
En 1.10, il me semble que c’est :
public NBTTagCompound writeToNBT(NBTTagCompound nbt) { }D’où l’utilité des annotations
-
Raté

Avec le override il ne la trouve pas non plus -

-
Il n’y a pas de méthode writeToNBT dans l’inverface IInventory.
Donc même avec un @Override tu ne vas rien trouver en effet.C’est à toi d’appeler la méthode.
-
Ah merde, j’aurai dû lire, excusez-moi, c’est pas de la classe TileEntity dont on parle
-
D’accord mais je l’appele ou dans closeInventory() ?
-
Oui.
-
Marche pas

import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.FurnaceRecipes; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.tileentity.TileEntityFurnace; import net.minecraft.util.text.ITextComponent; import net.minecraft.world.World; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; import net.spyman.utils.common.items.ItemPortableFurnace; import javax.annotation.Nullable; public class InventoryItemPortableFurnace implements IInventory { private ItemStack[] inventorySlots = new ItemStack[3]; public int workingTime = 0; public int workingTimeNeeded = 200; public int workingTimeNeededDefault = 200; public int burnTime = 0; public int burnTimeTotal = 0; public int inventorySize = inventorySlots.length; public int fuelSlot = 1; public int outputSlot = 2; public int inputSlot = 0; public InventoryItemPortableFurnace(ItemStack itemStack) { if (!itemStack.hasTagCompound()) { itemStack.setTagCompound(new NBTTagCompound()); } this.readFromNBT(itemStack.getTagCompound()); } public void readFromNBT(NBTTagCompound compound) { System.out.println("READ TO NBT"); NBTTagList nbttaglist = compound.getTagList("SpyUtilsItems", 10); this.inventorySlots = new ItemStack[this.getSizeInventory()]; for (int i = 0; i < nbttaglist.tagCount(); ++i) { NBTTagCompound nbtTagCompound1 = nbttaglist.getCompoundTagAt(i); int j = nbtTagCompound1.getByte("Slot") & 255; if (j >= 0 && j < this.inventorySlots.length) { this.inventorySlots[j] = ItemStack.loadItemStackFromNBT(nbtTagCompound1); } } this.workingTime = compound.getShort("workingTime"); this.burnTime = compound.getShort("burnTime"); this.burnTimeTotal = compound.getShort("burnTimeTotal"); } public NBTTagCompound writeToNBT(NBTTagCompound compound) { System.out.println("WRITE TO NBT"); NBTTagList nbttaglist = new NBTTagList(); for (int i = 0; i < this.inventorySlots.length; ++i) { if (this.inventorySlots* != null) { NBTTagCompound nbtTagCompound1 = new NBTTagCompound(); nbtTagCompound1.setByte("Slot", (byte) i); this.inventorySlots*.writeToNBT(nbtTagCompound1); nbttaglist.appendTag(nbtTagCompound1); } } compound.setTag("SpyUtilsItems", nbttaglist); compound.setShort("workingTime", (short) this.workingTime); compound.setShort("burnTime", (short) this.burnTime); compound.setShort("burnTimeTotal", (short) this.burnTimeTotal); return null; } @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 amount) { if (this.inventorySlots[index] != null) { ItemStack itemstack; if (this.inventorySlots[index].stackSize <= amount) { itemstack = this.inventorySlots[index]; this.inventorySlots[index] = null; this.markDirty(); return itemstack; } else { itemstack = this.inventorySlots[index].splitStack(amount); if (this.inventorySlots[index].stackSize == 0) { this.inventorySlots[index] = null; } this.markDirty(); return itemstack; } } else { return null; } } /** * Used to update item. * * @param stack * @param worldIn * @param entityIn * @param itemSlot * @param isSelected */ public void updateItem(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) { if (this.burnTime > 0) { burnTime–; } if (this.canSmelt()) { if (this.burnTime <= 0) { int time = TileEntityFurnace.getItemBurnTime(inventorySlots[1]); if (time > 0) { this.decrStackSize(1, 1); this.burnTimeTotal = time; this.burnTime = time; } } if (burnTime > 0) { workingTime++; } } if (this.workingTime >= this.workingTimeNeeded) { this.smeltItem(); this.workingTime = 0; } if (!this.canSmelt() || burnTime <= 0) { this.workingTime = 0; } } @Nullable @Override public ItemStack removeStackFromSlot(int index) { return null; } @Override public void setInventorySlotContents(int index, @Nullable ItemStack stack) { this.inventorySlots[index] = stack; } @Override public int getInventoryStackLimit() { return 64; } @Override public void markDirty() { } @Override public boolean isUseableByPlayer(EntityPlayer player) { return true; } @Override public void openInventory(EntityPlayer player) { } @Override public void closeInventory(EntityPlayer player) { this.writeToNBT(new NBTTagCompound()); } @Override public boolean isItemValidForSlot(int index, ItemStack stack) { return !(stack.getItem() instanceof ItemPortableFurnace); } public boolean isBurning() { return this.workingTime > 0; } protected boolean canSmelt() { if (this.inventorySlots[this.inputSlot] == null) { return false; } else { ItemStack itemstack = FurnaceRecipes.instance().getSmeltingResult(inventorySlots[this.inputSlot]); if (itemstack == null) { return false; } if (this.inventorySlots[this.outputSlot] == null) { return true; } if (!this.inventorySlots[this.outputSlot].isItemEqual(itemstack)) { return false; } int result = inventorySlots[this.outputSlot].stackSize + itemstack.stackSize; return result <= getInventoryStackLimit() && result <= this.inventorySlots[2].getMaxStackSize(); } } public void smeltItem() { if (this.canSmelt()) { ItemStack itemstack = FurnaceRecipes.instance().getSmeltingResult(inventorySlots[this.inputSlot]); if (this.inventorySlots[this.outputSlot] == null) { this.inventorySlots[this.outputSlot] = itemstack.copy(); } else if (this.inventorySlots[this.outputSlot].getItem() == itemstack.getItem()) { this.inventorySlots[this.outputSlot].stackSize += itemstack.stackSize; } this.decrStackSize(0, 1); } } @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.portable_furnace.gui"; } @Override public boolean hasCustomName() { return false; } @Override public ITextComponent getDisplayName() { return null; } @SideOnly(Side.CLIENT) public int getSmeltProcess() { return this.workingTime * 24 / this.workingTimeNeeded; } @SideOnly(Side.CLIENT) public int getBurnTime() { return this.burnTime * 14 / this.burnTimeTotal; } } -
Ha en effet, ici ça ne risque pas de fonctionner comme closeInventory n’est jamais appelé.
C’est dans le container qu’il faut le mettre. -
c’est déjà fait
-
Envoi toutes tes classes
-
-
Dans le container tu as oublié le write
-
Où sa ?
-
Dans la classe du container