use of org.spongepowered.api.event.entity.ChangeEntityEquipmentEvent in project SpongeCommon by SpongePowered.
the class InventoryEventFactory method callChangeEntityEquipmentEvent.
public static ChangeEntityEquipmentEvent callChangeEntityEquipmentEvent(final LivingEntity entity, final ItemStackSnapshot before, final ItemStackSnapshot after, final Slot slot) {
final ChangeEntityEquipmentEvent event;
try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
frame.pushCause(entity);
final Transaction<ItemStackSnapshot> transaction = new Transaction<>(before, after);
if (after.isEmpty()) {
event = SpongeEventFactory.createChangeEntityEquipmentEventBreak(frame.currentCause(), (Entity) entity, slot, transaction);
} else {
event = SpongeEventFactory.createChangeEntityEquipmentEvent(frame.currentCause(), (Entity) entity, slot, transaction);
}
SpongeCommon.post(event);
return event;
}
}
use of org.spongepowered.api.event.entity.ChangeEntityEquipmentEvent in project SpongeCommon by SpongePowered.
the class LivingEntityMixin_Inventory method impl$throwEquipmentEvent.
private boolean impl$throwEquipmentEvent(final EquipmentSlot equipmentslottype, final Slot slot, final ItemStack newStack, final ItemStack oldStack) {
final ChangeEntityEquipmentEvent event = InventoryEventFactory.callChangeEntityEquipmentEvent((LivingEntity) (Object) this, ItemStackUtil.snapshotOf(oldStack), ItemStackUtil.snapshotOf(newStack), slot);
if (event.isCancelled()) {
this.shadow$setItemSlot(equipmentslottype, oldStack);
return true;
}
final Transaction<@NonNull ItemStackSnapshot> transaction = event.transaction();
if (!transaction.isValid()) {
this.shadow$setItemSlot(equipmentslottype, oldStack);
return true;
}
final Optional<ItemStackSnapshot> optional = transaction.custom();
if (optional.isPresent()) {
final ItemStack custom = ItemStackUtil.fromSnapshotToNative(optional.get());
this.shadow$setItemSlot(equipmentslottype, custom);
return true;
}
return false;
}
Aggregations