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

Handle duplicate palette entries #4157

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions src/main/java/baritone/cache/FasterWorldScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import baritone.api.cache.ICachedWorld;
import baritone.api.cache.IWorldScanner;
import baritone.api.utils.BetterBlockPos;
import baritone.api.utils.BlockOptionalMeta;
import baritone.api.utils.BlockOptionalMetaLookup;
import baritone.api.utils.IPlayerContext;
import baritone.utils.accessor.IPalettedContainer;
Expand All @@ -45,6 +46,9 @@

public enum FasterWorldScanner implements IWorldScanner {
INSTANCE;

private static final BlockState[] PALETTE_REGISTRY_SENTINEL = new BlockState[0];

@Override
public List<BlockPos> scanChunkRadius(IPlayerContext ctx, BlockOptionalMetaLookup filter, int max, int yLevelThreshold, int maxSearchRadius) {
assert ctx.world() != null;
Expand Down Expand Up @@ -219,13 +223,18 @@ private void visitSection(BlockOptionalMetaLookup lookup, LevelChunkSection sect

private boolean[] getIncludedFilterIndices(BlockOptionalMetaLookup lookup, Palette<BlockState> palette) {
boolean commonBlockFound = false;
IdMapper<BlockState> paletteMap = getPalette(palette);
int size = paletteMap.size();
BlockState[] paletteMap = getPalette(palette);

if (paletteMap == PALETTE_REGISTRY_SENTINEL) {
return getIncludedFilterIndicesFromRegistry(lookup);
}

int size = paletteMap.length;

boolean[] isInFilter = new boolean[size];

for (int i = 0; i < size; i++) {
BlockState state = paletteMap.byId(i);
BlockState state = paletteMap[i];
if (lookup.has(state)) {
isInFilter[i] = true;
commonBlockFound = true;
Expand All @@ -240,21 +249,34 @@ private boolean[] getIncludedFilterIndices(BlockOptionalMetaLookup lookup, Palet
return isInFilter;
}

private boolean[] getIncludedFilterIndicesFromRegistry(BlockOptionalMetaLookup lookup) {
boolean[] isInFilter = new boolean[Block.BLOCK_STATE_REGISTRY.size()];

for (BlockOptionalMeta bom : lookup.blocks()) {
for (BlockState state : bom.getAllBlockStates()) {
isInFilter[Block.BLOCK_STATE_REGISTRY.getId(state)] = true;
}
}

return isInFilter;
}

/**
* cheats to get the actual map of id -> blockstate from the various palette implementations
*/
private static IdMapper<BlockState> getPalette(Palette<BlockState> palette) {
private static BlockState[] getPalette(Palette<BlockState> palette) {
if (palette instanceof GlobalPalette) {
return Block.BLOCK_STATE_REGISTRY;
// copying the entire registry is not nice so we treat it as a special case
return PALETTE_REGISTRY_SENTINEL;
} else {
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
palette.write(buf);
int size = buf.readVarInt();
IdMapper<BlockState> states = new IdMapper<>();
BlockState[] states = new BlockState[size];
for (int i = 0; i < size; i++) {
BlockState state = Block.BLOCK_STATE_REGISTRY.byId(buf.readVarInt());
assert state != null;
states.addMapping(state, i);
states[i] = state;
}
return states;
}
Expand Down