Skip to content

Commit

Permalink
added char-filters.cancel-message
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanguygab committed Jul 26, 2023
1 parent b0ea926 commit fd1150c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public class TranslationFile extends YamlConfigurationFile {
public final String socialSpyOn = getString("socialspy.on", "&aSocialSpy enabled.");
public final String socialSpyOff = getString("socialspy.off", "&cSocialSpy disabled.");

public final String cantSwear = getString("msg.cant-swear","&cYou are not allowed to swear on this server!");

public TranslationFile(InputStream source, File destination) throws IOException {
super(source,destination);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ public void onChat(TabPlayer sender, String message) {
if (cooldownTime != 0 && !sender.hasPermission("tabadditions.chat.bypass.cooldown"))
cooldown.put(sender.getUniqueId(),LocalDateTime.now());

if (chatFormatter.shouldBlock(message,sender)) {
sender.sendMessage(plugin.getTranslation().cantSwear,true);
return;
}

ChatFormat format = null;
if (commandsManager != null) {
format = commandsManager.getFromPrefix(sender,message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,21 @@

public class ChatFormatter {

public boolean filterEnabled;
public String filterChar;
public int filterFakeLength;
public String filterOutput;
public List<Pattern> filterPatterns;
public List<String> filterExempt;

public boolean itemEnabled;
public String itemMainHand;
public String itemOffHand;
public String itemOutput;
public String itemOutputSingle;
public String itemOutputAir;
public boolean itemPermission;
private final boolean filterEnabled;
private final boolean filterCancelMessage;
private final String filterChar;
private final int filterFakeLength;
private final String filterOutput;
private final List<Pattern> filterPatterns;
private final List<String> filterExempt;

private final boolean itemEnabled;
private final String itemMainHand;
private final String itemOffHand;
private final String itemOutput;
private final String itemOutputSingle;
private final String itemOutputAir;
private final boolean itemPermission;

private final Map<String,Map<String,Object>> customInteractions;

Expand All @@ -38,6 +39,7 @@ public class ChatFormatter {
@SuppressWarnings("unchecked")
public ChatFormatter(ConfigurationFile config) {
filterEnabled = config.getBoolean("char-filter.enabled",true);
filterCancelMessage = config.getBoolean("char-filter.cancel-message",false);
filterChar = config.getString("char-filter.char-replacement","*");
filterFakeLength = config.getInt("char-filter.fake-length",0);
filterOutput = ChatUtils.componentToMM(config.getConfigurationSection("char-filter.output"));
Expand Down Expand Up @@ -65,7 +67,6 @@ public ChatFormatter(ConfigurationFile config) {
urlsOutput = ChatUtils.componentToMM(config.getConfigurationSection("embed-urls.output"));
}


public String process(String message, TabPlayer sender) {
if (filterEnabled && !sender.hasPermission("tabadditions.chat.bypass.filter")) message = filter(message);
if (embedURLs) message = embedURLs(message);
Expand All @@ -78,6 +79,27 @@ public String process(String message, TabPlayer sender) {
return message;
}

protected boolean shouldBlock(String msg, TabPlayer sender) {
if (!filterEnabled || !filterCancelMessage || sender.hasPermission("tabadditions.chat.bypass.filter")) return false;

Map<Integer, String> map = new HashMap<>();
for (String bypass : filterExempt) {
if (!msg.contains(bypass)) continue;
Matcher m = Pattern.compile(bypass).matcher(msg);
while (m.find()) map.put(m.start(), bypass);
}
for (Pattern pattern : filterPatterns) {
Matcher matcher = pattern.matcher(msg);
if (matcher.find()) {
if (map.isEmpty()) return true;
for (int pos : map.keySet())
if (map.get(pos).length() <= matcher.group().length() || pos > matcher.start() || pos + map.get(pos).length() <= matcher.start())
return true;
}
}
return false;
}

private String filter(String msg) {
for (Pattern pattern : filterPatterns) {
Matcher matcher = pattern.matcher(msg);
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/chat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ emojis:

char-filter:
enabled: true
cancel-message: true
char-replacement: "*"
fake-length: 0
output:
Expand All @@ -171,6 +172,7 @@ char-filter:
- fuck(er(s)?)?
- bitch(es)?
- ass(hole|es)?
- (?i)n[i1l!|¡]gg[ae@3€](r)?
exempt:
- bypass
embed-urls:
Expand Down

0 comments on commit fd1150c

Please sign in to comment.