Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

items disappearing from slot when container is closed


bigbaddevil6
 Share

Recommended Posts

??? I provided code in my last thread

http://www.minecraftforge.net/forum/index.php/topic,25281.msg128794.html#msg128794

 

before that code I had none to show. Just asking concept designs.

 

Although I do admit I failed providing the code this time around.

 

Here is the Container

 

 

package com.bigbaddevil7.rustic.client.interfaces;


import com.bigbaddevil7.rustic.init.ModBlocks;
import com.bigbaddevil7.rustic.tileentity.TileEntityTable;
import com.bigbaddevil7.rustic.utility.LogHelper;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryCraftResult;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.inventory.Slot;
import net.minecraft.inventory.SlotCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;

public class ContainerTable extends Container {

    public InventoryCrafting craftMatrix;
    //public static InventoryCrafting toolSlot;
    public IInventory craftResult;
    private static TileEntityTable toolSlot;
    private World worldObj;
    private int posX, posY, posZ;

    public ContainerTable(InventoryPlayer invPlayer, World world, int x, int y, int z){
        craftMatrix = new InventoryCrafting(this,5 ,5);
        toolSlot = new TileEntityTable(this, 2, 2);
        craftResult = new InventoryCraftResult();
        worldObj = world;
        posX = x;
        posY = y;
        posZ = z;

        this.addSlotToContainer(new SlotCrafting(invPlayer.player, craftMatrix, craftResult, 0, 141, 11));


        for(int i = 0; i < 4; i++){
            for(int k = 0; k < 4; k++){
                this.addSlotToContainer(new SlotTool(toolSlot, k + i * 4, 115  + k * 18, 43 + i * 18));
            }
        }



        for(int i = 0; i < 5; i++){
            for(int k = 0; k < 5; k++){
                this.addSlotToContainer(new Slot(craftMatrix, k + i * 5, 8 + k * 18, 7 + i * 18));
            }
        }

        for(int i = 0; i < 3; i++){
            for(int k  = 0; k < 9; k++){
                this.addSlotToContainer(new Slot(invPlayer, k + i * 9 + 9, 8 + k * 18, 106 + i * 18));
            }
        }

        for(int i = 0; i < 9; i++){
            this.addSlotToContainer(new Slot(invPlayer, i, 8 + i * 18, 164));
        }

        onCraftMatrixChanged(craftMatrix);
    }

    public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2)
    {
        ItemStack itemstack = null;
        Slot slot = (Slot)this.inventorySlots.get(par2);

        if (slot != null && slot.getHasStack())
        {
            ItemStack itemstack1 = slot.getStack();
            itemstack = itemstack1.copy();

            if (par2 == 0)
            {
                if (!this.mergeItemStack(itemstack1, 10, 46, true))
                {
                    return null;
                }

                slot.onSlotChange(itemstack1, itemstack);
            }
            else if (par2 >= 10 && par2 < 37)
            {
                if (!this.mergeItemStack(itemstack1, 37, 46, false))
                {
                    return null;
                }
            }
            else if (par2 >= 37 && par2 < 46)
            {
                if (!this.mergeItemStack(itemstack1, 10, 37, false))
                {
                    return null;
                }
            }
            else if (!this.mergeItemStack(itemstack1, 10, 46, false))
            {
                return null;
            }

            if (itemstack1.stackSize == 0)
            {
                slot.putStack((ItemStack)null);
            }
            else
            {
                slot.onSlotChanged();
            }

            if (itemstack1.stackSize == itemstack.stackSize)
            {
                return null;
            }

            slot.onPickupFromSlot(par1EntityPlayer, itemstack1);
        }

        return itemstack;
    }

    public void onContainerClosed(EntityPlayer par1EntityPlayer)
    {
        //super.onContainerClosed(par1EntityPlayer);

        if (!this.worldObj.isRemote)
        {
            for (int i = 0; i < 25; ++i)
            {
                ItemStack itemstack = this.craftMatrix.getStackInSlotOnClosing(i);

                if (itemstack != null)
                {
                    par1EntityPlayer.dropPlayerItemWithRandomChoice(itemstack, false);
                }
            }
            /*for(int i = 0; i < 4; i++) {
                ItemStack itemStack2 = this.toolSlot.getStackInSlotOnClosing(i);
                if (itemStack2 != null) {
                    par1EntityPlayer.dropPlayerItemWithRandomChoice(itemStack2, false);
                }
            }*/
        }
    }

    public void onCraftMatrixChanged(IInventory inventory){
        craftResult.setInventorySlotContents(0, CraftingManagerTable.getInstance().findMatchingRecipe(craftMatrix, worldObj));
    }


    @Override
    public boolean canInteractWith(EntityPlayer player) {
        if(worldObj.getBlock(posX, posY, posZ) != ModBlocks.blockTable){
            return false;
        }else{
            return player.getDistanceSq((double)posX + 0.5D, (double)posY + 0.5D, (double)posZ + 0.5D) <= 64.0D;
        }
    }

    public static boolean toolInSlot(){
        boolean hasItem = true;
        for(int i = 0; i < 4; i++) {
            ItemStack slot = toolSlot.getStackInSlot(i);
            if (slot == null) {
                hasItem = false;
            }
        }
        return hasItem;
    }
}

 

 

 

Here is the TileEntity that acts for the slots

 

 

package com.bigbaddevil7.rustic.tileentity;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;

/**
* Created by bigbaddevil7 on 11/15/2014.
*/
public class TileEntityTable extends TileEntity implements IInventory {

    private ItemStack[] stackList;
    private int inventoryWidth;
    private static Container eventHandler;

    public TileEntityTable(){};

    public TileEntityTable(Container container, int x, int y){
        int k = x * y;
        this.stackList = new ItemStack[k];
        this.eventHandler = container;
        this.inventoryWidth = x;
    }

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

    @Override
    public ItemStack getStackInSlot(int slot) {
        return slot >= this.getSizeInventory() ? null : this.stackList[slot];
    }


    /**
     * Returns the itemstack in the slot specified (Top left is 0, 0). Args: row, column
     */
    public ItemStack getStackInRowAndColumn(int par1, int par2)
    {
        if (par1 >= 0 && par1 < this.inventoryWidth)
        {
            int k = par1 + par2 * this.inventoryWidth;
            return this.getStackInSlot(k);
        }
        else
        {
            return null;
        }
    }


    @Override
    public ItemStack decrStackSize(int par1, int par2)
    {
        if (this.stackList[par1] != null)
        {
            ItemStack itemstack;

            if (this.stackList[par1].stackSize <= par2)
            {
                itemstack = this.stackList[par1];
                this.stackList[par1] = null;
                this.eventHandler.onCraftMatrixChanged(this);
                return itemstack;
            }
            else
            {
                itemstack = this.stackList[par1].splitStack(par2);

                if (this.stackList[par1].stackSize == 0)
                {
                    this.stackList[par1] = null;
                }

                this.eventHandler.onCraftMatrixChanged(this);
                return itemstack;
            }
        }
        else
        {
            return null;
        }
    }

    @Override
    public ItemStack getStackInSlotOnClosing(int par1) {
        if (this.stackList[par1] != null)
        {
            ItemStack itemstack = this.stackList[par1];
            this.stackList[par1] = null;
            return itemstack;
        }
        else
        {
            return null;
        }
    }

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

        NBTTagList items = new NBTTagList();

        for(int i = 0; i < getSizeInventory(); i++){
            ItemStack stack = getStackInSlot(i);

            if(stack != null){
                NBTTagCompound item = new NBTTagCompound();
                item.setByte("Slot", (byte)i);
                stack.writeToNBT(item);
                items.appendTag(item);
            }
        }

        compound.setTag("Items", items);
    }


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

        NBTTagList items = compound.getTagList("Items", 10);

        for(int i = 0; i < items.tagCount(); i++){
            NBTTagCompound item = (NBTTagCompound)items.getCompoundTagAt(i);
            int slot = item.getByte("Slot");

            if(slot >= 0 && slot <getSizeInventory()){
                setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(item));
            }
        }
    }

    @Override
    public void setInventorySlotContents(int par1, ItemStack itemStack) {
        this.stackList[par1] = itemStack;
        this.eventHandler.onCraftMatrixChanged(this);

    }

    @Override
    public String getInventoryName() {
        return "container.prototyping table";
    }

    @Override
    public boolean hasCustomInventoryName() {
        return false;
    }

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

    @Override
    public boolean isUseableByPlayer(EntityPlayer var1) {
        return true;
    }

    @Override
    public void openInventory() {}

    @Override
    public void closeInventory() {}

    @Override
    public boolean isItemValidForSlot(int var1, ItemStack var2) {
        return false;
    }
}
[/spoiler]

 

Link to comment
Share on other sites

if you know what you are doing.

 

Yea... sounds tricky, although I think I may have an understanding of what you mean by it, would it be something along the lines of making two methods in the Container called buildContainer() and buildTileEntity() to handle the different types and making of different slots then call them accordingly?

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
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.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.