use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project SpongeCommon by SpongePowered.
the class DropItemWithHotkeyState method unwind.
@Override
public void unwind(InventoryPacketContext context) {
final EntityPlayerMP player = context.getPacketPlayer();
// final ItemStack usedStack = context.getItemUsed();
// final ItemStackSnapshot usedSnapshot = ItemStackUtil.snapshotOf(usedStack);
final Entity spongePlayer = EntityUtil.fromNative(player);
try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
Sponge.getCauseStackManager().pushCause(spongePlayer);
Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.DROPPED_ITEM);
context.getCapturedBlockSupplier().acceptAndClearIfNotEmpty(blocks -> TrackingUtil.processBlockCaptures(blocks, this, context));
context.getCapturedItemsSupplier().acceptAndClearIfNotEmpty(items -> {
final ArrayList<Entity> entities = new ArrayList<>();
for (EntityItem item : items) {
entities.add(EntityUtil.fromNative(item));
}
int usedButton = 0;
if (context.getPacket() instanceof CPacketPlayerDigging) {
final CPacketPlayerDigging packetIn = context.getPacket();
usedButton = packetIn.getAction() == CPacketPlayerDigging.Action.DROP_ITEM ? PacketPhase.PACKET_BUTTON_PRIMARY_ID : 1;
} else {
final CPacketClickWindow packetIn = context.getPacket();
usedButton = packetIn.getUsedButton();
}
Transaction<ItemStackSnapshot> cursorTrans = new Transaction<>(ItemStackSnapshot.NONE, ItemStackSnapshot.NONE);
final IMixinContainer mixinContainer = ContainerUtil.toMixin(player.openContainer);
List<SlotTransaction> slotTrans = mixinContainer.getCapturedTransactions();
ClickInventoryEvent.Drop dropItemEvent = ((DropItemWithHotkeyState) this).createInventoryEvent(player, ContainerUtil.fromNative(player.openContainer), cursorTrans, Lists.newArrayList(slotTrans), entities, usedButton);
SpongeImpl.postEvent(dropItemEvent);
if (dropItemEvent.isCancelled() || PacketPhaseUtil.allTransactionsInvalid(dropItemEvent.getTransactions())) {
((IMixinEntityPlayerMP) player).restorePacketItem(EnumHand.MAIN_HAND);
PacketPhaseUtil.handleSlotRestore(player, player.openContainer, dropItemEvent.getTransactions(), true);
} else {
processSpawnedEntities(player, dropItemEvent);
}
slotTrans.clear();
mixinContainer.setCaptureInventory(false);
});
context.getCapturedEntityDropSupplier().acceptIfNotEmpty(itemMapping -> {
});
final IMixinContainer mixinContainer = ContainerUtil.toMixin(player.openContainer);
mixinContainer.setCaptureInventory(false);
mixinContainer.getCapturedTransactions().clear();
}
}
use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project SpongeCommon by SpongePowered.
the class PacketPhaseUtil method validateCapturedTransactions.
public static void validateCapturedTransactions(int slotId, Container openContainer, List<SlotTransaction> capturedTransactions) {
if (capturedTransactions.size() == 0 && slotId >= 0 && slotId < openContainer.inventorySlots.size()) {
final Slot slot = openContainer.getSlot(slotId);
if (slot != null) {
ItemStackSnapshot snapshot = slot.getHasStack() ? ((org.spongepowered.api.item.inventory.ItemStack) slot.getStack()).createSnapshot() : ItemStackSnapshot.NONE;
final SlotTransaction slotTransaction = new SlotTransaction(ContainerUtil.getSlot(openContainer, slotId), snapshot, snapshot);
capturedTransactions.add(slotTransaction);
}
}
}
use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project SpongeCommon by SpongePowered.
the class PacketPhaseUtil method handleSlotRestore.
public static void handleSlotRestore(EntityPlayer player, @Nullable Container openContainer, List<SlotTransaction> slotTransactions, boolean eventCancelled) {
for (SlotTransaction slotTransaction : slotTransactions) {
if ((!slotTransaction.getCustom().isPresent() && slotTransaction.isValid()) && !eventCancelled) {
continue;
}
final SlotAdapter slot = (SlotAdapter) slotTransaction.getSlot();
ItemStackSnapshot snapshot = eventCancelled || !slotTransaction.isValid() ? slotTransaction.getOriginal() : slotTransaction.getCustom().get();
final ItemStack originalStack = ItemStackUtil.fromSnapshotToNative(snapshot);
if (openContainer == null) {
slot.set(((org.spongepowered.api.item.inventory.ItemStack) originalStack));
} else {
final int slotNumber = slot.slotNumber;
final Slot nmsSlot = openContainer.getSlot(slotNumber);
if (nmsSlot != null) {
nmsSlot.putStack(originalStack);
}
}
}
if (openContainer != null) {
openContainer.detectAndSendChanges();
// we must also validate the player still has the same container open after the event has been processed
if (eventCancelled && player.openContainer == openContainer && player instanceof EntityPlayerMP) {
((EntityPlayerMP) player).sendContainerToPlayer(openContainer);
}
}
}
use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project SpongeCommon by SpongePowered.
the class SpongeCommonEventFactory method callInventoryPickupEvent.
public static boolean callInventoryPickupEvent(IInventory inventory, ItemStack[] prevInventory) {
Inventory spongeInventory = InventoryUtil.toInventory(inventory, null);
List<SlotTransaction> trans = generateTransactions(spongeInventory, inventory, prevInventory);
if (trans.isEmpty()) {
return true;
}
ChangeInventoryEvent.Pickup event = SpongeEventFactory.createChangeInventoryEventPickup(Sponge.getCauseStackManager().getCurrentCause(), spongeInventory, trans);
SpongeImpl.postEvent(event);
applyTransactions(event);
return !event.isCancelled();
}
use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project SpongeCommon by SpongePowered.
the class SpongeCommonEventFactory method callCraftEventPost.
public static CraftItemEvent.Craft callCraftEventPost(EntityPlayer player, CraftingInventory inventory, ItemStackSnapshot result, @Nullable CraftingRecipe recipe, Container container, List<SlotTransaction> transactions) {
// Get previous cursor if captured
ItemStack previousCursor = ((IMixinContainer) container).getPreviousCursor();
if (previousCursor == null) {
// or get the current one
previousCursor = player.inventory.getItemStack();
}
Transaction<ItemStackSnapshot> cursorTransaction = new Transaction<>(ItemStackUtil.snapshotOf(previousCursor), ItemStackUtil.snapshotOf(player.inventory.getItemStack()));
CraftItemEvent.Craft event = SpongeEventFactory.createCraftItemEventCraft(Sponge.getCauseStackManager().getCurrentCause(), result, inventory, cursorTransaction, Optional.ofNullable(recipe), ((org.spongepowered.api.item.inventory.Container) container), transactions);
SpongeImpl.postEvent(event);
((IMixinContainer) container).setCaptureInventory(false);
// handle slot-transactions
PacketPhaseUtil.handleSlotRestore(player, container, new ArrayList<>(transactions), event.isCancelled());
if (event.isCancelled() || !event.getCursorTransaction().isValid() || event.getCursorTransaction().getCustom().isPresent()) {
// handle cursor-transaction
ItemStackSnapshot newCursor = event.isCancelled() || event.getCursorTransaction().isValid() ? event.getCursorTransaction().getOriginal() : event.getCursorTransaction().getFinal();
player.inventory.setItemStack(ItemStackUtil.fromSnapshotToNative(newCursor));
if (player instanceof EntityPlayerMP) {
((EntityPlayerMP) player).connection.sendPacket(new SPacketSetSlot(-1, -1, player.inventory.getItemStack()));
}
}
transactions.clear();
((IMixinContainer) container).setCaptureInventory(true);
return event;
}
Aggregations