Skip to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[1.20.1] why is chunk.getHeight returning -64

Featured Replies

@SubscribeEvent
public void onChunkLoad(ChunkEvent.Load event) {
    if (!(event.getLevel() instanceof Level level) || !event.isNewChunk()) return;

    if (level.isClientSide()) {
        ChunkAccess chunk = event.getChunk();
        ChunkPos pos = chunk.getPos();
        Vector2i coordinatesToWorld = new Vector2i(pos.x * 16, pos.z * 16);
        Vector2i chunk_origin = new Vector2i(
                Math.floorMod(coordinatesToWorld.x, 512),
                Math.floorMod(coordinatesToWorld.y, 512)
        );
        Vector2i region_coordinates = new Vector2i(
                pos.getRegionX(),
                pos.getRegionZ()
        );
        int[] colors_data = new int[256];

        for (int z = 0; z < 16; z++) {
            for (int x = 0; x < 16; x++)
            {
                int highestY = chunk.getHeight(
                        Heightmap.Types.MOTION_BLOCKING_NO_LEAVES,
                        x,
                        z
                );

                BlockPos blockPos = pos.getBlockAt(x, highestY-1, z);
                BlockState state = level.getBlockState(blockPos);
                int colorsValue = getColorsValue(state);

                System.out.println("block: " + state.getBlock() + ", height: " + highestY);

                colors_data[z * 16 + x] = colorsValue;
            }
        }

        MinecraftServer server = ServerLifecycleHooks.getCurrentServer();

        if (server != null) {
            ExampleMod.MOD_FOLDER.AttemptToModifyRegionArea(
                    chunk_origin.x,
                    chunk_origin.y,
                    16,
                    16,
                    colors_data,
                    new RegionRecord(
                            level.dimension().location().toString(),
                            server.getWorldData().getLevelName(),
                            region_coordinates
                    )
            );
        }
    }
}

Edited by Nofraag

I am not sure but

if (level.isClientSide()) { ... }

here always return false (singleplayer is some kind of local server) - so this is used for client-thread stuff like changing GUIs or working with effects

So this code block is completely ignored - to make sure, test it with LOGGER.info() lines or System.out.println()

So if I am correct, just unwrap it from the if (level.isClientSide()) { ... } check

Try my Modpack

spacer.png

>>Link to Curseforge<<

  • Author
44 minutes ago, TileEntity said:

I am not sure but

if (level.isClientSide()) { ... }

here always return false (singleplayer is some kind of local server) - so this is used for client-thread stuff like changing GUIs or working with effects

So this code block is completely ignored - to make sure, test it with LOGGER.info() lines or System.out.println()

So if I am correct, just unwrap it from the if (level.isClientSide()) { ... } check

no, no it does get reached the problem it's that chunk.getHeight for some reason returns -64 instead of the actual highest block on that position (the line System.out.println("block: " + state.getBlock() + ", height: " + highestY);) keeps printing block: minecraft:void_air, height: -64

If I had to guess, the client side chunk is not populated at the load time. As the server has to sync the data to the client.

Why not just do all of this server side?

I do Forge for free, however the servers to run it arn't free, so anything is appreciated.
Consider supporting the team on Patreon

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.