use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project LanternServer by LanternPowered.
the class VanillaContainerInteractionBehavior method handleDropKey.
@Override
public void handleDropKey(ClientContainer clientContainer, ClientSlot clientSlot, boolean ctrl) {
final LanternPlayer player = clientContainer.getPlayer();
if (player != this.container.getPlayerInventory().getCarrier().orElse(null) || !(clientSlot instanceof ClientSlot.Slot)) {
return;
}
final AbstractSlot slot = ((ClientSlot.Slot) clientSlot).getSlot();
final CauseStack causeStack = CauseStack.current();
causeStack.addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.DROPPED_ITEM);
final List<Entity> entities = new ArrayList<>();
final Transaction<ItemStackSnapshot> cursorTransaction;
List<SlotTransaction> slotTransactions = new ArrayList<>();
final ItemStackSnapshot item = LanternItemStack.toSnapshot(getCursorItem());
cursorTransaction = new Transaction<>(item, item);
final Optional<PeekedPollTransactionResult> result = ctrl ? slot.peekPoll(itemStack -> true) : slot.peekPoll(1, itemStack -> true);
if (result.isPresent()) {
final List<SlotTransaction> transactions = result.get().getTransactions();
slotTransactions.addAll(transactions);
final ItemStack itemStack = transactions.get(0).getOriginal().createStack();
itemStack.setQuantity(itemStack.getQuantity() - transactions.get(0).getFinal().getQuantity());
LanternEventHelper.handlePreDroppedItemSpawning(player.getTransform(), LanternItemStackSnapshot.wrap(itemStack)).ifPresent(entities::add);
}
slotTransactions = this.container.transformSlots(slotTransactions);
final ClickInventoryEvent.Drop event;
if (ctrl) {
event = SpongeEventFactory.createClickInventoryEventDropFull(causeStack.getCurrentCause(), cursorTransaction, entities, this.container, slotTransactions);
} else {
event = SpongeEventFactory.createClickInventoryEventDropSingle(causeStack.getCurrentCause(), cursorTransaction, entities, this.container, slotTransactions);
}
finishInventoryEvent(event);
}
use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project LanternServer by LanternPowered.
the class AbstractInventorySlot method peekSet.
@Override
public PeekedSetTransactionResult peekSet(@Nullable ItemStack stack) {
if (!LanternItemStack.isEmpty(stack) && !isValidItem(stack)) {
return new PeekedSetTransactionResult(InventoryTransactionResult.Type.FAILURE, ImmutableList.of(), stack, null);
}
stack = LanternItemStack.toNullable(stack);
final List<SlotTransaction> transactions = new ArrayList<>();
final ItemStackSnapshot oldItem = LanternItemStack.toSnapshot(this.itemStack);
ItemStack rejectedItem = null;
ItemStack replacedItem = oldItem.isEmpty() ? null : this.itemStack.copy();
ItemStackSnapshot newItem = ItemStackSnapshot.NONE;
if (stack != null) {
final int maxStackSize = Math.min(stack.getMaxStackQuantity(), this.maxStackSize);
final int quantity = stack.getQuantity();
if (quantity > maxStackSize) {
stack = stack.copy();
stack.setQuantity(maxStackSize);
newItem = LanternItemStack.toSnapshot(stack);
// Create the rest stack that was rejected,
// because the inventory doesn't allow so many items
rejectedItem = stack.copy();
rejectedItem.setQuantity(quantity - maxStackSize);
} else {
newItem = LanternItemStack.toSnapshot(stack);
}
}
transactions.add(new SlotTransaction(this, oldItem, newItem));
return new PeekedSetTransactionResult(InventoryTransactionResult.Type.SUCCESS, transactions, rejectedItem, replacedItem);
}
use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project LanternServer by LanternPowered.
the class AbstractChildrenInventory method peekPoll.
@Override
public Optional<PeekedPollTransactionResult> peekPoll(int limit, Predicate<ItemStack> matcher) {
checkNotNull(matcher, "matcher");
checkArgument(limit >= 0, "Limit may not be negative");
if (limit == 0) {
return Optional.empty();
}
PeekedPollTransactionResult peekResult = null;
// Loop through the children inventories
for (AbstractMutableInventory inventory : getChildren()) {
// Check whether the slot a item contains
if (peekResult == null) {
peekResult = inventory.peekPoll(limit, matcher).orElse(null);
if (peekResult != null) {
if (peekResult.getPolledItem().getQuantity() >= limit) {
return Optional.of(peekResult);
} else {
limit -= peekResult.getPolledItem().getQuantity();
if (!(matcher instanceof SimilarItemMatcher)) {
matcher = new SimilarItemMatcher(peekResult.getPolledItem());
}
}
}
} else {
final PeekedPollTransactionResult peekResult1 = inventory.peekPoll(limit, matcher).orElse(null);
if (peekResult1 != null) {
final int peekedStackSize = peekResult1.getPolledItem().getQuantity();
final ItemStack peekedItem = peekResult.getPolledItem();
limit -= peekedStackSize;
peekedItem.setQuantity(peekedItem.getQuantity() + peekedStackSize);
final List<SlotTransaction> transactions = new ArrayList<>();
transactions.addAll(peekResult.getTransactions());
transactions.addAll(peekResult1.getTransactions());
peekResult = new PeekedPollTransactionResult(transactions, peekedItem);
if (limit <= 0) {
return Optional.of(peekResult);
}
}
}
}
return Optional.ofNullable(peekResult);
}
use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project LanternServer by LanternPowered.
the class HandlerPlayInSwapHandItems method handle.
@Override
public void handle(NetworkContext context, MessagePlayInSwapHandItems message) {
final LanternPlayer player = context.getSession().getPlayer();
final LanternPlayerInventory inventory = player.getInventory();
final AbstractSlot hotbarSlot = inventory.getHotbar().getSelectedSlot();
final AbstractSlot offHandSlot = inventory.getOffhand();
final ItemStackSnapshot hotbarItem = hotbarSlot.peek().map(ItemStack::createSnapshot).orElse(ItemStackSnapshot.NONE);
final ItemStackSnapshot offHandItem = offHandSlot.peek().map(ItemStack::createSnapshot).orElse(ItemStackSnapshot.NONE);
final List<SlotTransaction> transactions = new ArrayList<>();
transactions.add(new SlotTransaction(hotbarSlot, hotbarItem, offHandItem));
transactions.add(new SlotTransaction(offHandSlot, offHandItem, hotbarItem));
try (CauseStack.Frame frame = CauseStack.current().pushCauseFrame()) {
frame.addContext(EventContextKeys.PLAYER, player);
frame.pushCause(player);
final ChangeInventoryEvent.SwapHand event = SpongeEventFactory.createChangeInventoryEventSwapHand(frame.getCurrentCause(), inventory, transactions);
Sponge.getEventManager().post(event);
if (!event.isCancelled()) {
transactions.stream().filter(Transaction::isValid).forEach(transaction -> transaction.getSlot().set(transaction.getFinal().createStack()));
final PlayerInventoryContainer inventoryContainer = context.getSession().getPlayer().getInventoryContainer();
inventoryContainer.getClientContainer().queueSilentSlotChange(hotbarSlot);
}
}
}
use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project LanternServer by LanternPowered.
the class JukeboxInteractionBehavior method tryInteract.
@Override
public BehaviorResult tryInteract(BehaviorPipeline<Behavior> pipeline, BehaviorContext context) {
final Location<World> location = context.requireContext(ContextKeys.INTERACTION_LOCATION);
final Optional<TileEntity> optTile = location.getTileEntity();
if (optTile.isPresent()) {
final TileEntity tile = optTile.get();
if (tile instanceof Jukebox) {
final LanternJukebox jukebox = (LanternJukebox) tile;
final Optional<Entity> optEjectedItem = jukebox.ejectRecordItem();
boolean success = false;
if (optEjectedItem.isPresent()) {
final Entity entity = optEjectedItem.get();
entity.getWorld().spawnEntity(optEjectedItem.get());
// TODO: Include the entity in the behavior context
success = true;
}
final Optional<ItemStack> optItemStack = context.getContext(ContextKeys.USED_ITEM_STACK);
if (optItemStack.isPresent()) {
final ItemStack itemStack = optItemStack.get();
final RecordProperty property = itemStack.getProperty(RecordProperty.class).orElse(null);
final RecordType recordType = property == null ? null : property.getValue();
if (recordType != null) {
final ItemStackSnapshot oldSnapshot = itemStack.createSnapshot();
itemStack.setQuantity(itemStack.getQuantity() - 1);
final ItemStackSnapshot newSnapshot = itemStack.createSnapshot();
context.getContext(ContextKeys.PLAYER).ifPresent(player -> {
if (!player.get(Keys.GAME_MODE).orElse(GameModes.NOT_SET).equals(GameModes.CREATIVE)) {
context.getContext(ContextKeys.USED_SLOT).ifPresent(slot -> context.addSlotChange(new SlotTransaction(slot, oldSnapshot, newSnapshot)));
}
});
itemStack.setQuantity(1);
jukebox.insertRecord(itemStack);
jukebox.playRecord();
success = true;
}
}
if (success) {
return BehaviorResult.SUCCESS;
}
}
}
return BehaviorResult.PASS;
}
Aggregations