Java Code Examples for net.minecraft.world.World#getChunk()

The following examples show how to use net.minecraft.world.World#getChunk() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MixinChunk.java    From Valkyrien-Skies with Apache License 2.0 6 votes vote down vote up
@Inject(method = "addEntity(Lnet/minecraft/entity/Entity;)V", at = @At("HEAD"), cancellable = true)
public void preAddEntity(Entity entityIn, CallbackInfo callbackInfo) {
    World world = this.world;

    int i = MathHelper.floor(entityIn.posX / 16.0D);
    int j = MathHelper.floor(entityIn.posZ / 16.0D);

    if (i == this.x && j == this.z) {
        // do nothing, and let vanilla code take over after our injected code is done
        // (now)
    } else {
        Chunk realChunkFor = world.getChunk(i, j);
        if (!realChunkFor.isEmpty() && realChunkFor.loaded) {
            realChunkFor.addEntity(entityIn);
            callbackInfo.cancel(); // don't run the code on this chunk!!!
        }
    }
}
 
Example 2
Source File: ChunkCacheSchematic.java    From litematica with GNU Lesser General Public License v3.0 5 votes vote down vote up
public ChunkCacheSchematic(World worldIn, BlockPos pos, int expand)
{
    this.world = worldIn;
    this.chunkStartX = (pos.getX() - expand) >> 4;
    this.chunkStartZ = (pos.getZ() - expand) >> 4;
    int chunkEndX = (pos.getX() + expand + 15) >> 4;
    int chunkEndZ = (pos.getZ() + expand + 15) >> 4;
    this.chunkArray = new Chunk[chunkEndX - this.chunkStartX + 1][chunkEndZ - this.chunkStartZ + 1];
    this.empty = true;

    for (int cx = this.chunkStartX; cx <= chunkEndX; ++cx)
    {
        for (int cz = this.chunkStartZ; cz <= chunkEndZ; ++cz)
        {
            this.chunkArray[cx - this.chunkStartX][cz - this.chunkStartZ] = worldIn.getChunk(cx, cz);
        }
    }

    for (int cx = pos.getX() >> 4; cx <= (pos.getX() + 15) >> 4; ++cx)
    {
        for (int cz = pos.getZ() >> 4; cz <= (pos.getZ() + 15) >> 4; ++cz)
        {
            Chunk chunk = this.chunkArray[cx - this.chunkStartX][cz - this.chunkStartZ];

            if (chunk != null && chunk.isEmptyBetween(pos.getY(), pos.getY() + 15) == false)
            {
                this.empty = false;
                break;
            }
        }
    }
}
 
Example 3
Source File: VS_APIPhysicsEntityManager.java    From Valkyrien-Skies with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public IPhysicsEntity getPhysicsEntityFromShipSpace(World world, BlockPos pos) {
    Chunk chunk = world.getChunk(pos);
    IPhysicsChunk physicsChunk = (IPhysicsChunk) chunk;
    if (physicsChunk.getPhysicsObjectOptional()
        .isPresent()) {
        return physicsChunk.getPhysicsObjectOptional()
            .get();
    } else {
        return null;
    }
}
 
Example 4
Source File: MoveBlocks.java    From Valkyrien-Skies with Apache License 2.0 5 votes vote down vote up
/**
 * @param world
 * @param oldPos
 * @param newPos
 * @param physicsObjectOptional Used when we're using this to copy from world to physics object;
 *                              should be empty when other way around.
 */
public static void copyBlockToPos(World world, BlockPos oldPos, BlockPos newPos,
    Optional<PhysicsObject> physicsObjectOptional) {
    // To avoid any updates crap, just edit the chunk data array directly.
    // These look switched, but trust me they aren't
    IBlockState oldState = world.getBlockState(newPos);
    IBlockState newState = world.getBlockState(oldPos);
    // A hacky way to set the block state within the chunk while avoiding any block updates.
    Chunk chunkToSet = world.getChunk(newPos);
    int storageIndex = newPos.getY() >> 4;
    // Check that we're placing the block in a valid position
    if (storageIndex < 0 || storageIndex >= chunkToSet.storageArrays.length) {
        // Invalid position, abort!
        return;
    }
    if (chunkToSet.storageArrays[storageIndex] == Chunk.NULL_BLOCK_STORAGE) {
        chunkToSet.storageArrays[storageIndex] = new ExtendedBlockStorage(storageIndex << 4,
            true);
    }
    chunkToSet.storageArrays[storageIndex]
        .set(newPos.getX() & 15, newPos.getY() & 15, newPos.getZ() & 15, newState);
    // Only want to send the update to clients and nothing else, so we use flag 2.
    world.notifyBlockUpdate(newPos, oldState, newState, 2);
    // Pretty messy to put this here but it works. Basically the ship keeps track of which of its chunks are
    // actually being used for performance reasons.
    if (physicsObjectOptional.isPresent()) {
        int minChunkX = physicsObjectOptional.get()
            .getOwnedChunks()
            .minX();
        int minChunkZ = physicsObjectOptional.get()
            .getOwnedChunks()
            .minZ();
    }
    // Now that we've copied the block to the position, copy the tile entity
    copyTileEntityToPos(world, oldPos, newPos, physicsObjectOptional);
}
 
Example 5
Source File: VSWorldEventListener.java    From Valkyrien-Skies with Apache License 2.0 5 votes vote down vote up
@Override
public void notifyBlockUpdate(World worldIn, BlockPos pos, IBlockState oldState,
    IBlockState newState, int flags) {
    Chunk chunk = worldIn.getChunk(pos);
    IPhysicsChunk physicsChunk = (IPhysicsChunk) chunk;
    if (physicsChunk.getPhysicsObjectOptional()
        .isPresent()) {
        physicsChunk.getPhysicsObjectOptional()
            .get()
            .onSetBlockState(oldState, newState, pos);
    }
}
 
Example 6
Source File: ValkyrienUtils.java    From Valkyrien-Skies with Apache License 2.0 5 votes vote down vote up
public static Optional<PhysicsObject> getPhysicsObject(@Nullable World world,
    @Nullable BlockPos pos, boolean includePartiallyLoaded) {
    // No physics object manages a null world or a null pos.
    if (world != null && pos != null && world.isBlockLoaded(pos)) {
        IPhysicsChunk physicsChunk = (IPhysicsChunk) world.getChunk(pos);
        Optional<PhysicsObject> physicsObject = physicsChunk.getPhysicsObjectOptional();
        if (physicsObject.isPresent()) {
            if (includePartiallyLoaded || physicsObject.get()
                .isFullyLoaded()) {
                return physicsObject;
            }
        }
    }
    return Optional.empty();
}
 
Example 7
Source File: PositionUtils.java    From enderutilities with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Gets a list of all the TileEntities within the given range of the center position.
 * The list is sorted by distance to the center position (closest one first)
 * @param world
 * @param centerPos
 * @param rangeH
 * @param rangeVertPos the range upwards from the center position
 * @param rangeVertNeg the range downwards from the center position
 * @param filter a predicate to check if the found TileEntity is suitable
 * @return
 */
public static List<BlockPosDistance> getTileEntityPositions(World world, BlockPos centerPos, int rangeH,
        int rangeVertPos, int rangeVertNeg, Predicate <? super TileEntity> filter)
{
    List<BlockPosDistance> posDist = new ArrayList<BlockPosDistance>();

    for (int cx = (centerPos.getX() - rangeH) >> 4; cx <= ((centerPos.getX() + rangeH) >> 4); cx++)
    {
        for (int cz = (centerPos.getZ() - rangeH) >> 4; cz <= ((centerPos.getZ() + rangeH) >> 4); cz++)
        {
            if (world.isBlockLoaded(new BlockPos(cx << 4, centerPos.getY(), cz << 4), world.isRemote) == false)
            {
                continue;
            }

            Chunk chunk = world.getChunk(cx, cz);
            if (chunk != null)
            {
                Map<BlockPos, TileEntity> map = chunk.getTileEntityMap();

                for (BlockPos pos : map.keySet())
                {
                    if (PositionUtils.isWithinRange(centerPos, pos, rangeH, rangeVertPos, rangeVertNeg) &&
                        (filter == null || filter.apply(map.get(pos))))
                    {
                        posDist.add(new BlockPosDistance(pos, centerPos));
                    }
                }
            }
        }
    }

    Collections.sort(posDist);

    return posDist;
}
 
Example 8
Source File: RenderEnqueue.java    From XRay-Mod with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Use Controller.requestBlockFinder() to trigger a scan.
 */
private void blockFinder() {
       HashMap<UUID, BlockData> blocks = Controller.getBlockStore().getStore();
       if ( blocks.isEmpty() ) {
	    if( !Render.syncRenderList.isEmpty() )
	        Render.syncRenderList.clear();
           return; // no need to scan the region if there's nothing to find
       }

	final World world = XRay.mc.world;
       final PlayerEntity player = XRay.mc.player;
       if( world == null || player == null )
       	return;

	final List<RenderBlockProps> renderQueue = new ArrayList<>();
	int lowBoundX, highBoundX, lowBoundY, highBoundY, lowBoundZ, highBoundZ;

	// Used for cleaning up the searching process
	BlockState currentState;
	FluidState currentFluid;

	ResourceLocation block;
	Pair<BlockData, UUID> dataWithUUID;

	// Loop on chunks (x, z)
	for ( int chunkX = box.minChunkX; chunkX <= box.maxChunkX; chunkX++ )
	{
		// Pre-compute the extend bounds on X
		int x = chunkX << 4; // lowest x coord of the chunk in block/world coordinates
		lowBoundX = (x < box.minX) ? box.minX - x : 0; // lower bound for x within the extend
		highBoundX = (x + 15 > box.maxX) ? box.maxX - x : 15;// and higher bound. Basically, we clamp it to fit the radius.

		for ( int chunkZ = box.minChunkZ; chunkZ <= box.maxChunkZ; chunkZ++ )
		{
			// Time to getStore the chunk (16x256x16) and split it into 16 vertical extends (16x16x16)
			if (!world.chunkExists(chunkX, chunkZ)) {
				continue; // We won't find anything interesting in unloaded chunks
			}

			Chunk chunk = world.getChunk( chunkX, chunkZ );
			ChunkSection[] extendsList = chunk.getSections();

			// Pre-compute the extend bounds on Z
			int z = chunkZ << 4;
			lowBoundZ = (z < box.minZ) ? box.minZ - z : 0;
			highBoundZ = (z + 15 > box.maxZ) ? box.maxZ - z : 15;

			// Loop on the extends around the player's layer (6 down, 2 up)
			for ( int curExtend = box.minChunkY; curExtend <= box.maxChunkY; curExtend++ )
			{
				ChunkSection ebs = extendsList[curExtend];
				if (ebs == null) // happens quite often!
					continue;

				// Pre-compute the extend bounds on Y
				int y = curExtend << 4;
				lowBoundY = (y < box.minY) ? box.minY - y : 0;
				highBoundY = (y + 15 > box.maxY) ? box.maxY - y : 15;

				// Now that we have an extend, let's check all its blocks
				for ( int i = lowBoundX; i <= highBoundX; i++ ) {
					for ( int j = lowBoundY; j <= highBoundY; j++ ) {
						for ( int k = lowBoundZ; k <= highBoundZ; k++ ) {
							currentState = ebs.getBlockState(i, j, k);
							currentFluid = currentState.getFluidState();

							if( (currentFluid.getFluid() == Fluids.LAVA || currentFluid.getFluid() == Fluids.FLOWING_LAVA) && Controller.isLavaActive() ) {
								renderQueue.add(new RenderBlockProps(x + i, y + j, z + k, 0xff0000));
								continue;
							}

							// Reject blacklisted blocks
							if( Controller.blackList.contains(currentState.getBlock()) )
								continue;

							block = currentState.getBlock().getRegistryName();
							if( block == null )
								continue;

							dataWithUUID = Controller.getBlockStore().getStoreByReference(block.toString());
							if( dataWithUUID == null )
								continue;

							if( dataWithUUID.getKey() == null || !dataWithUUID.getKey().isDrawing() ) // fail safe
								continue;

							// Push the block to the render queue
							renderQueue.add(new RenderBlockProps(x + i, y + j, z + k, dataWithUUID.getKey().getColor()));
						}
					}
				}
			}
		}
	}
	final BlockPos playerPos = player.func_233580_cy_(); // @mcp: func_233580_cy_ = getPosition (blockPos)
	renderQueue.sort((t, t1) -> Double.compare(t1.distanceSq(playerPos), t.distanceSq(playerPos)));
	Render.syncRenderList.clear();
	Render.syncRenderList.addAll( renderQueue ); // Add all our found blocks to the Render.syncRenderList list. To be use by Render when drawing.
}