use of io.github.nucleuspowered.nucleus.internal.text.NucleusTextTemplateImpl in project Nucleus by NucleusPowered.
the class AFKHandler method onTick.
public void onTick() {
synchronized (lock) {
activity.forEach(u -> data.compute(u, ((uuid, afkData) -> afkData == null ? new AFKData(uuid) : updateActivity(uuid, afkData))));
activity.clear();
}
List<UUID> uuidList = Sponge.getServer().getOnlinePlayers().stream().map(Player::getUniqueId).collect(Collectors.toList());
// Remove all offline players.
Set<Map.Entry<UUID, AFKData>> entries = data.entrySet();
entries.removeIf(refactor -> !uuidList.contains(refactor.getKey()));
entries.stream().filter(x -> !x.getValue().cacheValid).forEach(x -> x.getValue().updateFromPermissions());
long now = System.currentTimeMillis();
// Check AFK status.
entries.stream().filter(x -> x.getValue().isKnownAfk && !x.getValue().willKick && x.getValue().timeToKick > 0).forEach(e -> {
if (now - e.getValue().lastActivityTime > e.getValue().timeToKick) {
// Kick them
e.getValue().willKick = true;
NucleusTextTemplateImpl message = config.getMessages().getKickMessage();
TextRepresentable t;
if (message == null || message.isEmpty()) {
t = Nucleus.getNucleus().getMessageProvider().getTextMessageWithTextFormat("afk.kickreason");
} else {
t = message;
}
final NucleusTextTemplateImpl messageToServer = config.getMessages().getOnKick();
Sponge.getServer().getPlayer(e.getKey()).ifPresent(player -> {
MessageChannel mc;
if (config.isBroadcastOnKick()) {
mc = MessageChannel.TO_ALL;
} else {
mc = MessageChannel.permission(this.afkPermissionHandler.getPermissionWithSuffix("notify"));
}
AFKEvents.Kick events = new AFKEvents.Kick(player, messageToServer.getForCommandSource(player), mc);
if (Sponge.getEventManager().post(events)) {
// Cancelled.
return;
}
Text toSend = t instanceof NucleusTextTemplateImpl ? ((NucleusTextTemplateImpl) t).getForCommandSource(player) : t.toText();
Sponge.getScheduler().createSyncExecutor(Nucleus.getNucleus()).execute(() -> player.kick(toSend));
events.getMessage().ifPresent(m -> events.getChannel().send(player, m, ChatTypes.SYSTEM));
});
}
});
// Check AFK status.
entries.stream().filter(x -> !x.getValue().isKnownAfk && x.getValue().timeToAfk > 0).forEach(e -> {
if (now - e.getValue().lastActivityTime > e.getValue().timeToAfk) {
Sponge.getServer().getPlayer(e.getKey()).ifPresent(this::setAfk);
}
});
}
use of io.github.nucleuspowered.nucleus.internal.text.NucleusTextTemplateImpl in project Nucleus by NucleusPowered.
the class ServerListListener method onServerListPing.
@Listener
public void onServerListPing(ClientPingServerEvent event, @Getter("getResponse") ClientPingServerEvent.Response response) {
if (this.config == null) {
try {
onReload();
} catch (Exception e) {
e.printStackTrace();
return;
}
}
if (this.config.isModifyServerList()) {
List<NucleusTextTemplateImpl> list = null;
Optional<Text> ott = plugin.getGeneralService().get(ServerListGeneralDataModule.class).getMessage();
if (ott.isPresent()) {
response.setDescription(ott.get());
} else {
if (Sponge.getServer().hasWhitelist() && !this.config.getWhitelist().isEmpty()) {
list = this.config.getWhitelist();
} else if (!this.config.getMessages().isEmpty()) {
list = this.config.getMessages();
}
if (list != null) {
NucleusTextTemplate template = list.get(this.random.nextInt(list.size()));
response.setDescription(template.getForCommandSource(Sponge.getServer().getConsole()));
}
}
}
if (this.config.isHidePlayerCount()) {
response.setHidePlayers(true);
} else if (this.config.isHideVanishedPlayers()) {
Collection<GameProfile> players = Sponge.getServer().getOnlinePlayers().stream().filter(x -> !x.get(Keys.VANISH).orElse(false)).map(User::getProfile).collect(Collectors.toList());
response.getPlayers().ifPresent(y -> {
y.getProfiles().clear();
y.getProfiles().addAll(players);
y.setOnline(players.size());
});
}
}
use of io.github.nucleuspowered.nucleus.internal.text.NucleusTextTemplateImpl in project Nucleus by NucleusPowered.
the class WhitelistServerListListener method onServerListPing.
@Listener(order = Order.LATE)
public void onServerListPing(ClientPingServerEvent event, @Getter("getResponse") ClientPingServerEvent.Response response) {
if (!Sponge.getServer().hasWhitelist()) {
return;
}
Optional<Text> ott = plugin.getGeneralService().get(ServerListGeneralDataModule.class).getMessage();
if (!ott.isPresent() && !this.config.getWhitelist().isEmpty()) {
List<NucleusTextTemplateImpl> list = this.config.getWhitelist();
if (list != null) {
NucleusTextTemplate template = list.get(this.random.nextInt(list.size()));
response.setDescription(template.getForCommandSource(Sponge.getServer().getConsole()));
}
}
}
use of io.github.nucleuspowered.nucleus.internal.text.NucleusTextTemplateImpl in project Nucleus by NucleusPowered.
the class MessageHandler method sendMessage.
@Override
public boolean sendMessage(CommandSource sender, CommandSource receiver, String message) {
// Message is about to be sent. Send the event out. If canceled, then that's that.
boolean isBlocked = false;
boolean isCancelled = Sponge.getEventManager().post(new InternalNucleusMessageEvent(sender, receiver, message));
if (isCancelled) {
sender.sendMessage(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("message.cancel"));
// Only continue to show Social Spy messages if the subject is muted.
if (!messageConfig.isShowMessagesInSocialSpyWhileMuted()) {
return false;
}
}
// What about msgtoggle?
if (receiver instanceof Player && !sender.hasPermission(this.msgToggleBypass) && ucl.get((Player) receiver).map(x -> !x.get(MessageUserDataModule.class).isMsgToggle()).orElse(false)) {
isCancelled = true;
isBlocked = true;
sender.sendMessage(Nucleus.getNucleus().getMessageProvider().getTextMessageWithTextFormat("message.blocked", Nucleus.getNucleus().getNameUtil().getName((Player) receiver)));
if (!messageConfig.isShowMessagesInSocialSpyWhileMuted()) {
return false;
}
}
// Social Spies.
final UUID uuidSender = getUUID(sender);
final UUID uuidReceiver = getUUID(receiver);
final Map<String, Object> variables = Maps.newHashMap();
variables.put("from", sender);
variables.put("to", receiver);
// Create the tokens.
Map<String, Function<CommandSource, Optional<Text>>> tokens = Maps.newHashMap();
tokens.put("from", cs -> getNameFromCommandSource(sender, textParsingUtils::addCommandToName));
tokens.put("to", cs -> getNameFromCommandSource(receiver, textParsingUtils::addCommandToName));
tokens.put("fromdisplay", cs -> getNameFromCommandSource(sender, textParsingUtils::addCommandToDisplayName));
tokens.put("todisplay", cs -> getNameFromCommandSource(receiver, textParsingUtils::addCommandToDisplayName));
Text tm = useMessage(sender, message);
if (!isCancelled) {
sender.sendMessage(constructMessage(sender, tm, messageConfig.getMessageSenderPrefix(), tokens, variables));
receiver.sendMessage(constructMessage(sender, tm, messageConfig.getMessageReceiverPrefix(), tokens, variables));
}
NucleusTextTemplateImpl prefix = messageConfig.getMessageSocialSpyPrefix();
if (isBlocked) {
prefix = NucleusTextTemplateFactory.createFromAmpersandString(messageConfig.getBlockedTag() + prefix.getRepresentation());
}
if (isCancelled) {
prefix = NucleusTextTemplateFactory.createFromAmpersandString(messageConfig.getMutedTag() + prefix.getRepresentation());
}
MessageConfig.Targets targets = messageConfig.spyOn();
if (sender instanceof Player && targets.isPlayer() || sender instanceof ConsoleSource && targets.isCustom() || targets.isCustom()) {
Set<CommandSource> lm = onlinePlayersCanSpyOn(!uuidSender.equals(Util.consoleFakeUUID) && !uuidReceiver.equals(Util.consoleFakeUUID), sender, receiver);
MessageChannel mc = MessageChannel.fixed(lm);
if (!mc.getMembers().isEmpty()) {
mc.send(constructMessage(sender, tm, prefix, tokens, variables));
}
}
// Add the UUIDs to the reply list - the receiver will now reply to the sender.
if (!isCancelled) {
messagesReceived.put(uuidReceiver, uuidSender);
}
return !isCancelled;
}
Aggregations