use of com.almuradev.almura.feature.title.network.ClientboundAvailableTitlesResponsePacket 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.ClientboundAvailableTitlesResponsePacket 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);
}
}
}
use of com.almuradev.almura.feature.title.network.ClientboundAvailableTitlesResponsePacket in project Almura by AlmuraDev.
the class ServerTitleManager method loadTitles.
public boolean loadTitles() {
this.availableTitles.clear();
this.logger.info("Querying database for titles, please wait...");
this.scheduler.createTaskBuilder().async().execute(() -> {
try (final DSLContext context = this.databaseManager.createContext(true)) {
final Results results = TitleQueries.createFetchAllTitles().build(context).keepStatement(false).fetchMany();
final Map<String, Title> titles = new HashMap<>();
results.forEach(result -> {
for (Record record : result) {
final String id = record.getValue(com.almuradev.generated.title.tables.Title.TITLE.ID);
final Timestamp created = record.getValue(com.almuradev.generated.title.tables.Title.TITLE.CREATED);
final UUID creator = SerializationUtil.uniqueIdFromBytes(record.getValue(com.almuradev.generated.title.tables.Title.TITLE.CREATOR));
final String permission = record.getValue(com.almuradev.generated.title.tables.Title.TITLE.PERMISSION);
final boolean isHidden = record.getValue(com.almuradev.generated.title.tables.Title.TITLE.IS_HIDDEN);
final String content = record.getValue(com.almuradev.generated.title.tables.Title.TITLE.CONTENT);
titles.put(id, new Title(created, creator, id, permission, isHidden, content));
}
});
this.scheduler.createTaskBuilder().execute(() -> {
this.titles.clear();
if (!titles.isEmpty()) {
this.titles.putAll(titles);
}
this.logger.info("Loaded [{}] title(s).", this.titles.size());
// Re-send titles to everyone
this.network.sendToAll(new ClientboundTitlesRegistryPacket(this.titles.isEmpty() ? null : new HashSet<>(this.titles.values())));
// Re-calculate available titles
Sponge.getServer().getOnlinePlayers().forEach(player -> {
this.calculateAvailableTitlesFor(player);
this.verifySelectedTitle(player, null);
final Set<Title> availableTitles = this.getAvailableTitlesFor(player).orElse(null);
this.network.sendTo(player, new ClientboundAvailableTitlesResponsePacket(availableTitles));
});
// Send all selected titles out again as we've verified and corrected them in-case load changed
this.network.sendToAll(new ClientboundSelectedTitleBulkPacket(this.selectedTitles.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, v -> v.getValue().getId()))));
}).submit(this.container);
} catch (SQLException e) {
e.printStackTrace();
}
}).submit(this.container);
return true;
}
use of com.almuradev.almura.feature.title.network.ClientboundAvailableTitlesResponsePacket in project Almura by AlmuraDev.
the class ServerboundAvailableTitlesRequestPacketHandler method handleMessage.
@Override
public void handleMessage(ServerboundAvailableTitlesRequestPacket message, RemoteConnection connection, Platform.Type side) {
if (side.isServer() && connection instanceof PlayerConnection && PacketUtil.checkThreadAndEnqueue((MinecraftServer) Sponge.getServer(), message, this, connection, side)) {
final Player player = ((PlayerConnection) connection).getPlayer();
final Set<Title> availableTitles = this.titleManager.getAvailableTitlesFor(player).orElse(null);
if (availableTitles == null) {
// TODO Tell the player they have no available titles :'(
return;
} else {
this.network.sendTo(player, new ClientboundAvailableTitlesResponsePacket(availableTitles));
}
}
}
Aggregations