use of org.spongepowered.common.interfaces.world.IMixinWorldInfo in project SpongeCommon by SpongePowered.
the class WorldManager method renameWorld.
public static Optional<WorldProperties> renameWorld(WorldProperties worldProperties, String newName) {
checkNotNull(worldProperties);
checkNotNull(newName);
checkState(!worldByDimensionId.containsKey(((IMixinWorldInfo) worldProperties).getDimensionId()), "World is still loaded!");
final Path oldWorldFolder = getCurrentSavesDirectory().get().resolve(worldProperties.getWorldName());
final Path newWorldFolder = oldWorldFolder.resolveSibling(newName);
if (Files.exists(newWorldFolder)) {
return Optional.empty();
}
try {
Files.move(oldWorldFolder, newWorldFolder);
} catch (IOException e) {
return Optional.empty();
}
unregisterWorldProperties(worldProperties, false);
final WorldInfo info = new WorldInfo((WorldInfo) worldProperties);
info.setWorldName(newName);
// As we are moving a world, we want to move the dimension ID and UUID with the world to ensure
// plugins and Sponge do not break.
((IMixinWorldInfo) info).setUniqueId(worldProperties.getUniqueId());
if (((IMixinWorldInfo) worldProperties).getDimensionId() != null) {
((IMixinWorldInfo) info).setDimensionId(((IMixinWorldInfo) worldProperties).getDimensionId());
}
((IMixinWorldInfo) info).createWorldConfig();
new AnvilSaveHandler(WorldManager.getCurrentSavesDirectory().get().toFile(), newName, true, SpongeImpl.getDataFixer()).saveWorldInfo(info);
registerWorldProperties((WorldProperties) info);
return Optional.of((WorldProperties) info);
}
use of org.spongepowered.common.interfaces.world.IMixinWorldInfo in project SpongeCommon by SpongePowered.
the class WorldManager method copyWorld.
public static CompletableFuture<Optional<WorldProperties>> copyWorld(WorldProperties worldProperties, String copyName) {
checkArgument(worldPropertiesByFolderName.containsKey(worldProperties.getWorldName()), "World properties not registered!");
checkArgument(!worldPropertiesByFolderName.containsKey(copyName), "Destination world name already is registered!");
final WorldInfo info = (WorldInfo) worldProperties;
final WorldServer worldServer = worldByDimensionId.get(((IMixinWorldInfo) info).getDimensionId().intValue());
if (worldServer != null) {
try {
saveWorld(worldServer, true);
} catch (MinecraftException e) {
throw new RuntimeException(e);
}
((IMixinMinecraftServer) SpongeImpl.getServer()).setSaveEnabled(false);
}
final CompletableFuture<Optional<WorldProperties>> future = SpongeImpl.getScheduler().submitAsyncTask(new CopyWorldTask(info, copyName));
if (worldServer != null) {
// World was loaded
future.thenRun(() -> ((IMixinMinecraftServer) SpongeImpl.getServer()).setSaveEnabled(true));
}
return future;
}
use of org.spongepowered.common.interfaces.world.IMixinWorldInfo in project SpongeForge by SpongePowered.
the class MixinDimensionManager method initDimension.
/**
* @author Zidane - June 2nd, 2016
* @reason Forge's initDimension is very different from Sponge's multi-world. We basically rig it into our system so mods work.
* @param dim The dimension to load
*/
@Overwrite
public static void initDimension(int dim) {
// World is already loaded, bail
if (WorldManager.getWorldByDimensionId(dim).isPresent()) {
return;
}
if (dim == 0) {
throw new RuntimeException("Attempt made to initialize overworld!");
}
WorldManager.getWorldByDimensionId(0).orElseThrow(() -> new RuntimeException("Attempt made to initialize " + "dimension before overworld is loaded!"));
DimensionType dimensionType = WorldManager.getDimensionType(dim).orElse(null);
if (dimensionType == null) {
SpongeImpl.getLogger().warn("Attempt made to initialize dimension id {} which isn't registered!" + ", falling back to overworld.", dim);
return;
}
final WorldProvider provider = dimensionType.createDimension();
// make sure to set the dimension id to avoid getting a null save folder
provider.setDimension(dim);
String worldFolder = WorldManager.getWorldFolderByDimensionId(dim).orElse(provider.getSaveFolder());
WorldProperties properties = WorldManager.getWorldProperties(worldFolder).orElse(null);
final Path worldPath = WorldManager.getCurrentSavesDirectory().get().resolve(worldFolder);
if (properties == null || !Files.isDirectory(worldPath)) {
if (properties != null) {
WorldManager.unregisterWorldProperties(properties, false);
}
String modId = StaticMixinForgeHelper.getModIdFromClass(provider.getClass());
WorldArchetype archetype = Sponge.getRegistry().getType(WorldArchetype.class, modId + ":" + dimensionType.getName().toLowerCase()).orElse(null);
if (archetype == null) {
final WorldArchetype.Builder builder = WorldArchetype.builder().dimension((org.spongepowered.api.world.DimensionType) (Object) dimensionType).keepsSpawnLoaded(dimensionType.shouldLoadSpawn());
archetype = builder.build(modId + ":" + dimensionType.getName().toLowerCase(), dimensionType.getName());
}
IMixinWorldSettings worldSettings = (IMixinWorldSettings) archetype;
worldSettings.setDimensionType((org.spongepowered.api.world.DimensionType) (Object) dimensionType);
worldSettings.setLoadOnStartup(false);
properties = WorldManager.createWorldProperties(worldFolder, archetype, dim);
((IMixinWorldInfo) properties).setDimensionId(dim);
((IMixinWorldInfo) properties).setIsMod(true);
}
if (!properties.isEnabled()) {
return;
}
Optional<WorldServer> optWorld = WorldManager.loadWorld(properties);
if (!optWorld.isPresent()) {
SpongeImpl.getLogger().error("Could not load world [{}]!", properties.getWorldName());
}
}
use of org.spongepowered.common.interfaces.world.IMixinWorldInfo in project SpongeCommon by SpongePowered.
the class WorldManager method loadWorld.
private static Optional<WorldServer> loadWorld(String worldName, @Nullable ISaveHandler saveHandler, @Nullable WorldProperties properties) {
checkNotNull(worldName);
final Path currentSavesDir = WorldManager.getCurrentSavesDirectory().orElseThrow(() -> new IllegalStateException("Attempt " + "made to load world too early!"));
final MinecraftServer server = SpongeImpl.getServer();
final Optional<WorldServer> optExistingWorldServer = getWorld(worldName);
if (optExistingWorldServer.isPresent()) {
return optExistingWorldServer;
}
if (!server.getAllowNether()) {
SpongeImpl.getLogger().error("Unable to load world [{}]. Multi-world is disabled via [allow-nether] in [server.properties].", worldName);
return Optional.empty();
}
final Path worldFolder = currentSavesDir.resolve(worldName);
if (!Files.isDirectory(worldFolder)) {
SpongeImpl.getLogger().error("Unable to load world [{}]. We cannot find its folder under [{}].", worldFolder, currentSavesDir);
return Optional.empty();
}
if (saveHandler == null) {
saveHandler = new AnvilSaveHandler(currentSavesDir.toFile(), worldName, true, SpongeImpl.getDataFixer());
}
// We weren't given a properties, see if one is cached
if (properties == null) {
properties = (WorldProperties) saveHandler.loadWorldInfo();
// We tried :'(
if (properties == null) {
SpongeImpl.getLogger().error("Unable to load world [{}]. No world properties was found!", worldName);
return Optional.empty();
}
}
if (((IMixinWorldInfo) properties).getDimensionId() == null || ((IMixinWorldInfo) properties).getDimensionId() == Integer.MIN_VALUE) {
((IMixinWorldInfo) properties).setDimensionId(getNextFreeDimensionId());
}
setUuidOnProperties(getCurrentSavesDirectory().get(), properties);
registerWorldProperties(properties);
final WorldInfo worldInfo = (WorldInfo) properties;
((IMixinWorldInfo) worldInfo).createWorldConfig();
// check if enabled
if (!((WorldProperties) worldInfo).isEnabled()) {
SpongeImpl.getLogger().error("Unable to load world [{}]. It is disabled.", worldName);
return Optional.empty();
}
final int dimensionId = ((IMixinWorldInfo) properties).getDimensionId();
registerDimension(dimensionId, (DimensionType) (Object) properties.getDimensionType());
registerDimensionPath(dimensionId, worldFolder);
SpongeImpl.getLogger().info("Loading world [{}] ({})", properties.getWorldName(), getDimensionType(dimensionId).get().getName());
final WorldServer worldServer = createWorldFromProperties(dimensionId, saveHandler, (WorldInfo) properties, new WorldSettings((WorldInfo) properties));
// Set the worlds on the Minecraft server
reorderWorldsVanillaFirst();
return Optional.of(worldServer);
}
use of org.spongepowered.common.interfaces.world.IMixinWorldInfo in project SpongeCommon by SpongePowered.
the class WorldManager method createWorldInfoFromSettings.
public static WorldInfo createWorldInfoFromSettings(Path currentSaveRoot, org.spongepowered.api.world.DimensionType dimensionType, int dimensionId, String worldFolderName, WorldSettings worldSettings, String generatorOptions) {
final MinecraftServer server = SpongeImpl.getServer();
worldSettings.setGeneratorOptions(generatorOptions);
((IMixinWorldSettings) (Object) worldSettings).setDimensionType(dimensionType);
((IMixinWorldSettings) (Object) worldSettings).setGenerateSpawnOnLoad(((IMixinDimensionType) dimensionType).shouldGenerateSpawnOnLoad());
final WorldInfo worldInfo = new WorldInfo(worldSettings, worldFolderName);
setUuidOnProperties(dimensionId == 0 ? currentSaveRoot.getParent() : currentSaveRoot, (WorldProperties) worldInfo);
((IMixinWorldInfo) worldInfo).setDimensionId(dimensionId);
SpongeImpl.postEvent(SpongeEventFactory.createConstructWorldPropertiesEvent(Sponge.getCauseStackManager().getCurrentCause(), (WorldArchetype) (Object) worldSettings, (WorldProperties) worldInfo));
return worldInfo;
}
Aggregations