use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project SpongeCommon by SpongePowered.
the class MixinContainer method afterSlotChangedCraftingGrid.
@Inject(method = "slotChangedCraftingGrid", cancellable = true, at = @At(value = "INVOKE", target = "Lnet/minecraft/network/NetHandlerPlayServer;sendPacket(Lnet/minecraft/network/Packet;)V"))
private void afterSlotChangedCraftingGrid(World world, EntityPlayer player, InventoryCrafting craftingInventory, InventoryCraftResult output, CallbackInfo ci) {
if (this.firePreview && !this.capturedCraftPreviewTransactions.isEmpty()) {
Inventory inv = this.query(QueryOperationTypes.INVENTORY_TYPE.of(CraftingInventory.class));
if (!(inv instanceof CraftingInventory)) {
SpongeImpl.getLogger().warn("Detected crafting but Sponge could not get a CraftingInventory for " + this.getClass().getName());
return;
}
SlotTransaction previewTransaction = this.capturedCraftPreviewTransactions.get(this.capturedCraftPreviewTransactions.size() - 1);
IRecipe recipe = CraftingManager.findMatchingRecipe(craftingInventory, world);
SpongeCommonEventFactory.callCraftEventPre(player, ((CraftingInventory) inv), previewTransaction, ((CraftingRecipe) recipe), ((Container) (Object) this), this.capturedCraftPreviewTransactions);
this.capturedCraftPreviewTransactions.clear();
}
}
use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project SpongeCommon by SpongePowered.
the class InventorySetOpsTest method onMidas.
@Listener
public void onMidas(ChangeInventoryEvent.Held event, @Root Player player) {
// Checks if Slots are contained in the hotbar then may transform iron to gold
Inventory hotbar = event.getTargetInventory().query(QueryOperationTypes.INVENTORY_TYPE.of(Hotbar.class));
boolean nugget = false;
for (SlotTransaction transaction : event.getTransactions()) {
if (hotbar.containsInventory(transaction.getSlot())) {
if (ItemTypes.GOLD_NUGGET.equals(transaction.getOriginal().getType())) {
nugget = true;
}
if (nugget && ItemTypes.IRON_INGOT.equals(transaction.getOriginal().getType())) {
transaction.setCustom(ItemStack.of(ItemTypes.GOLD_INGOT, transaction.getOriginal().getQuantity()));
}
}
}
}
use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project modules-extra by CubeEngine.
the class ChangeInventoryReport method listen.
@Listener
public void listen(ClickInventoryEvent event) {
List<SlotTransaction> upperTransactions = new ArrayList<>();
int upperSize = event.getTargetInventory().iterator().next().capacity();
for (SlotTransaction transaction : event.getTransactions()) {
Integer affectedSlot = transaction.getSlot().getInventoryProperty(SlotIndex.class).map(SlotIndex::getValue).orElse(-1);
boolean upper = affectedSlot != -1 && affectedSlot < upperSize;
if (upper) {
upperTransactions.add(transaction);
}
}
Inventory te = event.getTargetInventory().query(QueryOperationTypes.TYPE.of(BlockCarrier.class));
if (!(te instanceof BlockCarrier)) {
te = te.first();
}
if (te instanceof BlockCarrier) {
Action action = this.observe(event);
action.addData(INVENTORY_CHANGES, Observe.transactions(upperTransactions));
action.addData(Report.LOCATION, Observe.location(((BlockCarrier) te).getLocation()));
this.report(action);
}
}
use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project AdamantineShield by Karanum.
the class InventoryChangeListener method onInventoryTransfer.
@Listener
public void onInventoryTransfer(AffectSlotEvent e, @First Player p) {
if (e.getTransactions().isEmpty())
return;
if (!(e.getTransactions().get(0).getSlot().parent() instanceof CarriedInventory))
return;
BlockCarrier carrier = null;
CarriedInventory<?> c = (CarriedInventory<?>) e.getTransactions().get(0).getSlot().parent();
if (c.getCarrier().get() instanceof BlockCarrier) {
carrier = (BlockCarrier) c.getCarrier().get();
}
if (carrier == null)
return;
if (!logContainers && !(carrier instanceof Chest))
return;
long timestamp = new Date().getTime();
int containerSize = c.iterator().next().capacity();
for (SlotTransaction transaction : e.getTransactions()) {
int slotId = transaction.getSlot().getProperty(SlotIndex.class, "slotindex").map(SlotIndex::getValue).orElse(-1);
if (slotId >= containerSize)
continue;
ItemStackSnapshot origItem = transaction.getOriginal();
ItemStackSnapshot finalItem = transaction.getFinal();
if (origItem == finalItem)
continue;
if (origItem.createGameDictionaryEntry().matches(finalItem.createStack()) && ItemStackComparators.ITEM_DATA.compare(origItem.createStack(), finalItem.createStack()) == 0) {
if (origItem.getQuantity() > finalItem.getQuantity()) {
ItemStackSnapshot stack = ItemStack.builder().itemType(origItem.getType()).quantity(origItem.getQuantity() - finalItem.getQuantity()).build().createSnapshot();
db.addToQueue(new InventoryQueueEntry(carrier, slotId, stack, ActionType.CONTAINER_REMOVE, p, timestamp));
} else if (origItem.getQuantity() < finalItem.getQuantity()) {
ItemStackSnapshot stack = ItemStack.builder().itemType(origItem.getType()).quantity(finalItem.getQuantity() - origItem.getQuantity()).build().createSnapshot();
db.addToQueue(new InventoryQueueEntry(carrier, slotId, stack, ActionType.CONTAINER_ADD, p, timestamp));
}
} else {
if (origItem.getType() != ItemTypes.NONE) {
db.addToQueue(new InventoryQueueEntry(carrier, slotId, origItem, ActionType.CONTAINER_REMOVE, p, timestamp));
}
if (finalItem.getType() != ItemTypes.NONE) {
db.addToQueue(new InventoryQueueEntry(carrier, slotId, finalItem, ActionType.CONTAINER_ADD, p, timestamp));
}
}
}
}
use of org.spongepowered.api.item.inventory.transaction.SlotTransaction in project core by CubeEngine.
the class InventoryGuard method onInventoryInteract.
@Listener
public void onInventoryInteract(ClickInventoryEvent event) {
if (!event.getTargetInventory().equals(this.container) && !((Slot) event.getTargetInventory().slots().iterator().next()).transform().parent().equals(this.inventory)) {
return;
}
if (!blockAllIn && !blockAllOut && blockIn.isEmpty() && blockOut.isEmpty()) {
return;
}
// System.out.print("Event:\n");
boolean cancel = false;
int upperSize = event.getTargetInventory().iterator().next().capacity();
for (SlotTransaction transaction : event.getTransactions()) {
ItemStack origStack = transaction.getOriginal().createStack();
ItemStack finalStack = transaction.getFinal().createStack();
String origString = origStack.getType().equals(ItemTypes.NONE) ? origStack.getType().getId() : origStack.getType().getId() + " " + origStack.getQuantity();
String finalString = finalStack.getType().equals(ItemTypes.NONE) ? finalStack.getType().getId() : finalStack.getType().getId() + " " + finalStack.getQuantity();
// System.out.print(origString + "->" + finalString + "\n");
// System.out.println("SI: " + transaction.getSlot().getProperty(SlotIndex.class, "slotindex").map(si -> si.getValue()).orElse(-1) + " " + transaction.getSlot().parent().capacity());
Integer affectedSlot = transaction.getSlot().getProperty(SlotIndex.class, "slotindex").map(SlotIndex::getValue).orElse(-1);
boolean upper = affectedSlot != -1 && affectedSlot < upperSize;
if (upper) {
if (checkTransaction(event, transaction, origStack, finalStack)) {
cancel = true;
} else {
runOnChange();
}
}
// else lower inventory was affected ; actually we don't care (yet)
}
if (cancel) {
// System.out.print("Cancelled\n");
}
}
Aggregations