use of org.spongepowered.api.item.inventory.transaction.SlotTransaction 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.item.inventory.transaction.SlotTransaction in project SpongeCommon by SpongePowered.
the class MixinEntityPlayerMP method dropItem.
/**
* @author gabizou, April 7th, 2016
*
* Technically an overwrite of {@link EntityPlayer#dropItem(boolean)}
* @param dropAll
* @return
*/
@Override
@Nullable
public EntityItem dropItem(boolean dropAll) {
final ItemStack currentItem = this.inventory.getCurrentItem();
if (currentItem.isEmpty()) {
return null;
}
// Add SlotTransaction to PlayerContainer
org.spongepowered.api.item.inventory.Slot slot = ((Inventory) this.inventoryContainer).query(QueryOperationTypes.INVENTORY_TYPE.of(Hotbar.class)).query(QueryOperationTypes.INVENTORY_PROPERTY.of(SlotIndex.of(this.inventory.currentItem)));
final ItemStackSnapshot originalItem = ItemStackUtil.snapshotOf(currentItem);
ItemStack itemToDrop = this.inventory.decrStackSize(this.inventory.currentItem, dropAll && !currentItem.isEmpty() ? currentItem.getCount() : 1);
((IMixinContainer) this.inventoryContainer).getCapturedTransactions().add(new SlotTransaction(slot, originalItem, ItemStackUtil.snapshotOf(this.inventory.getCurrentItem())));
return this.dropItem(itemToDrop, false, true);
}
use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project SpongeCommon by SpongePowered.
the class MixinInventoryPlayer method onAdd.
@Inject(method = "add", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/NonNullList;set(ILjava/lang/Object;)Ljava/lang/Object;", ordinal = 0))
public void onAdd(int index, ItemStack stack, CallbackInfoReturnable<Boolean> cir) {
if (this.doCapture) {
// Capture "damaged" items picked up
Slot slot = getSpongeSlot(index);
this.capturedTransactions.add(new SlotTransaction(slot, ItemStackSnapshot.NONE, ItemStackUtil.snapshotOf(stack)));
}
}
use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project SpongeCommon by SpongePowered.
the class MixinContainer method onPutStackInSlot.
@Inject(method = "putStackInSlot", at = @At(value = "HEAD"))
public void onPutStackInSlot(int slotId, ItemStack itemstack, CallbackInfo ci) {
if (this.captureInventory) {
this.spongeInit();
final Slot slot = getSlot(slotId);
if (slot != null) {
ItemStackSnapshot originalItem = slot.getStack().isEmpty() ? ItemStackSnapshot.NONE : ((org.spongepowered.api.item.inventory.ItemStack) slot.getStack()).createSnapshot();
ItemStackSnapshot newItem = itemstack.isEmpty() ? ItemStackSnapshot.NONE : ((org.spongepowered.api.item.inventory.ItemStack) itemstack).createSnapshot();
org.spongepowered.api.item.inventory.Slot adapter = this.getContainerSlot(slotId);
this.capturedSlotTransactions.add(new SlotTransaction(adapter, originalItem, newItem));
}
}
}
use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project SpongeCommon by SpongePowered.
the class MixinContainer method beforeSlotChangedCraftingGrid.
@Redirect(method = "slotChangedCraftingGrid", at = @At(value = "INVOKE", target = "Lnet/minecraft/inventory/InventoryCraftResult;setInventorySlotContents(ILnet/minecraft/item/ItemStack;)V"))
private void beforeSlotChangedCraftingGrid(InventoryCraftResult output, int index, ItemStack itemstack) {
if (!this.captureInventory) {
// Capture Inventory is true when caused by a vanilla inventory packet
// This is to prevent infinite loops when a client mod re-requests the recipe result after we modified/cancelled it
output.setInventorySlotContents(index, itemstack);
return;
}
this.spongeInit();
this.capturedCraftPreviewTransactions.clear();
ItemStackSnapshot orig = ItemStackUtil.snapshotOf(output.getStackInSlot(index));
output.setInventorySlotContents(index, itemstack);
ItemStackSnapshot repl = ItemStackUtil.snapshotOf(output.getStackInSlot(index));
SlotAdapter slot = this.adapters.get(index);
this.capturedCraftPreviewTransactions.add(new SlotTransaction(slot, orig, repl));
}
Aggregations