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);
}
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);
}
}
}
Aggregations