From aada9731a63e604637e39386ef653715d97af6a1 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Tue, 19 Mar 2019 18:10:53 -0700 Subject: [PATCH] more resiliency to invalid cache files --- src/main/java/baritone/cache/CachedChunk.java | 2 +- src/main/java/baritone/cache/CachedRegion.java | 3 ++- src/main/java/baritone/cache/ChunkPacker.java | 8 +++++++- src/main/java/baritone/process/MineProcess.java | 2 +- src/main/java/baritone/utils/ExampleBaritoneControl.java | 7 +++---- 5 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/main/java/baritone/cache/CachedChunk.java b/src/main/java/baritone/cache/CachedChunk.java index 08c80141..bc5e2ac9 100644 --- a/src/main/java/baritone/cache/CachedChunk.java +++ b/src/main/java/baritone/cache/CachedChunk.java @@ -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(); } } diff --git a/src/main/java/baritone/cache/CachedRegion.java b/src/main/java/baritone/cache/CachedRegion.java index e3a85dfc..d395f168 100644 --- a/src/main/java/baritone/cache/CachedRegion.java +++ b/src/main/java/baritone/cache/CachedRegion.java @@ -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 locs = new ArrayList<>(); location[x][z].put(blockName, locs); int numLocations = in.readShort() & 0xffff; diff --git a/src/main/java/baritone/cache/ChunkPacker.java b/src/main/java/baritone/cache/ChunkPacker.java index 66a11fe7..6834549b 100644 --- a/src/main/java/baritone/cache/ChunkPacker.java +++ b/src/main/java/baritone/cache/ChunkPacker.java @@ -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)); } diff --git a/src/main/java/baritone/process/MineProcess.java b/src/main/java/baritone/process/MineProcess.java index af06556b..f2ebcd55 100644 --- a/src/main/java/baritone/process/MineProcess.java +++ b/src/main/java/baritone/process/MineProcess.java @@ -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 diff --git a/src/main/java/baritone/utils/ExampleBaritoneControl.java b/src/main/java/baritone/utils/ExampleBaritoneControl.java index 04d1606b..6dbdf388 100644 --- a/src/main/java/baritone/utils/ExampleBaritoneControl.java +++ b/src/main/java/baritone/utils/ExampleBaritoneControl.java @@ -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);