Search in sources :

Example 11 with LevelChunk

use of net.minecraft.world.level.chunk.LevelChunk in project SpongeCommon by SpongePowered.

the class EntityActivationRange method activateEntities.

/**
 * Find what entities are in range of the players in the world and set
 * active if in range.
 *
 * @param world The world to perform activation checks in
 */
public static void activateEntities(final ServerLevel world) {
    if (((LevelBridge) world).bridge$isFake()) {
        return;
    }
    for (final ServerPlayer player : world.players()) {
        int maxRange = 0;
        for (final Integer range : EntityActivationRange.maxActivationRanges.values()) {
            if (range > maxRange) {
                maxRange = range;
            }
        }
        maxRange = Math.min((((ServerWorld) world).properties().viewDistance() << 4) - 8, maxRange);
        ((ActivationCapabilityBridge) player).activation$setActivatedTick(SpongeCommon.server().getTickCount());
        final AABB aabb = EntityActivationRange.maxBB;
        EntityActivationRange.growBb(aabb, player.getBoundingBox(), maxRange, 256, maxRange);
        final int i = Mth.floor(aabb.minX / 16.0D);
        final int j = Mth.floor(aabb.maxX / 16.0D);
        final int k = Mth.floor(aabb.minZ / 16.0D);
        final int l = Mth.floor(aabb.maxZ / 16.0D);
        for (int i1 = i; i1 <= j; ++i1) {
            for (int j1 = k; j1 <= l; ++j1) {
                final LevelChunk chunk = world.getChunkSource().getChunkNow(i1, j1);
                if (chunk != null) {
                    EntityActivationRange.activateChunkEntities(player, chunk);
                }
            }
        }
    }
}
Also used : ServerWorld(org.spongepowered.api.world.server.ServerWorld) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) LevelBridge(org.spongepowered.common.bridge.world.level.LevelBridge) ActivationCapabilityBridge(org.spongepowered.common.bridge.activation.ActivationCapabilityBridge) ServerPlayer(net.minecraft.server.level.ServerPlayer) AABB(net.minecraft.world.phys.AABB)

Example 12 with LevelChunk

use of net.minecraft.world.level.chunk.LevelChunk in project SpongeCommon by SpongePowered.

the class UpdateOrCreateNewTileEntityPostPlacementEffect 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 LevelChunk chunk = pipeline.getAffectedChunk();
    @Nullable final BlockEntity maybeNewTileEntity = chunk.getBlockEntity(oldState.pos, LevelChunk.EntityCreationType.CHECK);
    if (((BlockStateBridge) newState).bridge$hasTileEntity()) {
        if (maybeNewTileEntity == null) {
            // tileentity1 = ((ITileEntityProvider)block).createNewTileEntity(this.world); // Vanilla
            // tileentity1 = state.createTileEntity(this.world); // Forge
            // We cast to our bridge for easy access
            serverWorld.setBlockEntity(oldState.pos, ((BlockStateBridge) newState).bridge$createNewTileEntity(serverWorld));
        } else {
            maybeNewTileEntity.clearCache();
        }
    }
    return EffectResult.NULL_PASS;
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) Nullable(org.checkerframework.checker.nullness.qual.Nullable) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) BlockStateBridge(org.spongepowered.common.bridge.world.level.block.state.BlockStateBridge)

Example 13 with LevelChunk

use of net.minecraft.world.level.chunk.LevelChunk in project SpongeCommon by SpongePowered.

the class PacketState method associateNeighborStateNotifier.

@Override
public void associateNeighborStateNotifier(final P unwindingContext, final BlockPos sourcePos, final Block block, final BlockPos notifyPos, final ServerLevel minecraftWorld, final PlayerTracker.Type notifier) {
    final Player player = unwindingContext.getSpongePlayer();
    final LevelChunk chunk = minecraftWorld.getChunkAt(notifyPos);
    ((LevelChunkBridge) chunk).bridge$setBlockNotifier(notifyPos, player.uniqueId());
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge)

Example 14 with LevelChunk

use of net.minecraft.world.level.chunk.LevelChunk in project SpongeCommon by SpongePowered.

the class ChunkMapMixin method impl$onSetUnloaded.

@Redirect(method = "lambda$scheduleUnload$10", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/level/ServerLevel;unload(Lnet/minecraft/world/level/chunk/LevelChunk;)V"), slice = @Slice(from = @At(value = "INVOKE", target = "Lnet/minecraft/server/level/ChunkMap;save(Lnet/minecraft/world/level/chunk/ChunkAccess;)Z")))
private void impl$onSetUnloaded(final ServerLevel level, final LevelChunk chunk) {
    final Vector3i chunkPos = new Vector3i(chunk.getPos().x, 0, chunk.getPos().z);
    if (ShouldFire.CHUNK_EVENT_UNLOAD_PRE) {
        final ChunkEvent.Unload event = SpongeEventFactory.createChunkEventUnloadPre(PhaseTracker.getInstance().currentCause(), (WorldChunk) chunk, chunkPos, (ResourceKey) (Object) this.level.dimension().location());
        SpongeCommon.post(event);
    }
    level.unload(chunk);
    for (final Direction dir : Constants.Chunk.CARDINAL_DIRECTIONS) {
        final Vector3i neighborPos = chunkPos.add(dir.asBlockOffset());
        final ChunkAccess neighbor = this.level.getChunk(neighborPos.x(), neighborPos.z(), ChunkStatus.EMPTY, false);
        if (neighbor instanceof LevelChunk) {
            final int index = DirectionUtil.directionToIndex(dir);
            final int oppositeIndex = DirectionUtil.directionToIndex(dir.opposite());
            ((LevelChunkBridge) chunk).bridge$setNeighborChunk(index, null);
            ((LevelChunkBridge) neighbor).bridge$setNeighborChunk(oppositeIndex, null);
        }
    }
    if (ShouldFire.CHUNK_EVENT_UNLOAD_POST) {
        final ChunkEvent.Unload event = SpongeEventFactory.createChunkEventUnloadPost(PhaseTracker.getInstance().currentCause(), chunkPos, (ResourceKey) (Object) this.level.dimension().location());
        SpongeCommon.post(event);
    }
}
Also used : ChunkAccess(net.minecraft.world.level.chunk.ChunkAccess) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) Vector3i(org.spongepowered.math.vector.Vector3i) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) ChunkEvent(org.spongepowered.api.event.world.chunk.ChunkEvent) Direction(org.spongepowered.api.util.Direction) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 15 with LevelChunk

use of net.minecraft.world.level.chunk.LevelChunk in project SpongeCommon by SpongePowered.

the class ChunkHolderMixin method impl$throwChunkGeneratedEvent.

@Inject(method = "replaceProtoChunk(Lnet/minecraft/world/level/chunk/ImposterProtoChunk;)V", at = @At("TAIL"))
private void impl$throwChunkGeneratedEvent(final ImposterProtoChunk imposter, final CallbackInfo ci) {
    if (!ShouldFire.CHUNK_EVENT_GENERATED) {
        return;
    }
    final LevelChunk chunk = imposter.getWrapped();
    final Vector3i chunkPos = VecHelper.toVector3i(chunk.getPos());
    final ChunkEvent.Generated event = SpongeEventFactory.createChunkEventGenerated(PhaseTracker.getInstance().currentCause(), chunkPos, (ResourceKey) (Object) chunk.getLevel().dimension().location());
    SpongeCommon.post(event);
}
Also used : LevelChunk(net.minecraft.world.level.chunk.LevelChunk) Vector3i(org.spongepowered.math.vector.Vector3i) ChunkEvent(org.spongepowered.api.event.world.chunk.ChunkEvent) Inject(org.spongepowered.asm.mixin.injection.Inject)

Aggregations

LevelChunk (net.minecraft.world.level.chunk.LevelChunk)36 BlockPos (net.minecraft.core.BlockPos)13 ChunkAccess (net.minecraft.world.level.chunk.ChunkAccess)10 ServerLevel (net.minecraft.server.level.ServerLevel)9 LevelChunkBridge (org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge)8 Vector3i (org.spongepowered.math.vector.Vector3i)7 BlockState (net.minecraft.world.level.block.state.BlockState)6 Nullable (org.checkerframework.checker.nullness.qual.Nullable)6 BlockEntity (org.spongepowered.api.block.entity.BlockEntity)5 BlockLight (com.denizenscript.denizen.nms.abstracts.BlockLight)4 ChunkCoordinate (com.denizenscript.denizen.utilities.blocks.ChunkCoordinate)4 UUID (java.util.UUID)4 Tuple (net.minecraft.util.Tuple)4 Level (net.minecraft.world.level.Level)4 MonotonicNonNull (org.checkerframework.checker.nullness.qual.MonotonicNonNull)4 CompoundTag (net.minecraft.nbt.CompoundTag)3 ChunkPos (net.minecraft.world.level.ChunkPos)3 PhaseTracker (org.spongepowered.common.event.tracking.PhaseTracker)3 SpongeBlockChangeFlag (org.spongepowered.common.world.SpongeBlockChangeFlag)3 BitSet (java.util.BitSet)2