From 7996cfe0a535139442ce801f05ec05e751aa033b Mon Sep 17 00:00:00 2001 From: Leijurv Date: Wed, 22 Aug 2018 15:35:32 -0700 Subject: [PATCH] waypoints rough start --- src/main/java/baritone/chunk/CachedWorld.java | 13 ++++- src/main/java/baritone/chunk/Waypoints.java | 36 ++++++++++++ src/main/java/baritone/chunk/WorldData.java | 42 ++++++++++++++ ...dWorldProvider.java => WorldProvider.java} | 56 +++++++++++-------- .../java/baritone/event/GameEventHandler.java | 10 ++-- .../pathing/calc/AStarPathFinder.java | 6 +- .../baritone/utils/BlockStateInterface.java | 4 +- 7 files changed, 131 insertions(+), 36 deletions(-) create mode 100644 src/main/java/baritone/chunk/Waypoints.java create mode 100644 src/main/java/baritone/chunk/WorldData.java rename src/main/java/baritone/chunk/{CachedWorldProvider.java => WorldProvider.java} (56%) diff --git a/src/main/java/baritone/chunk/CachedWorld.java b/src/main/java/baritone/chunk/CachedWorld.java index 7fc20582..5374e837 100644 --- a/src/main/java/baritone/chunk/CachedWorld.java +++ b/src/main/java/baritone/chunk/CachedWorld.java @@ -23,6 +23,9 @@ import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; import net.minecraft.block.state.IBlockState; import net.minecraft.world.chunk.Chunk; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.BitSet; import java.util.concurrent.LinkedBlockingQueue; import java.util.function.Consumer; @@ -50,8 +53,14 @@ public final class CachedWorld implements IBlockTypeAccess { private final LinkedBlockingQueue toPack = new LinkedBlockingQueue<>(); - public CachedWorld(String directory) { - this.directory = directory; + CachedWorld(Path directory) { + if (!Files.exists(directory)) { + try { + Files.createDirectories(directory); + } catch (IOException ignored) {} + } + this.directory = directory.toString(); + System.out.println("Cached world directory: " + directory); // Insert an invalid region element cachedRegions.put(0, null); new PackerThread().start(); diff --git a/src/main/java/baritone/chunk/Waypoints.java b/src/main/java/baritone/chunk/Waypoints.java new file mode 100644 index 00000000..4617ad63 --- /dev/null +++ b/src/main/java/baritone/chunk/Waypoints.java @@ -0,0 +1,36 @@ +/* + * This file is part of Baritone. + * + * Baritone is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Baritone. If not, see . + */ + +package baritone.chunk; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +public class Waypoints { + private final Path directory; + + Waypoints(Path directory) { + this.directory = directory; + if (!Files.exists(directory)) { + try { + Files.createDirectories(directory); + } catch (IOException ignored) {} + } + System.out.println("Would save waypoints to " + directory); + } +} diff --git a/src/main/java/baritone/chunk/WorldData.java b/src/main/java/baritone/chunk/WorldData.java new file mode 100644 index 00000000..7145742e --- /dev/null +++ b/src/main/java/baritone/chunk/WorldData.java @@ -0,0 +1,42 @@ +/* + * This file is part of Baritone. + * + * Baritone is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Baritone. If not, see . + */ + +package baritone.chunk; + +import java.nio.file.Path; + +public class WorldData { + public final CachedWorld cache; + public final Waypoints waypoints; + //public final MapData map; + public final Path directory; + + WorldData(Path directory) { + this.directory = directory; + this.cache = new CachedWorld(directory.resolve("cache")); + this.waypoints = new Waypoints(directory.resolve("waypoints")); + } + + void onClose() { + new Thread() { + public void run() { + System.out.println("Started saving the world in a new thread"); + cache.save(); + } + }.start(); + } +} diff --git a/src/main/java/baritone/chunk/CachedWorldProvider.java b/src/main/java/baritone/chunk/WorldProvider.java similarity index 56% rename from src/main/java/baritone/chunk/CachedWorldProvider.java rename to src/main/java/baritone/chunk/WorldProvider.java index 2ba95d4b..af18e922 100644 --- a/src/main/java/baritone/chunk/CachedWorldProvider.java +++ b/src/main/java/baritone/chunk/WorldProvider.java @@ -17,9 +17,10 @@ package baritone.chunk; -import baritone.utils.Helper; +import baritone.Baritone; import baritone.launch.mixins.accessor.IAnvilChunkLoader; import baritone.launch.mixins.accessor.IChunkProviderServer; +import baritone.utils.Helper; import net.minecraft.client.multiplayer.WorldClient; import net.minecraft.server.integrated.IntegratedServer; import net.minecraft.world.WorldServer; @@ -36,53 +37,60 @@ import java.util.function.Consumer; * @author Brady * @since 8/4/2018 11:06 AM */ -public enum CachedWorldProvider implements Helper { +public enum WorldProvider implements Helper { INSTANCE; - private final Map singlePlayerWorldCache = new HashMap<>(); + private final Map worldCache = new HashMap<>(); - private CachedWorld currentWorld; + private WorldData currentWorld; - public final CachedWorld getCurrentWorld() { + public final WorldData getCurrentWorld() { return this.currentWorld; } public final void initWorld(WorldClient world) { - IntegratedServer integratedServer; - if ((integratedServer = mc.getIntegratedServer()) != null) { - - WorldServer localServerWorld = integratedServer.getWorld(world.provider.getDimensionType().getId()); + int dimensionID = world.provider.getDimensionType().getId(); + File directory; + IntegratedServer integratedServer = mc.getIntegratedServer(); + if (integratedServer != null) { + WorldServer localServerWorld = integratedServer.getWorld(dimensionID); IChunkProviderServer provider = (IChunkProviderServer) localServerWorld.getChunkProvider(); IAnvilChunkLoader loader = (IAnvilChunkLoader) provider.getChunkLoader(); + directory = loader.getChunkSaveLocation(); - Path dir = new File(new File(loader.getChunkSaveLocation(), "region"), "cache").toPath(); - if (!Files.exists(dir)) { - try { - Files.createDirectories(dir); - } catch (IOException ignored) {} + if (!directory.getParentFile().getName().equals("saves")) { + // subdirectory of the main save directory for this world + directory = directory.getParentFile(); } - this.currentWorld = this.singlePlayerWorldCache.computeIfAbsent(dir.toString(), CachedWorld::new); + directory = new File(directory, "baritone"); + + } else { + //remote + directory = new File(Baritone.INSTANCE.getDir(), mc.getCurrentServerData().serverIP); } - // TODO: Store server worlds + directory = new File(directory, "DIM" + dimensionID); + Path dir = directory.toPath(); + if (!Files.exists(dir)) { + try { + Files.createDirectories(dir); + } catch (IOException ignored) {} + } + System.out.println("Baritone world data dir: " + dir); + this.currentWorld = this.worldCache.computeIfAbsent(dir, WorldData::new); } public final void closeWorld() { - CachedWorld world = this.currentWorld; + WorldData world = this.currentWorld; this.currentWorld = null; if (world == null) { return; } - new Thread() { - public void run() { - System.out.println("Started saving the world in a new thread"); - world.save(); - } - }.start(); + world.onClose(); } - public final void ifWorldLoaded(Consumer currentWorldConsumer) { + public final void ifWorldLoaded(Consumer currentWorldConsumer) { if (this.currentWorld != null) currentWorldConsumer.accept(this.currentWorld); } diff --git a/src/main/java/baritone/event/GameEventHandler.java b/src/main/java/baritone/event/GameEventHandler.java index 8a63859e..c6ab20fd 100644 --- a/src/main/java/baritone/event/GameEventHandler.java +++ b/src/main/java/baritone/event/GameEventHandler.java @@ -35,7 +35,7 @@ package baritone.event; import baritone.Baritone; -import baritone.chunk.CachedWorldProvider; +import baritone.chunk.WorldProvider; import baritone.event.events.*; import baritone.event.events.type.EventState; import baritone.event.listener.IGameEventListener; @@ -113,9 +113,9 @@ public final class GameEventHandler implements IGameEventListener, Helper { if (Baritone.settings().chunkCaching.get()) { if (isPostPopulate || isPreUnload) { - CachedWorldProvider.INSTANCE.ifWorldLoaded(world -> { + WorldProvider.INSTANCE.ifWorldLoaded(world -> { Chunk chunk = mc.world.getChunk(event.getX(), event.getZ()); - world.queueForPacking(chunk); + world.cache.queueForPacking(chunk); }); } } @@ -126,7 +126,7 @@ public final class GameEventHandler implements IGameEventListener, Helper { @Override public final void onRenderPass(RenderEvent event) { /* - CachedWorldProvider.INSTANCE.ifWorldLoaded(world -> world.forEachRegion(region -> region.forEachChunk(chunk -> { + WorldProvider.INSTANCE.ifWorldLoaded(world -> world.forEachRegion(region -> region.forEachChunk(chunk -> { drawChunkLine(region.getX() * 512 + chunk.getX() * 16, region.getZ() * 512 + chunk.getZ() * 16, event.getPartialTicks()); }))); */ @@ -137,7 +137,7 @@ public final class GameEventHandler implements IGameEventListener, Helper { @Override public final void onWorldEvent(WorldEvent event) { if (Baritone.settings().chunkCaching.get()) { - CachedWorldProvider cache = CachedWorldProvider.INSTANCE; + WorldProvider cache = WorldProvider.INSTANCE; switch (event.getState()) { case PRE: diff --git a/src/main/java/baritone/pathing/calc/AStarPathFinder.java b/src/main/java/baritone/pathing/calc/AStarPathFinder.java index d0432960..6d8b3ac7 100644 --- a/src/main/java/baritone/pathing/calc/AStarPathFinder.java +++ b/src/main/java/baritone/pathing/calc/AStarPathFinder.java @@ -18,7 +18,7 @@ package baritone.pathing.calc; import baritone.Baritone; -import baritone.chunk.CachedWorldProvider; +import baritone.chunk.WorldProvider; import baritone.pathing.calc.openset.BinaryHeapOpenSet; import baritone.pathing.calc.openset.IOpenSet; import baritone.pathing.goals.Goal; @@ -113,8 +113,8 @@ public class AStarPathFinder extends AbstractNodeCostSearch implements Helper { BetterBlockPos dest = (BetterBlockPos) movementToGetToNeighbor.getDest(); boolean isPositionCached = false; if (cache) { - if (CachedWorldProvider.INSTANCE.getCurrentWorld() != null) { - if (CachedWorldProvider.INSTANCE.getCurrentWorld().getBlock(dest) != null) { + if (WorldProvider.INSTANCE.getCurrentWorld() != null) { + if (WorldProvider.INSTANCE.getCurrentWorld().cache.getBlock(dest) != null) { isPositionCached = true; } } diff --git a/src/main/java/baritone/utils/BlockStateInterface.java b/src/main/java/baritone/utils/BlockStateInterface.java index 1d084cc1..1c23e74a 100644 --- a/src/main/java/baritone/utils/BlockStateInterface.java +++ b/src/main/java/baritone/utils/BlockStateInterface.java @@ -19,7 +19,7 @@ package baritone.utils; import baritone.Baritone; import baritone.chunk.CachedWorld; -import baritone.chunk.CachedWorldProvider; +import baritone.chunk.WorldProvider; import net.minecraft.block.Block; import net.minecraft.block.BlockFalling; import net.minecraft.block.BlockLiquid; @@ -43,7 +43,7 @@ public class BlockStateInterface implements Helper { } } if (Baritone.settings().chunkCaching.get()) { - CachedWorld world = CachedWorldProvider.INSTANCE.getCurrentWorld(); + CachedWorld world = WorldProvider.INSTANCE.getCurrentWorld().cache; if (world != null) { IBlockState type = world.getBlock(pos); if (type != null) {