use of org.spongepowered.api.text.channel.MessageChannel 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 org.spongepowered.api.text.channel.MessageChannel in project Nucleus by NucleusPowered.
the class KickCommand method executeCommand.
@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
Player pl = args.<Player>getOne(player).get();
String r = args.<String>getOne(reason).orElse(plugin.getMessageProvider().getMessageWithFormat("command.kick.defaultreason"));
if (permissions.testSuffix(pl, "exempt.target", src, false)) {
throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.kick.exempt", pl.getName()));
}
pl.kick(TextSerializers.FORMATTING_CODE.deserialize(r));
MessageChannel mc = new PermissionMessageChannel(permissions.getPermissionWithSuffix("notify"));
mc.send(plugin.getMessageProvider().getTextMessageWithFormat("command.kick.message", pl.getName(), src.getName()));
mc.send(plugin.getMessageProvider().getTextMessageWithFormat("command.reason", r));
return CommandResult.success();
}
use of org.spongepowered.api.text.channel.MessageChannel in project Nucleus by NucleusPowered.
the class StaffChatCommand method executeCommand.
@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
Optional<String> toSend = args.getOne(message);
if (toSend.isPresent()) {
try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
frame.addContext(EventContexts.SHOULD_FORMAT_CHANNEL, StaffChatMessageChannel.getInstance().formatMessages());
if (src instanceof Player) {
Player pl = (Player) src;
frame.pushCause(pl);
frame.addContext(EventContextKeys.PLAYER_SIMULATED, pl.getProfile());
MessageChannel mc = pl.getMessageChannel();
pl.setMessageChannel(StaffChatMessageChannel.getInstance());
pl.simulateChat(TextParsingUtils.addUrls(toSend.get()), Sponge.getCauseStackManager().getCurrentCause());
pl.setMessageChannel(mc);
} else {
StaffChatMessageChannel.getInstance().send(src, TextParsingUtils.addUrls(toSend.get()), ChatTypes.CHAT);
}
return CommandResult.success();
}
}
if (!(src instanceof Player)) {
throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.staffchat.consoletoggle"));
}
Player player = (Player) src;
StaffChatTransientModule s = plugin.getUserDataManager().get(player).map(y -> y.getTransient(StaffChatTransientModule.class)).orElseGet(StaffChatTransientModule::new);
boolean result = !(src.getMessageChannel() instanceof StaffChatMessageChannel);
if (result) {
s.setPreviousMessageChannel(player.getMessageChannel());
src.setMessageChannel(StaffChatMessageChannel.getInstance());
} else {
src.setMessageChannel(s.getPreviousMessageChannel().orElse(MessageChannel.TO_ALL));
}
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.staffchat." + (result ? "on" : "off")));
return CommandResult.success();
}
use of org.spongepowered.api.text.channel.MessageChannel 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