use of org.spongepowered.api.world.storage.WorldProperties 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;
}
use of org.spongepowered.api.world.storage.WorldProperties 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();
}
use of org.spongepowered.api.world.storage.WorldProperties 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;
}
use of org.spongepowered.api.world.storage.WorldProperties in project SpongeCommon by SpongePowered.
the class MixinWorldServer method onConstruct.
@Inject(method = "<init>", at = @At("RETURN"))
private void onConstruct(MinecraftServer server, ISaveHandler saveHandlerIn, WorldInfo info, int dimensionId, Profiler profilerIn, CallbackInfo callbackInfo) {
if (info == null) {
SpongeImpl.getLogger().warn("World constructed without a WorldInfo! This will likely cause problems. Subsituting dummy info.", new RuntimeException("Stack trace:"));
this.worldInfo = new WorldInfo(new WorldSettings(0, GameType.NOT_SET, false, false, WorldType.DEFAULT), "sponge$dummy_world");
}
// Checks to make sure no mod has changed our worldInfo and if so, reverts back to original.
// Mods such as FuturePack replace worldInfo with a custom one for separate world time.
// This change is not needed as all worlds use separate save handlers.
this.worldInfo = info;
this.timings = new WorldTimingsHandler((WorldServer) (Object) this);
this.dimensionId = dimensionId;
this.prevWeather = getWeather();
this.weatherStartTime = this.worldInfo.getWorldTotalTime();
((World) (Object) this).getWorldBorder().addListener(new PlayerBorderListener(this.getMinecraftServer(), dimensionId));
PortalAgentType portalAgentType = ((WorldProperties) this.worldInfo).getPortalAgentType();
if (!portalAgentType.equals(PortalAgentTypes.DEFAULT)) {
try {
this.worldTeleporter = (Teleporter) portalAgentType.getPortalAgentClass().getConstructor(new Class<?>[] { WorldServer.class }).newInstance(new Object[] { this });
} catch (Exception e) {
SpongeImpl.getLogger().log(Level.ERROR, "Could not create PortalAgent of type " + portalAgentType.getId() + " for world " + this.getName() + ": " + e.getMessage() + ". Falling back to default...");
}
}
// Turn on capturing
updateWorldGenerator();
// Need to set the active config before we call it.
this.chunkGCLoadThreshold = SpongeHooks.getActiveConfig((WorldServer) (Object) this).getConfig().getWorld().getChunkLoadThreadhold();
this.chunkGCTickInterval = this.getActiveConfig().getConfig().getWorld().getTickInterval();
this.weatherIceAndSnowEnabled = this.getActiveConfig().getConfig().getWorld().getWeatherIceAndSnow();
this.weatherThunderEnabled = this.getActiveConfig().getConfig().getWorld().getWeatherThunder();
this.updateEntityTick = 0;
this.mixinChunkProviderServer = ((IMixinChunkProviderServer) this.getChunkProvider());
this.setMemoryViewDistance(this.chooseViewDistanceValue(this.getActiveConfig().getConfig().getWorld().getViewDistance()));
}
use of org.spongepowered.api.world.storage.WorldProperties in project SpongeCommon by SpongePowered.
the class WorldRenameTest method onInit.
@Listener
public void onInit(GameInitializationEvent event) {
Sponge.getCommandManager().register(this, CommandSpec.builder().arguments(GenericArguments.world(worldKey)).executor((source, context) -> {
Sponge.getServer().unloadWorld(Sponge.getServer().getWorld(context.<WorldProperties>getOne(worldKey).get().getUniqueId()).orElseThrow(() -> new CommandException(Text.of(TextColors.RED, "That world doesn't seem to be loaded."))));
return CommandResult.success();
}).build(), "unloadworld");
Sponge.getCommandManager().register(this, CommandSpec.builder().arguments(new WorldParameter()).executor((source, context) -> {
Sponge.getServer().unloadWorld(Sponge.getServer().getWorld(context.<WorldProperties>getOne(worldKey).get().getUniqueId()).orElseThrow(() -> new CommandException(Text.of(TextColors.RED, "That world doesn't seem to be loaded."))));
return CommandResult.success();
}).build(), "loadworld");
Sponge.getCommandManager().register(this, CommandSpec.builder().arguments(new WorldParameter(), GenericArguments.string(newNameKey)).executor((source, context) -> {
// Name clashes should be handled by the impl.
WorldProperties newProperties = Sponge.getServer().renameWorld(context.<WorldProperties>getOne(worldKey).get(), context.<String>getOne(newNameKey).get()).orElseThrow(() -> new CommandException(Text.of(TextColors.RED, "The world was not renamed.")));
source.sendMessage(Text.of("The world was renamed to " + newProperties.getWorldName()));
return CommandResult.success();
}).build(), "renameworld");
Sponge.getCommandManager().register(this, CommandSpec.builder().arguments(GenericArguments.string(newNameKey)).executor((source, context) -> {
try {
Sponge.getServer().createWorldProperties(context.<String>getOne(newNameKey).get(), WorldArchetypes.OVERWORLD);
} catch (IOException e) {
throw new CommandException(Text.of(TextColors.RED, "Could not create the world."), e);
}
source.sendMessage(Text.of("The world was created"));
return CommandResult.success();
}).build(), "createworld");
Sponge.getCommandManager().register(this, CommandSpec.builder().arguments(new WorldParameter(x -> true)).executor((source, context) -> {
WorldProperties wp = context.<WorldProperties>getOne(worldKey).get();
source.sendMessage(Text.of("World: ", wp.getWorldName(), " - UUID: ", wp.getUniqueId(), " - Dim ID:", wp.getAdditionalProperties().getInt(DataQuery.of("SpongeData", "dimensionId")).map(Object::toString).orElse("unknown")));
return CommandResult.success();
}).build(), "worldinfo");
}
Aggregations