Skip to content

Commit

Permalink
misc: Improved InfractionType #getConfig & #getMessages
Browse files Browse the repository at this point in the history
  • Loading branch information
4drian3d committed Jul 1, 2022
1 parent e858bef commit f4a53c8
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ private ConfigManager(){}
* @param plugin the plugin
*/
public static void sendWarningMessage(InfractionPlayer infractor, Result result, InfractionType type, ChatRegulator plugin){
final String message = type.getMessages().get().getWarningMessage();
final String message = type.getMessages().getWarningMessage();
final TagResolver placeholder = TagResolver.resolver(
Placeholder.unparsed("infraction", result.getInfractionString()),
plugin.placeholders().getPlaceholders(infractor));
switch(((MainConfig.Warning)type.getConfig().get()).getWarningType()){
switch(((MainConfig.Warning)type.getConfig()).getWarningType()){
case TITLE: sendTitle(message, infractor, placeholder); break;
case MESSAGE: infractor.sendMessage(plugin.getFormatter().parse(message, infractor.getPlayer(), placeholder)); break;
case ACTIONBAR: infractor.sendActionBar(plugin.getFormatter().parse(message, infractor.getPlayer(), placeholder)); break;
Expand Down Expand Up @@ -80,7 +80,7 @@ public static void sendAlertMessage(
final Result result
) {
final Component message = plugin.getFormatter().parse(
((Alert)type.getMessages().get()).getAlertMessage(),
((Alert)type.getMessages()).getAlertMessage(),
TagResolver.resolver(
plugin.placeholders().getPlaceholders(infractor),
Placeholder.unparsed("string", result.getInfractionString()))
Expand Down
116 changes: 92 additions & 24 deletions src/main/java/me/dreamerzero/chatregulator/enums/InfractionType.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package me.dreamerzero.chatregulator.enums;

import java.util.function.Supplier;

import org.jetbrains.annotations.NotNull;

import me.dreamerzero.chatregulator.config.Configuration;
import me.dreamerzero.chatregulator.config.MainConfig;
import me.dreamerzero.chatregulator.config.MainConfig.Toggleable;
import me.dreamerzero.chatregulator.config.Messages;
import me.dreamerzero.chatregulator.config.Messages.Warning;

/**
* Infraction Types
Expand All @@ -16,61 +16,129 @@ public enum InfractionType {
* Represents a regular violation, i.e.
* detection based on the blacklist.yml file.
*/
REGULAR(Permission.BYPASS_INFRACTIONS, () -> Configuration.getConfig().getInfractionsConfig(), () -> Configuration.getMessages().getInfractionsMessages()),
REGULAR(Permission.BYPASS_INFRACTIONS) {
@Override
public Toggleable getConfig() {
return Configuration.getConfig().getInfractionsConfig();
}

@Override
public Warning getMessages() {
return Configuration.getMessages().getInfractionsMessages();
}
},
/**
* Represents an infraction for repeating
* the same character several times in a row.
*/
FLOOD(Permission.BYPASS_FLOOD, () -> Configuration.getConfig().getFloodConfig(), () -> Configuration.getMessages().getFloodMessages()),
FLOOD(Permission.BYPASS_FLOOD) {
@Override
public Toggleable getConfig() {
return Configuration.getConfig().getFloodConfig();
}

@Override
public Warning getMessages() {
return Configuration.getMessages().getFloodMessages();
}
},
/**
* Represents an infraction for repeating
* the same word or command several times.
*/
SPAM(Permission.BYPASS_SPAM, () -> Configuration.getConfig().getSpamConfig(), () -> Configuration.getMessages().getSpamMessages()),
SPAM(Permission.BYPASS_SPAM) {
@Override
public Toggleable getConfig() {
return Configuration.getConfig().getSpamConfig();
}

@Override
public Warning getMessages() {
return Configuration.getMessages().getSpamMessages();
}
},
/**
* Represents a blocked command
*/
BCOMMAND(Permission.BYPASS_BCOMMAND, () -> Configuration.getConfig().getCommandBlacklistConfig(), () -> Configuration.getMessages().getBlacklistMessages()),
BCOMMAND(Permission.BYPASS_BCOMMAND) {
@Override
public Toggleable getConfig() {
return Configuration.getConfig().getCommandBlacklistConfig();
}

@Override
public Warning getMessages() {
return Configuration.getMessages().getBlacklistMessages();
}
},
/**
* Represents a Unicode check
*/
UNICODE(Permission.BYPASS_UNICODE, () -> Configuration.getConfig().getUnicodeConfig(), () -> Configuration.getMessages().getUnicodeMessages()),
UNICODE(Permission.BYPASS_UNICODE) {
@Override
public Toggleable getConfig() {
return Configuration.getConfig().getUnicodeConfig();
}

@Override
public Warning getMessages() {
return Configuration.getMessages().getUnicodeMessages();
}
},
/**
* Represents a Caps limit check
*/
CAPS(Permission.BYPASS_CAPS, () -> Configuration.getConfig().getCapsConfig(), () -> Configuration.getMessages().getCapsMessages()),
CAPS(Permission.BYPASS_CAPS) {
@Override
public Toggleable getConfig() {
return Configuration.getConfig().getCapsConfig();
}

@Override
public Warning getMessages() {
return Configuration.getMessages().getCapsMessages();
}
},
/**
* Represents a Syntax check
* <p>/minecraft:tp
*/
SYNTAX(Permission.BYPASS_SYNTAX, () -> Configuration.getConfig().getSyntaxConfig(), () -> Configuration.getMessages().getSyntaxMessages()),
SYNTAX(Permission.BYPASS_SYNTAX) {
@Override
public Toggleable getConfig() {
return Configuration.getConfig().getSyntaxConfig();
}

@Override
public Warning getMessages() {
return Configuration.getMessages().getSyntaxMessages();
}
},
/**
* Used internally to represent a
* multiple warning and in other cases more
*/
NONE(Permission.NO_PERMISSION);
NONE(Permission.NO_PERMISSION) {
@Override
public Toggleable getConfig() {
return null;
}

@Override
public Warning getMessages() {
return null;
}
};

private final Permission bypassPermission;
private Supplier<MainConfig.Toggleable> config;
private Supplier<Messages.Warning> messages;

InfractionType(Permission permission){
this.bypassPermission = permission;
}

InfractionType(Permission permission, Supplier<MainConfig.Toggleable> config, Supplier<Messages.Warning> messages){
this(permission);
this.config = config;
this.messages = messages;
}

public Supplier<MainConfig.Toggleable> getConfig(){
return this.config;
}
public abstract MainConfig.Toggleable getConfig();

public Supplier<Messages.Warning> getMessages(){
return this.messages;
}
public abstract Messages.Warning getMessages();

/**
* Get the bypass permission of this type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static void executeCommand(
return;
}

final CommandsConfig config = ((Executable)type.getConfig().get()).getCommandsConfig();
final CommandsConfig config = ((Executable)type.getConfig()).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 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public final class GeneralUtils {
* @return if the player can be checked
*/
public static boolean allowedPlayer(@NotNull Player player, InfractionType type){
return type.getConfig().get().enabled() && !type.bypassPermission().test(Objects.requireNonNull(player));
return type.getConfig().enabled() && !type.bypassPermission().test(Objects.requireNonNull(player));
}

/**
Expand Down

0 comments on commit f4a53c8

Please sign in to comment.