Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Populating a new chunk from scratch results in an ArrayIndexOutOfBoundsException #53

Open
toddharrison opened this issue Jan 3, 2021 · 4 comments

Comments

@toddharrison
Copy link

I am using this library to create an entirely new chunk copied from an existing chunk from the bottom up that I've loaded from elsewhere. I plan on making changes to the chunk as it copies in the future, but for now it's just a straight copy where I have to manually create the blockState for each x, y, and z. As it builds up the copied chunk, it seems to work fine until it hits a certain number of items in the palette. I get this exception:

java.lang.ArrayIndexOutOfBoundsException: Index 320 out of bounds for length 320
[20:15:52 WARN]:        at com.briarcraft.shadow.querz.mca.Section.setPaletteIndex(Section.java:199)
[20:15:52 WARN]:        at com.briarcraft.shadow.querz.mca.Section.adjustBlockStateBits(Section.java:294)
[20:15:52 WARN]:        at com.briarcraft.shadow.querz.mca.Section.setBlockStateAt(Section.java:138)
[20:15:52 WARN]:        at com.briarcraft.shadow.querz.mca.Chunk.setBlockStateAt(Chunk.java:305)

I believe this is happening when it has to resize the blockdata palette index byte size because we've added one too many palette items. In Section on line 137 I see some logic that looks like it's meant to address the issue.

I'm not sure if there is an issue with me manually creating the blockState or something else causing this issue. Thanks for your assistance!

@Querz
Copy link
Owner

Querz commented Jan 3, 2021

setPaletteIndex does not change the size of the blockStates array (as stated in the javadoc). You will have to know its size beforehand or adjust its size manually.

@toddharrison
Copy link
Author

toddharrison commented Jan 3, 2021

So if set it to the max number of blocks, will it reduce when I run the cleanup or will I need to do that as well? I suppose I could also do preprocessing, but I was doing it in a streaming manner instead for performance.

@Querz
Copy link
Owner

Querz commented Jan 3, 2021

You could set the initial length of the blockStates to its maximum length (4096) and at the end after creating a new Section object call cleanupPaletteAndBlockStates, but then you could also just set the palette indexes manually without using setPaletteIndex, something like this:

CompoundTag sectionData = new CompoundTag();
long[] blockStates = new long[4096];
ListTag<CompoundTag> palette = new ListTag<>(CompoundTag.class);

for (int index = 0; index < 4096; index++) {
  // calculate paletteIndex here
  blockStates[index] = paletteIndex; // setting palette index manually
}

sectionData.put("Palette", palette);
sectionData.put("BlockStates", blockStates);

Section section = new Section(sectionData, 2584);
section.cleanupPaletteAndBlockStates();

@toddharrison
Copy link
Author

Thanks! Giving it a shot. Want me to close this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants