public bool addItem(itemStack input) { foreach (itemStack stack in inventoryContents) { if (stack.isEmpty()) { stack.setStack(input); return(true); } else { if (itemStack.areItemsEqual(input, stack)) { if (stack.canAddTo(input.getCount())) { stack.increaseAmount(input.getCount()); return(true); } else { int difference = (stack.getCount() + input.getCount()) - stack.getItem().maxStack; stack.setCount(stack.getItem().maxStack); input.setCount(difference); } } } } return(false); }
public void setSlot(inventory attachedInventory, int SlotID, container attachedContainer) { this.SlotID = SlotID; this.attachedContainer = attachedContainer; myStack = attachedInventory.getStackInSlot(SlotID); inventoryManager = inventoryManager.INSTANCE; updateSlot(); }
// splits stack public itemStack splitStack(int amount) { int i = Mathf.Min(amount, count); itemStack copiedStack = this.copy(); copiedStack.setCount(i); this.decreaseAmount(i); Debug.Log("Split"); return(copiedStack); }
public void OnPointerDown(PointerEventData eventData) { itemStack currentDraggedStack = inventoryManager.getDraggedItem(); itemStack stackCopy = myStack.copy(); if (eventData.pointerId == -1) { onLeftClick(currentDraggedStack, stackCopy); } //throw new System.NotImplementedException(); }
private void onLeftClick(itemStack currentDraggedItem, itemStack itemCopy) { if (!myStack.isEmpty() && currentDraggedItem.isEmpty()) { inventoryManager.setDragged(itemCopy); this.setSlotContents(itemStack.Empty); } if (myStack.isEmpty() && !currentDraggedItem.isEmpty()) { this.setSlotContents(currentDraggedItem); inventoryManager.setDragged(itemStack.Empty); } if (!myStack.isEmpty() && !currentDraggedItem.isEmpty()) { if (itemStack.areItemsEqual(itemCopy, currentDraggedItem)) { if (itemCopy.canAddTo(currentDraggedItem.getCount())) { itemCopy.increaseAmount(currentDraggedItem.getCount()); this.setSlotContents(itemCopy); inventoryManager.setDragged(itemStack.Empty); } else { int difference = (itemCopy.getCount() + currentDraggedItem.getCount()) - itemCopy.getItem().maxStack; itemCopy.setCount(myStack.getItem().maxStack); itemStack dragCopy = currentDraggedItem.copy(); dragCopy.setCount(difference); this.setSlotContents(itemCopy); inventoryManager.setDragged(dragCopy); } } else { itemStack currentDragCopy = currentDraggedItem.copy(); this.setSlotContents(currentDraggedItem); inventoryManager.setDragged(itemCopy); } } }
private void setSlotContents(itemStack stackIn) { myStack.setStack(stackIn); updateSlot(); }
public void setDragged(itemStack itemIn) { dragItem.setDraggedItem(currentDraggedItem = itemIn); }
public void setDraggedItem(itemStack stackIn) { myStack = stackIn; }
// makes a stack public void setStack(itemStack itemIn) { this.item = itemIn.getItem(); this.count = itemIn.getCount(); }
// check item stacks are equal or not, if both a and b are not empty return false public static bool areItemsEqual(itemStack stackA, itemStack stackB) { return(stackA == stackB ? true : (!stackA.isEmpty() && !stackB.isEmpty() ? stackA.isItemEqual(stackB) : false)); }
//checks if items can stack or not public bool isItemEqual(itemStack stackIn) { //Debug.Log("Stacking"); return(!stackIn.isEmpty() && this.item == stackIn.getItem()); }