Skip to content

Commit

Permalink
fix: Fixed the execution of actions in case of getting a ReplaceCheck…
Browse files Browse the repository at this point in the history
…Result in the command detection
  • Loading branch information
4drian3d committed Sep 14, 2023
1 parent a17e033 commit 1fa6509
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package io.github._4drian3d.chatregulator.api.enums;

import java.util.function.Predicate;

import com.velocitypowered.api.command.CommandSource;

import java.util.function.Predicate;

/**
* ChatRegulator Permissions
*/
Expand Down Expand Up @@ -46,7 +46,7 @@ public enum Permission implements Predicate<CommandSource> {
/**Notifications */
NOTIFICATIONS("chatregulator.notifications"),
/**CommandSpy alert */
COMMANDSPY_ALERT("chatregulator.notifications.commandspy"),
COMMAND_SPY_ALERT("chatregulator.notifications.commandspy"),


/**Infractions Check Bypass */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,20 @@ public String toString() {
+ "]";
}

public boolean isAllowed(InfractionType type) {
public boolean isAllowed(final InfractionType type) {
return checksContainer.get().isEnabled(type) && !type.getBypassPermission().test(getPlayer());
}

private void sendWarningMessage(CheckResult result, InfractionType type) {
private void sendWarningMessage(final CheckResult.DetectedResult result, final String detected) {
final InfractionType type = result.infractionType();
final String message = requireNonNull(messagesContainer.get().getWarning(type)).getWarningMessage();
if (message.isBlank()) {
return;
}
final TagResolver.Builder builder = TagResolver.builder();
builder.resolver(getPlaceholders());

if (result instanceof CheckResult.ReplaceCheckResult replaceResult) {
builder.resolver(Placeholder.unparsed("infraction", replaceResult.replaced()));
}
builder.resolver(Placeholder.unparsed("infraction", detected));

final TagResolver resolver = builder.build();
final Checks.Warning configuration = checksContainer.get().getWarning(type);
Expand Down Expand Up @@ -176,8 +178,13 @@ private void sendSingleTitle(String title, TagResolver resolver, Formatter forma
sendTitlePart(TitlePart.SUBTITLE, formatter.parse(title, resolver));
}

private void sendAlertMessage(final InfractionType type, final CheckResult result, final String original) {
private void sendAlertMessage(final CheckResult.DetectedResult result, final String original) {
final InfractionType type = result.infractionType();
final Messages.Alert messages = requireNonNull(messagesContainer.get().getAlert(type));
final String alertMessage = messages.getAlertMessage();
if (alertMessage.isBlank()) {
return;
}

final TagResolver.Builder builder = TagResolver.builder();
builder.resolver(getPlaceholders());
Expand All @@ -189,7 +196,7 @@ private void sendAlertMessage(final InfractionType type, final CheckResult resul
}
builder.resolver(Placeholder.unparsed("original", original));

final Component message = formatter.parse(messages.getAlertMessage(), builder.build());
final Component message = formatter.parse(alertMessage, builder.build());

for (final Player player : proxyServer.getAllPlayers()) {
if (Permission.NOTIFICATIONS.test(player)) {
Expand Down Expand Up @@ -235,23 +242,16 @@ public void debug(String string, InfractionType detection) {
}
}

public void onDetected(CheckResult.DeniedCheckResult result, String string) {
this.sendWarningMessage(result, result.infractionType());
this.sendAlertMessage(result.infractionType(), result, string);
this.getInfractions().addViolation(result.infractionType());
this.executeCommands(result.infractionType());
this.debug(string, result.infractionType());
}

public void onDetected(CheckResult.ReplaceCheckResult result, String string) {
this.sendWarningMessage(result, result.infractionType());
this.sendAlertMessage(result.infractionType(), result, string);
public void onDetection(final CheckResult.DetectedResult result, final String string) {
this.sendWarningMessage(result, string);
this.sendAlertMessage(result, string);
this.getInfractions().addViolation(result.infractionType());
this.executeCommands(result.infractionType());
this.executeCommands(result);
this.debug(string, result.infractionType());
}

private void executeCommands(final @NotNull InfractionType type) {
private void executeCommands(final @NotNull CheckResult.DetectedResult result) {
final InfractionType type = result.infractionType();
final Player player = getPlayer();
if (player == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public final class ChatListener implements RegulatorExecutor<PlayerChatEvent> {
}).thenAccept(checkResult -> {
if (checkResult instanceof final CheckResult.DeniedCheckResult deniedResult) {
eventManager.fireAndForget(new ChatInfractionEvent(player, deniedResult.infractionType(), checkResult, event.getMessage()));
player.onDetected(deniedResult, event.getMessage());
player.onDetection(deniedResult, event.getMessage());
event.setResult(ChatResult.denied());
} else if (checkResult instanceof final CheckResult.ReplaceCheckResult replaceResult) {
String finalMessage = replaceResult.replaced();
Expand All @@ -80,7 +80,7 @@ public final class ChatListener implements RegulatorExecutor<PlayerChatEvent> {
}
player.getChain(SourceType.CHAT).executed(event.getMessage());
eventManager.fireAndForget(new ChatInfractionEvent(player, replaceResult.infractionType(), checkResult, event.getMessage()));
player.onDetected(replaceResult, event.getMessage());
player.onDetection(replaceResult, event.getMessage());
event.setResult(ChatResult.message(finalMessage));
} else {
player.getChain(SourceType.CHAT).executed(event.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,14 @@ private boolean checkIfCanCheck(final String command) {
} else {
if (result instanceof final CheckResult.DeniedCheckResult deniedResult) {
eventManager.fireAndForget(new CommandInfractionEvent(infractionPlayer, deniedResult.infractionType(), result, event.getCommand()));
infractionPlayer.onDetected(deniedResult, event.getCommand());
infractionPlayer.onDetection(deniedResult, event.getCommand());
event.setResult(CommandExecuteEvent.CommandResult.denied());
return null;
}
if (result instanceof final CheckResult.ReplaceCheckResult replaceResult) {
final String replacedCommand = replaceResult.replaced();
infractionPlayer.getChain(SourceType.COMMAND).executed(replacedCommand);
infractionPlayer.onDetection(replaceResult, event.getCommand());
event.setResult(CommandExecuteEvent.CommandResult.command(replacedCommand));
} else {
infractionPlayer.getChain(SourceType.COMMAND).executed(event.getCommand());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public EventTask executeAsync(final CommandExecuteEvent event) {
)
);
proxyServer.getAllPlayers().forEach(pl -> {
if (Permission.COMMANDSPY_ALERT.test(pl)) {
if (Permission.COMMAND_SPY_ALERT.test(pl)) {
pl.sendMessage(message);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public final class PlayerResolver implements TagResolver {
private final InfractionPlayer player;

public PlayerResolver(InfractionPlayer player) {
public PlayerResolver(final InfractionPlayer player) {
this.player = player;
}

Expand Down

0 comments on commit 1fa6509

Please sign in to comment.