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

[1.12.2] My custom crafting table inventory is working odd


Recommended Posts

I have everything handled and in order for this to work, and everything does work, except for the inventory. When i click on some slots and/or items in the inventory, they move to other places inside the inventory or duplicates or something else. I have no idea why this happens and would very much appreciate some help here. 

The block:

Quote

public class BlockCyclopsSmithy extends BlockBase
{
    public BlockCyclopsSmithy(String unlocalizedName, Material materialIn) 
    {
        super(unlocalizedName, materialIn);
        this.setSoundType(SoundType.STONE);
        this.setDefaultState(this.blockState.getBaseState());
    }
    
    @Override
    public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) 
    {
        if(worldIn.isRemote)
        {
            playerIn.openGui(MainClass.instance, MainClass.GUI_ENUM.CYCLOPS_SMITHY.ordinal(), worldIn, pos.getX(), pos.getY(), pos.getZ());
        }
        
        return true;
    }

The Container:

Quote

public class ContainerCyclopsSmithy extends Container
{
    /** The crafting matrix inventory (3x3). */
    public InventoryCrafting craftMatrix = new InventoryCyclopsSmithy(this, 3, 3);
    public InventoryCraftResult craftResult = new InventoryCraftResult();
    private final World world;
    /** Position of the workbench */
    private final BlockPos pos;
    private final EntityPlayer player;
    
    public ContainerCyclopsSmithy(InventoryPlayer playerInventory, World parWorld, BlockPos posIn) 
    {
               this.world = parWorld;
            this.pos = posIn;
            this.player = playerInventory.player;
            this.addSlotToContainer(new SlotCrafting(playerInventory.player, this.craftMatrix, this.craftResult, 0, 124, 35));

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

            for (int k = 0; k < 3; ++k)
            {
                for (int i1 = 0; i1 < 9; ++i1)
                {
                    this.addSlotToContainer(new Slot(playerInventory, i1 + k * 9 + 9, 8 + i1 * 18, 84 + k * 18));
                }
            }

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

        /**
         * Callback for when the crafting matrix is changed.
         */
        public void onCraftMatrixChanged(IInventory inventoryIn)
        {
            this.slotChangedCraftingGrid(this.world, this.player, this.craftMatrix, this.craftResult);
        }

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

            if (!this.world.isRemote)
            {
                this.clearContainer(playerIn, this.world, this.craftMatrix);
            }
        }

        /**
         * Determines whether supplied player can use this container
         */
        public boolean canInteractWith(EntityPlayer playerIn)
        {
            if (this.world.getBlockState(this.pos).getBlock() != EtauricBlocks.CYCLOPS_SMITHY)
            {
                return false;
            }
            else
            {
                return playerIn.getDistanceSq((double)this.pos.getX() + 0.5D, (double)this.pos.getY() + 0.5D, (double)this.pos.getZ() + 0.5D) <= 64.0D;
            }
        }

        /**
         * Handle when the stack in slot {@code index} is shift-clicked. Normally this moves the stack between the player
         * inventory and the other inventory(s).
         */
        public ItemStack transferStackInSlot(EntityPlayer playerIn, int index)
        {
            ItemStack itemstack = ItemStack.EMPTY;
            Slot slot = this.inventorySlots.get(index);

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

                if (index == 0)
                {
                    itemstack1.getItem().onCreated(itemstack1, this.world, playerIn);

                    if (!this.mergeItemStack(itemstack1, 10, 46, true))
                    {
                        return ItemStack.EMPTY;
                    }

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

                if (itemstack1.isEmpty())
                {
                    slot.putStack(ItemStack.EMPTY);
                }
                else
                {
                    slot.onSlotChanged();
                }

                if (itemstack1.getCount() == itemstack.getCount())
                {
                    return ItemStack.EMPTY;
                }

                ItemStack itemstack2 = slot.onTake(playerIn, itemstack1);

                if (index == 0)
                {
                    playerIn.dropItem(itemstack2, false);
                }
            }

            return itemstack;
        }

        /**
         * Called to determine if the current slot is valid for the stack merging (double-click) code. The stack passed in
         * is null for the initial slot that was double-clicked.
         */
        public boolean canMergeSlot(ItemStack stack, Slot slotIn)
        {
            return slotIn.inventory != this.craftResult && super.canMergeSlot(stack, slotIn);
        }

The Inventory:

Quote

public class InventoryCyclopsSmithy extends InventoryCrafting
{
    public InventoryCyclopsSmithy(Container eventHandlerIn, int width, int height) 
    {
        super(eventHandlerIn, width, height);
    }
}

Basically all of it is copy-pasted from the normal workbench, but with my own block instead. 

Edited by Triphion
Link to post
Share on other sites
2 hours ago, Triphion said:

if(worldIn.isRemote)
        {
            playerIn.openGui(MainClass.instance, MainClass.GUI_ENUM.CYCLOPS_SMITHY.ordinal(), worldIn, pos.getX(), pos.getY(), pos.getZ());
        }

I think this has to be if(!worldIn.isRemote) 

Because you want to do the following action if the statement is NOT true

 

EDIT: Just checked the workbench class, you should check the onBlockActivated method again.

You forgot an else statement

Edited by winnetrie
Link to post
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.

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.



×
×
  • Create New...

Important Information

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