use of org.spongepowered.common.interfaces.IMixinMinecraftServer in project SpongeCommon by SpongePowered.
the class WorldManager method createWorldFromProperties.
private static WorldServer createWorldFromProperties(int dimensionId, ISaveHandler saveHandler, WorldInfo worldInfo, @Nullable WorldSettings worldSettings) {
final MinecraftServer server = SpongeImpl.getServer();
final WorldServer worldServer = new WorldServer(server, saveHandler, worldInfo, dimensionId, server.profiler);
worldServer.init();
// WorldSettings is only non-null here if this is a newly generated WorldInfo and therefore we need to initialize to calculate spawn.
if (worldSettings != null) {
worldServer.initialize(worldSettings);
}
worldServer.addEventListener(new ServerWorldEventHandler(server, worldServer));
// This code changes from Mojang's to account for per-world API-set GameModes.
if (!server.isSinglePlayer() && worldServer.getWorldInfo().getGameType().equals(GameType.NOT_SET)) {
worldServer.getWorldInfo().setGameType(server.getGameType());
}
worldByDimensionId.put(dimensionId, worldServer);
weakWorldByWorld.put(worldServer, worldServer);
((IMixinMinecraftServer) SpongeImpl.getServer()).putWorldTickTimes(dimensionId, new long[100]);
((IMixinChunkProviderServer) worldServer.getChunkProvider()).setForceChunkRequests(true);
WorldManager.reorderWorldsVanillaFirst();
SpongeImpl.postEvent(SpongeEventFactory.createLoadWorldEvent(Sponge.getCauseStackManager().getCurrentCause(), (org.spongepowered.api.world.World) worldServer));
((IMixinMinecraftServer) server).prepareSpawnArea(worldServer);
((IMixinChunkProviderServer) worldServer.getChunkProvider()).setForceChunkRequests(false);
return worldServer;
}
use of org.spongepowered.common.interfaces.IMixinMinecraftServer in project SpongeCommon by SpongePowered.
the class WorldManager method unloadWorld.
// TODO Result
public static boolean unloadWorld(WorldServer worldServer, boolean checkConfig) {
checkNotNull(worldServer);
final MinecraftServer server = SpongeImpl.getServer();
// Likely leaked, don't want to drop leaked world data
if (!worldByDimensionId.containsValue(worldServer)) {
return false;
}
// Vanilla sometimes doesn't remove player entities from world first
if (server.isServerRunning()) {
if (!worldServer.playerEntities.isEmpty()) {
return false;
}
// We only check config if base game wants to unload world. If mods/plugins say unload, we unload
if (checkConfig) {
if (((WorldProperties) worldServer.getWorldInfo()).doesKeepSpawnLoaded()) {
return false;
}
}
}
try (PhaseContext<?> context = GeneralPhase.State.WORLD_UNLOAD.createPhaseContext().source(worldServer).buildAndSwitch()) {
if (SpongeImpl.postEvent(SpongeEventFactory.createUnloadWorldEvent(Sponge.getCauseStackManager().getCurrentCause(), (org.spongepowered.api.world.World) worldServer))) {
return false;
}
final IMixinWorldServer mixinWorldServer = (IMixinWorldServer) worldServer;
final int dimensionId = mixinWorldServer.getDimensionId();
try {
// Don't save if server is stopping to avoid duplicate saving.
if (server.isServerRunning()) {
saveWorld(worldServer, true);
}
mixinWorldServer.getActiveConfig().save();
} catch (MinecraftException e) {
e.printStackTrace();
} finally {
worldByDimensionId.remove(dimensionId);
weakWorldByWorld.remove(worldServer);
((IMixinMinecraftServer) server).removeWorldTickTimes(dimensionId);
SpongeImpl.getLogger().info("Unloading world [{}] (DIM{})", worldServer.getWorldInfo().getWorldName(), dimensionId);
reorderWorldsVanillaFirst();
}
if (!server.isServerRunning()) {
unregisterDimension(dimensionId);
}
}
return true;
}
use of org.spongepowered.common.interfaces.IMixinMinecraftServer 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;
}
Aggregations