more resiliency to invalid cache files

This commit is contained in:
Leijurv 2019-03-19 18:10:53 -07:00
parent 49658078b9
commit aada9731a6
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A
5 changed files with 14 additions and 8 deletions

View File

@ -177,7 +177,7 @@ public final class CachedChunk {
if (special != null) {
String str = special.get(index);
if (str != null) {
return ChunkPacker.stringToBlock(str).getDefaultState();
return ChunkPacker.stringToBlockRequired(str).getDefaultState();
}
}

View File

@ -240,7 +240,7 @@ public final class CachedRegion implements ICachedRegion {
for (int z = 0; z < 32; z++) {
if (present[x][z]) {
for (int i = 0; i < 256; i++) {
overview[x][z][i] = ChunkPacker.stringToBlock(in.readUTF()).getDefaultState();
overview[x][z][i] = ChunkPacker.stringToBlockRequired(in.readUTF()).getDefaultState();
}
}
}
@ -255,6 +255,7 @@ public final class CachedRegion implements ICachedRegion {
int numSpecialBlockTypes = in.readShort() & 0xffff;
for (int i = 0; i < numSpecialBlockTypes; i++) {
String blockName = in.readUTF();
ChunkPacker.stringToBlockRequired(blockName);
List<BlockPos> locs = new ArrayList<>();
location[x][z].put(blockName, locs);
int numLocations = in.readShort() & 0xffff;

View File

@ -115,7 +115,13 @@ public final class ChunkPacker {
return name;
}
public static Block stringToBlock(String name) {
public static Block stringToBlockRequired(String name) {
Block block = stringToBlockNullable(name);
Objects.requireNonNull(block);
return block;
}
public static Block stringToBlockNullable(String name) {
return resourceCache.computeIfAbsent(name, n -> Block.getBlockFromName(n.contains(":") ? n : "minecraft:" + n));
}

View File

@ -302,7 +302,7 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
@Override
public void mineByName(int quantity, String... blocks) {
mine(quantity, blocks == null || blocks.length == 0 ? null : Arrays.stream(blocks).map(ChunkPacker::stringToBlock).toArray(Block[]::new));
mine(quantity, blocks == null || blocks.length == 0 ? null : Arrays.stream(blocks).map(ChunkPacker::stringToBlockRequired).toArray(Block[]::new));
}
@Override

View File

@ -462,14 +462,13 @@ public class ExampleBaritoneControl extends Behavior implements Helper {
String[] blockTypes = msg.substring(4).trim().split(" ");
try {
int quantity = Integer.parseInt(blockTypes[1]);
Block block = ChunkPacker.stringToBlock(blockTypes[0]);
Objects.requireNonNull(block);
Block block = ChunkPacker.stringToBlockRequired(blockTypes[0]);
baritone.getMineProcess().mine(quantity, block);
logDirect("Will mine " + quantity + " " + blockTypes[0]);
return true;
} catch (NumberFormatException | ArrayIndexOutOfBoundsException | NullPointerException ex) {}
for (String s : blockTypes) {
if (ChunkPacker.stringToBlock(s) == null) {
if (ChunkPacker.stringToBlockNullable(s) == null) {
logDirect(s + " isn't a valid block name");
return true;
}
@ -552,7 +551,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper {
IWaypoint waypoint;
if (tag == null) {
String mining = waypointType;
Block block = ChunkPacker.stringToBlock(mining);
Block block = ChunkPacker.stringToBlockNullable(mining);
//logDirect("Not a valid tag. Tags are: " + Arrays.asList(Waypoint.Tag.values()).toString().toLowerCase());
if (block == null) {
waypoint = baritone.getWorldProvider().getCurrentWorld().getWaypoints().getAllWaypoints().stream().filter(w -> w.getName().equalsIgnoreCase(mining)).max(Comparator.comparingLong(IWaypoint::getCreationTimestamp)).orElse(null);