@author Brady @since 8/4/2018
| 44 | * @since 8/4/2018 |
| 45 | */ |
| 46 | public final class CachedWorld implements ICachedWorld, Helper { |
| 47 | |
| 48 | /** |
| 49 | * The maximum number of regions in any direction from (0,0) |
| 50 | */ |
| 51 | private static final int REGION_MAX = 30_000_000 / 512 + 1; |
| 52 | |
| 53 | /** |
| 54 | * A map of all of the cached regions. |
| 55 | */ |
| 56 | private Long2ObjectMap<CachedRegion> cachedRegions = new Long2ObjectOpenHashMap<>(); |
| 57 | |
| 58 | /** |
| 59 | * The directory that the cached region files are saved to |
| 60 | */ |
| 61 | private final String directory; |
| 62 | |
| 63 | /** |
| 64 | * Queue of positions to pack. Refers to the toPackMap, in that every element of this queue will be a |
| 65 | * key in that map. |
| 66 | */ |
| 67 | private final LinkedBlockingQueue<ChunkPos> toPackQueue = new LinkedBlockingQueue<>(); |
| 68 | |
| 69 | /** |
| 70 | * All chunk positions pending packing. This map will be updated in-place if a new update to the chunk occurs |
| 71 | * while waiting in the queue for the packer thread to get to it. |
| 72 | */ |
| 73 | private final Map<ChunkPos, LevelChunk> toPackMap = CacheBuilder.newBuilder().softValues().<ChunkPos, LevelChunk>build().asMap(); |
| 74 | |
| 75 | private final DimensionType dimension; |
| 76 | |
| 77 | CachedWorld(Path directory, DimensionType dimension) { |
| 78 | if (!Files.exists(directory)) { |
| 79 | try { |
| 80 | Files.createDirectories(directory); |
| 81 | } catch (IOException ignored) { |
| 82 | } |
| 83 | } |
| 84 | this.directory = directory.toString(); |
| 85 | this.dimension = dimension; |
| 86 | System.out.println("Cached world directory: " + directory); |
| 87 | Baritone.getExecutor().execute(new PackerThread()); |
| 88 | Baritone.getExecutor().execute(() -> { |
| 89 | try { |
| 90 | Thread.sleep(30000); |
| 91 | while (true) { |
| 92 | // since a region only saves if it's been modified since its last save |
| 93 | // saving every 10 minutes means that once it's time to exit |
| 94 | // we'll only have a couple regions to save |
| 95 | save(); |
| 96 | Thread.sleep(600000); |
| 97 | } |
| 98 | } catch (InterruptedException e) { |
| 99 | e.printStackTrace(); |
| 100 | } |
| 101 | }); |
| 102 | } |
| 103 |