Search in sources :

Example 1 with ChatRegulator

use of me.dreamerzero.chatregulator.ChatRegulator in project ChatRegulator by 4drian3d.

the class TestsUtils method createRegulator.

public static ChatRegulator createRegulator() {
    ProxyServer proxy = createProxy();
    Logger logger = LoggerFactory.getLogger(TestsUtils.class);
    Path path = Path.of("build", "reports", "tests", "test");
    return new ChatRegulator(proxy, logger, path, null) {

        @Override
        public IFormatter getFormatter() {
            return new NormalFormatter();
        }
    };
}
Also used : Path(java.nio.file.Path) NormalFormatter(me.dreamerzero.chatregulator.placeholders.formatter.NormalFormatter) ChatRegulator(me.dreamerzero.chatregulator.ChatRegulator) Logger(org.slf4j.Logger) ProxyServer(com.velocitypowered.api.proxy.ProxyServer)

Example 2 with ChatRegulator

use of me.dreamerzero.chatregulator.ChatRegulator in project ChatRegulator by 4drian3d.

the class CommandUtils method executeCommand.

/**
 * This will check if it is possible to execute the
 * configured commands when it detects that the limit
 * of a violation has been exceeded.
 * @param type the {@link InfractionType}
 * @param infractor the {@link InfractionPlayer} involved
 * @param plugin the plugin
 */
public static void executeCommand(@NotNull final InfractionType type, @NotNull final InfractionPlayer infractor, @NotNull final ChatRegulator plugin) {
    final Player player = Objects.requireNonNull(infractor).getPlayer();
    if (player == null) {
        return;
    }
    final CommandsConfig config = ((Executable) type.getConfig().get()).getCommandsConfig();
    if (config.executeCommand() && infractor.getViolations().getCount(type) % config.violationsRequired() == 0) {
        final String servername = player.getCurrentServer().map(sv -> sv.getServerInfo().getName()).orElse("");
        config.getCommandsToExecute().forEach(cmd -> {
            final String command = cmd.replace("<player>", infractor.username()).replace("<server>", servername);
            plugin.getProxy().getCommandManager().executeAsync(plugin.getProxy().getConsoleCommandSource(), command).handleAsync((status, ex) -> {
                if (ex != null) {
                    plugin.getLogger().warn("Error executing command", ex);
                } else if (!status.booleanValue()) {
                    plugin.getLogger().warn("Error executing command {}", command);
                }
                return null;
            });
        });
    }
}
Also used : Objects(java.util.Objects) CommandsConfig(me.dreamerzero.chatregulator.config.MainConfig.CommandsConfig) Locale(java.util.Locale) ChatRegulator(me.dreamerzero.chatregulator.ChatRegulator) Player(com.velocitypowered.api.proxy.Player) Executable(me.dreamerzero.chatregulator.config.MainConfig.Executable) Configuration(me.dreamerzero.chatregulator.config.Configuration) InfractionType(me.dreamerzero.chatregulator.enums.InfractionType) NotNull(org.jetbrains.annotations.NotNull) InfractionPlayer(me.dreamerzero.chatregulator.InfractionPlayer) Player(com.velocitypowered.api.proxy.Player) InfractionPlayer(me.dreamerzero.chatregulator.InfractionPlayer) CommandsConfig(me.dreamerzero.chatregulator.config.MainConfig.CommandsConfig) Executable(me.dreamerzero.chatregulator.config.MainConfig.Executable)

Aggregations

ChatRegulator (me.dreamerzero.chatregulator.ChatRegulator)2 Player (com.velocitypowered.api.proxy.Player)1 ProxyServer (com.velocitypowered.api.proxy.ProxyServer)1 Path (java.nio.file.Path)1 Locale (java.util.Locale)1 Objects (java.util.Objects)1 InfractionPlayer (me.dreamerzero.chatregulator.InfractionPlayer)1 Configuration (me.dreamerzero.chatregulator.config.Configuration)1 CommandsConfig (me.dreamerzero.chatregulator.config.MainConfig.CommandsConfig)1 Executable (me.dreamerzero.chatregulator.config.MainConfig.Executable)1 InfractionType (me.dreamerzero.chatregulator.enums.InfractionType)1 NormalFormatter (me.dreamerzero.chatregulator.placeholders.formatter.NormalFormatter)1 NotNull (org.jetbrains.annotations.NotNull)1 Logger (org.slf4j.Logger)1