• Moddeurs confirmés Rédacteurs

    Bonjour, voilà j’ai un problème sur l’update de la barre de progression de ma machine. Elle n’est update que lorsque je rouvre le gui mais n’avance pas au fil du temps.
    Paticularités de la machine :
    Possède plusieurs metadata, lorsque la recette est en train de procédé je change le bloc tout les 2 ticks (je le remplace par le bloc avec le metadata au dessus) d’après mes tests le problème viens de là.

    Classe du bloc

    :::

    package fr.andemar.common;
    
    import java.util.List;
    
    import net.minecraft.block.Block;
    import net.minecraft.block.BlockContainer;
    import net.minecraft.block.material.Material;
    import net.minecraft.client.renderer.texture.IIconRegister;
    import net.minecraft.creativetab.CreativeTabs;
    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.Item;
    import net.minecraft.item.ItemStack;
    import net.minecraft.nbt.NBTTagCompound;
    import net.minecraft.tileentity.TileEntity;
    import net.minecraft.util.IIcon;
    import net.minecraft.util.MathHelper;
    import net.minecraft.world.IBlockAccess;
    import net.minecraft.world.World;
    import cpw.mods.fml.relauncher.Side;
    import cpw.mods.fml.relauncher.SideOnly;
    
    public class BlockCompressor extends BlockContainer {
    
    IIcon bottom, front1, front2, front3, front4, front5, front6, front7, front8, front9;
    
    public BlockCompressor()
    {
    super(Material.rock);
    this.setCreativeTab(Andemar.creativeTab_andemar);
    this.setHarvestLevel("pickaxe", 2);
    this.setResistance(10.0F);
    this.setHardness(3.0F);
    this.setStepSound(soundTypeStone);
    }
    
    public void registerBlockIcons(IIconRegister iiconRegister)
       {
        this.blockIcon = iiconRegister.registerIcon(Andemar.MODID + ":compressor_side");
        this.bottom = iiconRegister.registerIcon(Andemar.MODID + ":compressor_bottom");
        this.front1 = iiconRegister.registerIcon(Andemar.MODID + ":compressor_front_1");
        this.front2 = iiconRegister.registerIcon(Andemar.MODID + ":compressor_front_2");
        this.front3 = iiconRegister.registerIcon(Andemar.MODID + ":compressor_front_3");
        this.front4 = iiconRegister.registerIcon(Andemar.MODID + ":compressor_front_4");
        this.front5 = iiconRegister.registerIcon(Andemar.MODID + ":compressor_front_5");
        this.front6 = iiconRegister.registerIcon(Andemar.MODID + ":compressor_front_6");
        this.front7 = iiconRegister.registerIcon(Andemar.MODID + ":compressor_front_7");
        this.front8 = iiconRegister.registerIcon(Andemar.MODID + ":compressor_front_8");
        this.front9 = iiconRegister.registerIcon(Andemar.MODID + ":compressor_front_9");
    
       }
    
    @SideOnly(Side.CLIENT)
       public IIcon getIcon(IBlockAccess world, int x, int y, int z, int side)
       {
        int metadata = world.getBlockMetadata(x, y, z);
        if(side == 0 || side == 1)
        return this.bottom;
        TileEntity tile = world.getTileEntity(x, y, z);
        if(tile instanceof TileEntityCompressor)
        {
        byte direction = ((TileEntityCompressor)tile).getDirection();
        IIcon texture = this.getTextureForMetadata(metadata);
        return side == 3 && direction == 0 ? texture : (side == 4 && direction == 1) ? texture : (side == 2 && direction == 2) ? texture : (side == 5 && direction == 3) ? texture : this.blockIcon;
        }
        return this.blockIcon;
       }
    
    public IIcon getIcon(int side, int metadata)
    {
    if(side==1)
    return this.bottom;
        switch(metadata)
        {
        case 0 :
        if(side == 4)
        return this.front1;
        if(side == 0)
        return this.bottom;
        return this.blockIcon;
        case 1 :
        if(side == 4)
        return this.front2;
        if(side == 0)
        return this.bottom;
        return this.blockIcon;
        case 2 :
        if(side == 4)
        return this.front3;
        if(side == 0)
        return this.bottom;
        return this.blockIcon;
        case 3 :
        if(side == 4)
        return this.front4;
        if(side == 0)
        return this.bottom;
        return this.blockIcon;
        case 4 :
        if(side == 4)
        return this.front5;
        if(side == 0)
        return this.bottom;
        return this.blockIcon;
        case 5 :
        if(side == 4)
        return this.front6;
        if(side == 0)
        return this.bottom;
        return this.blockIcon;
        case 6 :
        if(side == 4)
        return this.front7;
        if(side == 0)
        return this.bottom;
        return this.blockIcon;
        case 7 :
        if(side == 4)
        return this.front8;
        if(side == 0)
        return this.bottom;
        return this.blockIcon;
        case 8 :
        if(side == 4)
        return this.front9;
        if(side == 0)
        return this.bottom;
        return this.blockIcon;
        default :
        return this.blockIcon;
        }
    }
    
    public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entity, ItemStack stack)
       {
           int direction = MathHelper.floor_double((double)(entity.rotationYaw * 4.0F / 360.0F) + 2.5D) & 3;
    
           TileEntity tile = world.getTileEntity(x, y, z);
           if(tile instanceof TileEntityCompressor)
           {
            ((TileEntityCompressor)tile).setDirection((byte)direction);
           }
       }
    
    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(Andemar.instance, 0, world, x, y, z);
               return true;
           }
       }
    
       @Override
       public boolean hasTileEntity(int metadata)
       {
           return true;
       }
    
       @SuppressWarnings({ "unchecked", "rawtypes" })
    public void getSubBlocks(Item item, CreativeTabs tabs, List list)
    {
    for(int i = 0; i < 9; i++)
    {
    list.add(new ItemStack(item, 1, i));
    }
    }
    
       private IIcon getTextureForMetadata(int metadata)
       {
        switch(metadata)
        {
        case 0 :
        return this.front1;
        case 1 :
        return this.front2;
        case 2 :
        return this.front3;
        case 3 :
        return this.front4;
        case 4 :
        return this.front5;
        case 5 :
        return this.front6;
        case 6 :
        return this.front7;
        case 7 :
        return this.front8;
        case 8 :
        return this.front9;
        default :
        return this.front1;
        }
       }
    
       public static void updateCompressorState(World world, int x, int y, int z)
       {
        TileEntity tileentity = world.getTileEntity(x, y, z);
    
           int metadata = world.getBlockMetadata(x, y, z);
           if(metadata + 1 == 9)
           metadata = 0;
           world.setBlock(x, y, z, Andemar.compressor, metadata + 1, 2);
    
           if (tileentity != null)
           {
               tileentity.validate();
               world.setTileEntity(x, y, z, tileentity);
           }
       }
    
       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 TileEntityCompressor)
           {
            if(!((TileEntityCompressor)tileentity).isBlockUpdating())
           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);
       }
    
    @Override
    public TileEntity createNewTileEntity(World p_149915_1_, int p_149915_2_) {
    return new TileEntityCompressor();
    }
    }
    

    :::

    Classe du gui
    :::

    package fr.andemar.client;
    
    import org.lwjgl.opengl.GL11;
    
    import fr.andemar.common.Andemar;
    import fr.andemar.common.ContainerCompressor;
    import fr.andemar.common.TileEntityCompressor;
    import net.minecraft.client.gui.inventory.GuiContainer;
    import net.minecraft.client.resources.I18n;
    import net.minecraft.entity.player.InventoryPlayer;
    import net.minecraft.inventory.IInventory;
    import net.minecraft.util.ResourceLocation;
    
    public class GuiCompressor extends GuiContainer {
    
    private static final ResourceLocation texture = new ResourceLocation(Andemar.MODID,"textures/gui/gui_compressor.png");
       private TileEntityCompressor tileCompressor;
       private IInventory playerInv;
    
    public GuiCompressor(TileEntityCompressor tile, InventoryPlayer inventory)
    {
    super(new ContainerCompressor(tile, inventory));
           this.tileCompressor = tile;
           this.playerInv = inventory;
           this.allowUserInput = false;
           this.ySize = 207;
           this.xSize = 195;
    }
    
    @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, 46, this.xSize, this.ySize);
    
           if(this.tileCompressor.isBurning())
           {
            int i = this.tileCompressor.getCookProgress();
            this.drawTexturedModalRect(k + 47, l + 46, 0, 2, 100, i);
           }
    
    }
    
    protected void drawGuiContainerForegroundLayer(int x, int y)
       {
           this.fontRendererObj.drawString(this.playerInv.hasCustomInventoryName() ? this.playerInv.getInventoryName() : I18n.format(this.playerInv.getInventoryName()), 10, this.ySize - 98, 4210752);
       }
    
    }
    

    :::

    Classe du TileEntity

    :::

    package fr.andemar.common;
    
    import cpw.mods.fml.relauncher.Side;
    import cpw.mods.fml.relauncher.SideOnly;
    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.network.NetworkManager;
    import net.minecraft.network.Packet;
    import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
    import net.minecraft.tileentity.TileEntity;
    
    public class TileEntityCompressor extends TileEntity implements IInventory {
    
    private ItemStack[] contents = new ItemStack[4];
    private byte direction;
    private int workingTime = 0;
    private int workingTimeNeeded = 200;
    private boolean isBlockUpdating = false;
    
    @Override
    public void readFromNBT(NBTTagCompound compound)
    {
    super.readFromNBT(compound);
    this.direction = compound.getByte("Direction");
    
    NBTTagList nbttaglist = compound.getTagList("Items", 10);
    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");
    this.isBlockUpdating = compound.getBoolean("isBlockUpdating");
    }
    
    @Override
    public void writeToNBT(NBTTagCompound compound)
    {
    super.writeToNBT(compound);
    compound.setByte("Direction", this.direction);
    NBTTagList nbttaglist = new NBTTagList();
    
    for (int i = 0; i < this.contents.length; ++i)
    {
    if (this.contents* != null)
    {
    NBTTagCompound nbttagcompound1 = new NBTTagCompound();
    nbttagcompound1.setByte("Slot", (byte)i);
    this.contents*.writeToNBT(nbttagcompound1);
    nbttaglist.appendTag(nbttagcompound1);
    }
    }
    
    compound.setTag("Items", nbttaglist);
    compound.setShort("workingTime",(short)this.workingTime);
    compound.setShort("workingTimeNeeded", (short)this.workingTimeNeeded);
    compound.setBoolean("isBlockUpdating", this.isBlockUpdating);
    }
    
    @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)
    {
    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) {
    this.contents[slotIndex] = stack;
    
    if (stack != null && stack.stackSize > this.getInventoryStackLimit())
    {
    stack.stackSize = this.getInventoryStackLimit();
    }
    
    this.markDirty();
    }
    
    @Override
    public String getInventoryName() {
    return "tile.fermenter";
    }
    
    @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 slot, ItemStack stack) {
    return slot == 3 ? false : true;
    }
    
    public void setDirection(byte direction)
    {
    this.direction = direction;
    this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord);
    }
    
    public byte getDirection()
    {
    return this.direction;
    }
    
    public Packet getDescriptionPacket()
    {
    NBTTagCompound nbttagcompound = new NBTTagCompound();
    this.writeToNBT(nbttagcompound);
    return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, 0, nbttagcompound);
    }
    
    public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt)
    {
    this.readFromNBT(pkt.func_148857_g());
    this.worldObj.markBlockRangeForRenderUpdate(this.xCoord, this.yCoord, this.zCoord, this.xCoord, this.yCoord, this.zCoord);
    }
    
    public boolean isBurning()
    {
    return this.workingTime > 0;
    }
    
    private boolean canSmelt()
    {
    if (this.contents[0] == null || this.contents[1] == null || this.contents[2] == null)
    {
    return false;
    }
    else
    {
    ItemStack itemstack = CompressorRecipes.smelting().getSmeltingResult(new ItemStack[]{this.contents[0], this.contents[1], this.contents[2]});
    if (itemstack == null) return false;
    if (this.contents[3] == null) return true;
    if (!this.contents[3].isItemEqual(itemstack)) return false;
    int result = contents[3].stackSize + itemstack.stackSize;
    return result <= getInventoryStackLimit() && result <= this.contents[3].getMaxStackSize(); //Forge BugFix: Make it respect stack sizes properly.
    }
    }
    
    public void updateEntity()
    {
    if(this.isBurning() && this.canSmelt())
    {
    ++this.workingTime;
    if(this.workingTime % 2 == 0)
        {
        this.isBlockUpdating = true;
        BlockCompressor.updateCompressorState(this.worldObj, this.xCoord, this.yCoord, this.zCoord);
        this.markDirty();
        this.isBlockUpdating = false;
        }
    }
    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 = CompressorRecipes.smelting().getSmeltingResult(new ItemStack[]{this.contents[0], this.contents[1], this.contents[2]});
    if (this.contents[3] == null)
    {
    this.contents[3] = itemstack.copy();
    }
    else if (this.contents[3].getItem() == itemstack.getItem())
    {
    this.contents[3].stackSize += itemstack.stackSize; // Forge BugFix: Results may have multiple items
    }
    
    –this.contents[0].stackSize;
    –this.contents[1].stackSize;
    –this.contents[2].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;
    }
    }
    }
    
    public boolean isBlockUpdating()
    {
    return this.isBlockUpdating;
    }
    
    @SideOnly(Side.CLIENT)
    public int getCookProgress()
    {
    return this.workingTime * 41 / this.workingTimeNeeded;
    }
    }
    

    :::

    J’espère que vous pourrez m’aider

  • Administrateurs Rédacteurs Moddeurs confirmés

    Container ? Les fonctions qui update la bar se trouve dans le container.

  • Moddeurs confirmés Rédacteurs

    @‘robin4002’:

    Container ? Les fonctions qui update la bar se trouve dans le container.

    Classe du containter

    package fr.andemar.common;
    
    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 ContainerCompressor extends Container {
    
    private TileEntityCompressor tileCompressor;
    
    public ContainerCompressor(TileEntityCompressor tile, InventoryPlayer inventory)
    {
    this.tileCompressor = tile;
    this.addSlotToContainer(new Slot(tile, 0, 49, 29));
    this.addSlotToContainer(new Slot(tile, 1, 89, 29));
    this.addSlotToContainer(new Slot(tile, 2, 129, 29));
    this.addSlotToContainer(new SlotResult(inventory.player, tile, 3, 89, 89));
    this.bindPlayerInventory(inventory);
    
    }
    
    @Override
    public boolean canInteractWith(EntityPlayer player) {
    return this.tileCompressor.isUseableByPlayer(player);
    }
    
    private void bindPlayerInventory(InventoryPlayer inventory)
    {
    int i;
    for (i = 0; i < 3; ++i)
    {
    for (int j = 0; j < 9; ++j)
    {
    this.addSlotToContainer(new Slot(inventory, j + i * 9 + 9, 17 + j * 18, 125 + i * 18));
    }
    }
    
    for (i = 0; i < 9; ++i)
    {
    this.addSlotToContainer(new Slot(inventory, i, 17 + i * 18, 183));
    }
    }
    
    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.tileCompressor.getSizeInventory())
    {
    if (!this.mergeItemStack(itemstack1, this.tileCompressor.getSizeInventory(), this.inventorySlots.size(), true))
    {
    return null;
    }
    }
    else if (!this.mergeItemStack(itemstack1, 0, this.tileCompressor.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.tileCompressor.closeInventory();
    }
    
    }
    
    
  • Administrateurs Rédacteurs Moddeurs confirmés

  • Moddeurs confirmés Rédacteurs

    Merci beaucoup, j’espère que ça aura réglé un second problème que j’avais. Je passe en résolu