Search in sources :

Example 1 with ISaveHandler

use of net.minecraft.world.storage.ISaveHandler in project Minestuck by mraof.

the class MinestuckSaveHandler method onWorldLoad.

@SubscribeEvent
public void onWorldLoad(WorldEvent.Load event) {
    if (event.getWorld().provider.getDimension() != 0 || event.getWorld().isRemote)
        return;
    ISaveHandler saveHandler = event.getWorld().getSaveHandler();
    File dataFile = saveHandler.getMapFileFromName("MinestuckData");
    if (dataFile != null && dataFile.exists()) {
        NBTTagCompound nbt = null;
        try {
            nbt = CompressedStreamTools.readCompressed(new FileInputStream(dataFile));
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (nbt != null) {
            ServerEditHandler.loadData(nbt);
            MinestuckDimensionHandler.loadData(nbt);
            SkaianetHandler.loadData(nbt.getCompoundTag("skaianet"));
            MinestuckPlayerData.readFromNBT(nbt);
            TileEntityTransportalizer.loadTransportalizers(nbt.getCompoundTag("transportalizers"));
            ServerEventHandler.tickTasks.clear();
            if (nbt.hasKey("tickTasks", 9)) {
                NBTTagList list = nbt.getTagList("tickTasks", 10);
                for (int i = 0; i < list.tagCount(); i++) ServerEventHandler.tickTasks.add(new PostEntryTask(list.getCompoundTagAt(i)));
            }
            return;
        }
    }
    ServerEventHandler.tickTasks.clear();
    SkaianetHandler.loadData(null);
    MinestuckPlayerData.readFromNBT(null);
    ServerEditHandler.loadData(null);
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) ISaveHandler(net.minecraft.world.storage.ISaveHandler) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) IOException(java.io.IOException) PostEntryTask(com.mraof.minestuck.util.PostEntryTask) File(java.io.File) FileInputStream(java.io.FileInputStream) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 2 with ISaveHandler

use of net.minecraft.world.storage.ISaveHandler in project ArsMagica2 by Mithion.

the class PowerNodeCache method getFileFromChunk.

private File getFileFromChunk(World world, ChunkCoordIntPair chunk, boolean createNew) {
    File saveFolder = saveDirs.get(world.provider.dimensionId);
    if (saveFolder == null) {
        ISaveHandler handler = world.getSaveHandler();
        if (handler instanceof SaveHandler) {
            saveFolder = new File(((SaveHandler) handler).getWorldDirectory(), folder);
            saveFolder.mkdirs();
            saveFolder = new File(saveFolder, String.format("DIM%d", world.provider.dimensionId));
            saveFolder.mkdirs();
            saveDirs.put(world.provider.dimensionId, saveFolder);
        } else {
            return null;
        }
    }
    int rX = (int) Math.floor(chunk.chunkXPos / 32);
    int rZ = (int) Math.floor(chunk.chunkZPos / 32);
    String fileName = String.format("%d_%d%s", rX, rZ, extension);
    File file = saveFilesCached.get(fileName);
    if (file != null)
        return file;
    file = new File(saveFolder, fileName);
    if (!file.exists()) {
        if (createNew) {
            try {
                file.createNewFile();
            } catch (Throwable t) {
                t.printStackTrace();
            }
        } else {
            return null;
        }
    }
    saveFilesCached.put(fileName, file);
    return file;
}
Also used : ISaveHandler(net.minecraft.world.storage.ISaveHandler) SaveHandler(net.minecraft.world.storage.SaveHandler) ISaveHandler(net.minecraft.world.storage.ISaveHandler) File(java.io.File)

Example 3 with ISaveHandler

use of net.minecraft.world.storage.ISaveHandler in project MinecraftForge by MinecraftForge.

the class DimensionManager method initDimension.

public static void initDimension(int dim) {
    WorldServer overworld = getWorld(0);
    if (overworld == null) {
        throw new RuntimeException("Cannot Hotload Dim: Overworld is not Loaded!");
    }
    try {
        DimensionManager.getProviderType(dim);
    } catch (Exception e) {
        System.err.println("Cannot Hotload Dim: " + e.getMessage());
        // If a provider hasn't been registered then we can't hotload the dim
        return;
    }
    MinecraftServer mcServer = overworld.getMinecraftServer();
    ISaveHandler savehandler = overworld.getSaveHandler();
    //WorldSettings worldSettings = new WorldSettings(overworld.getWorldInfo());
    WorldServer world = (dim == 0 ? overworld : (WorldServer) (new WorldServerMulti(mcServer, savehandler, dim, overworld, mcServer.theProfiler).init()));
    world.addEventListener(new ServerWorldEventHandler(mcServer, world));
    MinecraftForge.EVENT_BUS.post(new WorldEvent.Load(world));
    if (!mcServer.isSinglePlayer()) {
        world.getWorldInfo().setGameType(mcServer.getGameType());
    }
    mcServer.setDifficultyForAllWorlds(mcServer.getDifficulty());
}
Also used : ISaveHandler(net.minecraft.world.storage.ISaveHandler) WorldEvent(net.minecraftforge.event.world.WorldEvent) WorldServer(net.minecraft.world.WorldServer) ServerWorldEventHandler(net.minecraft.world.ServerWorldEventHandler) MinecraftException(net.minecraft.world.MinecraftException) WorldServerMulti(net.minecraft.world.WorldServerMulti) MinecraftServer(net.minecraft.server.MinecraftServer)

Example 4 with ISaveHandler

use of net.minecraft.world.storage.ISaveHandler in project SpongeCommon by SpongePowered.

the class WorldManager method loadAllWorlds.

public static void loadAllWorlds(String worldName, long defaultSeed, WorldType defaultWorldType, String generatorOptions) {
    final MinecraftServer server = SpongeImpl.getServer();
    // We cannot call getCurrentSavesDirectory here as that would generate a savehandler and trigger a session lock.
    // We'll go ahead and make the directories for the save name here so that the migrator won't fail
    final Path currentSavesDir = server.anvilFile.toPath().resolve(server.getFolderName());
    try {
        // Symlink needs special handling
        if (Files.isSymbolicLink(currentSavesDir)) {
            final Path actualPathLink = Files.readSymbolicLink(currentSavesDir);
            if (Files.notExists(actualPathLink)) {
                // TODO Need to test symlinking to see if this is even legal...
                Files.createDirectories(actualPathLink);
            } else if (!Files.isDirectory(actualPathLink)) {
                throw new IOException("Saves directory [" + currentSavesDir + "] symlinked to [" + actualPathLink + "] is not a directory!");
            }
        } else {
            Files.createDirectories(currentSavesDir);
        }
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
    WorldManager.registerVanillaDimensionPaths(currentSavesDir);
    WorldMigrator.migrateWorldsTo(currentSavesDir);
    registerExistingSpongeDimensions(currentSavesDir);
    for (Map.Entry<Integer, DimensionType> entry : sortedDimensionMap().entrySet()) {
        final int dimensionId = entry.getKey();
        final DimensionType dimensionType = entry.getValue();
        // Skip all worlds besides dimension 0 if multi-world is disabled
        if (dimensionId != 0 && !server.getAllowNether()) {
            continue;
        }
        // Skip already loaded worlds by plugins
        if (getWorldByDimensionId(dimensionId).isPresent()) {
            continue;
        }
        // Step 1 - Grab the world's data folder
        final Path worldFolder = getWorldFolder(dimensionType, dimensionId);
        if (worldFolder == null) {
            SpongeImpl.getLogger().error("An attempt was made to load a world with dimension id [{}] that has no registered world folder!", dimensionId);
            continue;
        }
        final String worldFolderName = worldFolder.getFileName().toString();
        // Step 2 - See if we are allowed to load it
        if (dimensionId != 0) {
            final SpongeConfig<? extends GeneralConfigBase> activeConfig = SpongeHooks.getActiveConfig(((IMixinDimensionType) (Object) dimensionType).getConfigPath(), worldFolderName);
            if (!activeConfig.getConfig().getWorld().isWorldEnabled()) {
                SpongeImpl.getLogger().warn("World [{}] (DIM{}) is disabled. World will not be loaded...", worldFolder, dimensionId);
                continue;
            }
        }
        // Step 3 - Get our world information from disk
        final ISaveHandler saveHandler;
        if (dimensionId == 0) {
            saveHandler = server.getActiveAnvilConverter().getSaveLoader(server.getFolderName(), true);
        } else {
            saveHandler = new AnvilSaveHandler(WorldManager.getCurrentSavesDirectory().get().toFile(), worldFolderName, true, SpongeImpl.getDataFixer());
        }
        WorldInfo worldInfo = saveHandler.loadWorldInfo();
        WorldSettings worldSettings;
        // If this is integrated server, we need to use the WorldSettings from the client's Single Player menu to construct the worlds
        if (server instanceof IMixinIntegratedServer) {
            worldSettings = ((IMixinIntegratedServer) server).getSettings();
            // If this is overworld and a new save, the WorldInfo has already been made but we want to still fire the construct event.
            if (dimensionId == 0 && ((IMixinIntegratedServer) server).isNewSave()) {
                SpongeImpl.postEvent(SpongeEventFactory.createConstructWorldPropertiesEvent(Sponge.getCauseStackManager().getCurrentCause(), (WorldArchetype) (Object) worldSettings, (WorldProperties) worldInfo));
            }
        } else {
            // WorldSettings will be null here on dedicated server so we need to build one
            worldSettings = new WorldSettings(defaultSeed, server.getGameType(), server.canStructuresSpawn(), server.isHardcore(), defaultWorldType);
        }
        if (worldInfo == null) {
            // Step 4 - At this point, we have either have the WorldInfo or we have none. If we have none, we'll use the settings built above to
            // create the WorldInfo
            worldInfo = createWorldInfoFromSettings(currentSavesDir, (org.spongepowered.api.world.DimensionType) (Object) dimensionType, dimensionId, worldFolderName, worldSettings, generatorOptions);
        } else {
            // create config
            ((IMixinWorldInfo) worldInfo).setDimensionType((org.spongepowered.api.world.DimensionType) (Object) dimensionType);
            ((IMixinWorldInfo) worldInfo).createWorldConfig();
            ((WorldProperties) worldInfo).setGenerateSpawnOnLoad(((IMixinDimensionType) (Object) dimensionType).shouldGenerateSpawnOnLoad());
        }
        // Safety check to ensure we'll get a unique id no matter what
        if (((WorldProperties) worldInfo).getUniqueId() == null) {
            setUuidOnProperties(dimensionId == 0 ? currentSavesDir.getParent() : currentSavesDir, (WorldProperties) worldInfo);
        }
        // Safety check to ensure the world info has the dimension id set
        if (((IMixinWorldInfo) worldInfo).getDimensionId() == null) {
            ((IMixinWorldInfo) worldInfo).setDimensionId(dimensionId);
        }
        // Keep the LevelName in the LevelInfo up to date with the directory name
        if (!worldInfo.getWorldName().equals(worldFolderName)) {
            worldInfo.setWorldName(worldFolderName);
        }
        // Step 5 - Load server resource pack from dimension 0
        if (dimensionId == 0) {
            server.setResourcePackFromWorld(worldFolderName, saveHandler);
        }
        // Step 6 - Cache the WorldProperties we've made so we don't load from disk later.
        registerWorldProperties((WorldProperties) worldInfo);
        if (dimensionId != 0 && !((WorldProperties) worldInfo).loadOnStartup()) {
            SpongeImpl.getLogger().warn("World [{}] (DIM{}) is set to not load on startup. To load it later, enable [load-on-startup] in config " + "or use a plugin", worldFolder, dimensionId);
            continue;
        }
        // Step 7 - Finally, we can create the world and tell it to load
        final WorldServer worldServer = createWorldFromProperties(dimensionId, saveHandler, worldInfo, worldSettings);
        SpongeImpl.getLogger().info("Loading world [{}] ({})", ((org.spongepowered.api.world.World) worldServer).getName(), getDimensionType(dimensionId).get().getName());
    }
    // Set the worlds on the Minecraft server
    reorderWorldsVanillaFirst();
}
Also used : IMixinDimensionType(org.spongepowered.common.interfaces.world.IMixinDimensionType) DimensionType(net.minecraft.world.DimensionType) ISaveHandler(net.minecraft.world.storage.ISaveHandler) IMixinWorldInfo(org.spongepowered.common.interfaces.world.IMixinWorldInfo) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) WorldServer(net.minecraft.world.WorldServer) WorldSettings(net.minecraft.world.WorldSettings) IMixinWorldSettings(org.spongepowered.common.interfaces.world.IMixinWorldSettings) WorldArchetype(org.spongepowered.api.world.WorldArchetype) IMixinIntegratedServer(org.spongepowered.common.interfaces.IMixinIntegratedServer) WorldInfo(net.minecraft.world.storage.WorldInfo) IMixinWorldInfo(org.spongepowered.common.interfaces.world.IMixinWorldInfo) AnvilSaveHandler(net.minecraft.world.chunk.storage.AnvilSaveHandler) Path(java.nio.file.Path) IOException(java.io.IOException) IMixinMinecraftServer(org.spongepowered.common.interfaces.IMixinMinecraftServer) MinecraftServer(net.minecraft.server.MinecraftServer) Map(java.util.Map) BiMap(com.google.common.collect.BiMap) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HashBiMap(com.google.common.collect.HashBiMap) Int2ObjectMap(it.unimi.dsi.fastutil.ints.Int2ObjectMap) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Example 5 with ISaveHandler

use of net.minecraft.world.storage.ISaveHandler in project SpongeCommon by SpongePowered.

the class WorldManager method createWorldProperties.

public static WorldProperties createWorldProperties(String folderName, WorldArchetype archetype, Integer dimensionId) {
    checkNotNull(folderName);
    checkNotNull(archetype);
    final Optional<WorldServer> optWorldServer = getWorld(folderName);
    if (optWorldServer.isPresent()) {
        return ((org.spongepowered.api.world.World) optWorldServer.get()).getProperties();
    }
    final Optional<WorldProperties> optWorldProperties = WorldManager.getWorldProperties(folderName);
    if (optWorldProperties.isPresent()) {
        return optWorldProperties.get();
    }
    final ISaveHandler saveHandler = new AnvilSaveHandler(WorldManager.getCurrentSavesDirectory().get().toFile(), folderName, true, SpongeImpl.getDataFixer());
    WorldInfo worldInfo = saveHandler.loadWorldInfo();
    if (worldInfo == null) {
        worldInfo = new WorldInfo((WorldSettings) (Object) archetype, folderName);
    } else {
        // DimensionType must be set before world config is created to get proper path
        ((IMixinWorldInfo) worldInfo).setDimensionType(archetype.getDimensionType());
        ((IMixinWorldInfo) worldInfo).createWorldConfig();
        ((WorldProperties) worldInfo).setGeneratorModifiers(archetype.getGeneratorModifiers());
    }
    if (archetype.isSeedRandomized()) {
        ((WorldProperties) worldInfo).setSeed(SpongeImpl.random.nextLong());
    }
    setUuidOnProperties(getCurrentSavesDirectory().get(), (WorldProperties) worldInfo);
    if (dimensionId != null) {
        ((IMixinWorldInfo) worldInfo).setDimensionId(dimensionId);
    } else if (((IMixinWorldInfo) worldInfo).getDimensionId() == null || ((IMixinWorldInfo) worldInfo).getDimensionId() == Integer.MIN_VALUE || getWorldByDimensionId(((IMixinWorldInfo) worldInfo).getDimensionId()).isPresent()) {
        // DimensionID is null or 0 or the dimensionID is already assinged to a loaded world
        ((IMixinWorldInfo) worldInfo).setDimensionId(WorldManager.getNextFreeDimensionId());
    }
    ((WorldProperties) worldInfo).setGeneratorType(archetype.getGeneratorType());
    ((IMixinWorldInfo) worldInfo).getOrCreateWorldConfig().save();
    registerWorldProperties((WorldProperties) worldInfo);
    SpongeImpl.postEvent(SpongeEventFactory.createConstructWorldPropertiesEvent(Sponge.getCauseStackManager().getCurrentCause(), archetype, (WorldProperties) worldInfo));
    saveHandler.saveWorldInfoWithPlayer(worldInfo, SpongeImpl.getServer().getPlayerList().getHostPlayerData());
    return (WorldProperties) worldInfo;
}
Also used : ISaveHandler(net.minecraft.world.storage.ISaveHandler) IMixinWorldInfo(org.spongepowered.common.interfaces.world.IMixinWorldInfo) WorldInfo(net.minecraft.world.storage.WorldInfo) IMixinWorldInfo(org.spongepowered.common.interfaces.world.IMixinWorldInfo) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) WorldServer(net.minecraft.world.WorldServer) AnvilSaveHandler(net.minecraft.world.chunk.storage.AnvilSaveHandler) IMixinWorld(org.spongepowered.common.interfaces.world.IMixinWorld) World(net.minecraft.world.World) WorldSettings(net.minecraft.world.WorldSettings) IMixinWorldSettings(org.spongepowered.common.interfaces.world.IMixinWorldSettings) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Aggregations

ISaveHandler (net.minecraft.world.storage.ISaveHandler)5 WorldServer (net.minecraft.world.WorldServer)3 File (java.io.File)2 IOException (java.io.IOException)2 MinecraftServer (net.minecraft.server.MinecraftServer)2 WorldSettings (net.minecraft.world.WorldSettings)2 AnvilSaveHandler (net.minecraft.world.chunk.storage.AnvilSaveHandler)2 WorldInfo (net.minecraft.world.storage.WorldInfo)2 WorldProperties (org.spongepowered.api.world.storage.WorldProperties)2 IMixinWorldInfo (org.spongepowered.common.interfaces.world.IMixinWorldInfo)2 IMixinWorldServer (org.spongepowered.common.interfaces.world.IMixinWorldServer)2 IMixinWorldSettings (org.spongepowered.common.interfaces.world.IMixinWorldSettings)2 BiMap (com.google.common.collect.BiMap)1 HashBiMap (com.google.common.collect.HashBiMap)1 PostEntryTask (com.mraof.minestuck.util.PostEntryTask)1 Int2ObjectMap (it.unimi.dsi.fastutil.ints.Int2ObjectMap)1 Int2ObjectOpenHashMap (it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap)1 FileInputStream (java.io.FileInputStream)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1