Search in sources :

Example 1 with TimingBridge

use of org.spongepowered.common.bridge.TimingBridge in project SpongeCommon by SpongePowered.

the class TrackingUtil method updateTickBlock.

@SuppressWarnings("rawtypes")
public static void updateTickBlock(final TrackedWorldBridge mixinWorld, final net.minecraft.world.level.block.state.BlockState block, final BlockPos pos, final Random random) {
    final ServerLevel world = (ServerLevel) mixinWorld;
    final org.spongepowered.api.world.server.ServerWorld apiWorld = (org.spongepowered.api.world.server.ServerWorld) world;
    if (ShouldFire.TICK_BLOCK_EVENT) {
        final BlockSnapshot snapshot = mixinWorld.bridge$createSnapshot(block, pos, BlockChangeFlags.NONE);
        final TickBlockEvent event = SpongeEventFactory.createTickBlockEventScheduled(PhaseTracker.getCauseStackManager().currentCause(), snapshot);
        SpongeCommon.post(event);
        if (event.isCancelled()) {
            return;
        }
    }
    final LocatableBlock locatable = new SpongeLocatableBlockBuilder().world(apiWorld).position(pos.getX(), pos.getY(), pos.getZ()).state((BlockState) block).build();
    final BlockTickContext phaseContext = TickPhase.Tick.BLOCK.createPhaseContext(PhaseTracker.SERVER).source(locatable);
    // We have to associate any notifiers in case of scheduled block updates from other sources
    final PhaseContext<@NonNull ?> currentContext = PhaseTracker.getInstance().getPhaseContext();
    currentContext.appendNotifierPreBlockTick(world, pos, phaseContext);
    try (final PhaseContext<@NonNull ?> context = phaseContext;
        final Timing timing = ((TimingBridge) block.getBlock()).bridge$timings()) {
        timing.startTiming();
        context.buildAndSwitch();
        PhaseTracker.LOGGER.trace(TrackingUtil.BLOCK_TICK, () -> "Wrapping Block Tick: " + block.toString());
        block.tick(world, pos, random);
    } catch (final Exception | NoClassDefFoundError e) {
        PhasePrinter.printExceptionFromPhase(PhaseTracker.getInstance().stack, e, phaseContext);
    }
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) SpongeLocatableBlockBuilder(org.spongepowered.common.world.server.SpongeLocatableBlockBuilder) SpongeBlockSnapshot(org.spongepowered.common.block.SpongeBlockSnapshot) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) TickBlockEvent(org.spongepowered.api.event.block.TickBlockEvent) BlockState(org.spongepowered.api.block.BlockState) BlockTickContext(org.spongepowered.common.event.tracking.phase.tick.BlockTickContext) LocatableBlock(org.spongepowered.api.world.LocatableBlock) Timing(co.aikar.timings.Timing) TimingBridge(org.spongepowered.common.bridge.TimingBridge)

Example 2 with TimingBridge

use of org.spongepowered.common.bridge.TimingBridge in project SpongeCommon by SpongePowered.

the class ServerLevelMixin_Tracker method tracker$wrapBlockRandomTick.

/**
 * For PhaseTracking, we need to wrap around the
 * {@link BlockState#tick(ServerLevel, BlockPos, Random)} method, and the ScheduledTickList uses a lambda method
 * to {@code ServerWorld#tickBlock(NextTickListEntry)}, so it's either we customize the ScheduledTickList
 * or we wrap in this method here.
 *
 * @author gabizou - January 11th, 2020 - Minecraft 1.14.3
 */
@Redirect(method = "tickChunk", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/block/state/BlockState;randomTick(Lnet/minecraft/server/level/ServerLevel;Lnet/minecraft/core/BlockPos;Ljava/util/Random;)V"))
private void tracker$wrapBlockRandomTick(final BlockState blockState, final ServerLevel worldIn, final BlockPos posIn, final Random randomIn) {
    try (final Timing timing = ((TimingBridge) blockState.getBlock()).bridge$timings()) {
        timing.startTiming();
        TrackingUtil.randomTickBlock(this, blockState, posIn, this.random);
    }
}
Also used : Timing(co.aikar.timings.Timing) TimingBridge(org.spongepowered.common.bridge.TimingBridge) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 3 with TimingBridge

use of org.spongepowered.common.bridge.TimingBridge in project SpongeCommon by SpongePowered.

the class ServerLevelMixin_Tracker method tracker$wrapNormalEntityTick.

@Redirect(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/level/ServerLevel;guardEntityTick(Ljava/util/function/Consumer;Lnet/minecraft/world/entity/Entity;)V"), slice = @Slice(from = @At(value = "INVOKE_STRING", target = "Lnet/minecraft/util/profiling/ProfilerFiller;push(Ljava/lang/String;)V", args = "ldc=tick"), to = @At(value = "INVOKE_STRING", target = "Lnet/minecraft/util/profiling/ProfilerFiller;push(Ljava/lang/String;)V", args = "ldc=remove")))
private void tracker$wrapNormalEntityTick(final ServerLevel level, final Consumer<Entity> entityUpdateConsumer, final Entity entity) {
    final Timing entityTickTiming = ((TimingBridge) entity.getType()).bridge$timings();
    entityTickTiming.startTiming();
    final PhaseContext<@NonNull ?> currentState = PhaseTracker.SERVER.getPhaseContext();
    TrackingUtil.tickEntity(entityUpdateConsumer, entity);
    entityTickTiming.stopTiming();
}
Also used : Timing(co.aikar.timings.Timing) TimingBridge(org.spongepowered.common.bridge.TimingBridge) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 4 with TimingBridge

use of org.spongepowered.common.bridge.TimingBridge in project SpongeCommon by SpongePowered.

the class TrackingUtil method tickEntity.

public static void tickEntity(final Consumer<net.minecraft.world.entity.Entity> consumer, final net.minecraft.world.entity.Entity entity) {
    checkArgument(entity instanceof Entity, "Entity %s is not an instance of SpongeAPI's Entity!", entity);
    checkNotNull(entity, "Cannot capture on a null ticking entity!");
    if (!((TrackableBridge) entity).bridge$shouldTick()) {
        return;
    }
    final EntityTickContext tickContext = TickPhase.Tick.ENTITY.createPhaseContext(PhaseTracker.SERVER).source(entity);
    try (final EntityTickContext context = tickContext;
        final Timing entityTiming = ((TimingBridge) entity.getType()).bridge$timings()) {
        if (entity instanceof CreatorTrackedBridge) {
            ((CreatorTrackedBridge) entity).tracker$getNotifierUUID().ifPresent(context::notifier);
            ((CreatorTrackedBridge) entity).tracker$getCreatorUUID().ifPresent(context::creator);
        }
        context.buildAndSwitch();
        entityTiming.startTiming();
        consumer.accept(entity);
        if (ShouldFire.MOVE_ENTITY_EVENT) {
            SpongeCommonEventFactory.callNaturalMoveEntityEvent(entity);
        }
        if (ShouldFire.ROTATE_ENTITY_EVENT) {
            SpongeCommonEventFactory.callNaturalRotateEntityEvent(entity);
        }
    } catch (final Exception e) {
        PhasePrinter.printExceptionFromPhase(PhaseTracker.getInstance().stack, e, tickContext);
    }
}
Also used : TickableBlockEntity(net.minecraft.world.level.block.entity.TickableBlockEntity) BlockEntity(org.spongepowered.api.block.entity.BlockEntity) Entity(org.spongepowered.api.entity.Entity) EntityTickContext(org.spongepowered.common.event.tracking.phase.tick.EntityTickContext) TileEntityTickContext(org.spongepowered.common.event.tracking.phase.tick.TileEntityTickContext) CreatorTrackedBridge(org.spongepowered.common.bridge.CreatorTrackedBridge) Timing(co.aikar.timings.Timing) TimingBridge(org.spongepowered.common.bridge.TimingBridge)

Example 5 with TimingBridge

use of org.spongepowered.common.bridge.TimingBridge in project SpongeCommon by SpongePowered.

the class TrackingUtil method updateTickFluid.

public static void updateTickFluid(final TrackedWorldBridge mixinWorld, final FluidState fluidState, final BlockPos pos) {
    final ServerLevel world = (ServerLevel) mixinWorld;
    final org.spongepowered.api.world.server.ServerWorld apiWorld = (org.spongepowered.api.world.server.ServerWorld) world;
    final net.minecraft.world.level.block.state.BlockState blockState = fluidState.createLegacyBlock();
    if (ShouldFire.TICK_BLOCK_EVENT) {
        final BlockSnapshot snapshot = mixinWorld.bridge$createSnapshot(blockState, pos, BlockChangeFlags.NONE);
        final TickBlockEvent event = SpongeEventFactory.createTickBlockEventScheduled(PhaseTracker.getCauseStackManager().currentCause(), snapshot);
        SpongeCommon.post(event);
        if (event.isCancelled()) {
            return;
        }
    }
    final LocatableBlock locatable = new SpongeLocatableBlockBuilder().world(apiWorld).position(pos.getX(), pos.getY(), pos.getZ()).state((BlockState) blockState).build();
    final FluidTickContext phaseContext = TickPhase.Tick.FLUID.createPhaseContext(PhaseTracker.SERVER).source(locatable).fluid(fluidState);
    // We have to associate any notifiers in case of scheduled block updates from other sources
    final PhaseContext<@NonNull ?> currentContext = PhaseTracker.getInstance().getPhaseContext();
    currentContext.appendNotifierPreBlockTick(world, pos, phaseContext);
    try (final PhaseContext<?> context = phaseContext;
        final Timing timing = ((TimingBridge) blockState.getBlock()).bridge$timings()) {
        timing.startTiming();
        context.buildAndSwitch();
        PhaseTracker.LOGGER.trace(TrackingUtil.FLUID_TICK, () -> "Wrapping Fluid Tick: " + fluidState.toString());
        fluidState.tick(world, pos);
    } catch (final Exception | NoClassDefFoundError e) {
        PhasePrinter.printExceptionFromPhase(PhaseTracker.getInstance().stack, e, phaseContext);
    }
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) SpongeLocatableBlockBuilder(org.spongepowered.common.world.server.SpongeLocatableBlockBuilder) TickBlockEvent(org.spongepowered.api.event.block.TickBlockEvent) LocatableBlock(org.spongepowered.api.world.LocatableBlock) SpongeBlockSnapshot(org.spongepowered.common.block.SpongeBlockSnapshot) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) FluidTickContext(org.spongepowered.common.event.tracking.phase.tick.FluidTickContext) BlockState(org.spongepowered.api.block.BlockState) Timing(co.aikar.timings.Timing) TimingBridge(org.spongepowered.common.bridge.TimingBridge)

Aggregations

Timing (co.aikar.timings.Timing)6 TimingBridge (org.spongepowered.common.bridge.TimingBridge)6 ServerLevel (net.minecraft.server.level.ServerLevel)2 TickableBlockEntity (net.minecraft.world.level.block.entity.TickableBlockEntity)2 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)2 BlockState (org.spongepowered.api.block.BlockState)2 BlockEntity (org.spongepowered.api.block.entity.BlockEntity)2 Entity (org.spongepowered.api.entity.Entity)2 TickBlockEvent (org.spongepowered.api.event.block.TickBlockEvent)2 LocatableBlock (org.spongepowered.api.world.LocatableBlock)2 Redirect (org.spongepowered.asm.mixin.injection.Redirect)2 SpongeBlockSnapshot (org.spongepowered.common.block.SpongeBlockSnapshot)2 CreatorTrackedBridge (org.spongepowered.common.bridge.CreatorTrackedBridge)2 EntityTickContext (org.spongepowered.common.event.tracking.phase.tick.EntityTickContext)2 TileEntityTickContext (org.spongepowered.common.event.tracking.phase.tick.TileEntityTickContext)2 SpongeLocatableBlockBuilder (org.spongepowered.common.world.server.SpongeLocatableBlockBuilder)2 BlockTickContext (org.spongepowered.common.event.tracking.phase.tick.BlockTickContext)1 FluidTickContext (org.spongepowered.common.event.tracking.phase.tick.FluidTickContext)1