Jump to content

[Solved]Transferring itemStack on shift click without loosing items


Recommended Posts

Posted

Hello,

I created container with 5 slots following tutorial here on forge forums.

 

I also properly(at least I hope so) modified transferStackInSlot method. The problem is that I want my container to have capacity only 1 per slot (not 64). I set this using getInventoryStackLimit in my TileEntity for the container.

 

The problem is that each time I transfer stack from player inventory into the container holding shift, it removes whole stack from player inventory, but transfers only 1 piece into the container(obviously because the limit per slot is set to 1).

 

I would like to know how to force it to pick only so many pieces that fits into container for transferring(when using shift). If this would be too difficult, I would simply like to do it so it picks only one item even when using shift.

 

I guess I need to rewrite container method mergeItemStack? Or maybe something different?

 

Thanks in advance to anyone who tries to help :)

 

Here is my transferStackInSlot method if needed

 

 

public ItemStack transferStackInSlot(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 < 5)
         {
             if (!this.mergeItemStack(stackInSlot, 5, 41, true))
             {
                 return null;
             }
         }
         //places it into the tileEntity is possible since its in the player inventory
         else if (!this.mergeItemStack(stackInSlot, 0, 5, false))
         {
         return null;
         }

         if (stackInSlot.stackSize == 0)
         {
             slotObject.putStack(null);
         }
         else
         {
             slotObject.onSlotChanged();
         }

         if (stackInSlot.stackSize == stack.stackSize)
         {
             return null;
         }
         slotObject.onPickupFromSlot(player, stackInSlot);
     }
     return stack;
}

 

 

Sorry for mistakes, English is not my mother tongue

Posted

You could try checking the stack size with stack.stackSize, and check if it equals 1, otherwise subtract 1 from the stack, and add a new stack to the slot, then check if there is an empty slot and if there is, the repeat

Hope this helps, I'm still newbish to modding

"you seem to be THE best modder I've seen imo."

~spynathan

 

ლ(́◉◞౪◟◉‵ლ

Posted

I think I figured it out.

 

You don't need to change anything from the defaults except for the following:

change the TileEntity.getInventoryStackLimit to return 1, or any value you want.

 

Then in your Container class (ContainerTiny in the tutorial) add the following function:

 

 

@Override

        public boolean mergeItemStack(ItemStack stack, int begin, int end, boolean backwards) {

        int i = backwards ? end-1 : begin, increment = backwards ? -1 : 1;

boolean flag = false;

while(stack.stackSize > 0 && i >= begin && i < end) {

Slot slot = this.getSlot(i);

ItemStack slotStack = slot.getStack();

int slotStacklimit = i<tileEntity.getSizeInventory() ? tileEntity.getInventoryStackLimit() : 64;

int totalLimit = slotStacklimit < stack.getMaxStackSize() ? slotStacklimit : stack.getMaxStackSize();

 

if(slotStack==null) {

int transfer = totalLimit < stack.stackSize ? totalLimit : stack.stackSize;

ItemStack stackToPut = stack.copy();

stackToPut.stackSize = transfer;

slot.putStack(stackToPut);

slot.onSlotChanged();

stack.stackSize -= transfer;

flag = true;

}

else if(slotStack.itemID == stack.itemID && (!stack.getHasSubtypes() || stack.getItemDamage() == slotStack.getItemDamage()) && ItemStack.areItemStackTagsEqual(stack, slotStack)) {

int maxTransfer = totalLimit - slotStack.stackSize;

int transfer = maxTransfer > stack.stackSize ? stack.stackSize : maxTransfer;

slotStack.stackSize += transfer;

slot.onSlotChanged();

stack.stackSize -= transfer;

flag = true;

}

 

 

i += increment;

}

 

        return flag;

       

        }

 

 

 

Basically, this function was called from Container, which did not care about the value of tileEntity.getInventoryStackSize() at all.

I made it so when you shift right click the container will be filled as much as possible. You can change that in the code above pretty easily.

 

Hope that helps

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.