Search in sources :

Example 1 with NucleusTextTemplateImpl

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);
        }
    });
}
Also used : AFKConfig(io.github.nucleuspowered.nucleus.modules.afk.config.AFKConfig) ChatTypes(org.spongepowered.api.text.chat.ChatTypes) Keys(org.spongepowered.api.data.key.Keys) TextRepresentable(org.spongepowered.api.text.TextRepresentable) NoExceptionAutoClosable(io.github.nucleuspowered.nucleus.api.util.NoExceptionAutoClosable) NucleusPlugin(io.github.nucleuspowered.nucleus.NucleusPlugin) Multimap(com.google.common.collect.Multimap) HashMultimap(com.google.common.collect.HashMultimap) Lists(com.google.common.collect.Lists) Text(org.spongepowered.api.text.Text) Duration(java.time.Duration) Map(java.util.Map) Task(org.spongepowered.api.scheduler.Task) Util(io.github.nucleuspowered.nucleus.Util) NucleusAFKService(io.github.nucleuspowered.nucleus.api.service.NucleusAFKService) ServiceChangeListener(io.github.nucleuspowered.nucleus.internal.permissions.ServiceChangeListener) PluginContainer(org.spongepowered.api.plugin.PluginContainer) AFKConfigAdapter(io.github.nucleuspowered.nucleus.modules.afk.config.AFKConfigAdapter) CommandPermissionHandler(io.github.nucleuspowered.nucleus.internal.CommandPermissionHandler) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) User(org.spongepowered.api.entity.living.player.User) CauseStackHelper(io.github.nucleuspowered.nucleus.util.CauseStackHelper) Sponge(org.spongepowered.api.Sponge) Set(java.util.Set) Tuple(org.spongepowered.api.util.Tuple) UUID(java.util.UUID) Instant(java.time.Instant) GuardedBy(javax.annotation.concurrent.GuardedBy) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) AFKCommand(io.github.nucleuspowered.nucleus.modules.afk.commands.AFKCommand) Reloadable(io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable) Cause(org.spongepowered.api.event.cause.Cause) List(java.util.List) MessageChannel(org.spongepowered.api.text.channel.MessageChannel) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) Player(org.spongepowered.api.entity.living.player.Player) NucleusTextTemplateImpl(io.github.nucleuspowered.nucleus.internal.text.NucleusTextTemplateImpl) AFKEvents(io.github.nucleuspowered.nucleus.modules.afk.events.AFKEvents) TextRepresentable(org.spongepowered.api.text.TextRepresentable) NucleusTextTemplateImpl(io.github.nucleuspowered.nucleus.internal.text.NucleusTextTemplateImpl) Text(org.spongepowered.api.text.Text) AFKEvents(io.github.nucleuspowered.nucleus.modules.afk.events.AFKEvents) MessageChannel(org.spongepowered.api.text.channel.MessageChannel) UUID(java.util.UUID)

Example 2 with NucleusTextTemplateImpl

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());
        });
    }
}
Also used : Getter(org.spongepowered.api.event.filter.Getter) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) Keys(org.spongepowered.api.data.key.Keys) User(org.spongepowered.api.entity.living.player.User) ServerListGeneralDataModule(io.github.nucleuspowered.nucleus.modules.serverlist.datamodules.ServerListGeneralDataModule) Collection(java.util.Collection) ServerListModule(io.github.nucleuspowered.nucleus.modules.serverlist.ServerListModule) Sponge(org.spongepowered.api.Sponge) Random(java.util.Random) ClientPingServerEvent(org.spongepowered.api.event.server.ClientPingServerEvent) ServerListConfigAdapter(io.github.nucleuspowered.nucleus.modules.serverlist.config.ServerListConfigAdapter) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Reloadable(io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable) ServerListConfig(io.github.nucleuspowered.nucleus.modules.serverlist.config.ServerListConfig) List(java.util.List) Text(org.spongepowered.api.text.Text) NucleusTextTemplate(io.github.nucleuspowered.nucleus.api.text.NucleusTextTemplate) Optional(java.util.Optional) ListenerBase(io.github.nucleuspowered.nucleus.internal.ListenerBase) GameProfile(org.spongepowered.api.profile.GameProfile) NucleusTextTemplateImpl(io.github.nucleuspowered.nucleus.internal.text.NucleusTextTemplateImpl) Listener(org.spongepowered.api.event.Listener) NucleusTextTemplateImpl(io.github.nucleuspowered.nucleus.internal.text.NucleusTextTemplateImpl) User(org.spongepowered.api.entity.living.player.User) NucleusTextTemplate(io.github.nucleuspowered.nucleus.api.text.NucleusTextTemplate) Collection(java.util.Collection) Text(org.spongepowered.api.text.Text) ServerListGeneralDataModule(io.github.nucleuspowered.nucleus.modules.serverlist.datamodules.ServerListGeneralDataModule) Listener(org.spongepowered.api.event.Listener)

Example 3 with NucleusTextTemplateImpl

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()));
        }
    }
}
Also used : NucleusTextTemplateImpl(io.github.nucleuspowered.nucleus.internal.text.NucleusTextTemplateImpl) NucleusTextTemplate(io.github.nucleuspowered.nucleus.api.text.NucleusTextTemplate) Text(org.spongepowered.api.text.Text) ServerListGeneralDataModule(io.github.nucleuspowered.nucleus.modules.serverlist.datamodules.ServerListGeneralDataModule) Listener(org.spongepowered.api.event.Listener)

Example 4 with NucleusTextTemplateImpl

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;
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) NucleusTextTemplateImpl(io.github.nucleuspowered.nucleus.internal.text.NucleusTextTemplateImpl) Text(org.spongepowered.api.text.Text) CommandSource(org.spongepowered.api.command.CommandSource) Function(java.util.function.Function) MessageConfig(io.github.nucleuspowered.nucleus.modules.message.config.MessageConfig) MessageChannel(org.spongepowered.api.text.channel.MessageChannel) MessageUserDataModule(io.github.nucleuspowered.nucleus.modules.message.datamodules.MessageUserDataModule) ConsoleSource(org.spongepowered.api.command.source.ConsoleSource) UUID(java.util.UUID) InternalNucleusMessageEvent(io.github.nucleuspowered.nucleus.modules.message.events.InternalNucleusMessageEvent)

Aggregations

NucleusTextTemplateImpl (io.github.nucleuspowered.nucleus.internal.text.NucleusTextTemplateImpl)4 Text (org.spongepowered.api.text.Text)4 Nucleus (io.github.nucleuspowered.nucleus.Nucleus)2 NucleusTextTemplate (io.github.nucleuspowered.nucleus.api.text.NucleusTextTemplate)2 Reloadable (io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable)2 ServerListGeneralDataModule (io.github.nucleuspowered.nucleus.modules.serverlist.datamodules.ServerListGeneralDataModule)2 List (java.util.List)2 Optional (java.util.Optional)2 UUID (java.util.UUID)2 Function (java.util.function.Function)2 Collectors (java.util.stream.Collectors)2 Sponge (org.spongepowered.api.Sponge)2 Keys (org.spongepowered.api.data.key.Keys)2 Player (org.spongepowered.api.entity.living.player.Player)2 User (org.spongepowered.api.entity.living.player.User)2 Listener (org.spongepowered.api.event.Listener)2 MessageChannel (org.spongepowered.api.text.channel.MessageChannel)2 Preconditions (com.google.common.base.Preconditions)1 HashMultimap (com.google.common.collect.HashMultimap)1 Lists (com.google.common.collect.Lists)1