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);
}
}
Aggregations