From 2a8575caa88f97d3c468cde622a0d59f10f6905c Mon Sep 17 00:00:00 2001 From: Brady Date: Mon, 24 Sep 2018 18:45:41 -0500 Subject: [PATCH] Begin to create interfaces in api for cached World and Region --- .../baritone/api/cache}/IBlockTypeAccess.java | 5 +- .../baritone/api/cache/ICachedRegion.java | 49 +++++++++++ .../java/baritone/api/cache/ICachedWorld.java | 82 +++++++++++++++++++ .../java/baritone/api/cache/IWorldData.java | 7 ++ .../java/baritone/behavior/MineBehavior.java | 2 +- src/main/java/baritone/cache/CachedChunk.java | 5 +- .../java/baritone/cache/CachedRegion.java | 8 +- src/main/java/baritone/cache/CachedWorld.java | 20 ++--- src/main/java/baritone/cache/WorldData.java | 8 +- .../java/baritone/event/GameEventHandler.java | 2 +- .../baritone/utils/BlockStateInterface.java | 11 +-- .../utils/ExampleBaritoneControl.java | 8 +- 12 files changed, 178 insertions(+), 29 deletions(-) rename src/{main/java/baritone/utils/pathing => api/java/baritone/api/cache}/IBlockTypeAccess.java (89%) create mode 100644 src/api/java/baritone/api/cache/ICachedRegion.java create mode 100644 src/api/java/baritone/api/cache/ICachedWorld.java diff --git a/src/main/java/baritone/utils/pathing/IBlockTypeAccess.java b/src/api/java/baritone/api/cache/IBlockTypeAccess.java similarity index 89% rename from src/main/java/baritone/utils/pathing/IBlockTypeAccess.java rename to src/api/java/baritone/api/cache/IBlockTypeAccess.java index 4e34596a..242ff154 100644 --- a/src/main/java/baritone/utils/pathing/IBlockTypeAccess.java +++ b/src/api/java/baritone/api/cache/IBlockTypeAccess.java @@ -15,9 +15,8 @@ * along with Baritone. If not, see . */ -package baritone.utils.pathing; +package baritone.api.cache; -import baritone.utils.Helper; import net.minecraft.block.state.IBlockState; import net.minecraft.util.math.BlockPos; @@ -25,7 +24,7 @@ import net.minecraft.util.math.BlockPos; * @author Brady * @since 8/4/2018 2:01 AM */ -public interface IBlockTypeAccess extends Helper { +public interface IBlockTypeAccess { IBlockState getBlock(int x, int y, int z); diff --git a/src/api/java/baritone/api/cache/ICachedRegion.java b/src/api/java/baritone/api/cache/ICachedRegion.java new file mode 100644 index 00000000..5f9199fa --- /dev/null +++ b/src/api/java/baritone/api/cache/ICachedRegion.java @@ -0,0 +1,49 @@ +/* + * This file is part of Baritone. + * + * Baritone is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Baritone is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Baritone. If not, see . + */ + +package baritone.api.cache; + +/** + * @author Brady + * @since 9/24/2018 + */ +public interface ICachedRegion extends IBlockTypeAccess { + + /** + * Returns whether or not the block at the specified X and Z coordinates + * is cached in this world. Similar to {@link ICachedWorld#isCached(int, int)}, + * however, the block coordinates should in on a scale from 0 to 511 (inclusive) + * because region sizes are 512x512 blocks. + * + * @see ICachedWorld#isCached(int, int) + * + * @param blockX The block X coordinate + * @param blockZ The block Z coordinate + * @return Whether or not the specified XZ location is cached + */ + boolean isCached(int blockX, int blockZ); + + /** + * The X coordinate of this region + */ + int getX(); + + /** + * The Z coordinate of this region + */ + int getZ(); +} diff --git a/src/api/java/baritone/api/cache/ICachedWorld.java b/src/api/java/baritone/api/cache/ICachedWorld.java new file mode 100644 index 00000000..f8196ebb --- /dev/null +++ b/src/api/java/baritone/api/cache/ICachedWorld.java @@ -0,0 +1,82 @@ +/* + * This file is part of Baritone. + * + * Baritone is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Baritone is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Baritone. If not, see . + */ + +package baritone.api.cache; + +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.chunk.Chunk; + +import java.util.LinkedList; + +/** + * @author Brady + * @since 9/24/2018 + */ +public interface ICachedWorld { + + /** + * Returns the region at the specified region coordinates + * + * @param regionX The region X coordinate + * @param regionZ The region Z coordinate + * @return The region located at the specified coordinates + */ + ICachedRegion getRegion(int regionX, int regionZ); + + /** + * Queues the specified chunk for packing. This entails reading the contents + * of the chunk, then packing the data into the 2-bit format, and storing that + * in this cached world. + * + * @param chunk The chunk to pack and store + */ + void queueForPacking(Chunk chunk); + + /** + * Returns whether or not the block at the specified X and Z coordinates + * is cached in this world. + * + * @param blockX The block X coordinate + * @param blockZ The block Z coordinate + * @return Whether or not the specified XZ location is cached + */ + boolean isCached(int blockX, int blockZ); + + /** + * Scans the cached chunks for location of the specified special block. The + * information that is returned by this method may not be up to date, because + * older cached chunks can contain data that is much more likely to have changed. + * + * @param block The special block to search for + * @param maximum The maximum number of position results to receive + * @param maxRegionDistanceSq The maximum region distance, squared + * @return The locations found that match the special block + */ + LinkedList getLocationsOf(String block, int maximum, int maxRegionDistanceSq); + + /** + * Reloads all of the cached regions in this world from disk. Anything that is not saved + * will be lost. This operation does not execute in a new thread by default. + */ + void reloadAllFromDisk(); + + /** + * Saves all of the cached regions in this world to disk. This operation does not execute + * in a new thread by default. + */ + void save(); +} diff --git a/src/api/java/baritone/api/cache/IWorldData.java b/src/api/java/baritone/api/cache/IWorldData.java index c7ca580a..1031ba92 100644 --- a/src/api/java/baritone/api/cache/IWorldData.java +++ b/src/api/java/baritone/api/cache/IWorldData.java @@ -23,6 +23,13 @@ package baritone.api.cache; */ public interface IWorldData { + /** + * Returns the cached world for this world. A cached world is a simplified format + * of a regular world, intended for use on multiplayer servers where chunks are not + * traditionally stored to disk, allowing for long distance pathing with minimal disk usage. + */ + ICachedWorld getCachedWorld(); + /** * Returns the waypoint collection for this world. * diff --git a/src/main/java/baritone/behavior/MineBehavior.java b/src/main/java/baritone/behavior/MineBehavior.java index e429f956..2de6167a 100644 --- a/src/main/java/baritone/behavior/MineBehavior.java +++ b/src/main/java/baritone/behavior/MineBehavior.java @@ -139,7 +139,7 @@ public final class MineBehavior extends Behavior implements IMineBehavior, Helpe //long b = System.currentTimeMillis(); for (Block m : mining) { if (CachedChunk.BLOCKS_TO_KEEP_TRACK_OF.contains(m)) { - locs.addAll(WorldProvider.INSTANCE.getCurrentWorld().cache.getLocationsOf(ChunkPacker.blockToString(m), 1, 1)); + locs.addAll(WorldProvider.INSTANCE.getCurrentWorld().getCachedWorld().getLocationsOf(ChunkPacker.blockToString(m), 1, 1)); } else { uninteresting.add(m); } diff --git a/src/main/java/baritone/cache/CachedChunk.java b/src/main/java/baritone/cache/CachedChunk.java index f9a7d96b..b8d88a49 100644 --- a/src/main/java/baritone/cache/CachedChunk.java +++ b/src/main/java/baritone/cache/CachedChunk.java @@ -17,7 +17,8 @@ package baritone.cache; -import baritone.utils.pathing.IBlockTypeAccess; +import baritone.api.cache.IBlockTypeAccess; +import baritone.utils.Helper; import baritone.utils.pathing.PathingBlockType; import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; @@ -30,7 +31,7 @@ import java.util.*; * @author Brady * @since 8/3/2018 1:04 AM */ -public final class CachedChunk implements IBlockTypeAccess { +public final class CachedChunk implements IBlockTypeAccess, Helper { public static final Set BLOCKS_TO_KEEP_TRACK_OF = Collections.unmodifiableSet(new HashSet() {{ add(Blocks.DIAMOND_ORE); diff --git a/src/main/java/baritone/cache/CachedRegion.java b/src/main/java/baritone/cache/CachedRegion.java index 7409ff38..e9402bd5 100644 --- a/src/main/java/baritone/cache/CachedRegion.java +++ b/src/main/java/baritone/cache/CachedRegion.java @@ -17,7 +17,7 @@ package baritone.cache; -import baritone.utils.pathing.IBlockTypeAccess; +import baritone.api.cache.ICachedRegion; import net.minecraft.block.state.IBlockState; import net.minecraft.util.math.BlockPos; @@ -33,7 +33,8 @@ import java.util.zip.GZIPOutputStream; * @author Brady * @since 8/3/2018 9:35 PM */ -public final class CachedRegion implements IBlockTypeAccess { +public final class CachedRegion implements ICachedRegion { + private static final byte CHUNK_NOT_PRESENT = 0; private static final byte CHUNK_PRESENT = 1; @@ -77,6 +78,7 @@ public final class CachedRegion implements IBlockTypeAccess { return null; } + @Override public final boolean isCached(int x, int z) { return chunks[x >> 4][z >> 4] != null; } @@ -280,6 +282,7 @@ public final class CachedRegion implements IBlockTypeAccess { /** * @return The region x coordinate */ + @Override public final int getX() { return this.x; } @@ -287,6 +290,7 @@ public final class CachedRegion implements IBlockTypeAccess { /** * @return The region z coordinate */ + @Override public final int getZ() { return this.z; } diff --git a/src/main/java/baritone/cache/CachedWorld.java b/src/main/java/baritone/cache/CachedWorld.java index eff9391e..3901303d 100644 --- a/src/main/java/baritone/cache/CachedWorld.java +++ b/src/main/java/baritone/cache/CachedWorld.java @@ -18,6 +18,7 @@ package baritone.cache; import baritone.Baritone; +import baritone.api.cache.ICachedWorld; import baritone.utils.Helper; import it.unimi.dsi.fastutil.longs.Long2ObjectMap; import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; @@ -36,7 +37,7 @@ import java.util.concurrent.LinkedBlockingQueue; * @author Brady * @since 8/4/2018 12:02 AM */ -public final class CachedWorld implements Helper { +public final class CachedWorld implements ICachedWorld, Helper { /** * The maximum number of regions in any direction from (0,0) @@ -83,6 +84,7 @@ public final class CachedWorld implements Helper { }); } + @Override public final void queueForPacking(Chunk chunk) { try { toPack.put(chunk); @@ -91,6 +93,7 @@ public final class CachedWorld implements Helper { } } + @Override public final boolean isCached(int blockX, int blockZ) { CachedRegion region = getRegion(blockX >> 9, blockZ >> 9); if (region == null) { @@ -99,7 +102,8 @@ public final class CachedWorld implements Helper { return region.isCached(blockX & 511, blockZ & 511); } - public final LinkedList getLocationsOf(String block, int minimum, int maxRegionDistanceSq) { + @Override + public final LinkedList getLocationsOf(String block, int maximum, int maxRegionDistanceSq) { LinkedList res = new LinkedList<>(); int playerRegionX = playerFeet().getX() >> 9; int playerRegionZ = playerFeet().getZ() >> 9; @@ -121,7 +125,7 @@ public final class CachedWorld implements Helper { } } } - if (res.size() >= minimum) { + if (res.size() >= maximum) { return res; } searchRadius++; @@ -134,6 +138,7 @@ public final class CachedWorld implements Helper { region.updateCachedChunk(chunk.x & 31, chunk.z & 31, chunk); } + @Override public final void save() { if (!Baritone.settings().chunkCaching.get()) { System.out.println("Not saving to disk; chunk caching is disabled."); @@ -153,6 +158,7 @@ public final class CachedWorld implements Helper { return new ArrayList<>(this.cachedRegions.values()); } + @Override public final void reloadAllFromDisk() { long start = System.nanoTime() / 1000000L; allRegions().forEach(region -> { @@ -164,13 +170,7 @@ public final class CachedWorld implements Helper { System.out.println("World load took " + (now - start) + "ms"); } - /** - * Returns the region at the specified region coordinates - * - * @param regionX The region X coordinate - * @param regionZ The region Z coordinate - * @return The region located at the specified coordinates - */ + @Override public final synchronized CachedRegion getRegion(int regionX, int regionZ) { return cachedRegions.get(getRegionID(regionX, regionZ)); } diff --git a/src/main/java/baritone/cache/WorldData.java b/src/main/java/baritone/cache/WorldData.java index ce228f90..6899284c 100644 --- a/src/main/java/baritone/cache/WorldData.java +++ b/src/main/java/baritone/cache/WorldData.java @@ -18,6 +18,7 @@ package baritone.cache; import baritone.Baritone; +import baritone.api.cache.ICachedWorld; import baritone.api.cache.IWaypointCollection; import baritone.api.cache.IWorldData; @@ -30,7 +31,7 @@ import java.nio.file.Path; */ public class WorldData implements IWorldData { - public final CachedWorld cache; + private final CachedWorld cache; private final Waypoints waypoints; //public final MapData map; public final Path directory; @@ -48,6 +49,11 @@ public class WorldData implements IWorldData { }); } + @Override + public ICachedWorld getCachedWorld() { + return this.cache; + } + @Override public IWaypointCollection getWaypoints() { return this.waypoints; diff --git a/src/main/java/baritone/event/GameEventHandler.java b/src/main/java/baritone/event/GameEventHandler.java index 934dd532..61756e33 100644 --- a/src/main/java/baritone/event/GameEventHandler.java +++ b/src/main/java/baritone/event/GameEventHandler.java @@ -109,7 +109,7 @@ public final class GameEventHandler implements IGameEventListener, Helper { if (isPostPopulate || isPreUnload) { WorldProvider.INSTANCE.ifWorldLoaded(world -> { Chunk chunk = mc.world.getChunk(event.getX(), event.getZ()); - world.cache.queueForPacking(chunk); + world.getCachedWorld().queueForPacking(chunk); }); } diff --git a/src/main/java/baritone/utils/BlockStateInterface.java b/src/main/java/baritone/utils/BlockStateInterface.java index 068721cd..1d8f5b5c 100644 --- a/src/main/java/baritone/utils/BlockStateInterface.java +++ b/src/main/java/baritone/utils/BlockStateInterface.java @@ -18,6 +18,7 @@ package baritone.utils; import baritone.Baritone; +import baritone.api.cache.ICachedRegion; import baritone.cache.CachedRegion; import baritone.cache.WorldData; import baritone.cache.WorldProvider; @@ -36,7 +37,7 @@ import net.minecraft.world.chunk.Chunk; public class BlockStateInterface implements Helper { private static Chunk prev = null; - private static CachedRegion prevCached = null; + private static ICachedRegion prevCached = null; private static IBlockState AIR = Blocks.AIR.getDefaultState(); @@ -70,13 +71,13 @@ public class BlockStateInterface implements Helper { } // same idea here, skip the Long2ObjectOpenHashMap.get if at all possible // except here, it's 512x512 tiles instead of 16x16, so even better repetition - CachedRegion cached = prevCached; + ICachedRegion cached = prevCached; if (cached == null || cached.getX() != x >> 9 || cached.getZ() != z >> 9) { WorldData world = WorldProvider.INSTANCE.getCurrentWorld(); if (world == null) { return AIR; } - CachedRegion region = world.cache.getRegion(x >> 9, z >> 9); + ICachedRegion region = world.getCachedWorld().getRegion(x >> 9, z >> 9); if (region == null) { return AIR; } @@ -100,7 +101,7 @@ public class BlockStateInterface implements Helper { prev = prevChunk; return true; } - CachedRegion prevRegion = prevCached; + ICachedRegion prevRegion = prevCached; if (prevRegion != null && prevRegion.getX() == x >> 9 && prevRegion.getZ() == z >> 9) { return prevRegion.isCached(x & 511, z & 511); } @@ -108,7 +109,7 @@ public class BlockStateInterface implements Helper { if (world == null) { return false; } - prevRegion = world.cache.getRegion(x >> 9, z >> 9); + prevRegion = world.getCachedWorld().getRegion(x >> 9, z >> 9); if (prevRegion == null) { return false; } diff --git a/src/main/java/baritone/utils/ExampleBaritoneControl.java b/src/main/java/baritone/utils/ExampleBaritoneControl.java index 13879d52..4bbef7e2 100644 --- a/src/main/java/baritone/utils/ExampleBaritoneControl.java +++ b/src/main/java/baritone/utils/ExampleBaritoneControl.java @@ -184,7 +184,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper { Chunk chunk = cli.getLoadedChunk(x, z); if (chunk != null) { count++; - WorldProvider.INSTANCE.getCurrentWorld().cache.queueForPacking(chunk); + WorldProvider.INSTANCE.getCurrentWorld().getCachedWorld().queueForPacking(chunk); } } } @@ -272,20 +272,20 @@ public class ExampleBaritoneControl extends Behavior implements Helper { return; } if (msg.equals("reloadall")) { - WorldProvider.INSTANCE.getCurrentWorld().cache.reloadAllFromDisk(); + WorldProvider.INSTANCE.getCurrentWorld().getCachedWorld().reloadAllFromDisk(); logDirect("ok"); event.cancel(); return; } if (msg.equals("saveall")) { - WorldProvider.INSTANCE.getCurrentWorld().cache.save(); + WorldProvider.INSTANCE.getCurrentWorld().getCachedWorld().save(); logDirect("ok"); event.cancel(); return; } if (msg.startsWith("find")) { String blockType = msg.substring(4).trim(); - LinkedList locs = WorldProvider.INSTANCE.getCurrentWorld().cache.getLocationsOf(blockType, 1, 4); + LinkedList locs = WorldProvider.INSTANCE.getCurrentWorld().getCachedWorld().getLocationsOf(blockType, 1, 4); logDirect("Have " + locs.size() + " locations"); for (BlockPos pos : locs) { Block actually = BlockStateInterface.get(pos).getBlock();