GeyserMC / MCProtocolLib

A library for communication with a Minecraft client/server.
MIT License
728 stars 202 forks source link

IndexOutOfBounds on setBlock in a newly created chunk #156

Closed ghost closed 8 years ago

ghost commented 8 years ago

I create an array of chunks, then in a loop, I initialize them and then proceed to set the blocks inside of the chunks. However, when I try to .getBlocks().set(x, y, z, new BlockState(2, 0)); I get:

Exception in thread "Thread-2" java.lang.IndexOutOfBoundsException
    at org.spacehq.mc.protocol.data.game.chunk.FlexibleStorage.set(FlexibleStorage.java:58)
    at org.spacehq.mc.protocol.data.game.chunk.BlockStorage.set(BlockStorage.java:96)
    at org.netherrack.Login.loggedIn(Login.java:48)
    at org.spacehq.mc.protocol.ServerListener$UserAuthTask.run(ServerListener.java:185)
    at java.lang.Thread.run(Thread.java:745)

Here is my code:

Chunk[] chunks = new Chunk[16];

for (int amount = 0; amount < 16; amount++) {
    chunks[amount] = new Chunk(true);
    chunks[amount].getBlocks().set(1, 50, 1, new BlockState(2, 0));
}

Column column = new Column((int)Math.floor(1/16), (int)Math.floor(1/16), chunks);
session.send(new ServerChunkDataPacket(column));

Notice, that the y is set to 50. When it is set to 1, there is no stack trace. Is there a limit on the height that I'm not entering properly?

What am I doing wrong here? Also, does the library automatically round down and divide by 16 for the x and y of the chunk packet? It doesn't look like it.

It's also worth noting that when I connect to my test server using an external library to decode the packets the primary bitMap is set to 65535 not 100% sure if that's actually correct.

ghost commented 8 years ago

My mistake. It appears that the max height per column is 15. Although after changing this, the columns still aren't sending so I will leave this issue open for that.

Updated code:

Chunk[] chunks = new Chunk[16];

for (int amount = 0; amount < 16; amount++) {
    chunks[amount] = new Chunk(true);
    chunks[amount].getBlocks().set(1, 1, 1, new BlockState(2, 0));
}

//Column column = new Column((int)Math.floor(1/16), (int)Math.floor(1/16), chunks);
Column column = new Column(1, 1, chunks);
session.send(new ServerChunkDataPacket(column));
ghost commented 8 years ago

From further testing it seems like chunks are completely broken. The packet gets sent containing the chunks, but the chunks themselves don't load inside the users game. i.e.: I send the packet containing a fresh chunk, then the notchian client still says waiting for chunks. Any ideas?

ghost commented 8 years ago

Perfect, thanks :smile: