use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project LanternServer by LanternPowered.
the class VanillaContainerInteractionBehavior method updateCraftingGrid.
private void updateCraftingGrid(Player player, CraftingInventory craftingInventory, MatrixResult matrixResult, List<SlotTransaction> transactions) {
final CraftingMatrix matrix = matrixResult.getCraftingMatrix();
final CraftingGridInventory grid = craftingInventory.getCraftingGrid();
for (int x = 0; x < matrix.width(); x++) {
for (int y = 0; y < matrix.height(); y++) {
final ItemStack itemStack = matrix.get(x, y);
final Slot slot = grid.getSlot(x, y).get();
transactions.add(new SlotTransaction(slot, slot.peek().map(LanternItemStackSnapshot::wrap).orElse(LanternItemStackSnapshot.none()), LanternItemStackSnapshot.wrap(itemStack)));
}
}
final CauseStack causeStack = CauseStack.current();
causeStack.addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.DROPPED_ITEM);
final Transform<World> transform = player.getTransform();
final List<Entity> entities = LanternEventHelper.handlePreDroppedItemSpawning(matrixResult.getRest().stream().map(itemStack -> new Tuple<ItemStackSnapshot, Transform<World>>(LanternItemStackSnapshot.wrap(itemStack), transform)).collect(Collectors.toList()));
final SpawnEntityEvent event = SpongeEventFactory.createDropItemEventDispense(causeStack.getCurrentCause(), entities);
Sponge.getEventManager().post(event);
// Spawn all the entities in the world if the event isn't cancelled
LanternWorld.finishSpawnEntityEvent(event);
}
use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project LanternServer by LanternPowered.
the class AbstractChildrenInventory method peekOffer.
@Override
public PeekedOfferTransactionResult peekOffer(ItemStack itemStack) {
checkNotNull(itemStack, "itemStack");
final PeekedOfferTransactionResult peekResult;
final Set<Inventory> processed = new HashSet<>();
final Inventory inventory = query(QueryOperationTypes.ITEM_STACK_IGNORE_QUANTITY.of(itemStack));
if (inventory instanceof AbstractChildrenInventory) {
peekResult = ((AbstractChildrenInventory) inventory).peekOffer(itemStack, processed, true);
if (peekResult.isSuccess()) {
if (!peekResult.getRejectedItem().isPresent()) {
return peekResult;
}
itemStack = peekResult.getRejectedItem().get();
}
} else {
peekResult = null;
}
final PeekedOfferTransactionResult peekResult1 = peekOffer(itemStack, processed, false);
if (peekResult == null || !peekResult.isSuccess()) {
return peekResult1;
} else if (!peekResult1.isSuccess()) {
return peekResult;
}
return new PeekedOfferTransactionResult(InventoryTransactionResult.Type.SUCCESS, ImmutableList.<SlotTransaction>builder().addAll(peekResult.getTransactions()).addAll(peekResult1.getTransactions()).build(), peekResult1.getRejectedItem().orElse(null));
}
use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project LanternServer by LanternPowered.
the class AbstractInventorySlot method peekOffer.
@Override
public PeekedOfferTransactionResult peekOffer(ItemStack itemStack) {
checkNotNull(itemStack, "itemStack");
if (itemStack.isEmpty()) {
return new PeekedOfferTransactionResult(InventoryTransactionResult.Type.FAILURE, ImmutableList.of(), itemStack);
}
final int maxStackSize = Math.min(itemStack.getMaxStackQuantity(), this.maxStackSize);
if ((this.itemStack != null && (!this.itemStack.similarTo(itemStack) || this.itemStack.getQuantity() >= maxStackSize)) || !isValidItem(itemStack)) {
return new PeekedOfferTransactionResult(InventoryTransactionResult.Type.FAILURE, ImmutableList.of(), itemStack);
}
final List<SlotTransaction> transactions = new ArrayList<>();
// Get the amount of space we have left
final int availableSpace = this.itemStack == null ? maxStackSize : maxStackSize - this.itemStack.getQuantity();
final int quantity = itemStack.getQuantity();
if (quantity > availableSpace) {
ItemStack newStack;
if (this.itemStack == null) {
newStack = itemStack.copy();
} else {
newStack = this.itemStack.copy();
}
newStack.setQuantity(maxStackSize);
itemStack = itemStack.copy();
itemStack.setQuantity(quantity - availableSpace);
transactions.add(new SlotTransaction(this, LanternItemStack.toSnapshot(this.itemStack), LanternItemStackSnapshot.wrap(newStack)));
return new PeekedOfferTransactionResult(InventoryTransactionResult.Type.SUCCESS, transactions, itemStack);
} else {
final ItemStack newStack;
if (this.itemStack == null) {
newStack = itemStack.copy();
} else {
newStack = this.itemStack.copy();
newStack.setQuantity(newStack.getQuantity() + quantity);
}
transactions.add(new SlotTransaction(this, LanternItemStack.toSnapshot(this.itemStack), LanternItemStackSnapshot.wrap(newStack)));
return new PeekedOfferTransactionResult(InventoryTransactionResult.Type.SUCCESS, transactions, null);
}
}
use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project LanternServer by LanternPowered.
the class AbstractInventorySlot method peekPoll.
@Override
public Optional<PeekedPollTransactionResult> peekPoll(Predicate<ItemStack> matcher) {
checkNotNull(matcher, "matcher");
if (this.itemStack == null || !matcher.test(this.itemStack)) {
return Optional.empty();
}
final List<SlotTransaction> transactions = new ArrayList<>();
transactions.add(new SlotTransaction(this, this.itemStack.createSnapshot(), ItemStackSnapshot.NONE));
return Optional.of(new PeekedPollTransactionResult(transactions, this.itemStack.copy()));
}
use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project LanternServer by LanternPowered.
the class AbstractInventorySlot method peekPoll.
@Override
public Optional<PeekedPollTransactionResult> peekPoll(int limit, Predicate<ItemStack> matcher) {
checkNotNull(matcher, "matcher");
checkArgument(limit >= 0, "Limit may not be negative");
ItemStack itemStack = this.itemStack;
// There is no item available
if (limit == 0 || itemStack == null || !matcher.test(itemStack)) {
return Optional.empty();
}
final ItemStackSnapshot oldItem = itemStack.createSnapshot();
itemStack = itemStack.copy();
final int quantity = itemStack.getQuantity();
final ItemStackSnapshot newItem;
if (limit >= quantity) {
newItem = ItemStackSnapshot.NONE;
} else {
itemStack.setQuantity(quantity - limit);
newItem = LanternItemStack.toSnapshot(itemStack);
itemStack.setQuantity(limit);
}
final List<SlotTransaction> transactions = new ArrayList<>();
transactions.add(new SlotTransaction(this, oldItem, newItem));
return Optional.of(new PeekedPollTransactionResult(transactions, itemStack));
}
Aggregations