use of org.spongepowered.common.item.inventory.adapter.impl.slots.SlotAdapter 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));
}
use of org.spongepowered.common.item.inventory.adapter.impl.slots.SlotAdapter 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);
}
}
}
Aggregations