Jump to content

Getting wrong slots?


N1xx1

Recommended Posts

Hello.

 

I'm encountering some problems with GUI and Containers.

 

Basically, my problem is that when I want to get some items from my inventory and put them wherever I want in the TileEntity or I want to move it inside my inventory strange things happen.

 

A simple example:

I have an item on ther fourth slot of my player inventory, I press that slot to move the item, and the as I do that the item return to the slot. I can't pick it in any way. But, if I have an item in the sixteenth slot, as I press the slot I get another item of 8 slots before, and when I place it down it behaves strangely.

 

I think it's caused by some wrong communication between the local server and the client, but I don't know what is wrong.

 

Some code:

 

 

 

Part of common proxy

@Override
    public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
        TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
        if(tileEntity instanceof TileEntitySomething){
            return new ContainerSomething(player.inventory, (TileEntitySomething)tileEntity);
        }
        return null;
    }

    @Override
    public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
        return null;
    }

 

 

 

 

Part of client proxy

@Override
    public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
        TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
        if(tileEntity instanceof TileEntitySomething){
            return new GuiSomething(player.inventory, (TileEntitySomething) tileEntity);
        }
        return null;
    }

 

 

 

 

Part of the block

    @Override
    public TileEntity createNewTileEntity(World world) {
        return new TileEntitySomething();
    }
    
    @Override
    public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int i1, float f1, float f2, float f3) {
        TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
        if (tileEntity == null || player.isSneaking()) {
            return false;
        }
        player.openGui(MyMod.instance, 0, world, x, y, z);
        return true;
    }

 

 

 

 

The container class

public class ContainerSomething extends Container {

    public final TileEntitySomething tileEntity;
    public final InventoryPlayer inventoryPlayer;
    
    public ContainerSomething(InventoryPlayer ip, TileEntitySomething te){
        tileEntity = te;
        inventoryPlayer = ip;
        
        addSlotToContainer(new Slot(tileEntity, 0, 149, 17));
        
        
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 9; j++) {
                //                                           | id         | x         |  y        |
                addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
            }
        }

        for (int i = 0; i < 9; i++) {
            //                                         |id| x         |  y  |
            addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 142));
        }
    }
    
    @Override
    public boolean canInteractWith(EntityPlayer var1) {
        return true;
    }
    
    @Override
    // this is that method to handle shift click without the name, I think I have to update my MCP.
    public ItemStack func_82846_b(EntityPlayer player, int slot) {
        ItemStack stack = null;
        Slot slotObject = (Slot)inventorySlots.get(slot);

        //null checks and checks if the item can be stacked (maxStackSize > 1)
        if (slotObject != null && slotObject.getHasStack()) {
            ItemStack stackInSlot = slotObject.getStack();
            stack = stackInSlot.copy();

            //merges the item into player inventory since its in the tileEntity
            if (slot == 0) {
                if (!mergeItemStack(stackInSlot, 1, 37, true)) {
                    return null;
                }
            } else if (slot > 0) {
                if (!mergeItemStack(stackInSlot, 0, 0, false)) {
                    return null;
                }
            } else {
                return null;
            }

            if (stackInSlot.stackSize == 0) {
                slotObject.putStack(null);
            } else {
                slotObject.onSlotChanged();
            }
            
            if (stackInSlot.stackSize == stack.stackSize) {
                return null;
            }
            
            slotObject.func_82870_a(player, stackInSlot);
        }

        return stack;
    }
    
}

 

 

 

 

The TileEntity class

public class TileEntitySomething extends TileEntity implements IInventory, ISidedInventory {

    private ItemStack[] inv = new ItemStack[1];

    public TileEntitySomething(){
        
    }

    @Override
    public int getSizeInventory() {
        return inv.length;
    }

    @Override
    public ItemStack getStackInSlot(int slot) {
        return inv[slot];
    }

    @Override
    public void setInventorySlotContents(int slot, ItemStack stack) {
        inv[slot] = stack;
        if (stack != null && stack.stackSize > getInventoryStackLimit()) {
            stack.stackSize = getInventoryStackLimit();
        }               
    }

    @Override
    public ItemStack decrStackSize(int slot, int amount) {
        ItemStack stack = getStackInSlot(slot);
        if (stack != null) {
            if (stack.stackSize <= amount) {
                setInventorySlotContents(slot, null);
            } else {
                stack = stack.splitStack(amount);
                if (stack.stackSize == 0) {
                    setInventorySlotContents(slot, null);
                }
            }
        }
        return stack;
    }

    @Override
    public ItemStack getStackInSlotOnClosing(int slot) {
        ItemStack stack = getStackInSlot(slot);
        if (stack != null) {
            setInventorySlotContents(slot, null);
        }
        return stack;
    }

    @Override
    public int getInventoryStackLimit() {
        return 64;
    }

    @Override
    public boolean isUseableByPlayer(EntityPlayer player) {
        return worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) == this && player.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) < 64;
    }

    @Override
    public void openChest() {}

    @Override
    public void closeChest() {}

    @Override
    public void readFromNBT(NBTTagCompound tagCompound) {
        super.readFromNBT(tagCompound);

        NBTTagList tagList = tagCompound.getTagList("Inventory");
        for (int i = 0; i < tagList.tagCount(); i++) {
            NBTTagCompound tag = (NBTTagCompound) tagList.tagAt(i);
            byte slot = tag.getByte("Slot");
            if (slot >= 0 && slot < inv.length) {
                inv[slot] = ItemStack.loadItemStackFromNBT(tag);
            }
        }
    }

    @Override
    public void writeToNBT(NBTTagCompound tagCompound) {
        super.writeToNBT(tagCompound);

        NBTTagList itemList = new NBTTagList();
        for (int i = 0; i < inv.length; i++) {
            ItemStack stack = inv[i];
            if (stack != null) {
                NBTTagCompound tag = new NBTTagCompound();
                tag.setByte("Slot", (byte) i);
                stack.writeToNBT(tag);
                itemList.appendTag(tag);
            }
        }
        tagCompound.setTag("Inventory", itemList);
    }

    @Override
    public String getInvName() {
        return new StringBuilder().append(MyMod.id).append(".tile.something").toString();
    }

    @Override
    public int getStartInventorySide(ForgeDirection side) {
        return 0;
    }

    @Override
    public int getSizeInventorySide(ForgeDirection side) {
        return 64;
    }
}

 

 

 

 

And the GUI constructor (of course it extends GuiContainer)

    public GuiSomething(InventoryPlayer playerinventory, TileEntitySomething tileentity){
        super(new ContainerSomething(playerinventory, tileentity));
    }

 

 

 

Well, I have no idea of what I'm missing...

 

Thank you.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.