use of org.spongepowered.api.event.item.inventory.ClickInventoryEvent in project SpongeCommon by SpongePowered.
the class EnchantItemPacketState method unwind.
@Override
public void unwind(InventoryPacketContext context) {
// TODO - Pre changes of merging PacketFunction into the phase states, enchantments did NOT have any processing....
final EntityPlayerMP player = context.getPacketPlayer();
// The server will disable the player's crafting after receiving a
// client packet
// that did not pass validation (server click item != packet click item)
// The server then sends a SPacketConfirmTransaction and waits for a
// CPacketConfirmTransaction to re-enable crafting confirming that the
// client
// acknowledged the denied transaction.
// To detect when this happens, we turn off capturing so we can avoid
// firing
// invalid events.
// See MixinNetHandlerPlayServer processClickWindow redirect for rest of
// fix.
// --bloodmc
final IMixinContainer mixinContainer = ContainerUtil.toMixin(player.openContainer);
if (!mixinContainer.capturingInventory()) {
mixinContainer.getCapturedTransactions().clear();
return;
}
// TODO clear this shit out of the context
final CPacketEnchantItem packetIn = context.getPacket();
final ItemStackSnapshot lastCursor = context.getCursor();
final ItemStackSnapshot newCursor = ItemStackUtil.snapshotOf(player.inventory.getItemStack());
final Transaction<ItemStackSnapshot> transaction = new Transaction<>(lastCursor, newCursor);
final net.minecraft.inventory.Container openContainer = player.openContainer;
final List<SlotTransaction> slotTransactions = mixinContainer.getCapturedTransactions();
final int usedButton = packetIn.getButton();
final List<Entity> capturedItems = new ArrayList<>();
for (EntityItem entityItem : context.getCapturedItems()) {
capturedItems.add(EntityUtil.fromNative(entityItem));
}
try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
Sponge.getCauseStackManager().pushCause(player);
Sponge.getCauseStackManager().pushCause(openContainer);
final ClickInventoryEvent inventoryEvent;
inventoryEvent = this.createInventoryEvent(player, ContainerUtil.fromNative(openContainer), transaction, Lists.newArrayList(slotTransactions), capturedItems, usedButton);
// If this happens and we captured no entities, avoid firing events
if (mixinContainer.getCapturedTransactions().isEmpty() && capturedItems.isEmpty()) {
mixinContainer.setCaptureInventory(false);
return;
}
if (inventoryEvent != null) {
// Don't fire inventory drop events when there are no entities
if (inventoryEvent instanceof AffectEntityEvent && ((AffectEntityEvent) inventoryEvent).getEntities().isEmpty()) {
slotTransactions.clear();
mixinContainer.setCaptureInventory(false);
return;
}
// packet has everything we want.
if (!(inventoryEvent instanceof ClickInventoryEvent.Drag)) {
PacketPhaseUtil.validateCapturedTransactions(packetIn.getWindowId(), openContainer, inventoryEvent.getTransactions());
}
SpongeImpl.postEvent(inventoryEvent);
if (inventoryEvent.isCancelled() || PacketPhaseUtil.allTransactionsInvalid(inventoryEvent.getTransactions())) {
if (inventoryEvent instanceof ClickInventoryEvent.Drop) {
capturedItems.clear();
}
// Restore cursor
PacketPhaseUtil.handleCustomCursor(player, inventoryEvent.getCursorTransaction().getOriginal());
// Restore target slots
PacketPhaseUtil.handleSlotRestore(player, openContainer, inventoryEvent.getTransactions(), true);
} else {
PacketPhaseUtil.handleSlotRestore(player, openContainer, inventoryEvent.getTransactions(), false);
// Handle cursor
if (!inventoryEvent.getCursorTransaction().isValid()) {
PacketPhaseUtil.handleCustomCursor(player, inventoryEvent.getCursorTransaction().getOriginal());
} else if (inventoryEvent.getCursorTransaction().getCustom().isPresent()) {
PacketPhaseUtil.handleCustomCursor(player, inventoryEvent.getCursorTransaction().getFinal());
} else if (inventoryEvent instanceof ClickInventoryEvent.Drag) {
int increment;
increment = slotTransactions.stream().filter((t) -> !t.isValid()).mapToInt((t) -> t.getFinal().getQuantity()).sum();
final ItemStack cursor = inventoryEvent.getCursorTransaction().getFinal().createStack();
cursor.setQuantity(cursor.getQuantity() + increment);
PacketPhaseUtil.handleCustomCursor(player, cursor.createSnapshot());
} else if (inventoryEvent instanceof ClickInventoryEvent.Double && !(inventoryEvent instanceof ClickInventoryEvent.Shift)) {
int decrement;
decrement = slotTransactions.stream().filter((t) -> !t.isValid()).mapToInt((t) -> t.getOriginal().getQuantity()).sum();
final ItemStack cursor = inventoryEvent.getCursorTransaction().getFinal().createStack();
cursor.setQuantity(cursor.getQuantity() - decrement);
PacketPhaseUtil.handleCustomCursor(player, cursor.createSnapshot());
}
if (inventoryEvent instanceof SpawnEntityEvent) {
processSpawnedEntities(player, (SpawnEntityEvent) inventoryEvent);
} else if (!context.getCapturedEntitySupplier().isEmpty()) {
SpawnEntityEvent spawnEntityEvent = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), context.getCapturedEntities());
SpongeImpl.postEvent(spawnEntityEvent);
if (!spawnEntityEvent.isCancelled()) {
processSpawnedEntities(player, spawnEntityEvent);
}
}
}
}
}
slotTransactions.clear();
mixinContainer.setCaptureInventory(false);
}
use of org.spongepowered.api.event.item.inventory.ClickInventoryEvent in project SpongeCommon by SpongePowered.
the class PlaceRecipePacketState method unwind.
@Override
public void unwind(InventoryPacketContext context) {
CPacketPlaceRecipe packet = context.getPacket();
boolean shift = packet.func_194319_c();
IRecipe recipe = packet.func_194317_b();
final EntityPlayerMP player = context.getPacketPlayer();
((IMixinContainer) player.openContainer).detectAndSendChanges(true);
((IMixinContainer) player.openContainer).setCaptureInventory(false);
((IMixinContainer) player.openContainer).setFirePreview(true);
Inventory craftInv = ((Inventory) player.openContainer).query(QueryOperationTypes.INVENTORY_TYPE.of(CraftingInventory.class));
if (!(craftInv instanceof CraftingInventory)) {
SpongeImpl.getLogger().warn("Detected crafting without a InventoryCrafting!? Crafting Event will not fire.");
return;
}
List<SlotTransaction> previewTransactions = ((IMixinContainer) player.openContainer).getPreviewTransactions();
if (previewTransactions.isEmpty()) {
CraftingOutput slot = ((CraftingInventory) craftInv).getResult();
SlotTransaction st = new SlotTransaction(slot, ItemStackSnapshot.NONE, ItemStackUtil.snapshotOf(slot.peek().orElse(ItemStack.empty())));
previewTransactions.add(st);
}
SpongeCommonEventFactory.callCraftEventPre(player, ((CraftingInventory) craftInv), previewTransactions.get(0), ((CraftingRecipe) recipe), player.openContainer, previewTransactions);
previewTransactions.clear();
final Entity spongePlayer = EntityUtil.fromNative(player);
try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
frame.pushCause(spongePlayer);
frame.pushCause(player.openContainer);
List<SlotTransaction> transactions = ((IMixinContainer) player.openContainer).getCapturedTransactions();
ItemStackSnapshot cursor = ItemStackUtil.snapshotOf(player.inventory.getItemStack());
Transaction<ItemStackSnapshot> cursorTransaction = new Transaction<>(cursor, cursor);
ClickInventoryEvent event;
if (shift) {
event = SpongeEventFactory.createClickInventoryEventShiftPrimary(frame.getCurrentCause(), cursorTransaction, ((Container) player.openContainer), transactions);
} else {
event = SpongeEventFactory.createClickInventoryEventPrimary(frame.getCurrentCause(), cursorTransaction, ((Container) player.openContainer), transactions);
}
SpongeImpl.postEvent(event);
if (event.isCancelled() || !event.getCursorTransaction().isValid()) {
PacketPhaseUtil.handleCustomCursor(player, event.getCursorTransaction().getOriginal());
} else {
PacketPhaseUtil.handleCustomCursor(player, event.getCursorTransaction().getFinal());
}
PacketPhaseUtil.handleSlotRestore(player, player.openContainer, event.getTransactions(), event.isCancelled());
event.getTransactions().clear();
}
}
use of org.spongepowered.api.event.item.inventory.ClickInventoryEvent in project SpongeCommon by SpongePowered.
the class BasicInventoryPacketState method unwind.
@Override
public void unwind(InventoryPacketContext context) {
final EntityPlayerMP player = context.getPacketPlayer();
// The server will disable the player's crafting after receiving a
// client packet
// that did not pass validation (server click item != packet click item)
// The server then sends a SPacketConfirmTransaction and waits for a
// CPacketConfirmTransaction to re-enable crafting confirming that the
// client
// acknowledged the denied transaction.
// To detect when this happens, we turn off capturing so we can avoid
// firing
// invalid events.
// See MixinNetHandlerPlayServer processClickWindow redirect for rest of
// fix.
// --bloodmc
final IMixinContainer mixinContainer = ContainerUtil.toMixin(player.openContainer);
if (!mixinContainer.capturingInventory()) {
mixinContainer.getCapturedTransactions().clear();
return;
}
// TODO clear this shit out of the context
final CPacketClickWindow packetIn = context.getPacket();
final ItemStackSnapshot lastCursor = context.getCursor();
final ItemStackSnapshot newCursor = ItemStackUtil.snapshotOf(player.inventory.getItemStack());
final Transaction<ItemStackSnapshot> transaction = new Transaction<>(lastCursor, newCursor);
final net.minecraft.inventory.Container openContainer = player.openContainer;
final List<SlotTransaction> slotTransactions = mixinContainer.getCapturedTransactions();
final int usedButton = packetIn.getUsedButton();
final List<Entity> capturedItems = new ArrayList<>();
for (EntityItem entityItem : context.getCapturedItems()) {
capturedItems.add(EntityUtil.fromNative(entityItem));
}
try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
Sponge.getCauseStackManager().pushCause(openContainer);
Sponge.getCauseStackManager().pushCause(player);
final ClickInventoryEvent inventoryEvent;
inventoryEvent = this.createInventoryEvent(player, ContainerUtil.fromNative(openContainer), transaction, Lists.newArrayList(slotTransactions), capturedItems, usedButton);
// If this happens and we captured no entities, avoid firing events
if (mixinContainer.getCapturedTransactions().isEmpty() && capturedItems.isEmpty() && transaction.getOriginal().equals(transaction.getFinal())) {
mixinContainer.setCaptureInventory(false);
return;
}
if (inventoryEvent != null) {
// Don't fire inventory drop events when there are no entities
if (inventoryEvent instanceof AffectEntityEvent && ((AffectEntityEvent) inventoryEvent).getEntities().isEmpty()) {
slotTransactions.clear();
mixinContainer.setCaptureInventory(false);
return;
}
// packet has everything we want.
if (!(inventoryEvent instanceof ClickInventoryEvent.Drag)) {
PacketPhaseUtil.validateCapturedTransactions(packetIn.getSlotId(), openContainer, inventoryEvent.getTransactions());
}
SpongeImpl.postEvent(inventoryEvent);
if (inventoryEvent.isCancelled() || PacketPhaseUtil.allTransactionsInvalid(inventoryEvent.getTransactions())) {
if (inventoryEvent instanceof ClickInventoryEvent.Drop) {
capturedItems.clear();
}
// Restore cursor
if (inventoryEvent.isCancelled() || !inventoryEvent.getCursorTransaction().isValid()) {
PacketPhaseUtil.handleCustomCursor(player, inventoryEvent.getCursorTransaction().getOriginal());
} else if (inventoryEvent.getCursorTransaction().getCustom().isPresent()) {
PacketPhaseUtil.handleCustomCursor(player, inventoryEvent.getCursorTransaction().getFinal());
}
// Restore target slots
PacketPhaseUtil.handleSlotRestore(player, openContainer, inventoryEvent.getTransactions(), true);
} else {
PacketPhaseUtil.handleSlotRestore(player, openContainer, inventoryEvent.getTransactions(), false);
// Handle cursor
if (!inventoryEvent.getCursorTransaction().isValid()) {
PacketPhaseUtil.handleCustomCursor(player, inventoryEvent.getCursorTransaction().getOriginal());
} else if (inventoryEvent.getCursorTransaction().getCustom().isPresent()) {
PacketPhaseUtil.handleCustomCursor(player, inventoryEvent.getCursorTransaction().getFinal());
} else if (inventoryEvent instanceof ClickInventoryEvent.Drag) {
int increment;
increment = slotTransactions.stream().filter((t) -> !t.isValid()).mapToInt((t) -> t.getFinal().getQuantity()).sum();
final ItemStack cursor = inventoryEvent.getCursorTransaction().getFinal().createStack();
cursor.setQuantity(cursor.getQuantity() + increment);
PacketPhaseUtil.handleCustomCursor(player, cursor.createSnapshot());
} else if (inventoryEvent instanceof ClickInventoryEvent.Double && !(inventoryEvent instanceof ClickInventoryEvent.Shift)) {
int decrement;
decrement = slotTransactions.stream().filter((t) -> !t.isValid()).mapToInt((t) -> t.getOriginal().getQuantity()).sum();
final ItemStack cursor = inventoryEvent.getCursorTransaction().getFinal().createStack();
cursor.setQuantity(cursor.getQuantity() - decrement);
PacketPhaseUtil.handleCustomCursor(player, cursor.createSnapshot());
}
if (inventoryEvent instanceof SpawnEntityEvent) {
processSpawnedEntities(player, (SpawnEntityEvent) inventoryEvent);
} else if (!context.getCapturedEntitySupplier().isEmpty()) {
SpawnEntityEvent spawnEntityEvent = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), context.getCapturedEntities());
SpongeImpl.postEvent(spawnEntityEvent);
if (!spawnEntityEvent.isCancelled()) {
processSpawnedEntities(player, spawnEntityEvent);
}
}
}
}
}
slotTransactions.clear();
mixinContainer.setCaptureInventory(false);
}
use of org.spongepowered.api.event.item.inventory.ClickInventoryEvent in project LanternServer by LanternPowered.
the class VanillaContainerInteractionBehavior method handleClick.
@Override
public void handleClick(ClientContainer clientContainer, @Nullable ClientSlot clientSlot, MouseButton mouseButton) {
final LanternPlayer player = clientContainer.getPlayer();
if (player != this.container.getPlayerInventory().getCarrier().orElse(null) || (clientSlot != null && !(clientSlot instanceof ClientSlot.Slot))) {
return;
}
final CauseStack causeStack = CauseStack.current();
if (clientSlot == null) {
causeStack.addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.DROPPED_ITEM);
final List<Entity> entities = new ArrayList<>();
final Transaction<ItemStackSnapshot> cursorTransaction;
// Clicking outside the container
ItemStackSnapshot oldItem = ItemStackSnapshot.NONE;
ItemStackSnapshot newItem = ItemStackSnapshot.NONE;
if (getCursorItem() != null) {
oldItem = getCursorItem().createSnapshot();
final ItemStackSnapshot droppedItem;
if (mouseButton != MouseButton.LEFT) {
final ItemStack stack = getCursorItem().copy();
stack.setQuantity(stack.getQuantity() - 1);
newItem = LanternItemStack.toSnapshot(stack);
stack.setQuantity(1);
droppedItem = LanternItemStack.toSnapshot(stack);
} else {
droppedItem = oldItem;
}
LanternEventHelper.handlePreDroppedItemSpawning(player.getTransform(), droppedItem).ifPresent(entities::add);
}
cursorTransaction = new Transaction<>(oldItem, newItem);
final ClickInventoryEvent.Drop event;
if (mouseButton == MouseButton.LEFT) {
event = SpongeEventFactory.createClickInventoryEventDropOutsidePrimary(causeStack.getCurrentCause(), cursorTransaction, entities, this.container, new ArrayList<>());
} else {
event = SpongeEventFactory.createClickInventoryEventDropOutsideSecondary(causeStack.getCurrentCause(), cursorTransaction, entities, this.container, new ArrayList<>());
}
finishInventoryEvent(event);
return;
}
// Clicking inside the container
final AbstractSlot slot = ((ClientSlot.Slot) clientSlot).getSlot();
if (mouseButton == MouseButton.MIDDLE) {
final ItemStackSnapshot oldItem = LanternItemStack.toSnapshot(getCursorItem());
Transaction<ItemStackSnapshot> cursorTransaction = null;
final Optional<GameMode> gameMode = player.get(Keys.GAME_MODE);
if (gameMode.isPresent() && gameMode.get().equals(GameModes.CREATIVE) && getCursorItem() == null) {
final ItemStack stack = slot.peek().orElse(null);
if (stack != null) {
stack.setQuantity(stack.getMaxStackQuantity());
cursorTransaction = new Transaction<>(oldItem, stack.createSnapshot());
}
}
if (cursorTransaction == null) {
cursorTransaction = new Transaction<>(oldItem, oldItem);
}
final ClickInventoryEvent.Middle event = SpongeEventFactory.createClickInventoryEventMiddle(causeStack.getCurrentCause(), cursorTransaction, this.container, new ArrayList<>());
finishInventoryEvent(event);
} else {
// Crafting slots have special click behavior
if (slot instanceof CraftingOutput) {
List<SlotTransaction> transactions = new ArrayList<>();
Transaction<ItemStackSnapshot> cursorTransaction;
final AbstractInventory parent = slot.parent();
if (parent instanceof CraftingInventory) {
ClickInventoryEvent event;
final CraftingInventory inventory = (CraftingInventory) parent;
final Optional<ExtendedCraftingResult> optResult = Lantern.getRegistry().getCraftingRecipeRegistry().getExtendedResult(inventory.getCraftingGrid(), player.getWorld());
final ItemStackSnapshot originalCursorItem = LanternItemStack.toSnapshot(getCursorItem());
if (optResult.isPresent()) {
final CraftingResult result = optResult.get().getResult();
final ItemStackSnapshot resultItem = result.getResult();
int quantity = -1;
if (getCursorItem() == null) {
quantity = resultItem.getQuantity();
} else if (LanternItemStack.areSimilar(resultItem.createStack(), getCursorItem())) {
final int quantity1 = resultItem.getQuantity() + getCursorItem().getQuantity();
if (quantity1 < getCursorItem().getMaxStackQuantity()) {
quantity = quantity1;
}
}
if (quantity == -1) {
cursorTransaction = new Transaction<>(originalCursorItem, originalCursorItem);
transactions.add(new SlotTransaction(slot, resultItem, resultItem));
} else {
final LanternItemStack itemStack = (LanternItemStack) resultItem.createStack();
itemStack.setQuantity(quantity);
cursorTransaction = new Transaction<>(originalCursorItem, itemStack.createSnapshot());
transactions.add(new SlotTransaction(slot, resultItem, ItemStackSnapshot.NONE));
updateCraftingGrid(player, inventory, optResult.get().getMatrixResult(1), transactions);
}
} else {
cursorTransaction = new Transaction<>(originalCursorItem, originalCursorItem);
// No actual transaction, there shouldn't have been a item in the crafting result slot
transactions.add(new SlotTransaction(slot, ItemStackSnapshot.NONE, ItemStackSnapshot.NONE));
}
transactions = this.container.transformSlots(transactions);
if (mouseButton == MouseButton.LEFT) {
event = SpongeEventFactory.createClickInventoryEventPrimary(causeStack.getCurrentCause(), cursorTransaction, this.container, transactions);
} else {
event = SpongeEventFactory.createClickInventoryEventSecondary(causeStack.getCurrentCause(), cursorTransaction, this.container, transactions);
}
finishInventoryEvent(event);
return;
} else {
Lantern.getLogger().warn("Found a CraftingOutput slot without a CraftingInventory as parent.");
}
}
ClickInventoryEvent event;
if (mouseButton == MouseButton.LEFT) {
final List<SlotTransaction> transactions = new ArrayList<>();
Transaction<ItemStackSnapshot> cursorTransaction = null;
if (getCursorItem() != null && !(slot instanceof OutputSlot)) {
final PeekedOfferTransactionResult result = slot.peekOffer(getCursorItem());
if (result.isSuccess()) {
transactions.addAll(result.getTransactions());
cursorTransaction = new Transaction<>(getCursorItem().createSnapshot(), LanternItemStack.toSnapshot(result.getRejectedItem().orElse(null)));
} else {
final PeekedSetTransactionResult result1 = slot.peekSet(getCursorItem());
if (result1.isSuccess()) {
cursorTransaction = new Transaction<>(getCursorItem().createSnapshot(), LanternItemStack.toSnapshot(result1.getReplacedItem().orElse(null)));
transactions.addAll(result1.getTransactions());
}
}
} else if (getCursorItem() == null) {
final PeekedPollTransactionResult result = slot.peekPoll(stack -> true).orElse(null);
if (result != null) {
cursorTransaction = new Transaction<>(ItemStackSnapshot.NONE, LanternItemStack.toSnapshot(result.getPolledItem()));
transactions.addAll(result.getTransactions());
} else {
cursorTransaction = new Transaction<>(ItemStackSnapshot.NONE, ItemStackSnapshot.NONE);
}
}
if (cursorTransaction == null) {
final ItemStackSnapshot cursorItem = LanternItemStack.toSnapshot(getCursorItem());
cursorTransaction = new Transaction<>(cursorItem, cursorItem);
}
event = SpongeEventFactory.createClickInventoryEventPrimary(causeStack.getCurrentCause(), cursorTransaction, this.container, this.container.transformSlots(transactions));
} else {
final List<SlotTransaction> transactions = new ArrayList<>();
Transaction<ItemStackSnapshot> cursorTransaction = null;
if (getCursorItem() == null) {
int stackSize = slot.getStackSize();
if (stackSize != 0) {
stackSize = stackSize - (stackSize / 2);
final PeekedPollTransactionResult result = slot.peekPoll(stackSize, stack -> true).get();
transactions.addAll(result.getTransactions());
cursorTransaction = new Transaction<>(ItemStackSnapshot.NONE, result.getPolledItem().createSnapshot());
}
} else {
final ItemStack itemStack = getCursorItem().copy();
itemStack.setQuantity(1);
final PeekedOfferTransactionResult result = slot.peekOffer(itemStack);
if (result.isSuccess()) {
final ItemStackSnapshot oldCursor = getCursorItem().createSnapshot();
int quantity = getCursorItem().getQuantity() - 1;
if (quantity <= 0) {
cursorTransaction = new Transaction<>(oldCursor, ItemStackSnapshot.NONE);
} else {
final ItemStack newCursorItem = getCursorItem().copy();
newCursorItem.setQuantity(quantity);
cursorTransaction = new Transaction<>(oldCursor, newCursorItem.createSnapshot());
}
transactions.addAll(result.getTransactions());
} else {
final PeekedSetTransactionResult result1 = slot.peekSet(getCursorItem());
if (result1.isSuccess()) {
final ItemStack replacedItem = result1.getReplacedItem().orElse(null);
if (replacedItem != null) {
setCursorItem(replacedItem);
cursorTransaction = new Transaction<>(getCursorItem().createSnapshot(), LanternItemStack.toSnapshot(replacedItem));
} else {
cursorTransaction = new Transaction<>(getCursorItem().createSnapshot(), ItemStackSnapshot.NONE);
}
transactions.addAll(result1.getTransactions());
}
}
}
if (cursorTransaction == null) {
final ItemStackSnapshot cursorItem = LanternItemStack.toSnapshot(getCursorItem());
cursorTransaction = new Transaction<>(cursorItem, cursorItem);
}
event = SpongeEventFactory.createClickInventoryEventSecondary(causeStack.getCurrentCause(), cursorTransaction, this.container, this.container.transformSlots(transactions));
}
finishInventoryEvent(event);
}
}
Aggregations