Originally Posted by
Kris
E.g. being able to(this line is how it works on my server):
player.getInventory().addItem(new Item(11802, 5)).onFailure(remainderItem -> World.spawnFloorItem(remainderItems, player, location));
What does your Inventory method addItem(Item i) return?
Some derived functional interface type?
You could perhaps consider organizing your callback functions internally in your
Inventory class in order to slightly increase readability in external calls, i.e.
Code:
// Somewhere external to Inventory.java ...
player.getInventory().addItem(new Item(995, 1), CallbackPolicy.AT_FEET);
// In Inventory.jjava
public void addItem(Item i, CallbackPolicy policy) {
if(policy == CallbackPolicy.AT_FEET)
addItemDropRemainder(i);
else doSomeOtherAddItem(i);
}
private void addItemDropRemainder(Item i) {
// your onFailure logic here.
}
You can of course have many different CallbackPolicy types, but I can't imagine
needing more than 2-3.
In any event I like your functional approach.
Edit:
Extending upon this, if you wanted to really get fancy for custom callbacks...
Code:
//externally
player.getInventory().addItem(new Item(995, 345), remainder -> {
Logging.info("There was some left over... " + remainder.getAmount());
})
// internally
public void addItem(Item i, Consumer<Item> callback) {
// do logic to get remainder that couldn't fit...
Item remainder = ...
callback.accept(remainder);
}