Search in sources :

Example 1 with ChangeEntityEquipmentEvent

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;
    }
}
Also used : LivingEntity(net.minecraft.world.entity.LivingEntity) HopperBlockEntity(net.minecraft.world.level.block.entity.HopperBlockEntity) ItemEntity(net.minecraft.world.entity.item.ItemEntity) Entity(org.spongepowered.api.entity.Entity) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) PlayerInventoryTransaction(org.spongepowered.common.event.tracking.context.transaction.inventory.PlayerInventoryTransaction) Transaction(org.spongepowered.api.data.Transaction) CauseStackManager(org.spongepowered.api.event.CauseStackManager) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) ChangeEntityEquipmentEvent(org.spongepowered.api.event.entity.ChangeEntityEquipmentEvent)

Example 2 with ChangeEntityEquipmentEvent

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;
}
Also used : ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) ItemStack(net.minecraft.world.item.ItemStack) ChangeEntityEquipmentEvent(org.spongepowered.api.event.entity.ChangeEntityEquipmentEvent)

Aggregations

ChangeEntityEquipmentEvent (org.spongepowered.api.event.entity.ChangeEntityEquipmentEvent)2 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)2 LivingEntity (net.minecraft.world.entity.LivingEntity)1 ItemEntity (net.minecraft.world.entity.item.ItemEntity)1 ItemStack (net.minecraft.world.item.ItemStack)1 HopperBlockEntity (net.minecraft.world.level.block.entity.HopperBlockEntity)1 Transaction (org.spongepowered.api.data.Transaction)1 Entity (org.spongepowered.api.entity.Entity)1 CauseStackManager (org.spongepowered.api.event.CauseStackManager)1 SlotTransaction (org.spongepowered.api.item.inventory.transaction.SlotTransaction)1 PlayerInventoryTransaction (org.spongepowered.common.event.tracking.context.transaction.inventory.PlayerInventoryTransaction)1