use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project SpongeCommon by SpongePowered.
the class SpongeCommonEventFactory method callCreativeClickInventoryEvent.
public static ClickInventoryEvent.Creative callCreativeClickInventoryEvent(EntityPlayerMP player, CPacketCreativeInventoryAction packetIn) {
Sponge.getCauseStackManager().pushCause(player);
// Creative doesn't inform server of cursor status so there is no way of knowing what the final stack is
// Due to this, we can only send the original item that was clicked in slot
Transaction<ItemStackSnapshot> cursorTransaction = new Transaction<>(ItemStackSnapshot.NONE, ItemStackSnapshot.NONE);
if (((IMixinContainer) player.openContainer).getCapturedTransactions().size() == 0 && packetIn.getSlotId() >= 0 && packetIn.getSlotId() < player.openContainer.inventorySlots.size()) {
Slot slot = player.openContainer.getSlot(packetIn.getSlotId());
if (slot != null) {
ItemStackSnapshot clickedItem = slot.getStack() == null ? ItemStackSnapshot.NONE : ((org.spongepowered.api.item.inventory.ItemStack) slot.getStack()).createSnapshot();
SlotTransaction slotTransaction = new SlotTransaction(((org.spongepowered.api.item.inventory.Slot) slot), clickedItem, ItemStackSnapshot.NONE);
((IMixinContainer) player.openContainer).getCapturedTransactions().add(slotTransaction);
}
}
ClickInventoryEvent.Creative event = SpongeEventFactory.createClickInventoryEventCreative(Sponge.getCauseStackManager().getCurrentCause(), cursorTransaction, (org.spongepowered.api.item.inventory.Container) player.openContainer, ((IMixinContainer) player.openContainer).getCapturedTransactions());
SpongeImpl.postEvent(event);
Sponge.getCauseStackManager().popCause();
return event;
}
use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project SpongeCommon by SpongePowered.
the class SpongeCommonEventFactory method captureTransaction.
/**
* Captures a transaction
*
* @param captureIn the {@link IMixinInventory} to capture the transaction in
* @param inv the Inventory
* @param index the affected SlotIndex
* @param transaction the transaction to execute
* @return the result if the transaction
*/
public static ItemStack captureTransaction(IMixinInventory captureIn, Inventory inv, int index, Supplier<ItemStack> transaction) {
// TODO make sure we never got null
if (captureIn == null || inv == null) {
return transaction.get();
}
Inventory ordered = inv.query(QueryOperationTypes.INVENTORY_TYPE.of(OrderedInventory.class));
if (ordered instanceof OrderedInventory) {
Optional<org.spongepowered.api.item.inventory.Slot> slot = ((OrderedInventory) ordered).getSlot(SlotIndex.of(index));
if (slot.isPresent()) {
ItemStackSnapshot original = slot.get().peek().map(ItemStackUtil::snapshotOf).orElse(ItemStackSnapshot.NONE);
ItemStack remaining = transaction.get();
if (remaining.isEmpty()) {
ItemStackSnapshot replacement = slot.get().peek().map(ItemStackUtil::snapshotOf).orElse(ItemStackSnapshot.NONE);
captureIn.getCapturedTransactions().add(new SlotTransaction(slot.get(), original, replacement));
}
return remaining;
}
}
// else inventory was missing the slot for some reason
return transaction.get();
}
use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project SpongeCommon by SpongePowered.
the class SwapHandItemsState method unwind.
@Override
public void unwind(InventoryPacketContext context) {
final EntityPlayerMP player = context.getPacketPlayer();
final Entity spongePlayer = EntityUtil.fromNative(player);
try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
frame.pushCause(spongePlayer);
final IMixinInventoryPlayer mixinInventory = ((IMixinInventoryPlayer) player.inventory);
List<SlotTransaction> trans = mixinInventory.getCapturedTransactions();
ChangeInventoryEvent.SwapHand swapItemEvent = this.createInventoryEvent(((Inventory) player.inventory), trans);
SpongeImpl.postEvent(swapItemEvent);
PacketPhaseUtil.handleSlotRestore(player, null, swapItemEvent.getTransactions(), swapItemEvent.isCancelled());
mixinInventory.setCapture(false);
mixinInventory.getCapturedTransactions().clear();
}
}
use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project SpongeCommon by SpongePowered.
the class SwitchHotbarScrollState method unwind.
@Override
public void unwind(InventoryPacketContext context) {
final EntityPlayerMP player = context.getPacketPlayer();
final CPacketHeldItemChange itemChange = context.getPacket();
final int previousSlot = context.getHighlightedSlotId();
final net.minecraft.inventory.Container inventoryContainer = player.inventoryContainer;
final InventoryPlayer inventory = player.inventory;
// Crafting Grid & Result
int preHotbarSize = inventory.mainInventory.size() - InventoryPlayer.getHotbarSize() + inventory.armorInventory.size() + 4 + 1;
final Slot sourceSlot = inventoryContainer.getSlot(previousSlot + preHotbarSize);
final Slot targetSlot = inventoryContainer.getSlot(itemChange.getSlotId() + preHotbarSize);
ItemStackSnapshot sourceSnapshot = ItemStackUtil.snapshotOf(sourceSlot.getStack());
ItemStackSnapshot targetSnapshot = ItemStackUtil.snapshotOf(targetSlot.getStack());
SlotTransaction sourceTransaction = new SlotTransaction(ContainerUtil.getSlot(inventoryContainer, previousSlot + preHotbarSize), sourceSnapshot, sourceSnapshot);
SlotTransaction targetTransaction = new SlotTransaction(ContainerUtil.getSlot(inventoryContainer, itemChange.getSlotId() + preHotbarSize), targetSnapshot, targetSnapshot);
try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
Sponge.getCauseStackManager().pushCause(player);
ImmutableList<SlotTransaction> transactions = new ImmutableList.Builder<SlotTransaction>().add(sourceTransaction).add(targetTransaction).build();
final ChangeInventoryEvent.Held changeInventoryEventHeld = SpongeEventFactory.createChangeInventoryEventHeld(Sponge.getCauseStackManager().getCurrentCause(), (Inventory) inventoryContainer, transactions);
net.minecraft.inventory.Container openContainer = player.openContainer;
SpongeImpl.postEvent(changeInventoryEventHeld);
if (changeInventoryEventHeld.isCancelled() || PacketPhaseUtil.allTransactionsInvalid(changeInventoryEventHeld.getTransactions())) {
player.connection.sendPacket(new SPacketHeldItemChange(previousSlot));
inventory.currentItem = previousSlot;
} else {
PacketPhaseUtil.handleSlotRestore(player, openContainer, changeInventoryEventHeld.getTransactions(), false);
inventory.currentItem = itemChange.getSlotId();
player.markPlayerActive();
}
}
}
use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project SpongeCommon by SpongePowered.
the class DragInventoryStopState method unwindCraftPreview.
public static void unwindCraftPreview(InventoryPacketContext context) {
final EntityPlayerMP player = context.getPacketPlayer();
((IMixinContainer) player.openContainer).setFirePreview(true);
Inventory craftInv = ((Inventory) player.openContainer).query(QueryOperationTypes.INVENTORY_TYPE.of(CraftingInventory.class));
if (craftInv instanceof CraftingInventory) {
List<SlotTransaction> previewTransactions = ((IMixinContainer) player.openContainer).getPreviewTransactions();
if (!previewTransactions.isEmpty()) {
CraftingRecipe recipe = SpongeCraftingRecipeRegistry.getInstance().findMatchingRecipe(((CraftingInventory) craftInv).getCraftingGrid(), ((World) player.world)).orElse(null);
SpongeCommonEventFactory.callCraftEventPre(player, ((CraftingInventory) craftInv), previewTransactions.get(0), recipe, player.openContainer, previewTransactions);
previewTransactions.clear();
}
}
}
Aggregations