mirror of
https://github.com/Minestom/Minestom.git
synced 2024-06-25 22:14:53 +02:00
48 lines
1.7 KiB
Java
48 lines
1.7 KiB
Java
package net.minestom.server.inventory;
|
|
|
|
import net.minestom.server.item.ItemStack;
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import java.util.Map;
|
|
|
|
@FunctionalInterface
|
|
public interface TransactionOption<T> {
|
|
|
|
/**
|
|
* Performs as much of the operation as is possible.
|
|
* Returns the remaining item in the operation (can be air).
|
|
*/
|
|
TransactionOption<ItemStack> ALL = (inventory, result, itemChangesMap) -> {
|
|
itemChangesMap.forEach(inventory::setItemStack);
|
|
return result;
|
|
};
|
|
|
|
/**
|
|
* Performs the operation atomically (only if the operation resulted in air), returning whether the operation
|
|
* was performed.
|
|
*/
|
|
TransactionOption<Boolean> ALL_OR_NOTHING = (inventory, result, itemChangesMap) -> {
|
|
if (result.isAir()) {
|
|
// Item can be fully placed inside the inventory, do so
|
|
itemChangesMap.forEach(inventory::setItemStack);
|
|
return true;
|
|
} else {
|
|
// Inventory cannot accept the item fully
|
|
return false;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Discards the result of the operation, returning whether the operation could have finished.
|
|
*/
|
|
TransactionOption<Boolean> DRY_RUN = (inventory, result, itemChangesMap) -> result.isAir();
|
|
|
|
@NotNull
|
|
T fill(@NotNull Inventory inventory, @NotNull ItemStack result, @NotNull Map<Integer, ItemStack> itemChangesMap);
|
|
|
|
default @NotNull T fill(@NotNull TransactionType type, @NotNull Inventory inventory, @NotNull ItemStack itemStack) {
|
|
final TransactionType.Entry result = type.apply(itemStack, inventory::getItemStack);
|
|
return fill(inventory, result.remaining(), result.changes());
|
|
}
|
|
}
|