Merge pull request #4157 from ZacSharp/pr/1.19.4/worldscanner/handleDuplicatePaletteEntries

Handle duplicate palette entries
This commit is contained in:
leijurv 2023-10-31 11:23:26 -07:00 committed by GitHub
commit e19fd1161d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 7 deletions

View File

@ -20,6 +20,7 @@ package baritone.cache;
import baritone.api.cache.ICachedWorld; import baritone.api.cache.ICachedWorld;
import baritone.api.cache.IWorldScanner; import baritone.api.cache.IWorldScanner;
import baritone.api.utils.BetterBlockPos; import baritone.api.utils.BetterBlockPos;
import baritone.api.utils.BlockOptionalMeta;
import baritone.api.utils.BlockOptionalMetaLookup; import baritone.api.utils.BlockOptionalMetaLookup;
import baritone.api.utils.IPlayerContext; import baritone.api.utils.IPlayerContext;
import baritone.utils.accessor.IPalettedContainer; import baritone.utils.accessor.IPalettedContainer;
@ -45,6 +46,9 @@ import java.util.stream.Stream;
public enum FasterWorldScanner implements IWorldScanner { public enum FasterWorldScanner implements IWorldScanner {
INSTANCE; INSTANCE;
private static final BlockState[] PALETTE_REGISTRY_SENTINEL = new BlockState[0];
@Override @Override
public List<BlockPos> scanChunkRadius(IPlayerContext ctx, BlockOptionalMetaLookup filter, int max, int yLevelThreshold, int maxSearchRadius) { public List<BlockPos> scanChunkRadius(IPlayerContext ctx, BlockOptionalMetaLookup filter, int max, int yLevelThreshold, int maxSearchRadius) {
assert ctx.world() != null; assert ctx.world() != null;
@ -219,13 +223,18 @@ public enum FasterWorldScanner implements IWorldScanner {
private boolean[] getIncludedFilterIndices(BlockOptionalMetaLookup lookup, Palette<BlockState> palette) { private boolean[] getIncludedFilterIndices(BlockOptionalMetaLookup lookup, Palette<BlockState> palette) {
boolean commonBlockFound = false; boolean commonBlockFound = false;
IdMapper<BlockState> paletteMap = getPalette(palette); BlockState[] paletteMap = getPalette(palette);
int size = paletteMap.size();
if (paletteMap == PALETTE_REGISTRY_SENTINEL) {
return getIncludedFilterIndicesFromRegistry(lookup);
}
int size = paletteMap.length;
boolean[] isInFilter = new boolean[size]; boolean[] isInFilter = new boolean[size];
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
BlockState state = paletteMap.byId(i); BlockState state = paletteMap[i];
if (lookup.has(state)) { if (lookup.has(state)) {
isInFilter[i] = true; isInFilter[i] = true;
commonBlockFound = true; commonBlockFound = true;
@ -240,21 +249,34 @@ public enum FasterWorldScanner implements IWorldScanner {
return isInFilter; 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 * 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) { 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 { } else {
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer()); FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
palette.write(buf); palette.write(buf);
int size = buf.readVarInt(); int size = buf.readVarInt();
IdMapper<BlockState> states = new IdMapper<>(); BlockState[] states = new BlockState[size];
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
BlockState state = Block.BLOCK_STATE_REGISTRY.byId(buf.readVarInt()); BlockState state = Block.BLOCK_STATE_REGISTRY.byId(buf.readVarInt());
assert state != null; assert state != null;
states.addMapping(state, i); states[i] = state;
} }
return states; return states;
} }