use of org.lanternpowered.server.event.LanternCauseStack in project LanternServer by LanternPowered.
the class LanternServer method start.
void start() throws IOException {
final GlobalConfig globalConfig = this.game.getGlobalConfig();
// Enable the query server if needed
if (globalConfig.isQueryEnabled()) {
this.queryServer = new QueryServer(this.game, globalConfig.getShowPluginsToQuery());
}
// Enable the rcon server if needed
if (globalConfig.isRconEnabled()) {
this.rconServer = new RconServer(globalConfig.getRconPassword());
this.game.getServiceManager().setProvider(this.pluginContainer, RconService.class, this.rconServer);
}
if (globalConfig.getProxyType() == ProxyType.NONE && !globalConfig.isOnlineMode()) {
this.logger.warn("It is not recommend to run the server in offline mode, this allows people to");
this.logger.warn("choose any username they want. The server does will use the account attached");
this.logger.warn("to the username, it doesn't care if it's in offline mode, this will only");
this.logger.warn("disable the authentication and allow non registered usernames to be used.");
}
this.consoleManager.start();
try {
bind();
} catch (BindException e) {
// descriptive bind error messages
this.logger.error("The server could not bind to the requested address.");
if (e.getMessage().startsWith("Cannot assign requested address")) {
this.logger.error("The 'server.ip' in your global.conf file may not be valid.");
this.logger.error("Unless you are sure you need it, try removing it.");
this.logger.error(e.toString());
} else if (e.getMessage().startsWith("Address already in use")) {
this.logger.error("The address was already in use. Check that no server is");
this.logger.error("already running on that port. If needed, try killing all");
this.logger.error("Java processes using Task Manager or similar.");
this.logger.error(e.toString());
} else {
this.logger.error("An unknown bind error has occurred.", e);
}
System.exit(1);
return;
}
bindQuery();
bindRcon();
this.logger.info("Ready for connections.");
this.worldManager.init();
final Cause gameCause = Cause.of(EventContext.empty(), this.game);
this.game.postGameStateChange(SpongeEventFactory.createGameAboutToStartServerEvent(gameCause));
this.game.postGameStateChange(SpongeEventFactory.createGameStartingServerEvent(gameCause));
final GlobalConfig config = this.game.getGlobalConfig();
this.maxPlayers = config.getMaxPlayers();
this.onlineMode = config.isOnlineMode();
final Path faviconPath = Paths.get(config.getFavicon());
if (Files.exists(faviconPath)) {
try {
this.favicon = LanternFavicon.load(faviconPath);
} catch (IOException e) {
this.logger.error("Failed to load the favicon", e);
}
} else {
try {
this.favicon = LanternFavicon.load(getGame().getAssetManager().getAsset(InternalPluginsInfo.Implementation.IDENTIFIER, "icon/favicon.png").get().getUrl());
} catch (IOException e) {
throw new IllegalStateException("Failed to load the default favicon.");
}
}
final String resourcePackPath = config.getDefaultResourcePack();
if (!resourcePackPath.isEmpty()) {
try {
this.resourcePack = ResourcePacks.fromUri(URI.create(resourcePackPath));
} catch (FileNotFoundException e) {
this.logger.warn("Couldn't find a valid resource pack at the location: {}", resourcePackPath, e);
}
}
// Initialize a CauseStack on the server thread.
this.executor.submit(() -> CauseStack.set(new LanternCauseStack()));
// Start server ticking.
this.executor.scheduleAtFixedRate(() -> {
try {
pulse();
} catch (Exception e) {
this.logger.error("Error while pulsing", e);
}
}, 0, LanternGame.TICK_DURATION, TimeUnit.MILLISECONDS);
this.game.postGameStateChange(SpongeEventFactory.createGameStartedServerEvent(gameCause));
}
use of org.lanternpowered.server.event.LanternCauseStack in project LanternServer by LanternPowered.
the class LanternWorldManager method addWorldTask.
/**
* Adds the task for the world to tick it.
*/
private void addWorldTask(LanternWorld world) {
if (this.worldThreads.containsKey(world)) {
return;
}
final Thread thread = ThreadHelper.newFastThreadLocalThread(thread0 -> {
try {
// Initialize the world cause stack.
CauseStack.set(new LanternCauseStack());
while (!thread0.isInterrupted() && !this.tickEnd.isTerminated()) {
this.tickBegin.arriveAndAwaitAdvance();
try {
world.pulse();
} catch (Exception e) {
this.logger.error("Error occurred while pulsing the world {}", world.getName(), e);
} finally {
this.tickEnd.arriveAndAwaitAdvance();
}
}
} finally {
this.tickBegin.arriveAndDeregister();
this.tickEnd.arriveAndDeregister();
}
}, "world-" + world.getName());
this.worldThreads.put(world, thread);
this.tickBegin.register();
this.tickEnd.register();
thread.start();
}
Aggregations