baritone/src/main/java/baritone/chunk/CachedWorldProvider.java

90 lines
2.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 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,
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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
2018-08-22 20:15:56 +00:00
package baritone.chunk;
2018-08-22 20:15:56 +00:00
import baritone.utils.Helper;
import baritone.launch.mixins.accessor.IAnvilChunkLoader;
import baritone.launch.mixins.accessor.IChunkProviderServer;
import net.minecraft.client.multiplayer.WorldClient;
import net.minecraft.server.integrated.IntegratedServer;
import net.minecraft.world.WorldServer;
import java.io.File;
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;
/**
* @author Brady
* @since 8/4/2018 11:06 AM
*/
public enum CachedWorldProvider implements Helper {
INSTANCE;
private final Map<String, CachedWorld> singlePlayerWorldCache = new HashMap<>();
private CachedWorld currentWorld;
public final CachedWorld 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());
IChunkProviderServer provider = (IChunkProviderServer) localServerWorld.getChunkProvider();
IAnvilChunkLoader loader = (IAnvilChunkLoader) provider.getChunkLoader();
Path dir = new File(new File(loader.getChunkSaveLocation(), "region"), "cache").toPath();
if (!Files.exists(dir)) {
try {
Files.createDirectories(dir);
} catch (IOException ignored) {}
}
this.currentWorld = this.singlePlayerWorldCache.computeIfAbsent(dir.toString(), CachedWorld::new);
}
// TODO: Store server worlds
}
public final void closeWorld() {
2018-08-21 22:18:35 +00:00
CachedWorld world = this.currentWorld;
this.currentWorld = null;
2018-08-21 22:18:35 +00:00
if (world == null) {
return;
}
new Thread() {
public void run() {
System.out.println("Started saving the world in a new thread");
world.save();
}
}.start();
}
public final void ifWorldLoaded(Consumer<CachedWorld> currentWorldConsumer) {
if (this.currentWorld != null)
currentWorldConsumer.accept(this.currentWorld);
}
}