net.minestom.server.inventory.TransactionOption Maven / Gradle / Ivy
Show all versions of minestom-snapshots Show documentation
package net.minestom.server.inventory;
import net.minestom.server.item.ItemStack;
import org.jetbrains.annotations.NotNull;
import java.util.Map;
@FunctionalInterface
public interface TransactionOption {
/**
* Place as much as the item as possible.
*
* The remaining, can be air.
*/
TransactionOption ALL = (inventory, result, itemChangesMap) -> {
itemChangesMap.forEach(inventory::safeItemInsert);
return result;
};
/**
* Only place the item if can be fully added.
*
* Returns true if the item has been added, false if nothing changed.
*/
TransactionOption ALL_OR_NOTHING = (inventory, result, itemChangesMap) -> {
if (result.isAir()) {
// Item can be fully placed inside the inventory, do so
itemChangesMap.forEach(inventory::safeItemInsert);
return true;
} else {
// Inventory cannot accept the item fully
return false;
}
};
/**
* Loop through the inventory items without changing anything.
*
* Returns true if the item can be fully added, false otherwise.
*/
TransactionOption DRY_RUN = (inventory, result, itemChangesMap) -> result.isAir();
@NotNull T fill(@NotNull AbstractInventory inventory,
@NotNull ItemStack result,
@NotNull Map<@NotNull Integer, @NotNull ItemStack> itemChangesMap);
default @NotNull T fill(@NotNull TransactionType type,
@NotNull AbstractInventory inventory,
@NotNull ItemStack itemStack) {
var pair = type.process(inventory, itemStack);
return fill(inventory, pair.left(), pair.right());
}
}