package gtanks.battles.chat; import gtanks.StringUtils; import…
- User
package gtanks.battles.chat;
import gtanks.StringUtils; import gtanks.battles.BattlefieldModel; import gtanks.battles.BattlefieldPlayerController; import gtanks.battles.bonuses.BonusType; import gtanks.commands.Type; import gtanks.json.JSONUtils; import gtanks.lobby.LobbyManager; import gtanks.main.database.DatabaseManager; import gtanks.main.database.impl.DatabaseManagerImpl; import gtanks.main.params.OnlineStats; import gtanks.services.BanServices; import gtanks.services.LobbysServices; import gtanks.services.TanksServices; import gtanks.services.annotations.ServicesInject; import gtanks.services.ban.BanChatCommads; import gtanks.services.ban.BanTimeType; import gtanks.services.ban.BanType; import gtanks.services.ban.DateFormater; import gtanks.services.ban.block.BlockGameReason; import gtanks.users.TypeUser; import gtanks.users.User; import gtanks.users.karma.Karma; import java.util.Date;
public class BattlefieldChatModel { private BattlefieldModel bfModel; private final int MAX_WARNING = 5; @ServicesInject( target = TanksServices.class ) private TanksServices tanksServices = TanksServices.getInstance(); @ServicesInject( target = DatabaseManager.class ) private DatabaseManager database = DatabaseManagerImpl.instance(); @ServicesInject( target = LobbysServices.class ) private LobbysServices lobbyServices = LobbysServices.getInstance(); @ServicesInject( target = BanServices.class ) private BanServices banServices = BanServices.getInstance();
public BattlefieldChatModel(BattlefieldModel bfModel) { this.bfModel = bfModel; } public void onMessage(BattlefieldPlayerController player, String message, boolean team) { if (!(message = message.trim()).isEmpty()) { Karma karma = this.database.getKarmaByUser(player.getUser()); if (karma.isChatBanned()) { long currDate = System.currentTimeMillis(); Date banTo = karma.getChatBannedBefore(); long delta = currDate - banTo.getTime(); if (delta <= 0L) { player.parentLobby.send(Type.LOBBY_CHAT, "system", StringUtils.concatStrings("Вы отключены от чата. Вы вернётесь в ЭФИР через ", DateFormater.formatTimeToUnban(delta), ". Причина: " + karma.getReasonChatBan())); return; }
code - Assistant
To add an anti-profanity filter to the BattlefieldChatModel, you can create a method that checks if the message contains any profanity words and remove or replace them before sending the chat message. Here's an example implementation:
- Create a list of profanity words in the BattlefieldChatModel class:
javaprivate List<String> profanityWords = Arrays.asList("word1", "word2", "word3");- Implement a method to filter and replace the profanity words:
javaprivate String filterProfanity(String message) { for (String word : profanityWords) { message = message.replaceAll("(?i)" + word, "*censored*"); } return message; }This method uses the
replaceAllmethod to replace any profanity words with "". The flag makes the replacement case-insensitive. - User
сделай чтобы мат фильтр заменял заменял слово на * и чтобы количество * было как у количества мат слова
- Assistant
To modify the profanity filter to replace each profanity word with asterisks (*) equal to the number of characters in the word, you can update the
filterProfanitymethod:javaprivate String filterProfanity(String message) { for (String word : profanityWords) { String replacement = "*".repeat(word.length()); message = message.replaceAll("(?i)" + word, replacement); } return message; }This implementation uses the
repeatmethod to repeat the asterisk character '*' the same number of times as the length of the profanity word. The(?i)flag ensures that the replacement is case-insensitive.Now, when a profanity word is detected in the message, it will be replaced with asterisks that have the same length as the word. For example, if the word is "bad", it will be replaced with "***".
- User
private String filterProfanity(String message) { for (String word : profanityWords) { String replacement = "".repeat(word.length()); message = message.replaceAll("(?i)" + word, replacement); } return message; } жалуется на repeat
- Assistant
I apologize for the confusion. The
String.repeat()method is available from Java 11 onwards. If you're using an older version of Java, you can replace it with a loop to repeat the asterisk character. Here's an updated version of thefilterProfanitymethod that works with older Java versions:javaprivate String filterProfanity(String message) { for (String word : profanityWords) { StringBuilder replacement = new StringBuilder(); for (int i = 0; i < word.length(); i++) { replacement.append("*"); } message = message.replaceAll("(?i)" + word, replacement.toString()); } return message; }This version uses a loop to iterate over the characters of the word and appends an asterisk '*' to the
replacementStringBuilder for each character in the word. Finally, it converts thereplacementStringBuilder to a String usingtoString().Now, the profanity filter will correctly replace each profanity word with asterisks (*) equal to the length of the word, regardless of the Java version you are using.
- User
вместо звездочек пусто просто
- Assistant
Apologies for the confusion. To replace each profanity word with an empty string instead of asterisks, you can update the
filterProfanitymethod as follows:javaprivate String filterProfanity(String message) { for (String word : profanityWords) { message = message.replaceAll("(?i)" + word, ""); } return message; }This modified implementation directly replaces each profanity word with an empty string using the
replaceAllmethod. As a result, the profanity words will be removed from themessagestring without any replacement characters. - User
как сделать чтобы антимат фильтру было все равно какие буквы заглавные или маленькие
- Assistant
To make the profanity filter case-insensitive and match profanity words regardless of whether they are uppercase or lowercase, you can update the
filterProfanitymethod with thePattern.CASE_INSENSITIVEflag:javaimport java.util.regex.Pattern; // ... private String filterProfanity(String message) { for (String word : profanityWords) { String pattern = "(?i)" + word; message = message.replaceAll(pattern, ""); } return message; }By adding the
(?i)flag to the regular expression pattern, thereplaceAllmethod will perform a case-insensitive matching, replacing profanity words regardless of their case. Now, profanity words like "bad", "BAD", "BaD", etc., will be removed from the message regardless of their letter case.