Search in sources :

Example 1 with GameProfileCache

use of org.spongepowered.api.profile.GameProfileCache in project LanternServer by LanternPowered.

the class LanternServer method shutdown.

@SuppressWarnings("deprecation")
@Override
public void shutdown(Text kickMessage) {
    checkNotNull(kickMessage, "kickMessage");
    if (this.shuttingDown) {
        return;
    }
    this.shuttingDown = true;
    // Stop the console
    this.consoleManager.shutdown();
    final Cause gameCause = Cause.of(EventContext.empty(), this.game);
    this.game.postGameStateChange(SpongeEventFactory.createGameStoppingServerEvent(gameCause));
    // Debug a message
    this.logger.info("Stopping the server... ({})", LanternTexts.toLegacy(kickMessage));
    // Kick all the online players
    getOnlinePlayers().forEach(player -> ((LanternPlayer) player).getConnection().disconnect(kickMessage));
    // Stop the network servers - starts the shutdown process
    // It may take a second or two for Netty to totally clean up
    this.networkManager.shutdown();
    if (this.queryServer != null) {
        this.queryServer.shutdown();
    }
    if (this.rconServer != null) {
        this.rconServer.shutdown();
    }
    // Stop the world manager
    this.worldManager.shutdown();
    // Shutdown the executor
    this.executor.shutdown();
    // Stop the async scheduler
    this.game.getScheduler().shutdownAsyncScheduler(10, TimeUnit.SECONDS);
    final Collection<ProviderRegistration<?>> serviceRegistrations;
    try {
        final ServiceManager serviceManager = this.game.getServiceManager();
        checkState(serviceManager instanceof SimpleServiceManager || serviceManager instanceof LanternServiceManager);
        final Field field = (serviceManager instanceof SimpleServiceManager ? SimpleServiceManager.class : LanternServiceManager.class).getDeclaredField("providers");
        field.setAccessible(true);
        // noinspection unchecked
        final Map<Class<?>, ProviderRegistration<?>> map = (Map<Class<?>, ProviderRegistration<?>>) field.get(serviceManager);
        serviceRegistrations = map.values();
    } catch (NoSuchFieldException | IllegalAccessException e) {
        throw new IllegalStateException(e);
    }
    // Close all the services if possible
    serviceRegistrations.forEach(provider -> {
        final Object service = provider.getProvider();
        if (service instanceof CloseableService) {
            try {
                ((CloseableService) service).close();
            } catch (Exception e) {
                this.logger.error("A error occurred while closing the {}.", provider.getService().getName(), e);
            }
        }
    });
    // Shutdown the game profile manager
    this.game.getGameProfileManager().getDefaultCache().save();
    final GameProfileCache cache = this.game.getGameProfileManager().getCache();
    if (cache instanceof CloseableService) {
        try {
            ((CloseableService) cache).close();
        } catch (Exception e) {
            this.logger.error("A error occurred while closing the GameProfileCache.", e);
        }
    }
    try {
        this.game.getOpsConfig().save();
    } catch (IOException e) {
        this.logger.error("A error occurred while saving the ops config.", e);
    }
    this.game.postGameStateChange(SpongeEventFactory.createGameStoppedServerEvent(gameCause));
    this.game.postGameStateChange(SpongeEventFactory.createGameStoppingEvent(gameCause));
    this.game.postGameStateChange(SpongeEventFactory.createGameStoppedEvent(gameCause));
    // Wait for a while and terminate any rogue threads
    new ShutdownMonitorThread().start();
}
Also used : LanternServiceManager(org.lanternpowered.server.service.LanternServiceManager) GameProfileCache(org.spongepowered.api.profile.GameProfileCache) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) BindException(java.net.BindException) IOException(java.io.IOException) Field(java.lang.reflect.Field) CloseableService(org.lanternpowered.server.service.CloseableService) LanternServiceManager(org.lanternpowered.server.service.LanternServiceManager) SimpleServiceManager(org.spongepowered.api.service.SimpleServiceManager) ServiceManager(org.spongepowered.api.service.ServiceManager) Cause(org.spongepowered.api.event.cause.Cause) ShutdownMonitorThread(org.lanternpowered.server.util.ShutdownMonitorThread) ProviderRegistration(org.spongepowered.api.service.ProviderRegistration) SimpleServiceManager(org.spongepowered.api.service.SimpleServiceManager) Map(java.util.Map) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Example 2 with GameProfileCache

use of org.spongepowered.api.profile.GameProfileCache in project LanternServer by LanternPowered.

the class HandlerLoginFinish method handle.

@Override
public void handle(NetworkContext context, MessageLoginInFinish message) {
    final LanternGameProfile gameProfile = message.getGameProfile();
    final NetworkSession session = context.getSession();
    int compressionThreshold = Lantern.getGame().getGlobalConfig().getNetworkCompressionThreshold();
    if (compressionThreshold != -1) {
        session.sendWithFuture(new MessageLoginOutSetCompression(compressionThreshold)).addListener(future -> context.getChannel().pipeline().replace(NetworkSession.COMPRESSION, NetworkSession.COMPRESSION, new MessageCompressionHandler(compressionThreshold)));
    } else {
        // Remove the compression handler placeholder
        context.getChannel().pipeline().remove(NetworkSession.COMPRESSION);
    }
    final GameProfileCache gameProfileCache = Lantern.getGame().getGameProfileManager().getCache();
    // Store the old profile temporarily
    gameProfileCache.getById(gameProfile.getUniqueId()).ifPresent(profile -> context.getChannel().attr(NetworkSession.PREVIOUS_GAME_PROFILE).set(profile));
    // Cache the new profile
    gameProfileCache.add(gameProfile, true, (Instant) null);
    session.sendWithFuture(new MessageLoginOutSuccess(gameProfile.getUniqueId(), gameProfile.getName().get())).addListener(future -> {
        session.setGameProfile(gameProfile);
        session.setProtocolState(ProtocolState.FORGE_HANDSHAKE);
        session.messageReceived(new MessageForgeHandshakeInStart());
    });
}
Also used : MessageCompressionHandler(org.lanternpowered.server.network.pipeline.MessageCompressionHandler) MessageForgeHandshakeInStart(org.lanternpowered.server.network.forge.message.type.handshake.MessageForgeHandshakeInStart) NetworkSession(org.lanternpowered.server.network.NetworkSession) LanternGameProfile(org.lanternpowered.server.profile.LanternGameProfile) GameProfileCache(org.spongepowered.api.profile.GameProfileCache) MessageLoginOutSetCompression(org.lanternpowered.server.network.vanilla.message.type.login.MessageLoginOutSetCompression) MessageLoginOutSuccess(org.lanternpowered.server.network.vanilla.message.type.login.MessageLoginOutSuccess)

Aggregations

GameProfileCache (org.spongepowered.api.profile.GameProfileCache)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 BindException (java.net.BindException)1 Map (java.util.Map)1 LanternPlayer (org.lanternpowered.server.entity.living.player.LanternPlayer)1 NetworkSession (org.lanternpowered.server.network.NetworkSession)1 MessageForgeHandshakeInStart (org.lanternpowered.server.network.forge.message.type.handshake.MessageForgeHandshakeInStart)1 MessageCompressionHandler (org.lanternpowered.server.network.pipeline.MessageCompressionHandler)1 MessageLoginOutSetCompression (org.lanternpowered.server.network.vanilla.message.type.login.MessageLoginOutSetCompression)1 MessageLoginOutSuccess (org.lanternpowered.server.network.vanilla.message.type.login.MessageLoginOutSuccess)1 LanternGameProfile (org.lanternpowered.server.profile.LanternGameProfile)1 CloseableService (org.lanternpowered.server.service.CloseableService)1 LanternServiceManager (org.lanternpowered.server.service.LanternServiceManager)1 ShutdownMonitorThread (org.lanternpowered.server.util.ShutdownMonitorThread)1 Cause (org.spongepowered.api.event.cause.Cause)1 ProviderRegistration (org.spongepowered.api.service.ProviderRegistration)1 ServiceManager (org.spongepowered.api.service.ServiceManager)1 SimpleServiceManager (org.spongepowered.api.service.SimpleServiceManager)1