Search in sources :

Example 1 with ImmutableSpongeValue

use of org.spongepowered.common.data.value.ImmutableSpongeValue in project SpongeCommon by SpongePowered.

the class EntityMixin method impl$ThrowIgniteEventForFire.

@Redirect(method = "setRemainingFireTicks", at = @At(value = "FIELD", target = "Lnet/minecraft/world/entity/Entity;remainingFireTicks:I", opcode = Opcodes.PUTFIELD))
private void impl$ThrowIgniteEventForFire(final Entity entity, final int ticks) {
    if (!((LevelBridge) this.level).bridge$isFake() && ShouldFire.IGNITE_ENTITY_EVENT && this.remainingFireTicks < 1 && ticks >= Constants.Entity.MINIMUM_FIRE_TICKS && this.impl$canCallIgniteEntityEvent()) {
        try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
            frame.pushCause(((org.spongepowered.api.entity.Entity) this).location().world());
            final IgniteEntityEvent event = SpongeEventFactory.createIgniteEntityEvent(frame.currentCause(), Ticks.of(ticks), Ticks.of(ticks), (org.spongepowered.api.entity.Entity) this);
            if (SpongeCommon.post(event)) {
                // Don't do anything
                return;
            }
            final DataTransactionResult transaction = DataTransactionResult.builder().replace(new ImmutableSpongeValue<>(Keys.FIRE_TICKS, Ticks.of(Math.max(this.remainingFireTicks, 0)))).success(new ImmutableSpongeValue<>(Keys.FIRE_TICKS, event.fireTicks())).result(DataTransactionResult.Type.SUCCESS).build();
            final ChangeDataHolderEvent.ValueChange valueChange = SpongeEventFactory.createChangeDataHolderEventValueChange(PhaseTracker.SERVER.currentCause(), transaction, (DataHolder.Mutable) this);
            Sponge.eventManager().post(valueChange);
            if (valueChange.isCancelled()) {
                // If the event is cancelled, well, don't change the underlying value.
                return;
            }
            this.remainingFireTicks = valueChange.endResult().successfulValue(Keys.FIRE_TICKS).map(Value::get).map(t -> (int) t.ticks()).orElse(0);
        }
        return;
    }
    // Vanilla functionality
    this.remainingFireTicks = ticks;
}
Also used : LivingEntity(net.minecraft.world.entity.LivingEntity) ItemEntity(net.minecraft.world.entity.item.ItemEntity) Entity(net.minecraft.world.entity.Entity) CauseStackManager(org.spongepowered.api.event.CauseStackManager) DataHolder(org.spongepowered.api.data.DataHolder) DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) IgniteEntityEvent(org.spongepowered.api.event.entity.IgniteEntityEvent) ImmutableSpongeValue(org.spongepowered.common.data.value.ImmutableSpongeValue) ChangeDataHolderEvent(org.spongepowered.api.event.data.ChangeDataHolderEvent) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Aggregations

Entity (net.minecraft.world.entity.Entity)1 LivingEntity (net.minecraft.world.entity.LivingEntity)1 ItemEntity (net.minecraft.world.entity.item.ItemEntity)1 DataHolder (org.spongepowered.api.data.DataHolder)1 DataTransactionResult (org.spongepowered.api.data.DataTransactionResult)1 CauseStackManager (org.spongepowered.api.event.CauseStackManager)1 ChangeDataHolderEvent (org.spongepowered.api.event.data.ChangeDataHolderEvent)1 IgniteEntityEvent (org.spongepowered.api.event.entity.IgniteEntityEvent)1 Redirect (org.spongepowered.asm.mixin.injection.Redirect)1 ImmutableSpongeValue (org.spongepowered.common.data.value.ImmutableSpongeValue)1