Search in sources :

Example 26 with ServerLevel

use of net.minecraft.server.level.ServerLevel in project SpongeCommon by SpongePowered.

the class AddTileEntityToWorldWhileProcessingEffect method processSideEffect.

@Override
public EffectResult processSideEffect(final BlockPipeline pipeline, final PipelineCursor oldState, final BlockState newState, final SpongeBlockChangeFlag flag, final int limit) {
    final ServerLevel serverWorld = pipeline.getServerWorld();
    final BlockEntity tileEntity = oldState.tileEntity;
    if (tileEntity == null) {
        return EffectResult.NULL_RETURN;
    }
    if (((LevelAccessor) serverWorld).accessor$updatingBlockEntities()) {
        ServerLevelAccessor.accessor$LOGGER().error("Adding block entity while ticking: {} @ {}", () -> Registry.BLOCK_ENTITY_TYPE.getKey(tileEntity.getType()), tileEntity::getBlockPos);
        final boolean add = ((LevelAccessor) serverWorld).accessor$pendingBlockEntities().add(tileEntity);
        if (add) {
            return new EffectResult(oldState.state, true);
        } else {
            return EffectResult.NULL_RETURN;
        }
    }
    return EffectResult.NULL_PASS;
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) ServerLevelAccessor(org.spongepowered.common.accessor.server.level.ServerLevelAccessor) LevelAccessor(org.spongepowered.common.accessor.world.level.LevelAccessor) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity)

Example 27 with ServerLevel

use of net.minecraft.server.level.ServerLevel 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 28 with ServerLevel

use of net.minecraft.server.level.ServerLevel in project SpongeCommon by SpongePowered.

the class RemoveProposedTileEntitiesDuringSetIfWorldProcessingEffect method processSideEffect.

@Override
public EffectResult processSideEffect(final BlockPipeline pipeline, final PipelineCursor oldState, final BlockState newState, final SpongeBlockChangeFlag flag, final int limit) {
    final ServerLevel serverWorld = pipeline.getServerWorld();
    @Nullable final BlockEntity tileEntity = oldState.tileEntity;
    final BlockPos pos = oldState.pos;
    if (tileEntity == null || tileEntity.isRemoved()) {
        return EffectResult.NULL_RETURN;
    }
    if (((LevelAccessor) serverWorld).accessor$updatingBlockEntities()) {
        final Iterator<BlockEntity> iterator = ((LevelAccessor) serverWorld).accessor$pendingBlockEntities().iterator();
        while (iterator.hasNext()) {
            final BlockEntity tileentity = iterator.next();
            if (tileentity.getBlockPos().equals(pos)) {
                tileentity.setRemoved();
                iterator.remove();
            }
        }
        serverWorld.blockEntityList.add(tileEntity);
        return EffectResult.NULL_RETURN;
    }
    return EffectResult.NULL_PASS;
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) LevelAccessor(org.spongepowered.common.accessor.world.level.LevelAccessor) BlockPos(net.minecraft.core.BlockPos) Nullable(org.checkerframework.checker.nullness.qual.Nullable) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity)

Example 29 with ServerLevel

use of net.minecraft.server.level.ServerLevel in project SpongeCommon by SpongePowered.

the class RemoveTileEntityFromWorldEffect method processSideEffect.

@Override
public EffectResult processSideEffect(final BlockPipeline pipeline, final PipelineCursor oldState, final BlockState newState, final SpongeBlockChangeFlag flag, final int limit) {
    @Nullable final BlockEntity tileEntity = oldState.tileEntity;
    if (tileEntity == null) {
        return EffectResult.NULL_RETURN;
    }
    final ServerLevel serverWorld = pipeline.getServerWorld();
    final LevelAccessor worldAccessor = (LevelAccessor) serverWorld;
    if (worldAccessor.accessor$updatingBlockEntities()) {
        tileEntity.setRemoved();
        worldAccessor.accessor$pendingBlockEntities().remove(tileEntity);
        return EffectResult.NULL_RETURN;
    }
    worldAccessor.accessor$pendingBlockEntities().remove(tileEntity);
    serverWorld.blockEntityList.remove(tileEntity);
    serverWorld.tickableBlockEntities.remove(tileEntity);
    return EffectResult.NULL_PASS;
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) LevelAccessor(org.spongepowered.common.accessor.world.level.LevelAccessor) Nullable(org.checkerframework.checker.nullness.qual.Nullable) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity)

Example 30 with ServerLevel

use of net.minecraft.server.level.ServerLevel in project SpongeCommon by SpongePowered.

the class NetherPortalType method findPortalInternal.

static Optional<BlockUtil.FoundRectangle> findPortalInternal(final ServerLocation location) {
    final ServerLevel serverWorld = (ServerLevel) location.world();
    final BlockPos position = VecHelper.toBlockPos(location.blockPosition());
    return serverWorld.getPortalForcer().findPortalAround(position, serverWorld.dimension() == Level.NETHER);
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) BlockPos(net.minecraft.core.BlockPos)

Aggregations

ServerLevel (net.minecraft.server.level.ServerLevel)115 BlockPos (net.minecraft.core.BlockPos)30 Nullable (org.checkerframework.checker.nullness.qual.Nullable)21 Level (net.minecraft.world.level.Level)20 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)16 Entity (net.minecraft.world.entity.Entity)14 ServerPlayer (net.minecraft.server.level.ServerPlayer)11 ItemStack (net.minecraft.world.item.ItemStack)10 SpongeBlockSnapshot (org.spongepowered.common.block.SpongeBlockSnapshot)10 IOException (java.io.IOException)9 LevelChunk (net.minecraft.world.level.chunk.LevelChunk)9 CraftWorld (org.bukkit.craftbukkit.v1_18_R1.CraftWorld)9 ArrayList (java.util.ArrayList)8 BlockState (net.minecraft.world.level.block.state.BlockState)8 List (java.util.List)7 Optional (java.util.Optional)7 UUID (java.util.UUID)7 Vec3 (net.minecraft.world.phys.Vec3)7 CraftWorld (org.bukkit.craftbukkit.v1_17_R1.CraftWorld)7 Cause (org.spongepowered.api.event.Cause)7