use of org.lanternpowered.server.network.message.Message in project LanternServer by LanternPowered.
the class NetworkSession method send.
/**
* Sends a array of {@link Message}s.
*
* @param messages The messages
*/
public void send(Message... messages) {
checkNotNull(messages, "messages");
checkArgument(messages.length != 0, "messages cannot be empty");
if (!this.channel.isActive()) {
return;
}
final ChannelPromise voidPromise = this.channel.voidPromise();
if (messages.length == 1) {
this.channel.writeAndFlush(messages[0], voidPromise);
} else {
final EventLoop eventLoop = this.channel.eventLoop();
if (eventLoop.inEventLoop()) {
for (Message message : messages) {
ReferenceCountUtil.retain(message);
this.channel.writeAndFlush(message, voidPromise);
}
} else {
// If there are more then one message, combine them inside the
// event loop to reduce overhead of wakeup calls and object creation
// Create a copy of the list, to avoid concurrent modifications
final List<Message> messages0 = ImmutableList.copyOf(messages);
messages0.forEach(ReferenceCountUtil::retain);
eventLoop.submit(() -> {
for (Message message0 : messages0) {
this.channel.writeAndFlush(message0, voidPromise);
}
});
}
}
}
use of org.lanternpowered.server.network.message.Message in project LanternServer by LanternPowered.
the class LanternScore method setScore.
@Override
public void setScore(int score) {
if (this.score == score) {
return;
}
this.score = score;
final Multimap<Scoreboard, Objective> scoreboards = HashMultimap.create();
for (Objective objective : this.objectives) {
for (Scoreboard scoreboard : ((LanternObjective) objective).scoreboards) {
scoreboards.put(scoreboard, objective);
}
}
if (!scoreboards.isEmpty()) {
final Map<Objective, Message> messages = new HashMap<>();
for (Map.Entry<Scoreboard, Objective> entry : scoreboards.entries()) {
((LanternScoreboard) entry.getKey()).sendToPlayers(() -> Collections.singletonList(messages.computeIfAbsent(entry.getValue(), obj -> new MessagePlayOutScoreboardScore.CreateOrUpdate(obj.getName(), this.legacyName, score))));
}
}
}
use of org.lanternpowered.server.network.message.Message in project LanternServer by LanternPowered.
the class LanternWorld method broadcast.
public void broadcast(Supplier<Message> message, @Nullable Predicate<LanternPlayer> filter) {
Set<LanternPlayer> players = this.players;
if (filter != null) {
players = players.stream().filter(filter).collect(Collectors.toSet());
}
if (players.isEmpty()) {
return;
}
final Message message0 = message.get();
players.forEach(player -> player.getConnection().send(message0));
}
use of org.lanternpowered.server.network.message.Message in project LanternServer by LanternPowered.
the class NetworkSession method sendWithFuture.
/**
* Sends a array of {@link Message}s and returns the {@link ChannelFuture}.
*
* @param messages The messages
* @return The channel future
*/
public ChannelFuture sendWithFuture(Message... messages) {
checkNotNull(messages, "messages");
checkArgument(messages.length != 0, "messages cannot be empty");
final ChannelPromise promise = this.channel.newPromise();
if (!this.channel.isActive()) {
return promise;
}
promise.addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
// there is only one message.
if (messages.length == 1) {
this.channel.writeAndFlush(messages[0], promise);
} else {
final EventLoop eventLoop = this.channel.eventLoop();
final ChannelPromise voidPromise = this.channel.voidPromise();
if (eventLoop.inEventLoop()) {
final int last = messages.length - 1;
for (int i = 0; i < last; i++) {
ReferenceCountUtil.retain(messages[i]);
this.channel.writeAndFlush(messages[i], voidPromise);
}
ReferenceCountUtil.retain(messages[last]);
this.channel.writeAndFlush(messages[last], promise);
} else {
// If there are more then one message, combine them inside the
// event loop to reduce overhead of wakeup calls and object creation
// Create a copy of the list, to avoid concurrent modifications
final List<Message> messages0 = ImmutableList.copyOf(messages);
messages0.forEach(ReferenceCountUtil::retain);
eventLoop.submit(() -> {
final Iterator<Message> it0 = messages0.iterator();
do {
final Message message0 = it0.next();
// Only use a normal channel promise for the last message
this.channel.writeAndFlush(message0, it0.hasNext() ? voidPromise : promise);
} while (it0.hasNext());
});
}
}
return promise;
}
use of org.lanternpowered.server.network.message.Message in project LanternServer by LanternPowered.
the class LanternScoreboard method createObjectiveInitMessages.
private List<Message> createObjectiveInitMessages(Objective objective) {
final List<Message> messages = new ArrayList<>();
messages.add(new MessagePlayOutScoreboardObjective.Create(objective.getName(), ((LanternObjective) objective).getLegacyDisplayName(), objective.getDisplayMode()));
for (Score score : ((LanternObjective) objective).scores.values()) {
messages.add(new MessagePlayOutScoreboardScore.CreateOrUpdate(objective.getName(), LanternTexts.toLegacy(score.getName()), score.getScore()));
}
return messages;
}
Aggregations