Search in sources :

Example 1 with TileEntityTickContext

use of org.spongepowered.common.event.tracking.phase.tick.TileEntityTickContext in project SpongeCommon by SpongePowered.

the class TrackingUtil method tickTileEntity.

@SuppressWarnings({ "unused", "try" })
public static void tickTileEntity(IMixinWorldServer mixinWorldServer, ITickable tile) {
    checkArgument(tile instanceof TileEntity, "ITickable %s is not a TileEntity!", tile);
    checkNotNull(tile, "Cannot capture on a null ticking tile entity!");
    final net.minecraft.tileentity.TileEntity tileEntity = (net.minecraft.tileentity.TileEntity) tile;
    final IMixinTileEntity mixinTileEntity = (IMixinTileEntity) tile;
    final BlockPos pos = tileEntity.getPos();
    final IMixinChunk chunk = ((IMixinTileEntity) tile).getActiveChunk();
    if (!mixinTileEntity.shouldTick()) {
        return;
    }
    final TileEntityTickContext context = TickPhase.Tick.TILE_ENTITY.createPhaseContext().source(tile);
    try (final StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame();
        final PhaseContext<?> phaseContext = context) {
        Sponge.getCauseStackManager().pushCause(tile);
        // Add notifier and owner so we don't have to perform lookups during the phases and other processing
        chunk.getBlockNotifier(pos).ifPresent(notifier -> {
            Sponge.getCauseStackManager().addContext(EventContextKeys.NOTIFIER, notifier);
            phaseContext.notifier(notifier);
        });
        User blockOwner = mixinTileEntity.getSpongeOwner();
        if (!mixinTileEntity.hasSetOwner()) {
            blockOwner = chunk.getBlockOwner(pos).orElse(null);
            mixinTileEntity.setSpongeOwner(blockOwner);
        }
        if (blockOwner != null) {
            Sponge.getCauseStackManager().addContext(EventContextKeys.OWNER, blockOwner);
            phaseContext.owner(blockOwner);
        }
        phaseContext.owner = blockOwner;
        // Add the block snapshot of the tile entity for caches to avoid creating multiple snapshots during processing
        // This is a lazy evaluating snapshot to avoid the overhead of snapshot creation
        // Finally, switch the context now that we have the owner and notifier
        phaseContext.buildAndSwitch();
        try (Timing timing = mixinTileEntity.getTimingsHandler().startTiming()) {
            tile.update();
        }
    } catch (Exception e) {
        PhaseTracker.getInstance().printExceptionFromPhase(e, context);
    }
}
Also used : User(org.spongepowered.api.entity.living.player.User) IMixinChunk(org.spongepowered.common.interfaces.IMixinChunk) TileEntity(org.spongepowered.api.block.tileentity.TileEntity) IMixinTileEntity(org.spongepowered.common.interfaces.block.tile.IMixinTileEntity) IMixinTileEntity(org.spongepowered.common.interfaces.block.tile.IMixinTileEntity) StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) BlockPos(net.minecraft.util.math.BlockPos) Timing(co.aikar.timings.Timing) TileEntityTickContext(org.spongepowered.common.event.tracking.phase.tick.TileEntityTickContext)

Aggregations

Timing (co.aikar.timings.Timing)1 BlockPos (net.minecraft.util.math.BlockPos)1 TileEntity (org.spongepowered.api.block.tileentity.TileEntity)1 User (org.spongepowered.api.entity.living.player.User)1 StackFrame (org.spongepowered.api.event.CauseStackManager.StackFrame)1 TileEntityTickContext (org.spongepowered.common.event.tracking.phase.tick.TileEntityTickContext)1 IMixinChunk (org.spongepowered.common.interfaces.IMixinChunk)1 IMixinTileEntity (org.spongepowered.common.interfaces.block.tile.IMixinTileEntity)1