baritone/src/main/java/baritone/cache/WorldProvider.java

155 lines
5.9 KiB
Java
Raw Normal View History

2018-08-08 03:16:53 +00:00
/*
* 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
2018-08-08 03:16:53 +00:00
* 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,
2018-08-08 03:16:53 +00:00
* 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.
2018-08-08 03:16:53 +00:00
*
* You should have received a copy of the GNU Lesser General Public License
2018-08-08 03:16:53 +00:00
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
2018-09-11 17:28:03 +00:00
package baritone.cache;
2018-08-22 22:35:32 +00:00
import baritone.Baritone;
import baritone.api.cache.IWorldProvider;
2019-04-18 01:10:47 +00:00
import baritone.api.utils.Helper;
2019-01-25 05:00:44 +00:00
import org.apache.commons.lang3.SystemUtils;
import java.io.File;
2018-08-23 21:37:08 +00:00
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
2021-06-23 17:24:32 +00:00
import net.minecraft.client.server.IntegratedServer;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.dimension.DimensionType;
import net.minecraft.world.level.storage.LevelResource;
/**
* @author Brady
* @since 8/4/2018
*/
public class WorldProvider implements IWorldProvider, Helper {
private static final Map<Path, WorldData> worldCache = new HashMap<>(); // this is how the bots have the same cached world
2018-08-22 22:35:32 +00:00
private WorldData currentWorld;
2023-01-29 00:25:17 +00:00
private Level mcWorld; // this let's us detect a broken load/unload hook
@Override
public final WorldData getCurrentWorld() {
detectAndHandleBrokenLoading();
return this.currentWorld;
}
2018-11-11 22:22:00 +00:00
/**
* Called when a new world is initialized to discover the
*
* @param world The world's Registry Data
2018-11-11 22:22:00 +00:00
*/
public final void initWorld(ResourceKey<Level> worldKey, DimensionType world) {
2018-11-13 00:39:58 +00:00
File directory;
File readme;
2018-11-11 22:22:00 +00:00
IntegratedServer integratedServer = mc.getSingleplayerServer();
2018-11-11 22:22:00 +00:00
// If there is an integrated server running (Aka Singleplayer) then do magic to find the world save file
if (mc.hasSingleplayerServer()) {
directory = DimensionType.getStorageFolder(worldKey, integratedServer.getWorldPath(LevelResource.ROOT).toFile());
// Gets the "depth" of this directory relative the the game's run directory, 2 is the location of the world
if (directory.toPath().relativize(mc.gameDirectory.toPath()).getNameCount() != 2) {
2018-08-22 22:35:32 +00:00
// subdirectory of the main save directory for this world
directory = directory.getParentFile();
}
2018-08-22 22:35:32 +00:00
directory = new File(directory, "baritone");
2018-08-23 21:40:42 +00:00
readme = directory;
2018-11-11 22:22:00 +00:00
} else { // Otherwise, the server must be remote...
2021-03-29 04:32:48 +00:00
String folderName;
2023-01-29 00:25:17 +00:00
if (mc.getCurrentServer() != null) {
folderName = mc.isConnectedToRealms() ? "realms" : mc.getCurrentServer().ip;
2022-02-02 18:17:02 +00:00
} else {
2023-01-29 00:25:17 +00:00
//replaymod causes null currentServer and false singleplayer.
System.out.println("World seems to be a replay. Not loading Baritone cache.");
2021-03-29 04:32:48 +00:00
currentWorld = null;
2023-01-29 00:25:17 +00:00
mcWorld = mc.level;
2021-03-29 04:32:48 +00:00
return;
}
2019-01-25 05:00:44 +00:00
if (SystemUtils.IS_OS_WINDOWS) {
folderName = folderName.replace(":", "_");
}
directory = new File(Baritone.getDir(), folderName);
2018-11-06 04:01:46 +00:00
readme = Baritone.getDir();
2018-08-22 22:35:32 +00:00
}
2018-11-11 22:22:00 +00:00
2018-08-23 21:40:42 +00:00
// lol wtf is this baritone folder in my minecraft save?
try (FileOutputStream out = new FileOutputStream(new File(readme, "readme.txt"))) {
2018-08-23 21:37:08 +00:00
// good thing we have a readme
out.write("https://github.com/cabaletta/baritone\n".getBytes());
} catch (IOException ignored) {}
2018-08-23 21:40:42 +00:00
2018-11-11 22:22:00 +00:00
// We will actually store the world data in a subfolder: "DIM<id>"
Path dir = getDimDir(worldKey, world.logicalHeight(), directory);
2018-08-22 22:35:32 +00:00
if (!Files.exists(dir)) {
try {
Files.createDirectories(dir);
} catch (IOException ignored) {}
}
2018-11-11 22:22:00 +00:00
2018-08-22 22:35:32 +00:00
System.out.println("Baritone world data dir: " + dir);
2018-12-04 01:20:56 +00:00
synchronized (worldCache) {
2020-07-19 20:50:56 +00:00
this.currentWorld = worldCache.computeIfAbsent(dir, d -> new WorldData(d, world));
2018-12-04 01:20:56 +00:00
}
2023-01-29 00:25:17 +00:00
this.mcWorld = mc.level;
}
public final Path getDimDir(ResourceKey<Level> level, int height, File directory) {
return directory.toPath().resolve(level.location().getNamespace()).resolve(level.location().getPath() + "_" + height);
}
public final void closeWorld() {
2018-08-22 22:35:32 +00:00
WorldData world = this.currentWorld;
this.currentWorld = null;
this.mcWorld = null;
2018-08-21 22:18:35 +00:00
if (world == null) {
return;
}
2018-08-22 22:35:32 +00:00
world.onClose();
}
2018-08-22 22:35:32 +00:00
public final void ifWorldLoaded(Consumer<WorldData> currentWorldConsumer) {
detectAndHandleBrokenLoading();
2018-09-08 04:32:25 +00:00
if (this.currentWorld != null) {
currentWorldConsumer.accept(this.currentWorld);
2018-09-08 04:32:25 +00:00
}
}
private final void detectAndHandleBrokenLoading() {
2023-01-29 00:25:17 +00:00
if (this.mcWorld != mc.level) {
if (this.currentWorld != null) {
System.out.println("mc.world unloaded unnoticed! Unloading Baritone cache now.");
closeWorld();
}
2023-01-29 00:25:17 +00:00
if (mc.level != null) {
System.out.println("mc.world loaded unnoticed! Loading Baritone cache now.");
2023-01-29 00:25:17 +00:00
initWorld(mc.level.dimension(), mc.level.dimensionType());
}
2023-01-29 00:25:17 +00:00
} else if (currentWorld == null && mc.level != null && (mc.hasSingleplayerServer() || mc.getCurrentServer() != null)) {
System.out.println("Retrying to load Baritone cache");
2023-01-29 00:25:17 +00:00
initWorld(mc.level.dimension(), mc.level.dimensionType());
}
}
}