Soucis Block Type Coffre
-
Bon, je bloque depuis 1 / 2 heures je pense que c’est un truc tout con mais bon :
(inspiré de là)En gros, le block doit etre ouvert et utilisé comme un coffre avec un seul slot et quand on le casse, et qu’on le repose, on doit avoir l’item qu’on avait mit dedans initialement.
Voilà l’erreur :
[15:53:35] [Client thread/FATAL]: Reported exception thrown! net.minecraft.util.ReportedException: Rendering screen at net.minecraft.client.renderer.EntityRenderer.updateCameraAndRender(EntityRenderer.java:1168) ~[EntityRenderer.class:?] at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1067) ~[Minecraft.class:?] at net.minecraft.client.Minecraft.run(Minecraft.java:962) [Minecraft.class:?] at net.minecraft.client.main.Main.main(Main.java:164) [Main.class:?] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_111] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_111] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_111] at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_111] at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) [launchwrapper-1.12.jar:?] at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [launchwrapper-1.12.jar:?] at net.minecraftforge.gradle.GradleStartCommon.launch(Unknown Source) [start/:?] at GradleStart.main(Unknown Source) [start/:?] Caused by: java.lang.NullPointerException at net.minecraft.inventory.Slot.getStack(Slot.java:88) ~[Slot.class:?] at net.minecraft.client.gui.inventory.GuiContainer.func_146977_a(GuiContainer.java:219) ~[GuiContainer.class:?] at net.minecraft.client.gui.inventory.GuiContainer.drawScreen(GuiContainer.java:114) ~[GuiContainer.class:?] at net.minecraft.client.renderer.EntityRenderer.updateCameraAndRender(EntityRenderer.java:1137) ~[EntityRenderer.class:?] … 11 more [15:53:35] [Client thread/INFO] [STDOUT]: [net.minecraft.client.Minecraft:displayCrashReport:388]: –-- Minecraft Crash Report ---- // Why did you do that? Time: 23/12/16 15:53 Description: Rendering screen java.lang.NullPointerException: Rendering screen at net.minecraft.inventory.Slot.getStack(Slot.java:88) at net.minecraft.client.gui.inventory.GuiContainer.func_146977_a(GuiContainer.java:219) at net.minecraft.client.gui.inventory.GuiContainer.drawScreen(GuiContainer.java:114) at net.minecraft.client.renderer.EntityRenderer.updateCameraAndRender(EntityRenderer.java:1137) at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1067) at net.minecraft.client.Minecraft.run(Minecraft.java:962) at net.minecraft.client.main.Main.main(Main.java:164) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) at net.minecraft.launchwrapper.Launch.main(Launch.java:28) at net.minecraftforge.gradle.GradleStartCommon.launch(Unknown Source) at GradleStart.main(Unknown Source)
Dans mon Main :
NetworkRegistry.INSTANCE.registerGuiHandler(instance, new GuiHandler()); BlocksMod.Init(event);
La Class du Block :
package fr.lokoliveh.opalitemod.Block; import fr.lokoliveh.opalitemod.Main; import fr.lokoliveh.opalitemod.init.CreativeTabOpalite; import fr.lokoliveh.opalitemod.tiles.TileEntityLuckyBlock; import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.client.renderer.texture.IIconRegister; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.IIcon; import net.minecraft.world.World; public class LuckyBlock extends Block { IIcon side; IIcon top; IIcon bottom; public LuckyBlock() { super(Material.iron); setResistance(8.0F); setHarvestLevel("pickaxe", 2); setCreativeTab(CreativeTabOpalite.OpaTab); setBlockName("luckyblock"); setHardness(5.0F); } public TileEntity createNewTileEntity(World world, int metadata) { return new TileEntityLuckyBlock(); } public boolean hasTileEntity() { return true; } public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ) { if(world.isRemote) { return true; } else { player.openGui(Main.instance, 2, world, x, y, z); return true; } } public void breakBlock(World world, int x, int y, int z, Block block, int metadata) { TileEntity tileentity = world.getTileEntity(x, y, z); if(tileentity instanceof IInventory) { IInventory inv = (IInventory)tileentity; for(int i1 = 0; i1 < inv.getSizeInventory(); ++i1) { ItemStack itemstack = inv.getStackInSlot(i1); if(itemstack != null) { float f = world.rand.nextFloat() * 0.8F + 0.1F; float f1 = world.rand.nextFloat() * 0.8F + 0.1F; EntityItem entityitem; for(float f2 = world.rand.nextFloat() * 0.8F + 0.1F; itemstack.stackSize > 0; world.spawnEntityInWorld(entityitem)) { int j1 = world.rand.nextInt(21) + 10; if(j1 > itemstack.stackSize) { j1 = itemstack.stackSize; } itemstack.stackSize -= j1; entityitem = new EntityItem(world, (double)((float)x + f), (double)((float)y + f1), (double)((float)z + f2), new ItemStack(itemstack.getItem(), j1, itemstack.getItemDamage())); float f3 = 0.05F; entityitem.motionX = (double)((float)world.rand.nextGaussian() * f3); entityitem.motionY = (double)((float)world.rand.nextGaussian() * f3 + 0.2F); entityitem.motionZ = (double)((float)world.rand.nextGaussian() * f3); if(itemstack.hasTagCompound()) { entityitem.getEntityItem().setTagCompound((NBTTagCompound)itemstack.getTagCompound().copy()); } } } } world.func_147453_f(x, y, z, block); } super.breakBlock(world, x, y, z, block, metadata); } public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase living, ItemStack stack) { TileEntity tile = world.getTileEntity(x, y, z); if(tile instanceof TileEntityLuckyBlock) { if(stack.hasDisplayName()) { ((TileEntityLuckyBlock)tile).setCustomName(stack.getDisplayName()); } } } public void registerBlockIcons(IIconRegister iiconRegister) { this.side = iiconRegister.registerIcon(Main.MODID+":LuckyBlock_Sides"); this.top = iiconRegister.registerIcon(Main.MODID+":LuckyBlock_Top"); this.bottom = iiconRegister.registerIcon(Main.MODID + "LuckyBlock_Bottom"); } public IIcon getIcon(int side, int metadata) { if (side == 0) { return this.top; } else if (side == 1){ return this.bottom; } return this.side; } }
Dans le GuiHandler :
package fr.lokoliveh.opalitemod.gui; import cpw.mods.fml.common.network.IGuiHandler; import fr.lokoliveh.opalitemod.inventory.InventoryBackpack; import fr.lokoliveh.opalitemod.tiles.TileEntityAlchemyStacker; import fr.lokoliveh.opalitemod.tiles.TileEntityLuckyBlock; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.world.World; public class GuiHandler implements IGuiHandler { public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { switch (ID) { case 0: return new ContainerBackpack(player.inventory, new InventoryBackpack(player.getHeldItem())); case 1: return new ContainerAlchemyStacker((TileEntityAlchemyStacker)world.getTileEntity(x, y, z), player.inventory); case 2: return new ContainerLuckyBlock((TileEntityLuckyBlock)world.getTileEntity(x, y, z), player.inventory); } return null; } public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { switch (ID) { case 0: return new GuiBackpack(player.inventory, new InventoryBackpack(player.getHeldItem())); case 1: return new GuiAlchemyStacker((TileEntityAlchemyStacker)world.getTileEntity(x, y, z), player.inventory); case 2: return new GuiLuckyBlock((TileEntityLuckyBlock)world.getTileEntity(x, y, z), player.inventory); } return null; } }
Gui :
package fr.lokoliveh.opalitemod.gui; import fr.lokoliveh.opalitemod.slot.SlotLuckyBlock; import fr.lokoliveh.opalitemod.slot.SlotPotion; import fr.lokoliveh.opalitemod.tiles.TileEntityLuckyBlock; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; public class ContainerLuckyBlock extends Container { private final TileEntityLuckyBlock tileent; public ContainerLuckyBlock(TileEntityLuckyBlock tile, InventoryPlayer inventory) { this.tileent = tile; addSlotToContainer(new SlotLuckyBlock(tile, 2, 80, 18)); bindPlayerInventory(inventory); } private void bindPlayerInventory(InventoryPlayer inventory) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 9; j++) { addSlotToContainer(new Slot(inventory, j + i * 9 + 9, 8 + j * 18, 84 + i * 18)); } } for (int i = 0; i < 9; i++) { addSlotToContainer(new Slot(inventory, i, 8 + i * 18, 142)); } } public ItemStack transferStackInSlot(EntityPlayer player, int slotIndex) { ItemStack itemstack = null; Slot slot = (Slot)this.inventorySlots.get(slotIndex); if(slot != null && slot.getHasStack()) { ItemStack itemstack1 = slot.getStack(); itemstack = itemstack1.copy(); if(slotIndex < this.tileent.getSizeInventory()) { if(!this.mergeItemStack(itemstack1, this.tileent.getSizeInventory(), this.inventorySlots.size(), true)) { return null; } } else if(!this.mergeItemStack(itemstack1, 0, this.tileent.getSizeInventory(), false)) { return null; } if(itemstack1.stackSize == 0) { slot.putStack((ItemStack)null); } else { slot.onSlotChanged(); } } return itemstack; } public void onContainerClosed(EntityPlayer player) { super.onContainerClosed(player); this.tileent.closeInventory(); } @Override public boolean canInteractWith(EntityPlayer player) { return this.tileent.isUseableByPlayer(player); } }
Le TileEntity :
package fr.lokoliveh.opalitemod.tiles; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.tileentity.TileEntity; import net.minecraftforge.common.util.Constants; public class TileEntityLuckyBlock extends TileEntity implements IInventory{ private ItemStack[] contents = new ItemStack[1]; private String customName; @Override public void readFromNBT(NBTTagCompound compound) { super.readFromNBT(compound); // exécute ce qui se trouve dans la fonction readFromNBT de la classe mère (lecture de la position du tile entity) if(compound.hasKey("CustomName", Constants.NBT.TAG_STRING)) // si un tag custom name de type string existe { this.customName = compound.getString("CustomName"); // on le lit } NBTTagList nbttaglist = compound.getTagList("Items", Constants.NBT.TAG_COMPOUND); // on obtient la liste de tags nommée Items this.contents = new ItemStack[this.getSizeInventory()]; // on réinitialise le tableau for(int i = 0; i < nbttaglist.tagCount(); ++i) // i varie de 0 à la taille la liste { NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i); // on lit le tag nbt int j = nbttagcompound1.getByte("Slot") & 255; // on lit à quel slot se trouve l'item stack if(j >= 0 && j < this.contents.length) { this.contents[j] = ItemStack.loadItemStackFromNBT(nbttagcompound1); // on lit l'item stack qui se trouve dans le tag } } } @Override public void writeToNBT(NBTTagCompound compound) { super.writeToNBT(compound); // exécute se qui se trouve dans la fonction writeToNBT de la classe mère (écriture de la position du tile entity) if(this.hasCustomInventoryName()) // s'il y a un nom custom { compound.setString("CustomName", this.customName); // on le met dans le tag nbt } NBTTagList nbttaglist = new NBTTagList(); // on créé une nouvelle liste de tags for(int i = 0; i < this.contents.length; ++i) // i varie de 0 à la taille de notre tableau { if(this.contents[ i] != null) // si l'item stack à l'emplacement i du tableau n'est pas null { NBTTagCompound nbttagcompound1 = new NBTTagCompound(); // on créé un tag nbt nbttagcompound1.setByte("Slot", (byte)i); // on enregistre son emplacement dans le tableau this.contents[ i].writeToNBT(nbttagcompound1); // on écrit l'item dans le tag nbttaglist.appendTag(nbttagcompound1); // on ajoute le tab à la liste } } compound.setTag("Items", nbttaglist); // on enregistre la liste dans le tag nbt } @Override public int getSizeInventory() { return this.contents.length; } @Override public ItemStack getStackInSlot(int slotIndex) { return this.contents[slotIndex]; } @Override public ItemStack decrStackSize(int slotIndex, int amount) { if(this.contents[slotIndex] != null) // si le contenu dans l'emplacement n'est pas null { ItemStack itemstack; if(this.contents[slotIndex].stackSize <= amount) // si la quantité est inférieur où égale à ce qu'on souhaite retirer { itemstack = this.contents[slotIndex]; // la variable itemstack prends la valeur du contenu this.contents[slotIndex] = null; // on retire ce qui est dans la variable contents this.markDirty(); // met à jour le tile entity return itemstack; // renvoie itemstack } else // sinon { itemstack = this.contents[slotIndex].splitStack(amount); // la fonction splitStack(quantité) retire dans this.contents[slotIndex] le contenu et le met dans itemstack if(this.contents[slotIndex].stackSize == 0) // au cas où la quantité passe à 0 (ce qui ne devrait pas arriver en temps normal) { this.contents[slotIndex] = null; // on met sur null, ça évite de se retrouver avec des itemstack bugué qui contiennent 0 } this.markDirty(); // met à jour le tile entity return itemstack; // renvoie itemstack } } else // sinon si le contenu dans cette emplacement est null { return null; // renvoie null, puisqu'il n'y a rien dans cette emplacement } } @Override public ItemStack getStackInSlotOnClosing(int slotIndex) { if(this.contents[slotIndex] != null) { ItemStack itemstack = this.contents[slotIndex]; this.contents[slotIndex] = null; return itemstack; } else { return null; } } @Override public void setInventorySlotContents(int slotIndex, ItemStack stack) { this.contents[slotIndex] = stack; // met l'item stack dans le tableau if(stack != null && stack.stackSize > this.getInventoryStackLimit()) // si la taille de l'item stack dépasse la limite maximum de l'inventaire { stack.stackSize = this.getInventoryStackLimit(); // on le remet sur la limite } this.markDirty(); // met à jour le tile entity } @Override public String getInventoryName() { return this.hasCustomInventoryName() ? this.customName : "tile.luckyblock"; } public void setCustomName(String customName) { this.customName = customName; } @Override public boolean hasCustomInventoryName() { return false; } @Override public int getInventoryStackLimit() { return 64; } @Override public boolean isUseableByPlayer(EntityPlayer player) { return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : player.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D; } @Override public void openInventory() { } @Override public void closeInventory() { } @Override public boolean isItemValidForSlot(int slotIndex, ItemStack stack) { return true; } }
Le Container :
package fr.lokoliveh.opalitemod.gui; import fr.lokoliveh.opalitemod.slot.SlotLuckyBlock; import fr.lokoliveh.opalitemod.slot.SlotPotion; import fr.lokoliveh.opalitemod.tiles.TileEntityLuckyBlock; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; public class ContainerLuckyBlock extends Container { private final TileEntityLuckyBlock tileent; public ContainerLuckyBlock(TileEntityLuckyBlock tile, InventoryPlayer inventory) { this.tileent = tile; addSlotToContainer(new SlotLuckyBlock(tile, 2, 80, 18)); bindPlayerInventory(inventory); } private void bindPlayerInventory(InventoryPlayer inventory) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 9; j++) { addSlotToContainer(new Slot(inventory, j + i * 9 + 9, 8 + j * 18, 84 + i * 18)); } } for (int i = 0; i < 9; i++) { addSlotToContainer(new Slot(inventory, i, 8 + i * 18, 142)); } } public ItemStack transferStackInSlot(EntityPlayer player, int slotIndex) { ItemStack itemstack = null; Slot slot = (Slot)this.inventorySlots.get(slotIndex); if(slot != null && slot.getHasStack()) { ItemStack itemstack1 = slot.getStack(); itemstack = itemstack1.copy(); if(slotIndex < this.tileent.getSizeInventory()) { if(!this.mergeItemStack(itemstack1, this.tileent.getSizeInventory(), this.inventorySlots.size(), true)) { return null; } } else if(!this.mergeItemStack(itemstack1, 0, this.tileent.getSizeInventory(), false)) { return null; } if(itemstack1.stackSize == 0) { slot.putStack((ItemStack)null); } else { slot.onSlotChanged(); } } return itemstack; } public void onContainerClosed(EntityPlayer player) { super.onContainerClosed(player); this.tileent.closeInventory(); } @Override public boolean canInteractWith(EntityPlayer player) { return this.tileent.isUseableByPlayer(player); } }
J’imagine que j’ai fait une erreur à la con comme d’habitude mais là je vois pas.
-
Salut,
Le jeu crash ici :
at net.minecraft.inventory.Slot.getStack(Slot.java:88)La ligne en question :
return this.inventory.getStackInSlot(this.slotIndex);
La seule chose qui peut causer un npe ici, c’est que inventory soit null.
inventory correspond à ton tile entity.Donc il y a un endroit où ton tile entity est null au lieu d’avoir la bonne valeur.
Reste à trouver où. -
@‘robin4002’:
Salut,
Le jeu crash ici :
at net.minecraft.inventory.Slot.getStack(Slot.java:88)La ligne en question :
return this.inventory.getStackInSlot(this.slotIndex);
La seule chose qui peut causer un npe ici, c’est que inventory soit null.
inventory correspond à ton tile entity.Donc il y a un endroit où ton tile entity est null au lieu d’avoir la bonne valeur.
Reste à trouver où.En soit, le seul moment ou quelquechose peut etre nul et causer un NPE, c’est quand il n’y a rien dans le block …
Mais là je vois pas sinon -
Le problème là c’est que ton tile entity est null.
Ce qui dans le tile entity peut être null dans le cas il n’y a pas d’item, c’est un cas prévus.Le fait que le tile entity soit null n’est pas un cas prévu, d’où le crash.
Il faut trouver pourquoi c’est null alors que ça ne devrait pas.Pour ça tu peux utiliser des points d’arrêt + le debug d’eclipse, ou print la valeur du tile entity dans la console a différent endroit du code pour trouver où c’est null, et ensuite comprendre pourquoi.