Add Regex value type, add regular expression filter to ChatFilterModule

This commit is contained in:
rafern 2021-08-08 12:46:00 +01:00
parent ee2e43769b
commit c8585c60c6
No known key found for this signature in database
GPG Key ID: FE8BE3E64992D5CF
6 changed files with 107 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package me.rigamortis.seppuku.api.module;
import com.mojang.realmsclient.gui.ChatFormatting;
import me.rigamortis.seppuku.Seppuku;
import me.rigamortis.seppuku.api.value.Regex;
import me.rigamortis.seppuku.api.value.Value;
import net.minecraft.util.text.Style;
import net.minecraft.util.text.TextComponentString;
@ -111,6 +112,10 @@ public class Module {
msg.appendSibling(new TextComponentString(valuePrefix + v.getName() + ChatFormatting.GRAY + " <" + options.toString() + ">" + ChatFormatting.RESET + ": " + ChatFormatting.YELLOW + val.name().toLowerCase()).setStyle(new Style().setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new TextComponentString(v.getName() + "\n" + ChatFormatting.GOLD + ((v.getDesc() == null || v.getDesc().equals("")) ? "There is no description for this enum value." : v.getDesc()) + ChatFormatting.RESET + "\n " + ChatFormatting.GRAY + "<" + options.toString() + ">")))));
}
if (v.getValue() instanceof Regex) {
msg.appendSibling(new TextComponentString(valuePrefix + v.getName() + ChatFormatting.GRAY + " <regex>" + ChatFormatting.RESET + ": " + v.getValue()).setStyle(new Style().setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new TextComponentString(v.getName() + "\n" + ChatFormatting.GOLD + ((v.getDesc() == null || v.getDesc().equals("")) ? "There is no description for this regular expression value." : v.getDesc()) + ChatFormatting.RESET + "\n " + ChatFormatting.GRAY + "<regex>")))));
}
}
return msg;

View File

@ -0,0 +1,46 @@
package me.rigamortis.seppuku.api.value;
import javax.annotation.Nullable;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
public class Regex {
private String patternString;
private Pattern pattern;
public Regex() {
this.setPatternString("");
}
public Regex(String patternString) {
this.setPatternString(patternString);
}
public void setPatternString(String patternString) {
this.patternString = patternString;
if(patternString == "") {
this.pattern = null;
return;
}
try {
this.pattern = Pattern.compile(patternString);
} catch (PatternSyntaxException exception) {
this.pattern = null;
}
}
public String getPatternString() {
return this.patternString;
}
@Nullable
public Pattern getPattern() {
return this.pattern;
}
@Override
public String toString() {
return this.patternString;
}
}

View File

@ -4,6 +4,7 @@ import com.google.gson.JsonObject;
import me.rigamortis.seppuku.api.config.Configurable;
import me.rigamortis.seppuku.api.module.Module;
import me.rigamortis.seppuku.api.util.FileUtil;
import me.rigamortis.seppuku.api.value.Regex;
import me.rigamortis.seppuku.api.value.Value;
import java.awt.*;
@ -67,6 +68,8 @@ public class ModuleConfig extends Configurable {
val.setEnumValue(entry.getValue().getAsString());
} else if (val.getValue() instanceof Color) {
val.setValue(new Color((int) Long.parseLong(entry.getValue().getAsString(), 16)));
} else if (val.getValue() instanceof Regex) {
val.setValue(new Regex(entry.getValue().getAsString()));
}
}
}
@ -99,6 +102,8 @@ public class ModuleConfig extends Configurable {
moduleJsonObject.addProperty(value.getName(), ((Enum) value.getValue()).name());
} else if (value.getValue() instanceof Color) {
moduleJsonObject.addProperty(value.getName(), Integer.toHexString(((Color) value.getValue()).getRGB()).toUpperCase());
} else if (value.getValue() instanceof Regex) {
moduleJsonObject.addProperty(value.getName(), ((Regex) value.getValue()).getPatternString());
}
});
}

View File

@ -8,6 +8,7 @@ import me.rigamortis.seppuku.api.gui.hud.component.*;
import me.rigamortis.seppuku.api.module.Module;
import me.rigamortis.seppuku.api.texture.Texture;
import me.rigamortis.seppuku.api.util.RenderUtil;
import me.rigamortis.seppuku.api.value.Regex;
import me.rigamortis.seppuku.api.value.Value;
import me.rigamortis.seppuku.impl.config.ModuleConfig;
import me.rigamortis.seppuku.impl.gui.hud.GuiHudEditor;
@ -632,6 +633,22 @@ public final class ModuleListComponent extends ResizableHudComponent {
this.addComponentToButtons(blocksComponent);
}
}
} else if (value.getValue() instanceof Regex) {
TextComponent valueText = new TextComponent(value.getName(), value.getValue().toString(), false);
valueText.setTooltipText(value.getDesc());
valueText.returnListener = new ComponentListener() {
@Override
public void onComponentEvent() {
final Regex regex = (Regex) value.getValue();
regex.setPatternString(valueText.displayValue);
if(regex.getPattern() == null)
Seppuku.INSTANCE.logfChat("%s - %s: Invalid or empty regular expression; no input will match with pattern.", module.getDisplayName(), value.getName());
Seppuku.INSTANCE.getConfigManager().save(ModuleConfig.class); // save configs
Seppuku.INSTANCE.getEventManager().dispatchEvent(new EventUIValueChanged(value));
}
};
components.add(valueText);
this.addComponentToButtons(valueText);
}
}
}

View File

@ -6,6 +6,7 @@ import me.rigamortis.seppuku.api.event.command.EventCommandLoad;
import me.rigamortis.seppuku.api.module.Module;
import me.rigamortis.seppuku.api.util.ReflectionUtil;
import me.rigamortis.seppuku.api.util.StringUtil;
import me.rigamortis.seppuku.api.value.Regex;
import me.rigamortis.seppuku.api.value.Value;
import me.rigamortis.seppuku.impl.command.*;
import me.rigamortis.seppuku.impl.config.ModuleConfig;
@ -225,6 +226,29 @@ public final class CommandManager {
Seppuku.INSTANCE.errorChat("Invalid input " + "\"" + split[2] + "\" expected a string");
}
}
if (v.getValue() instanceof Regex) {
if (!this.clamp(input, 2, 3)) {
this.printUsage();
return;
}
final Regex regex = (Regex) v.getValue();
if (split.length == 2 || split[2] == "") {
regex.setPatternString("");
Seppuku.INSTANCE.logChat(module.getDisplayName() + " \2477" + v.getName() + "\247f cleared");
} else {
final String oldPatternString = regex.getPatternString();
regex.setPatternString(split[2]);
if (regex.getPattern() == null) {
regex.setPatternString(oldPatternString);
Seppuku.INSTANCE.errorChat("Invalid input " + "\"" + split[2] + "\" expected a valid regular expression or an empty string");
} else {
Seppuku.INSTANCE.logChat(module.getDisplayName() + " \2477" + v.getName() + "\247f set to " + split[2]);
Seppuku.INSTANCE.getConfigManager().save(ModuleConfig.class);
}
}
}
} else {
Seppuku.INSTANCE.errorChat("Invalid input " + "\"" + split[1] + "\"");
this.printUsage();

View File

@ -5,6 +5,7 @@ import me.rigamortis.seppuku.api.event.EventStageable;
import me.rigamortis.seppuku.api.event.network.EventReceivePacket;
import me.rigamortis.seppuku.api.module.Module;
import me.rigamortis.seppuku.api.util.StringUtil;
import me.rigamortis.seppuku.api.value.Regex;
import me.rigamortis.seppuku.api.value.Value;
import net.minecraft.client.Minecraft;
import net.minecraft.network.play.server.SPacketChat;
@ -13,6 +14,7 @@ import team.stiff.pomelo.impl.annotated.handler.annotation.Listener;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
/**
* Author Seth
@ -28,6 +30,7 @@ public final class ChatFilterModule extends Module {
public final Value<Boolean> death = new Value<>("Death", new String[]{"dead", "d"}, "Attempts to prevent death messages.", false);
public final Value<Boolean> blue = new Value<>("BlueText", new String[]{"Blue", "b"}, "Cancels blue-text containing messages.", false);
public final Value<Boolean> green = new Value<>("GreenText", new String[]{"Green", "g"}, "Cancels green-text containing messages.", false);
public final Value<Regex> regex = new Value<>("Regex", new String[]{"re"}, "Messages matching this regular expression will be hidden. Leave it blank to disable.", new Regex());
private final List<String> cache = new ArrayList<>();
@ -170,6 +173,13 @@ public final class ChatFilterModule extends Module {
}
}
}
final Pattern regexPattern = this.regex.getValue().getPattern();
if (regexPattern != null) {
if (regexPattern.matcher(packet.getChatComponent().getUnformattedText()).matches()) {
event.setCanceled(true);
}
}
}
}
}