Skip to content

Commit

Permalink
misc: Improved executeCommand method
Browse files Browse the repository at this point in the history
  • Loading branch information
4drian3d committed Apr 25, 2022
1 parent e212558 commit 78ce6a0
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions src/main/java/me/dreamerzero/chatregulator/utils/CommandUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,31 @@ private CommandUtils(){}
* configured commands when it detects that the limit
* of a violation has been exceeded.
* @param type the {@link InfractionType}
* @param infractorPlayer the {@link InfractionPlayer} involved
* @param infractor the {@link InfractionPlayer} involved
* @param plugin the plugin
*/
public static void executeCommand(@NotNull InfractionType type, @NotNull InfractionPlayer infractorPlayer, ChatRegulator plugin){
Player infractor = Objects.requireNonNull(infractorPlayer).getPlayer();
if(infractor != null){
execute(infractor, infractorPlayer, type, plugin);
public static void executeCommand(
final @NotNull InfractionType type,
final @NotNull InfractionPlayer infractor,
final @NotNull ChatRegulator plugin
) {
final Player player = Objects.requireNonNull(infractor).getPlayer();
if(player == null){
return;
}
}

private static void execute(@NotNull Player infractor, @NotNull InfractionPlayer iPlayer, @NotNull InfractionType type, ChatRegulator plugin){
CommandsConfig config = ((Executable)type.getConfig().get()).getCommandsConfig();
if(config.executeCommand() && iPlayer.getViolations().getCount(type) % config.violationsRequired() == 0){
final String servername = infractor.getCurrentServer().map(sv -> sv.getServerInfo().getName()).orElse("");
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 commandToSend = cmd.replace("<player>", infractor.getUsername()).replace("<server>", servername);
plugin.getProxy().getCommandManager().executeAsync(plugin.getProxy().getConsoleCommandSource(), commandToSend).thenAcceptAsync(status -> {
if(!status.booleanValue()){
plugin.getLogger().warn("Error executing command {}", commandToSend);
}
});
final String command = cmd.replace("<player>", infractor.username()).replace("<server>", servername);
plugin.getProxy().getCommandManager()
.executeAsync(plugin.getProxy().getConsoleCommandSource(), command)
.thenAcceptAsync(status -> {
if(!status.booleanValue()){
plugin.getLogger().warn("Error executing command {}", command);
}
});
});
}
}
Expand All @@ -55,7 +59,7 @@ private static void execute(@NotNull Player infractor, @NotNull InfractionPlayer
* @param command the command executed
* @return if the command is to be checked
*/
public static boolean isCommand(@NotNull String command){
public static boolean isCommand(@NotNull String command) {
final String firstArgument = getFirstArgument(Objects.requireNonNull(command));
return Configuration.getBlacklist().getBlockedCommands().stream()
.anyMatch(firstArgument::equalsIgnoreCase);
Expand All @@ -66,8 +70,8 @@ public static boolean isCommand(@NotNull String command){
* @param string the string
* @return the first argument
*/
public static @NotNull String getFirstArgument(@NotNull String string){
int index = Objects.requireNonNull(string).indexOf(" ");
public static @NotNull String getFirstArgument(final @NotNull String string) {
final int index = Objects.requireNonNull(string).indexOf(" ");
if (index == -1) {
return string;
}
Expand Down

0 comments on commit 78ce6a0

Please sign in to comment.