Search in sources :

Example 1 with Timing

use of co.aikar.timings.Timing in project SpongeCommon by SpongePowered.

the class TrackingUtil method tickRidingEntity.

public static void tickRidingEntity(net.minecraft.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!");
    final IMixinEntity mixinEntity = EntityUtil.toMixin(entity);
    if (!mixinEntity.shouldTick()) {
        return;
    }
    final Optional<User> notifierUser = mixinEntity.getNotifierUser();
    final Optional<User> creatorUser = mixinEntity.getCreatorUser();
    final EntityTickContext tickContext = TickPhase.Tick.ENTITY.createPhaseContext().source(entity).notifier(() -> notifierUser).owner(() -> creatorUser);
    try (final StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame();
        final EntityTickContext context = tickContext.buildAndSwitch();
        final Timing entityTiming = mixinEntity.getTimingsHandler().startTiming()) {
        Sponge.getCauseStackManager().pushCause(entity);
        notifierUser.ifPresent(notifier -> frame.addContext(EventContextKeys.NOTIFIER, notifier));
        creatorUser.ifPresent(notifier -> frame.addContext(EventContextKeys.OWNER, notifier));
        entity.updateRidden();
    } catch (Exception | NoClassDefFoundError e) {
        PhaseTracker.getInstance().printExceptionFromPhase(e, tickContext);
    }
}
Also used : IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) TileEntity(org.spongepowered.api.block.tileentity.TileEntity) IMixinTileEntity(org.spongepowered.common.interfaces.block.tile.IMixinTileEntity) Entity(org.spongepowered.api.entity.Entity) EntityTickContext(org.spongepowered.common.event.tracking.phase.tick.EntityTickContext) TileEntityTickContext(org.spongepowered.common.event.tracking.phase.tick.TileEntityTickContext) User(org.spongepowered.api.entity.living.player.User) StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) Timing(co.aikar.timings.Timing)

Example 2 with Timing

use of co.aikar.timings.Timing 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)

Example 3 with Timing

use of co.aikar.timings.Timing in project SpongeCommon by SpongePowered.

the class TrackingUtil method tickEntity.

public static void tickEntity(net.minecraft.entity.Entity entityIn) {
    checkArgument(entityIn instanceof Entity, "Entity %s is not an instance of SpongeAPI's Entity!", entityIn);
    checkNotNull(entityIn, "Cannot capture on a null ticking entity!");
    final IMixinEntity mixinEntity = EntityUtil.toMixin(entityIn);
    if (!mixinEntity.shouldTick()) {
        return;
    }
    final EntityTickContext tickContext = TickPhase.Tick.ENTITY.createPhaseContext().source(entityIn);
    try (final StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame();
        final EntityTickContext context = tickContext;
        final Timing entityTiming = mixinEntity.getTimingsHandler().startTiming()) {
        mixinEntity.getNotifierUser().ifPresent(notifier -> {
            frame.addContext(EventContextKeys.NOTIFIER, notifier);
            context.notifier(notifier);
        });
        mixinEntity.getCreatorUser().ifPresent(owner -> {
            if (mixinEntity instanceof EntityFallingBlock) {
                frame.pushCause(owner);
            }
            frame.addContext(EventContextKeys.OWNER, owner);
            context.owner(owner);
        });
        context.buildAndSwitch();
        entityIn.onUpdate();
    } catch (Exception | NoClassDefFoundError e) {
        PhaseTracker.getInstance().printExceptionFromPhase(e, tickContext);
    }
}
Also used : IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) TileEntity(org.spongepowered.api.block.tileentity.TileEntity) IMixinTileEntity(org.spongepowered.common.interfaces.block.tile.IMixinTileEntity) Entity(org.spongepowered.api.entity.Entity) EntityTickContext(org.spongepowered.common.event.tracking.phase.tick.EntityTickContext) TileEntityTickContext(org.spongepowered.common.event.tracking.phase.tick.TileEntityTickContext) EntityFallingBlock(net.minecraft.entity.item.EntityFallingBlock) StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) Timing(co.aikar.timings.Timing)

Example 4 with Timing

use of co.aikar.timings.Timing in project SpongeCommon by SpongePowered.

the class SpongeChunkGenerator method populate.

@Override
public void populate(int chunkX, int chunkZ) {
    IMixinWorldServer world = (IMixinWorldServer) this.world;
    world.getTimingsHandler().chunkPopulate.startTimingIfSync();
    this.chunkGeneratorTiming.startTimingIfSync();
    final PhaseTracker phaseTracker = PhaseTracker.getInstance();
    this.rand.setSeed(this.world.getSeed());
    long i1 = this.rand.nextLong() / 2L * 2L + 1L;
    long j1 = this.rand.nextLong() / 2L * 2L + 1L;
    this.rand.setSeed(chunkX * i1 + chunkZ * j1 ^ this.world.getSeed());
    BlockFalling.fallInstantly = true;
    // Have to regeneate the biomes so that any virtual biomes can be passed
    // to the populator.
    this.cachedBiomes.reuse(new Vector3i(chunkX * 16, 0, chunkZ * 16));
    this.biomeGenerator.generateBiomes(this.cachedBiomes);
    ImmutableBiomeVolume biomeBuffer = this.cachedBiomes.getImmutableBiomeCopy();
    BlockPos blockpos = new BlockPos(chunkX * 16, 0, chunkZ * 16);
    BiomeType biome = (BiomeType) this.world.getBiome(blockpos.add(16, 0, 16));
    org.spongepowered.api.world.Chunk chunk = (org.spongepowered.api.world.Chunk) this.world.getChunkFromChunkCoords(chunkX, chunkZ);
    BiomeGenerationSettings settings = getBiomeSettings(biome);
    List<Populator> populators = new ArrayList<>(this.pop);
    Populator snowPopulator = null;
    Iterator<Populator> itr = populators.iterator();
    while (itr.hasNext()) {
        Populator populator = itr.next();
        if (populator instanceof SnowPopulator) {
            itr.remove();
            snowPopulator = populator;
            break;
        }
    }
    populators.addAll(settings.getPopulators());
    if (snowPopulator != null) {
        populators.add(snowPopulator);
    }
    Sponge.getGame().getEventManager().post(SpongeEventFactory.createPopulateChunkEventPre(Sponge.getCauseStackManager().getCurrentCause(), populators, chunk));
    List<String> flags = Lists.newArrayList();
    Vector3i min = new Vector3i(chunkX * 16 + 8, 0, chunkZ * 16 + 8);
    org.spongepowered.api.world.World spongeWorld = (org.spongepowered.api.world.World) this.world;
    Extent volume = new SoftBufferExtentViewDownsize(chunk.getWorld(), min, min.add(15, 255, 15), min.sub(8, 0, 8), min.add(23, 255, 23));
    for (Populator populator : populators) {
        final PopulatorType type = populator.getType();
        if (type == null) {
            System.err.printf("Found a populator with a null type: %s populator%n", populator);
        }
        if (Sponge.getGame().getEventManager().post(SpongeEventFactory.createPopulateChunkEventPopulate(Sponge.getCauseStackManager().getCurrentCause(), populator, chunk))) {
            continue;
        }
        try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
            Timing timing = null;
            if (Timings.isTimingsEnabled()) {
                timing = this.populatorTimings.get(populator.getType().getId());
                if (timing == null) {
                    // ,
                    timing = SpongeTimingsFactory.ofSafe("populate - " + populator.getType().getId());
                    // this.chunkGeneratorTiming);
                    this.populatorTimings.put(populator.getType().getId(), timing);
                }
                timing.startTimingIfSync();
            }
            try (PhaseContext<?> context = GenerationPhase.State.POPULATOR_RUNNING.createPhaseContext().world(world).populator(type).buildAndSwitch()) {
                if (populator instanceof IFlaggedPopulator) {
                    ((IFlaggedPopulator) populator).populate(spongeWorld, volume, this.rand, biomeBuffer, flags);
                } else {
                    populator.populate(spongeWorld, volume, this.rand, biomeBuffer);
                }
                if (Timings.isTimingsEnabled()) {
                    timing.stopTimingIfSync();
                }
            }
        }
    }
    // populate method so that its particular changes are used.
    if (this.baseGenerator instanceof SpongeGenerationPopulator) {
        Timing timing = null;
        if (Timings.isTimingsEnabled()) {
            IGenerationPopulator spongePopulator = (IGenerationPopulator) this.baseGenerator;
            timing = spongePopulator.getTimingsHandler();
            timing.startTimingIfSync();
        }
        ((SpongeGenerationPopulator) this.baseGenerator).getHandle(this.world).populate(chunkX, chunkZ);
        if (Timings.isTimingsEnabled()) {
            timing.stopTimingIfSync();
        }
    }
    PopulateChunkEvent.Post event = SpongeEventFactory.createPopulateChunkEventPost(Sponge.getCauseStackManager().getCurrentCause(), ImmutableList.copyOf(populators), chunk);
    SpongeImpl.postEvent(event);
    BlockFalling.fallInstantly = false;
    this.chunkGeneratorTiming.stopTimingIfSync();
    world.getTimingsHandler().chunkPopulate.stopTimingIfSync();
}
Also used : Extent(org.spongepowered.api.world.extent.Extent) ArrayList(java.util.ArrayList) IGenerationPopulator(org.spongepowered.common.interfaces.world.gen.IGenerationPopulator) PopulatorType(org.spongepowered.api.world.gen.PopulatorType) World(net.minecraft.world.World) BiomeType(org.spongepowered.api.world.biome.BiomeType) SoftBufferExtentViewDownsize(org.spongepowered.common.world.extent.SoftBufferExtentViewDownsize) ChunkGeneratorOverworld(net.minecraft.world.gen.ChunkGeneratorOverworld) IChunkProviderOverworld(org.spongepowered.common.interfaces.world.gen.IChunkProviderOverworld) CauseStackManager(org.spongepowered.api.event.CauseStackManager) BlockPos(net.minecraft.util.math.BlockPos) BiomeGenerationSettings(org.spongepowered.api.world.biome.BiomeGenerationSettings) SpongeBiomeGenerationSettings(org.spongepowered.common.world.biome.SpongeBiomeGenerationSettings) GenerationPopulator(org.spongepowered.api.world.gen.GenerationPopulator) Populator(org.spongepowered.api.world.gen.Populator) IFlaggedPopulator(org.spongepowered.common.interfaces.world.gen.IFlaggedPopulator) SnowPopulator(org.spongepowered.common.world.gen.populators.SnowPopulator) IGenerationPopulator(org.spongepowered.common.interfaces.world.gen.IGenerationPopulator) ImmutableBiomeVolume(org.spongepowered.api.world.extent.ImmutableBiomeVolume) IFlaggedPopulator(org.spongepowered.common.interfaces.world.gen.IFlaggedPopulator) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) PopulateChunkEvent(org.spongepowered.api.event.world.chunk.PopulateChunkEvent) IMixinChunk(org.spongepowered.common.interfaces.IMixinChunk) Chunk(net.minecraft.world.chunk.Chunk) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) Vector3i(com.flowpowered.math.vector.Vector3i) SnowPopulator(org.spongepowered.common.world.gen.populators.SnowPopulator) Timing(co.aikar.timings.Timing)

Example 5 with Timing

use of co.aikar.timings.Timing in project SpongeForge by SpongePowered.

the class MixinGameRegistry method onGenerateWorld.

@Redirect(method = "generateWorld", at = @At(value = "INVOKE", target = WORLD_GENERATOR_GENERATE, remap = false))
private static void onGenerateWorld(IWorldGenerator worldGenerator, Random random, int chunkX, int chunkZ, World world, IChunkGenerator chunkGenerator, IChunkProvider chunkProvider) {
    Timing timing = null;
    if (Timings.isTimingsEnabled()) {
        timing = worldGeneratorTimings.get(worldGenerator.getClass());
        if (timing == null) {
            String modId = StaticMixinForgeHelper.getModIdFromClass(worldGenerator.getClass());
            timing = SpongeTimingsFactory.ofSafe("worldGenerator (" + modId + ":" + worldGenerator.getClass().getName() + ")");
            worldGeneratorTimings.put(worldGenerator.getClass(), timing);
        }
        timing.startTimingIfSync();
    }
    worldGenerator.generate(random, chunkX, chunkZ, world, chunkGenerator, chunkProvider);
    if (Timings.isTimingsEnabled()) {
        timing.stopTimingIfSync();
    }
}
Also used : Timing(co.aikar.timings.Timing) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Aggregations

Timing (co.aikar.timings.Timing)22 TileEntityTickContext (org.spongepowered.common.event.tracking.phase.tick.TileEntityTickContext)6 TimingBridge (org.spongepowered.common.bridge.TimingBridge)5 DataPacketSendEvent (cn.nukkit.event.server.DataPacketSendEvent)4 Entity (org.spongepowered.api.entity.Entity)4 EntityTickContext (org.spongepowered.common.event.tracking.phase.tick.EntityTickContext)4 BlockPos (net.minecraft.util.math.BlockPos)3 TileEntity (org.spongepowered.api.block.tileentity.TileEntity)3 StackFrame (org.spongepowered.api.event.CauseStackManager.StackFrame)3 IMixinTileEntity (org.spongepowered.common.interfaces.block.tile.IMixinTileEntity)3 TickableBlockEntity (net.minecraft.world.level.block.entity.TickableBlockEntity)2 BlockEntity (org.spongepowered.api.block.entity.BlockEntity)2 User (org.spongepowered.api.entity.living.player.User)2 Redirect (org.spongepowered.asm.mixin.injection.Redirect)2 CreatorTrackedBridge (org.spongepowered.common.bridge.CreatorTrackedBridge)2 IMixinChunk (org.spongepowered.common.interfaces.IMixinChunk)2 IMixinEntity (org.spongepowered.common.interfaces.entity.IMixinEntity)2 Type (cn.nukkit.AdventureSettings.Type)1 cn.nukkit.block (cn.nukkit.block)1 BlockEntity (cn.nukkit.blockentity.BlockEntity)1