Bug inventaire avec block de type four



  • Bonjour, bonjour ! Encore moi :!

    Cette fois-ci, j'ai créé un block de type four, sauf que le seul problème assez gênant, est que je ne peux pas utiliser mon inventaire dans ce four. C'est à dire que quand j'ouvre le GUI, je ne peux pas déplacer des objets. Je sais que y'a un histoire côté client et serveur, mais je ne suis pas capable de trouver le problème…

    Alors voilà mes classes:

    La classe de mon block:

    package fr.breakfight.infectiion21.blocks;
    
    import fr.breakfight.infectiion21.common.BreakfMain;
    import fr.breakfight.infectiion21.init.BlockInit;
    import fr.breakfight.infectiion21.tileentity.TileEntityPainiteFurnace;
    import net.minecraft.block.BlockContainer;
    import net.minecraft.block.ITileEntityProvider;
    import net.minecraft.block.SoundType;
    import net.minecraft.block.material.Material;
    import net.minecraft.block.state.IBlockState;
    import net.minecraft.creativetab.CreativeTabs;
    import net.minecraft.entity.player.EntityPlayer;
    import net.minecraft.inventory.InventoryHelper;
    import net.minecraft.tileentity.TileEntity;
    import net.minecraft.util.EnumBlockRenderType;
    import net.minecraft.util.EnumFacing;
    import net.minecraft.util.EnumHand;
    import net.minecraft.util.math.BlockPos;
    import net.minecraft.world.World;
    
    public class BlockPainiteFurnace extends BlockContainer implements ITileEntityProvider {
    
        public BlockPainiteFurnace() {
        super(Material.ROCK);
    setUnlocalizedName(BlockInit.BreakFightBlocks.PAINITEFURNACE.getUnlocalizedName());
    setRegistryName(BlockInit.BreakFightBlocks.PAINITEFURNACE.getRegistryName());
    setHardness(3.0F);
    setResistance(5.0F);
    setSoundType(SoundType.STONE);
    setHarvestLevel("pickaxe", 5);
    setCreativeTab(CreativeTabs.DECORATIONS);
    }
    
        @Override
        public boolean hasTileEntity() { 
        return true; 
        }
    
        @Override
        public TileEntity createTileEntity(World world, IBlockState state) {
        return new TileEntityPainiteFurnace();
        }
    
        @Override
        public TileEntity createNewTileEntity(World world, int metadata)  { 
        return new TileEntityPainiteFurnace(); 
        }
    
        @Override
        public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing heldItem, float side, float hitX, float hitY) {
        if (worldIn.isRemote) {
        playerIn.openGui(BreakfMain.instance, 0, worldIn, pos.getX(), pos.getY(), pos.getZ());
        } 
        return super.onBlockActivated(worldIn, pos, state, playerIn, hand, heldItem, side, hitX, hitY);
        }
        @Override
        public EnumBlockRenderType getRenderType(IBlockState state) {
        return EnumBlockRenderType.MODEL;
        }
        @Override
        public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {
        TileEntity tileentity = worldIn.getTileEntity(pos);
    
        if (tileentity instanceof TileEntityPainiteFurnace) {
        InventoryHelper.dropInventoryItems(worldIn, pos,
        (TileEntityPainiteFurnace) tileentity);
        }
    
        super.breakBlock(worldIn, pos, state);
        }
    
    }
    
    ``` 
    
    La classe TileEntity de mon block:
    ```java
    package fr.breakfight.infectiion21.tileentity;
    
    import fr.breakfight.infectiion21.init.CraftingInit;
    import net.minecraft.entity.player.EntityPlayer;
    import net.minecraft.inventory.IInventory;
    import net.minecraft.inventory.ItemStackHelper;
    import net.minecraft.item.ItemStack;
    import net.minecraft.nbt.NBTTagCompound;
    import net.minecraft.network.NetworkManager;
    import net.minecraft.network.play.server.SPacketUpdateTileEntity;
    import net.minecraft.tileentity.TileEntity;
    import net.minecraft.util.ITickable;
    import net.minecraft.util.NonNullList;
    import net.minecraftforge.common.capabilities.ICapabilityProvider;
    
    public class TileEntityPainiteFurnace extends TileEntity implements ITickable, ICapabilityProvider, IInventory {
    
    private NonNullList <itemstack>stacks = NonNullList.withSize(8, ItemStack.EMPTY);
    private String painiteFurnace;
    private int timePassed = 0;
    private int burningTimeLeft = -0;
    
    public TileEntityPainiteFurnace() {
    }
    
    @Override
    public void readFromNBT(NBTTagCompound nbt) {
    super.readFromNBT(nbt);
    this.stacks = NonNullList. <itemstack>withSize(this.getSizeInventory(), ItemStack.EMPTY);
    ItemStackHelper.loadAllItems(nbt, this.stacks);
    
    if (nbt.hasKey("painiteFurnace", 8)) {
    this.painiteFurnace = nbt.getString("painiteFurnace");
    }
    this.burningTimeLeft = nbt.getInteger("burningTimeLeft");
    this.timePassed = nbt.getInteger("timePassed");
    }
    
    @Override
    public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
    super.writeToNBT(nbt);
    ItemStackHelper.saveAllItems(nbt, this.stacks);
    
    if (this.hasCustomName()){
    nbt.setString("painiteFurnace", this.painiteFurnace);
    }
    
    nbt.setInteger("burningTimeleft", this.burningTimeLeft);
    nbt.setInteger("timePassed", this.timePassed);
    
    return nbt;
    }
    
    @Override
    public SPacketUpdateTileEntity getUpdatePacket() {
    NBTTagCompound nbt = new NBTTagCompound();
    this.writeToNBT(nbt);
    int metadata = getBlockMetadata();
    return new SPacketUpdateTileEntity(this.pos, metadata, nbt);
    }
    
    @Override
    public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
    this.readFromNBT(pkt.getNbtCompound());
    }
    
    @Override
    public NBTTagCompound getUpdateTag() {
    NBTTagCompound nbt = new NBTTagCompound();
    this.writeToNBT(nbt);
    return nbt;
    }
    
    @Override
    public void handleUpdateTag(NBTTagCompound tag) {
    this.readFromNBT(tag);
    }
    
    @Override
    public NBTTagCompound getTileData() {
    NBTTagCompound nbt = new NBTTagCompound();
    this.writeToNBT(nbt);
    return nbt;
    }
    
    @Override
    public String getName() {
    return hasCustomName() ? this.painiteFurnace : "tile.painiteFurnace";
    }
    
    @Override
    public boolean hasCustomName() {
    return this.painiteFurnace != null && !this.painiteFurnace.isEmpty();
    }
    
    public void setCustomName(String name) {
    this.painiteFurnace = name;
    }
    
    @Override
    public int getSizeInventory() {
    return this.stacks.size();
    }
    
    @Override
    public boolean isEmpty() {
    for(ItemStack stack : this.stacks){
    if (!stack.isEmpty()){
    return false;
    }
    }
    return true;
    }
    
    @Override
    public ItemStack getStackInSlot(int index) {
    return this.stacks.get(index);
    }
    
    @Override
    public ItemStack decrStackSize(int index, int count) {
    return ItemStackHelper.getAndSplit(this.stacks, index, count);
    }
    
    @Override
    public ItemStack removeStackFromSlot(int index) {
    return ItemStackHelper.getAndRemove(stacks, index);
    }
    
    @Override
    public void setInventorySlotContents(int index, ItemStack stack) {
    this.stacks.set(index, stack);
    
    if (stack.getCount() > this.getInventoryStackLimit()){
    stack.setCount(this.getInventoryStackLimit());
    }
    }
    
    @Override
    public int getInventoryStackLimit() {
    return 64;
    }
    
    @Override
    public boolean isUsableByPlayer(EntityPlayer player) {
    return this.world.getTileEntity(this.pos) != this ? false : player.getDistanceSq((double) this.pos.getX() + 0.5D,
                                                                            (double) this.pos.getY() + 0.5D, 
                                                                            (double) this.pos.getZ() + 0.5D) <= 64.0D;
    }
    
    @Override
    public void openInventory(EntityPlayer player) {
    
    }
    
    @Override
    public void closeInventory(EntityPlayer player) {
    
    }
    
    @Override
    public boolean isItemValidForSlot(int index, ItemStack stack) {
    
    if (index == 5)
    return false;
    
    if (index == 6)
    return false;
    
    return true;
    }
    
    @Override
    public int getField(int id) {
    switch (id){
    case 0:
    return this.burningTimeLeft;
    case 1:
    return this.timePassed;
    }
    return 0;
    }
    
    @Override
    public void setField(int id, int value) {
    switch (id){
    case 0:
    this.burningTimeLeft = value;
    case 1:
    this.timePassed = value;
    }
    
    }
    
    @Override
    public int getFieldCount() {
    return 2;
    }
    
    @Override
    public void clear() {
    for(int i =0; i < this.stacks.size(); i++) {
    this.stacks.set(i, ItemStack.EMPTY);
    }
    }
    
    public boolean hasFuelEmpty() {
    return this.getStackInSlot(2).isEmpty() || this.getStackInSlot(3).isEmpty();
    }
    
    public ItemStack getRecipeResult() {
    return CraftingInit.getRecipeResult(new ItemStack[] {
    this.getStackInSlot(0), this.getStackInSlot(1)});
    }
    
    public boolean canSmelt() {
    ItemStack result = this.getRecipeResult();
    
    if(result != null) {
    ItemStack slot4 = this.getStackInSlot(4);
    
    if (slot4.isEmpty())
    return true;
    if (slot4.getItem() == result.getItem() && slot4.getItemDamage() == result.getItemDamage()) {
    int newStackSize = slot4.getCount() + result.getCount();
    if (newStackSize <= this.getInventoryStackLimit() && newStackSize <= slot4.getMaxStackSize()) {
    return true;
    }
    }
    
    }
    return false;
    }
    
    public void smelt() {
    ItemStack result = this.getRecipeResult();
    
    this.decrStackSize(0, 1);
    this.decrStackSize(0, 1);
    
    ItemStack stack4 = this.getStackInSlot(4);
    
    if (stack4.isEmpty()) {
    this.setInventorySlotContents(4, result.copy());
    }else {
    stack4.setCount(stack4.getCount() + result.getCount());
    }
    }
    
    /** Temps de cuisson de la recette */
    public int getFullRecipeTime() {
    return 200;
    }
    
    /** Temps que dure 1 unité de carburant (ici : 1 planche + 1 blé) */
    public int getFullBurnTime() {
    return 300;
    }
    
    /** Renvoie vrai si le feu est allumé */
    public boolean isBurning() {
    return burningTimeLeft > 0;
    }
    
    @Override
    public void update() {
    
    if (!this.world.isRemote) {
    if (this.isBurning()) {
    this.burningTimeLeft--;
    }
    if (!this.isBurning() && this.canSmelt() && !this.hasFuelEmpty()) {
    this.burningTimeLeft = this.getFullBurnTime();
    this.decrStackSize(2, 1);
    this.decrStackSize(3, 1);
    }
    if (this.isBurning() && this.canSmelt()) {
    this.timePassed++;
    if (timePassed >= this.getFullRecipeTime()) {
    timePassed = 0;
    this.smelt();
    }
    } else {
    timePassed = 0;
    }
    this.markDirty();
    }
    
    }
    }
    

    La classe Container de mon block:

    package fr.breakfight.infectiion21.container;
    
    import fr.breakfight.infectiion21.tileentity.TileEntityPainiteFurnace;
    import net.minecraft.entity.player.EntityPlayer;
    import net.minecraft.entity.player.InventoryPlayer;
    import net.minecraft.inventory.Container;
    import net.minecraft.inventory.IContainerListener;
    import net.minecraft.inventory.IInventory;
    import net.minecraft.inventory.Slot;
    import net.minecraft.item.ItemStack;
    import net.minecraftforge.fml.relauncher.Side;
    import net.minecraftforge.fml.relauncher.SideOnly;
    
    public class ContainerPainiteFurnace extends Container{
    
    private TileEntityPainiteFurnace te;
    private int timePassed = 0;
    private int burnTimeLeft = 0;
    
    public ContainerPainiteFurnace(TileEntityPainiteFurnace te, InventoryPlayer playerInventory){
    this.te = te;
    
    int i;
    this.addSlotToContainer(new Slot(te, 0, 20, 17));
    this.addSlotToContainer(new Slot(te, 1, 38, 17));
    this.addSlotToContainer(new Slot(te, 2, 56, 17));
    this.addSlotToContainer(new Slot(te, 3, 74, 17));
    
    this.addSlotToContainer(new Slot(te, 6, 38, 53));
    this.addSlotToContainer(new Slot(te, 7, 56, 53));
    
    this.addSlotToContainer(new SlotOutput(te, 4, 116, 35));
    this.addSlotToContainer(new SlotOutput(te, 5, 142, 35));
    
    for(i = 0; i < 3; ++i) {
    for(int j = 0; j < 9; ++j) {
    this.addSlotToContainer(new Slot(playerInventory, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
    }
    }
    
    for(i = 0; i < 9; ++i) {
    this.addSlotToContainer(new Slot(playerInventory, i, 8 + i * 18, 142));
    }
    }
    
    @Override
    public boolean canInteractWith(EntityPlayer player) {
    return te.isUsableByPlayer(player);
    }
    
    @Override
    public void addListener(IContainerListener listener) {
    super.addListener(listener);
    listener.sendAllWindowProperties(this, this.te);
    }
    
    @Override
    public void detectAndSendChanges() {
    super.detectAndSendChanges();
    
    for(int i = 0; i < this.listeners.size(); ++i) {
    IContainerListener icontainerlistener = (IContainerListener) this.listeners
    .get(i);
    
    if (this.burnTimeLeft != this.te.getField(0)) {
    icontainerlistener.sendProgressBarUpdate(this, 0,
    this.te.getField(0));
    }
    
    if (this.timePassed != this.te.getField(1)) {
    icontainerlistener.sendProgressBarUpdate(this, 1,
    this.te.getField(1));
    }
    }
    
    this.burnTimeLeft = this.te.getField(0);
    this.timePassed = this.te.getField(1);
    }
    
    @Override
    @SideOnly(Side.CLIENT)
    public void updateProgressBar(int id, int data) {
    this.te.setField(id, data);
    }
    
    @Override
    public ItemStack transferStackInSlot(EntityPlayer playerIn, int index) {
    return ItemStack.EMPTY;
    }
    
    public class SlotOutput extends Slot {
    
    public SlotOutput(IInventory inventoryIn, int index, int xPosition, int yPosition) {
    super(inventoryIn, index, xPosition, yPosition);
    }
    
    @Override
    public boolean isItemValid(ItemStack stack) {
    return false;
    }
    
    }
    
    }
    
    

    Et finalement le GUI de mon block:

    package fr.breakfight.infectiion21.client.gui;
    
    import fr.breakfight.infectiion21.common.BreakfMain;
    import fr.breakfight.infectiion21.container.ContainerPainiteFurnace;
    import fr.breakfight.infectiion21.tileentity.TileEntityPainiteFurnace;
    import net.minecraft.client.gui.inventory.GuiContainer;
    import net.minecraft.client.resources.I18n;
    import net.minecraft.entity.player.InventoryPlayer;
    import net.minecraft.util.ResourceLocation;
    
    public class GuiPainiteFurnace extends GuiContainer{
    
    private TileEntityPainiteFurnace te;
    private InventoryPlayer playerInv;
    
    public GuiPainiteFurnace(TileEntityPainiteFurnace te, InventoryPlayer playerInv) {
    super(new ContainerPainiteFurnace(te, playerInv));
    
    this.xSize = 175;
    this.ySize = 165;
    
    this.te = te;
    this.playerInv = playerInv;
    }
    
    /**@Override
    protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
    
    GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
    this.mc.getTextureManager().bindTexture(new ResourceLocation(BreakfMain.MODID, "textures/gui/container/painitefurnace.png"));
    this.drawTexturedModalRect(this.guiLeft, this.guiTop, 0, 0, this.xSize, this.ySize);
    
    if (this.te.isBurning()) {
    int burningTime = this.te.getField(0);
    int textureHeight = (int) (12f / this.te.getFullBurnTime() * burningTime);
    this.drawTexturedModalRect(this.xSize + 37, this.ySize + 26 + 12 - textureHeight,
    177, 12 - textureHeight, 27, textureHeight);
    }
    }**/
    @Override
    protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
    int i = (this.width - this.xSize) / 2;
    int j = (this.height - this.ySize) / 2;
    this.drawDefaultBackground();
    this.mc.getTextureManager().bindTexture(new ResourceLocation(BreakfMain.MODID, "textures/gui/container/painitefurnace.png"));
    this.drawTexturedModalRect(i, j, 0, 0, this.xSize, this.ySize);
    
    int timePassed = this.te.getField(1);
    int textureWidth = (int) (23f / 200f * timePassed);
    this.drawTexturedModalRect(i + 81, j + 24, 177, 18, textureWidth, 7);
    
    if (this.te.isBurning()) {
    int burningTime = this.te.getField(0);
    int textureHeight = (int) (12f / this.te.getFullBurnTime() * burningTime);
    this.drawTexturedModalRect(i + 37, j + 26 + 12 - textureHeight,
    177, 12 - textureHeight, 27, textureHeight);
    }
    }
    
    @Override
        protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY){
    
    String s = I18n.format("container.painiteFurnace");
    this.fontRendererObj.drawString(s, this.xSize / 2 - this.fontRendererObj.getStringWidth(s) / 2, 6, 4210752); 
    this.fontRendererObj.drawString(this.playerInv.getDisplayName().getFormattedText(), 8, this.ySize - 96 + 2, 4210752); 
    
        }
    }
    
    

    Voilà, voilà ! Merci de votre aide :D</itemstack></itemstack>



  • Personne n'a d'idée 😕 ?


  • Administrateurs

    C'est le shift click qui ne fonctionne pas où le transfert tout court ?

    Car pour le shift + click, c'est normal vu le contenu de ta fonction transferStackInSlot de ton container.



  • Non, vraiment je reste bugué quand je veux déplacer un objet. Le shift click, c'est effectivement normal. Genre je suis pas capable de clicker sur un objet et le déplacer dans le four ou tout simplement dans mon inventaire…



  • Le GuiHandler est-il correct ?
    Et retire également le world.isRemote dans ta fonction onBlockActivated, openGui doit s'effectuer sur les 2 sides.



  • @Plaigon nope openGui peut netre appelé que sur le serveur et ça marche très bien (fait pour) ^^
    Mais le problème c'est sur là tu l'appelles que sur le client, mets donc un "!" devant ton "world.isRemote" dans ton onblockActivated.


  • Administrateurs

    Ah si openGui est appelé côté client au lieu de côté serveur ça explique le problème.