Jump to content

[1.7.2]Custom Workbench Container Class


Skytanic

Recommended Posts

I have a hopefully easy to answer question. I want to add a slot to this machine using

 

this.addSlotToContainer(new Slot(MolecularAssemblerTileEntity, ID, xCoord, yCoord);

 

Now, I have a few questions, first off, how do the ID's between the tile entity slots, the slotCrafting slots, and the player inventory slots work? Because in my code (which I took from vanilla) it assigns the id 0 to the following slots

 

SlotCrafting( the first slot added)

craftMatrix(the second slot added)

action bar (the first slot added in the third loop down)

 

So my question is how are these handled exactly? Obviously they can't all be assigned to 0 otherwise the transferStackInSlot method would not work correctly.

 

My next question is this, if I were to add the slot I want to (the first MolecularAssemblerTileEntitySlot) and assigned it to 0 as well, how would that be handled in relation to the rest? Would it "push" the other slots by one?

 

And finally, if I did add this slot, how would I alter my transferStackInSlot method to compensate for the "push" mentioned above (assuming that is what is happening).

 

 

I have tried this already and found out that with both the Tile Entity slot and SlotCrafting slot assigned to 0, when I debug in transferStackInSlot it seems that the Tile Entity slot is read as 0 and the SlotCrafting slot is read as 1. However, if I for example use

 

if (par2 == 0) // Should take what is in the Tile Entity slot and place it in the inventory
            {
                if (!this.mergeItemStack(itemstack1, 11, 47, false))
                {
                    return null;
                }
            }

if (par2 == 1) // Identical to the original if check in the code below, but functions differently
            {
                if (!this.mergeItemStack(itemstack1, 11, 47, true))
                {
                    return null;
                }

                slot.onSlotChange(itemstack1, itemstack);
            }

 

first of all, the first if check does place the item from the TileEntity slot into my inventory, but leaves a copy in the slot that I have to manually take out. The second if check should function as normal because the only change is that I added one more slot (which I accommodated for by changing 10 and 46 to 11 and 47) but instead, moves the result to my inventory but does not remove the item used to craft it.

 

I'm sorry, I realize how confusing I'm making this sound but it is really hard for me to describe the actual problems. If you need clarification on anything please ask!!

 

Container Class:

package skytanic.atomicmanipulation.blocks.magui;

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;
import skytanic.atomicmanipulation.blocks.crafting.MolecularAssemblerCraftingManager;
import skytanic.atomicmanipulation.blocks.tileentity.MolecularAssemblerTileEntity;
import skytanic.atomicmanipulation.common.AtomicManipulation;



public class MolecularAssemblerContainer extends Container
{
    /** The crafting matrix inventory (3x3). */
    public InventoryCrafting craftMatrix = new InventoryCrafting(this, 3, 3);
    public IInventory craftResult = new InventoryCraftResult();
    private World worldObj;
    private int posX;
    private int posY;
    private int posZ;

    public MolecularAssemblerContainer(InventoryPlayer par1InventoryPlayer, MolecularAssemblerTileEntity entity, World par2World, int par3, int par4, int par5)
    {
    	worldObj = par2World;
        this.posX = par3;
        this.posY = par4;
        this.posZ = par5;
        
        this.addSlotToContainer(new SlotCrafting(par1InventoryPlayer.player, this.craftMatrix, this.craftResult, 0, 121, 35));

        for (int i = 0; i < 3; ++i)
        {
            for (int j = 0; j < 3; ++j)
            {
                this.addSlotToContainer(new Slot(this.craftMatrix, j + i * 3, 39 + j * 18, 17 + i * 18));
            }
        }

        for (int i = 0; i < 3; ++i)
	{
		for (int j = 0; j < 9; ++j)
		{
			this.addSlotToContainer(new Slot(par1InventoryPlayer, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
		}
	}

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

        this.onCraftMatrixChanged(this.craftMatrix);
    }
    
    public void onCraftMatrixChanged(IInventory iinventory){
    	craftResult.setInventorySlotContents(0, MolecularAssemblerCraftingManager.getInstance().findMatchingRecipe(craftMatrix, worldObj));
    }

    /**
     * Called when the container is closed.
     */
    public void onContainerClosed(EntityPlayer par1EntityPlayer)
    {
        super.onContainerClosed(par1EntityPlayer);

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

                if (itemstack != null)
                {
                    par1EntityPlayer.dropPlayerItemWithRandomChoice(itemstack, false);
                }
            }
        }
    }

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

    /**
     * Called when a player shift-clicks on a slot. You must override this or you will crash when someone does that.
     */
    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;
    }
}

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.