Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slot constraints #76

Open
ye-yu opened this issue Jul 20, 2020 · 2 comments
Open

Slot constraints #76

ye-yu opened this issue Jul 20, 2020 · 2 comments
Labels
enhancement New feature or request

Comments

@ye-yu
Copy link

ye-yu commented Jul 20, 2020

I just found out about WItemSlot$setInsertingAllowed and WItemSlot$setTakingAllowed, and honestly I felt dumb for not investigating more on the widget itself.

However, I am still requesting to add slot capacity as the feature enhancement slot constraint. The desired effect on setting slot capacity is that:

  1. If the incoming stack is combineable with the slot stack, then fill up the slot and the extra stack is put into the player's cursor
  2. If the slot stack is empty, then fill up the slot and the extra stack is put into the player's cursor
  3. If the incoming stack is not combineable with the slot stack, drop the original slot stack, and then fill up the slot and the extra stack is put into the player's cursor

The use case is too specific, but I really appreciate it if the feature can be added. As of currently, I am using my original workaround to solve my problem.

Original post

In SyncedGuiDescription, I made a lot of constraints for the blockInventory slot of my GUI, e.g. allowing only certain items to be put into the slot, or defining the slot capacity, etc. Right now, I did the checking in onSlotClick for when user place an item, pick up an item, swap an item, and quick moving an item, and from that, I did a lot of copy-pasting of codes.

For the feature request, I'd like to have something like a slot constraint variable so that I can define in my SyncedGuiDescription constructor like

// can only place cobblestone in this slot 
slotPredicate.set(0, (incomingStack, currentStack) -> incomingStack.item == Items.COBBLESTONE)  

// can only pickup in this slot 
slotPredicate.set(1, (incomingStack, currentStack) -> incomingStack.isEmpty())

// can only place at most 5 items in this slot
slotCapacity.set(0, 5)

and maybe an example of implementation in SyncedGuiDescription$onSlotClick is

...
// when player is placing item into a slot
ItemStack incomingStack = playerInventory.getCursorStack()
ItemStack currentStack = slots[slotNumber].getStack()
if (!slotPredicate.get(slotNumber).test(incomingStack, currentStack)
    return ItemStack.EMPTY
int capacity = slotCapacity.get(slotNumber)
// do some capacity checking here
...
// when player is shift-clicking a slot
ItemStack incomingStack = getFirstEmptySlot(slots).getStack()
ItemStack currentStack = slots[slotNumber].getStack()
if (!slotPredicate.get(slotNumber).test(incomingStack, currentStack)
    return ItemStack.EMPTY
...
@ye-yu ye-yu added the enhancement New feature or request label Jul 20, 2020
@ye-yu
Copy link
Author

ye-yu commented Jul 20, 2020

i will be forking but i wont do PR because i really need immediate implementation (lol)

@ye-yu
Copy link
Author

ye-yu commented Jul 20, 2020

Relevant compare

Sorry with the messy compare, I accidentally auto-formatted on my IDE.

I did changes only to onSlotClick, insertIntoExisting and insertIntoEmpty methods, and added some relevant fields + methods. slotPredicates is for whether or not the item can be placed into the slot or not, slotCapacity is the slot capacity.

Perhaps, is there a better way of adding this feature?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant