Search in sources :

Example 1 with ClientboundSelectedTitlePacket

use of com.almuradev.almura.feature.title.network.ClientboundSelectedTitlePacket in project Almura by AlmuraDev.

the class ServerTitleManager method onClientConnectionEventJoin.

@Listener
public void onClientConnectionEventJoin(final ClientConnectionEvent.Join event, @Getter("getTargetEntity") final Player player) {
    // Clear everything out for joining player
    this.selectedTitles.remove(player.getUniqueId());
    // Send titles to joiner
    this.network.sendTo(player, new ClientboundTitlesRegistryPacket(this.titles.values().stream().filter(title -> {
        if (!title.isHidden()) {
            return true;
        }
        return player.hasPermission(Almura.ID + ".title.admin");
    }).collect(Collectors.toSet())));
    // Send selected titles to joiner
    this.network.sendTo(player, new ClientboundSelectedTitleBulkPacket(this.selectedTitles.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, v -> v.getValue().getId()))));
    // Cache available titles for the joiner
    this.calculateAvailableTitlesFor(player);
    // Send joiner available titles (to cache)
    this.getAvailableTitlesFor(player).ifPresent(availableTitles -> this.network.sendTo(player, new ClientboundAvailableTitlesResponsePacket(availableTitles)));
    // Query database for selected title for joiner
    this.scheduler.createTaskBuilder().async().execute(() -> {
        try (final DSLContext context = this.databaseManager.createContext(true)) {
            final TitleSelectRecord record = TitleQueries.createFetchSelectedTitleFor(player.getUniqueId()).build(context).keepStatement(false).fetchOne();
            String titleId = "lostsoul";
            if (record != null && !record.getTitle().isEmpty()) {
                titleId = record.getTitle();
                System.err.println("[ServerTitleManager]: Assigning player configured title: [" + titleId + "] for player: [" + player.getName() + "].");
            } else {
                // The following are setup as fallback titles when a record doesn't exist in the DB for the player.
                if (player.hasPermission("almura.title.soldier"))
                    titleId = "soldier";
                if (player.hasPermission("almura.title.survivor"))
                    titleId = "survivor";
                if (player.hasPermission("almura.title.citizen"))
                    titleId = "citizen";
                if (player.hasPermission("almura.title.explorer"))
                    titleId = "explorer";
                if (player.hasPermission("almura.title.pioneer"))
                    titleId = "pioneer";
                if (player.hasPermission("almura.title.architect"))
                    titleId = "architect";
                if (player.hasPermission("almura.title.ancient"))
                    titleId = "ancient";
                // The following debug was left in place so Dockter can see how many of these are happening at the server console.
                System.err.println("[ServerTitleManager]: Assigning fallback title: [" + titleId + "] for player: [" + player.getName() + "].");
            }
            final String finalizedTitleId = titleId;
            this.scheduler.createTaskBuilder().execute(() -> {
                final Title selectedTitle = this.getTitle(finalizedTitleId).orElse(null);
                if (this.verifySelectedTitle(player, selectedTitle)) {
                    this.selectedTitles.put(player.getUniqueId(), selectedTitle);
                    // Send everyone joiner's selected title
                    this.network.sendToAll(new ClientboundSelectedTitlePacket(player.getUniqueId(), finalizedTitleId));
                }
            }).submit(this.container);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }).submit(this.container);
}
Also used : Almura(com.almuradev.almura.Almura) Results(org.jooq.Results) Getter(org.spongepowered.api.event.filter.Getter) GameStartingServerEvent(org.spongepowered.api.event.game.state.GameStartingServerEvent) HashMap(java.util.HashMap) Singleton(javax.inject.Singleton) HashSet(java.util.HashSet) Inject(javax.inject.Inject) DatabaseManager(com.almuradev.almura.shared.database.DatabaseManager) ClientboundSelectedTitlePacket(com.almuradev.almura.feature.title.network.ClientboundSelectedTitlePacket) SQLException(java.sql.SQLException) Text(org.spongepowered.api.text.Text) Map(java.util.Map) DSLContext(org.jooq.DSLContext) GameState(org.spongepowered.api.GameState) PluginContainer(org.spongepowered.api.plugin.PluginContainer) Nullable(javax.annotation.Nullable) Record(org.jooq.Record) Logger(org.slf4j.Logger) ClientboundTitlesRegistryPacket(com.almuradev.almura.feature.title.network.ClientboundTitlesRegistryPacket) Timestamp(java.sql.Timestamp) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Scheduler(org.spongepowered.api.scheduler.Scheduler) ServerNotificationManager(com.almuradev.almura.feature.notification.ServerNotificationManager) Sponge(org.spongepowered.api.Sponge) Set(java.util.Set) NetworkConfig(com.almuradev.almura.shared.network.NetworkConfig) UUID(java.util.UUID) ClientboundSelectedTitleBulkPacket(com.almuradev.almura.feature.title.network.ClientboundSelectedTitleBulkPacket) Collectors(java.util.stream.Collectors) TitleSelectRecord(com.almuradev.generated.title.tables.records.TitleSelectRecord) ClientConnectionEvent(org.spongepowered.api.event.network.ClientConnectionEvent) ChannelBinding(org.spongepowered.api.network.ChannelBinding) SerializationUtil(com.almuradev.almura.shared.util.SerializationUtil) TitleQueries(com.almuradev.almura.feature.title.database.TitleQueries) Witness(com.almuradev.core.event.Witness) ChannelId(org.spongepowered.api.network.ChannelId) Optional(java.util.Optional) ClientboundAvailableTitlesResponsePacket(com.almuradev.almura.feature.title.network.ClientboundAvailableTitlesResponsePacket) Player(org.spongepowered.api.entity.living.player.Player) Listener(org.spongepowered.api.event.Listener) ClientboundSelectedTitleBulkPacket(com.almuradev.almura.feature.title.network.ClientboundSelectedTitleBulkPacket) ClientboundSelectedTitlePacket(com.almuradev.almura.feature.title.network.ClientboundSelectedTitlePacket) TitleSelectRecord(com.almuradev.generated.title.tables.records.TitleSelectRecord) SQLException(java.sql.SQLException) DSLContext(org.jooq.DSLContext) ClientboundAvailableTitlesResponsePacket(com.almuradev.almura.feature.title.network.ClientboundAvailableTitlesResponsePacket) ClientboundTitlesRegistryPacket(com.almuradev.almura.feature.title.network.ClientboundTitlesRegistryPacket) HashMap(java.util.HashMap) Map(java.util.Map) Listener(org.spongepowered.api.event.Listener)

Example 2 with ClientboundSelectedTitlePacket

use of com.almuradev.almura.feature.title.network.ClientboundSelectedTitlePacket in project Almura by AlmuraDev.

the class ServerTitleManager method setSelectedTitleFor.

public void setSelectedTitleFor(final Player player, @Nullable final String titleId) {
    checkNotNull(player);
    if (titleId == null) {
        this.selectedTitles.remove(player.getUniqueId());
        this.network.sendToAll(new ClientboundSelectedTitlePacket(player.getUniqueId(), null));
        this.scheduler.createTaskBuilder().async().execute(() -> {
            try (final DSLContext context = this.databaseManager.createContext(true)) {
                TitleQueries.createDeleteSelectedTitleFor(player.getUniqueId()).build(context).keepStatement(false).execute();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }).submit(this.container);
        return;
    }
    final Title title = this.getTitle(titleId).orElse(null);
    if (title != null) {
        final Set<Title> availableTitles = this.getAvailableTitlesFor(player).orElse(null);
        if (availableTitles == null || !availableTitles.contains(title)) {
            this.network.sendTo(player, new ClientboundTitlesRegistryPacket(new HashSet<>(this.titles.values())));
            this.network.sendTo(player, new ClientboundAvailableTitlesResponsePacket(availableTitles));
        } else {
            final Title previousTitle = this.selectedTitles.remove(player.getUniqueId());
            this.selectedTitles.put(player.getUniqueId(), title);
            this.network.sendToAll(new ClientboundSelectedTitlePacket(player.getUniqueId(), titleId));
            this.scheduler.createTaskBuilder().async().execute(() -> {
                try (final DSLContext context = this.databaseManager.createContext(true)) {
                    if (previousTitle != null) {
                        TitleQueries.createDeleteSelectedTitleFor(player.getUniqueId()).build(context).keepStatement(false).execute();
                        TitleQueries.createInsertSelectedTitleHistoryFor(player.getUniqueId(), previousTitle.getId()).build(context).keepStatement(false).execute();
                    }
                    TitleQueries.createInsertSelectedTitleFor(player.getUniqueId(), titleId).build(context).keepStatement(false).execute();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }).submit(this.container);
        }
    }
}
Also used : ClientboundSelectedTitlePacket(com.almuradev.almura.feature.title.network.ClientboundSelectedTitlePacket) SQLException(java.sql.SQLException) DSLContext(org.jooq.DSLContext) ClientboundAvailableTitlesResponsePacket(com.almuradev.almura.feature.title.network.ClientboundAvailableTitlesResponsePacket) ClientboundTitlesRegistryPacket(com.almuradev.almura.feature.title.network.ClientboundTitlesRegistryPacket) HashSet(java.util.HashSet)

Aggregations

ClientboundAvailableTitlesResponsePacket (com.almuradev.almura.feature.title.network.ClientboundAvailableTitlesResponsePacket)2 ClientboundSelectedTitlePacket (com.almuradev.almura.feature.title.network.ClientboundSelectedTitlePacket)2 ClientboundTitlesRegistryPacket (com.almuradev.almura.feature.title.network.ClientboundTitlesRegistryPacket)2 SQLException (java.sql.SQLException)2 HashSet (java.util.HashSet)2 DSLContext (org.jooq.DSLContext)2 Almura (com.almuradev.almura.Almura)1 ServerNotificationManager (com.almuradev.almura.feature.notification.ServerNotificationManager)1 TitleQueries (com.almuradev.almura.feature.title.database.TitleQueries)1 ClientboundSelectedTitleBulkPacket (com.almuradev.almura.feature.title.network.ClientboundSelectedTitleBulkPacket)1 DatabaseManager (com.almuradev.almura.shared.database.DatabaseManager)1 NetworkConfig (com.almuradev.almura.shared.network.NetworkConfig)1 SerializationUtil (com.almuradev.almura.shared.util.SerializationUtil)1 Witness (com.almuradev.core.event.Witness)1 TitleSelectRecord (com.almuradev.generated.title.tables.records.TitleSelectRecord)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Timestamp (java.sql.Timestamp)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Optional (java.util.Optional)1