use of net.minecraft.server.MinecraftServer 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 net.minecraft.server.MinecraftServer 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 net.minecraft.server.MinecraftServer 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 net.minecraft.server.MinecraftServer in project Gaia-Dimension by Andromander.
the class GDGaiaPortal method changeDimension.
private void changeDimension(Entity toTeleport, int dimensionIn) {
if (!toTeleport.world.isRemote && !toTeleport.isDead) {
if (!net.minecraftforge.common.ForgeHooks.onTravelToDimension(toTeleport, dimensionIn))
return;
toTeleport.world.profiler.startSection("changeDimension");
MinecraftServer minecraftserver = toTeleport.getServer();
int i = toTeleport.dimension;
WorldServer worldserver = minecraftserver.getWorld(i);
WorldServer worldserver1 = minecraftserver.getWorld(dimensionIn);
toTeleport.dimension = dimensionIn;
if (i == 1 && dimensionIn == 1) {
worldserver1 = minecraftserver.getWorld(0);
toTeleport.dimension = 0;
}
toTeleport.world.removeEntity(toTeleport);
toTeleport.isDead = false;
toTeleport.world.profiler.startSection("reposition");
BlockPos blockpos;
if (dimensionIn == 1) {
blockpos = worldserver1.getSpawnCoordinate();
} else {
double d0 = toTeleport.posX;
double d1 = toTeleport.posZ;
double d2 = 8.0D;
d0 = (double) MathHelper.clamp((int) d0, -29999872, 29999872);
d1 = (double) MathHelper.clamp((int) d1, -29999872, 29999872);
float f = toTeleport.rotationYaw;
toTeleport.setLocationAndAngles(d0, toTeleport.posY, d1, 90.0F, 0.0F);
Teleporter teleporter = TeleporterGaia.getTeleporterForDim(minecraftserver, dimensionIn);
teleporter.placeInExistingPortal(toTeleport, f);
blockpos = new BlockPos(toTeleport);
}
worldserver.updateEntityWithOptionalForce(toTeleport, false);
toTeleport.world.profiler.endStartSection("reloading");
Entity entity = EntityList.newEntity(toTeleport.getClass(), worldserver1);
if (entity != null) {
entity.copyDataFromOld(toTeleport);
if (i == 1 && dimensionIn == 1) {
BlockPos blockpos1 = worldserver1.getTopSolidOrLiquidBlock(worldserver1.getSpawnPoint());
entity.moveToBlockPosAndAngles(blockpos1, entity.rotationYaw, entity.rotationPitch);
} else {
entity.setLocationAndAngles((double) blockpos.getX(), (double) blockpos.getY(), (double) blockpos.getZ(), entity.rotationYaw, entity.rotationPitch);
}
boolean flag = entity.forceSpawn;
entity.forceSpawn = true;
worldserver1.spawnEntity(entity);
entity.forceSpawn = flag;
worldserver1.updateEntityWithOptionalForce(entity, false);
}
toTeleport.isDead = true;
toTeleport.world.profiler.endSection();
worldserver.resetUpdateEntityTick();
worldserver1.resetUpdateEntityTick();
toTeleport.world.profiler.endSection();
}
}
use of net.minecraft.server.MinecraftServer in project minecolonies by Minecolonies.
the class ItemScanTool method saveStructure.
/**
* Scan the structure and save it to the disk.
*
* @param world Current world.
* @param from First corner.
* @param to Second corner.
* @param player causing this action.
*/
private static void saveStructure(@Nullable final World world, @Nullable final BlockPos from, @Nullable final BlockPos to, @NotNull final EntityPlayer player) {
//todo if on clientSide check if isRemote and if it is -> don't seed message, execute right away.
if (world == null || from == null || to == null) {
throw new IllegalArgumentException("Invalid method call, arguments can't be null. Contact a developer.");
}
final BlockPos blockpos = new BlockPos(Math.min(from.getX(), to.getX()), Math.min(from.getY(), to.getY()), Math.min(from.getZ(), to.getZ()));
final BlockPos blockpos1 = new BlockPos(Math.max(from.getX(), to.getX()), Math.max(from.getY(), to.getY()), Math.max(from.getZ(), to.getZ()));
final BlockPos size = blockpos1.subtract(blockpos).add(1, 1, 1);
final WorldServer worldserver = (WorldServer) world;
final MinecraftServer minecraftserver = world.getMinecraftServer();
final TemplateManager templatemanager = worldserver.getStructureTemplateManager();
final long currentMillis = System.currentTimeMillis();
final String currentMillisString = Long.toString(currentMillis);
final String fileName = "/minecolonies/scans/" + LanguageHandler.format("item.scepterSteel.scanFormat", "", currentMillisString + ".nbt");
final Template template = templatemanager.getTemplate(minecraftserver, new ResourceLocation(fileName));
template.takeBlocksFromWorld(world, blockpos, size, true, Blocks.STRUCTURE_VOID);
template.setAuthor(Constants.MOD_ID);
MineColonies.getNetwork().sendTo(new SaveScanMessage(template.writeToNBT(new NBTTagCompound()), currentMillis), (EntityPlayerMP) player);
}
Aggregations