Non résolu Problème de Slot dans mon Compressor Machine
-
Bonjour à vous,
j’écrit ce message afin d’avoir de l’aide sur un problème de slot. Je m’explique, quand on souhaites mettre un item dans la machine bah au lieux de mettre l’item dans le slot bah il disparaît et donc la machine n’ai pas du tout utilisable.
Voici les codes :
Le Block : java
public class BlockCompressorMachine extends BlockContainer { private IIcon top, bottom, right, left, infrontof, behind; public BlockCompressorMachine() { super(Material.rock); this.setResistance(8.0F); this.setHarvestLevel("pickaxe", 2); this.setBlockTextureName(References.MOD_ID + "compresor_machine"); this.setCreativeTab(ValaMod.Valamod); this.setBlockName("compressor_machine"); } public void registerBlockIcons(IIconRegister iiconRegister) { this.blockIcon = iiconRegister.registerIcon("compresor_machine"); this.left = iiconRegister.registerIcon("valamod:compresor_machine"); this.top = iiconRegister.registerIcon("valamod:compresor_machine"); this.bottom = iiconRegister.registerIcon("valamod:compresor_machine"); this.right = iiconRegister.registerIcon("valamod:compresor_machine"); this.infrontof = iiconRegister.registerIcon("valamod:compressor_machine_front"); this.behind = iiconRegister.registerIcon("valamod:compresor_machine"); } 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(ValaMod.instance, 3, world, x, y, z); return true; } } public IIcon getIcon(int side, int metadata) { if (side == 0) { return this.bottom; } else if (side == 1) { return this.top; } else if (side == 2) { return this.behind; } else if (side == 3) { return this.infrontof; } else if (side == 4) { return this.left; } else if (side == 5) { return this.right; } return this.blockIcon; } public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase p_149689_5_, ItemStack itemStack) { int l = MathHelper.floor_double(p_149689_5_.rotationYaw * 4.0F / 360.0F + 0.5D) & 0x3; if (l == 0) { world.setBlockMetadataWithNotify(x, y, z, 2, 2); } if (l == 1) { world.setBlockMetadataWithNotify(x, y, z, 5, 2); } if (l == 2) { world.setBlockMetadataWithNotify(x, y, z, 3, 2); } if (l == 3) { world.setBlockMetadataWithNotify(x, y, z, 4, 2); } } @Override public TileEntity createNewTileEntity(World world, int metadata) { return new TileEntityCompressorMachine(); } @Override public boolean hasTileEntity(int metadata) { 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); } }
Gui :
public class GuiCompressorMachine extends GuiContainer { private static final ResourceLocation texture = new ResourceLocation("valamod:textures/gui/compressor_machine.png"); private TileEntityCompressorMachine tile; private IInventory playerInv; public GuiCompressorMachine(TileEntityCompressorMachine tile, InventoryPlayer inventory) { super(new ContainerCompressorMachine(tile, inventory)); this.tile = tile; this.playerInv = inventory; this.allowUserInput = false; this.ySize = 256; this.xSize = 256; } protected void drawGuiContainerForegroundLayer(int x, int y) { String tileName = this.tile.hasCustomInventoryName() ? this.tile.getInventoryName() : I18n.format(this.tile.getInventoryName()); this.fontRendererObj.drawString(tileName, this.xSize / 2 - this.fontRendererObj.getStringWidth(tileName) / 2, 6, 0); String invName = this.playerInv.hasCustomInventoryName() ? this.playerInv.getInventoryName() : I18n.format(this.playerInv.getInventoryName()); this.fontRendererObj.drawString(invName, (this.xSize - this.fontRendererObj.getStringWidth(invName)) / 2, this.ySize - 102, 0); } @Override protected void drawGuiContainerBackgroundLayer(float partialRenderTick, int x, int y) { GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); this.mc.getTextureManager().bindTexture(texture); int k = (this.width - this.xSize) / 2; int l = (this.height - this.ySize) / 2; this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.ySize); if (this.tile.isBurning()) { int i = this.tile.getCookProgressScaled(41); this.drawTexturedModalRect(k + 151, l + 81, 222, 81, 16, i); } } }
Container :
public class ContainerCompressorMachine extends Container { private TileEntityCompressorMachine tile; public ContainerCompressorMachine(TileEntityCompressorMachine tile, InventoryPlayer inventory) { this.tile = tile; this.addSlotToContainer(new Slot(tile, 0, 19, 18)); this.addSlotToContainer(new Slot(tile, 1, 37, 18)); this.addSlotToContainer(new Slot(tile, 2, 55, 18)); this.addSlotToContainer(new Slot(tile, 4, 19, 36)); this.addSlotToContainer(new Slot(tile, 5, 37, 36)); this.addSlotToContainer(new Slot(tile, 6, 55, 36)); this.addSlotToContainer(new Slot(tile, 7, 19, 54)); this.addSlotToContainer(new Slot(tile, 8, 37, 54)); this.addSlotToContainer(new Slot(tile, 9, 55, 54)); this.addSlotToContainer(new Slot(tile, 10, 19, 67)); this.addSlotToContainer(new SlotResult(tile, 11, 145, 38)); this.bindPlayerInventory(inventory); } @Override public boolean canInteractWith(EntityPlayer player) { return this.tile.isUseableByPlayer(player); } 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, 140 + i * 18)); } } int i; for (i = 0; i < 9; i++) { addSlotToContainer(new Slot(inventory, i, 8 + i * 18, 198)); } } public ItemStack transferStackInSlot(EntityPlayer player, int quantity) { ItemStack itemstack = null; Slot slot = (Slot) this.inventorySlots.get(quantity); if (slot != null && slot.getHasStack()) { ItemStack itemstack1 = slot.getStack(); itemstack = itemstack1.copy(); if (quantity < this.tile.getSizeInventory()) { if (!this.mergeItemStack(itemstack1, this.tile.getSizeInventory(), this.inventorySlots.size(), true)) { return null; } } else if (!this.mergeItemStack(itemstack1, 0, this.tile.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.tile.closeInventory(); } }
TileEntity :
public class TileEntityCompressorMachine extends TileEntity implements IInventory { private ItemStack[] contents = new ItemStack[11]; private int workingTime = 0; private int workingTimeNeeded = 400; @Override public void readFromNBT(NBTTagCompound compound) { super.readFromNBT(compound); NBTTagList nbttaglist = compound.getTagList("Items", 11); this.contents = 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.contents.length) { this.contents[j] = ItemStack.loadItemStackFromNBT(nbttagcompound1); } } this.workingTime = compound.getShort("workingTime"); this.workingTimeNeeded = compound.getShort("workingTimeNeeded"); } @Override public void writeToNBT(NBTTagCompound compound) { super.writeToNBT(compound); NBTTagList nbttaglist = new NBTTagList(); for (int i = 0; i < this.contents.length; ++i) { if (this.contents[i] != null) { NBTTagCompound nbttagcompound1 = new NBTTagCompound(); nbttagcompound1.setByte("slot", (byte) i); this.contents[i].writeToNBT(nbttagcompound1); nbttaglist.appendTag(nbttagcompound1); } } compound.setTag("Items", nbttaglist); compound.setShort("workingTime", (short) this.workingTime); compound.setShort("workingTimeNeeded", (short) this.workingTimeNeeded); } @Override public int getSizeInventory() { return this.contents.length; } @Override public ItemStack getStackInSlot(int slotIndex) { return null; } @Override public ItemStack decrStackSize(int slotIndex, int amount) { if (this.contents[slotIndex] != null) { ItemStack itemstack; if (this.contents[slotIndex].stackSize <= amount) { itemstack = this.contents[slotIndex]; this.contents[slotIndex] = null; this.markDirty(); return itemstack; } else { itemstack = this.contents[slotIndex].splitStack(amount); if (this.contents[slotIndex].stackSize == 0) { this.contents[slotIndex] = null; } this.markDirty(); return itemstack; } } else { return null; } } @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) { if (stack != null && stack.stackSize > this.getInventoryStackLimit()) { stack.stackSize = this.getInventoryStackLimit(); } this.markDirty(); } @Override public String getInventoryName() { return "tile.compressor_machine"; } @Override public boolean hasCustomInventoryName() { return true; } @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 slot, ItemStack stack) { return slot == 11 ? false : true; } public boolean isBurning() { return this.workingTime > 0; } private boolean canSmelt() { if (this.contents[0] == null || this.contents[1] == null || this.contents[2] == null || this.contents[3] == null || this.contents[4] == null || this.contents[5] == null || this.contents[6] == null || this.contents[7] == null || this.contents[8] == null || this.contents[9] == null) return false; else { ItemStack itemstack = CompressorMachineRecipes.smelting() .getSmeltingResult(new ItemStack[] { this.contents[0], this.contents[1], this.contents[2], this.contents[3], this.contents[4], this.contents[5], this.contents[6], this.contents[7], this.contents[8], this.contents[9] }); if (itemstack == null) return false; if (this.contents[10] == null) return true; if (!this.contents[10].isItemEqual(itemstack)) return false; int result = contents[10].stackSize + itemstack.stackSize; return result <= getInventoryStackLimit() && result <= this.contents[10].getMaxStackSize(); } } @Override public void updateEntity() { if (this.isBurning() && this.canSmelt()) { ++this.workingTime; } if (this.canSmelt() && !this.isBurning()) { this.workingTime = 1; } if (this.canSmelt() && this.workingTime == this.workingTimeNeeded) { this.smeltItem(); this.workingTime = 0; } if (!this.canSmelt()) { this.workingTime = 0; } } public void smeltItem() { if (this.canSmelt()) { ItemStack itemstack = CompressorMachineRecipes.smelting() .getSmeltingResult(new ItemStack[] { this.contents[0], this.contents[1], this.contents[2], this.contents[3], this.contents[4], this.contents[5], this.contents[6], this.contents[7], this.contents[8], this.contents[9] }); if (this.contents[10] == null) { this.contents[10] = itemstack.copy(); } else if (this.contents[10].getItem() == itemstack.getItem()) { this.contents[10].stackSize += itemstack.stackSize; } --this.contents[0].stackSize; --this.contents[1].stackSize; --this.contents[2].stackSize; --this.contents[3].stackSize; --this.contents[4].stackSize; --this.contents[5].stackSize; --this.contents[6].stackSize; --this.contents[7].stackSize; --this.contents[8].stackSize; --this.contents[9].stackSize; if (this.contents[0].stackSize <= 0) { this.contents[0] = null; } if (this.contents[1].stackSize <= 0) { this.contents[1] = null; } if (this.contents[2].stackSize <= 0) { this.contents[2] = null; } if (this.contents[3].stackSize <= 0) { this.contents[3] = null; } if (this.contents[4].stackSize <= 0) { this.contents[4] = null; } if (this.contents[5].stackSize <= 0) { this.contents[5] = null; } if (this.contents[6].stackSize <= 0) { this.contents[6] = null; } if (this.contents[7].stackSize <= 0) { this.contents[7] = null; } if (this.contents[8].stackSize <= 0) { this.contents[8] = null; } if (this.contents[9].stackSize <= 0) { this.contents[9] = null; } } } public int getCookProgressScaled(int i) { return this.workingTime * i / 200; } }
Merci d’avance de votre aide !
-
Salut,
La fonction ‘getStackInSlot’ de ton TileEntity retourne null, donc le gui n’a pas d’item à afficher.
-
@aymericred Mais quand elle null bah ça crash
https://pastebin.com/k1J3G5Rk -
@Override public ItemStack getStackInSlot(int index) { return this.contents[index]; }
et aussi tu connais ou pas les boucles en java car ca
--this.contents[0].stackSize; --this.contents[1].stackSize; --this.contents[2].stackSize; --this.contents[3].stackSize; --this.contents[4].stackSize; --this.contents[5].stackSize; --this.contents[6].stackSize; --this.contents[7].stackSize; --this.contents[8].stackSize; --this.contents[9].stackSize; if (this.contents[0].stackSize <= 0) { this.contents[0] = null; } if (this.contents[1].stackSize <= 0) { this.contents[1] = null; } if (this.contents[2].stackSize <= 0) { this.contents[2] = null; } if (this.contents[3].stackSize <= 0) { this.contents[3] = null; } if (this.contents[4].stackSize <= 0) { this.contents[4] = null; } if (this.contents[5].stackSize <= 0) { this.contents[5] = null; } if (this.contents[6].stackSize <= 0) { this.contents[6] = null; } if (this.contents[7].stackSize <= 0) { this.contents[7] = null; } if (this.contents[8].stackSize <= 0) { this.contents[8] = null; } if (this.contents[9].stackSize <= 0) { this.contents[9] = null; }
c’est degeulasse
-
@spyman Je suis pas l’auteur de ce code c’est un autre développeur