use of org.spongepowered.api.event.item.inventory.UpdateAnvilEvent in project SpongeCommon by SpongePowered.
the class InventoryEventFactory method callUpdateAnvilEvent.
public static UpdateAnvilEvent callUpdateAnvilEvent(final AnvilMenu anvil, final ItemStack slot1, final ItemStack slot2, final ItemStack result, final String name, final int levelCost, final int materialCost) {
final Transaction<ItemStackSnapshot> transaction = new Transaction<>(ItemStackSnapshot.empty(), ItemStackUtil.snapshotOf(result));
final UpdateAnvilEventCost costs = new UpdateAnvilEventCost(levelCost, materialCost);
final UpdateAnvilEvent event = SpongeEventFactory.createUpdateAnvilEvent(PhaseTracker.getCauseStackManager().currentCause(), new Transaction<>(costs, costs), (Inventory) anvil, name, ItemStackUtil.snapshotOf(slot1), transaction, ItemStackUtil.snapshotOf(slot2));
SpongeCommon.post(event);
return event;
}
use of org.spongepowered.api.event.item.inventory.UpdateAnvilEvent in project SpongeCommon by SpongePowered.
the class AnvilMenuMixin_Inventory method impl$throwUpdateAnvilEvent.
@Inject(method = "createResult", at = @At(value = "RETURN"))
private void impl$throwUpdateAnvilEvent(final CallbackInfo ci) {
if (!ShouldFire.UPDATE_ANVIL_EVENT || !PhaseTracker.SERVER.onSidedThread()) {
return;
}
final ItemStack itemstack = this.inputSlots.getItem(0);
final ItemStack itemstack2 = this.inputSlots.getItem(1);
final ItemStack result = this.resultSlots.getItem(0);
final UpdateAnvilEvent event = InventoryEventFactory.callUpdateAnvilEvent((AnvilMenu) (Object) this, itemstack, itemstack2, result, this.itemName == null ? "" : this.itemName, this.cost.get(), this.repairItemCountCost);
final ItemStackSnapshot finalItem = event.result().finalReplacement();
if (event.isCancelled() || finalItem.isEmpty()) {
this.resultSlots.setItem(0, ItemStack.EMPTY);
this.cost.set(0);
this.repairItemCountCost = 0;
((AnvilMenu) (Object) this).broadcastChanges();
return;
}
this.resultSlots.setItem(0, ItemStackUtil.fromSnapshotToNative(event.result().finalReplacement()));
this.cost.set(event.costs().finalReplacement().levelCost());
this.repairItemCountCost = event.costs().finalReplacement().materialCost();
((AnvilMenu) (Object) this).broadcastChanges();
}
Aggregations