diff --git a/src/main/java/me/rigamortis/seppuku/api/module/Module.java b/src/main/java/me/rigamortis/seppuku/api/module/Module.java index 663bbe0..e729793 100644 --- a/src/main/java/me/rigamortis/seppuku/api/module/Module.java +++ b/src/main/java/me/rigamortis/seppuku/api/module/Module.java @@ -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 + " " + 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 + ""))))); + } } return msg; diff --git a/src/main/java/me/rigamortis/seppuku/api/value/Regex.java b/src/main/java/me/rigamortis/seppuku/api/value/Regex.java new file mode 100644 index 0000000..873187d --- /dev/null +++ b/src/main/java/me/rigamortis/seppuku/api/value/Regex.java @@ -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; + } +} \ No newline at end of file diff --git a/src/main/java/me/rigamortis/seppuku/impl/config/ModuleConfig.java b/src/main/java/me/rigamortis/seppuku/impl/config/ModuleConfig.java index d264249..c42a643 100644 --- a/src/main/java/me/rigamortis/seppuku/impl/config/ModuleConfig.java +++ b/src/main/java/me/rigamortis/seppuku/impl/config/ModuleConfig.java @@ -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()); } }); } diff --git a/src/main/java/me/rigamortis/seppuku/impl/gui/hud/component/module/ModuleListComponent.java b/src/main/java/me/rigamortis/seppuku/impl/gui/hud/component/module/ModuleListComponent.java index 01c9335..f546128 100644 --- a/src/main/java/me/rigamortis/seppuku/impl/gui/hud/component/module/ModuleListComponent.java +++ b/src/main/java/me/rigamortis/seppuku/impl/gui/hud/component/module/ModuleListComponent.java @@ -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); } } } diff --git a/src/main/java/me/rigamortis/seppuku/impl/management/CommandManager.java b/src/main/java/me/rigamortis/seppuku/impl/management/CommandManager.java index 127c497..4722d0c 100644 --- a/src/main/java/me/rigamortis/seppuku/impl/management/CommandManager.java +++ b/src/main/java/me/rigamortis/seppuku/impl/management/CommandManager.java @@ -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(); diff --git a/src/main/java/me/rigamortis/seppuku/impl/module/misc/ChatFilterModule.java b/src/main/java/me/rigamortis/seppuku/impl/module/misc/ChatFilterModule.java index 1388201..4211e93 100644 --- a/src/main/java/me/rigamortis/seppuku/impl/module/misc/ChatFilterModule.java +++ b/src/main/java/me/rigamortis/seppuku/impl/module/misc/ChatFilterModule.java @@ -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 death = new Value<>("Death", new String[]{"dead", "d"}, "Attempts to prevent death messages.", false); public final Value blue = new Value<>("BlueText", new String[]{"Blue", "b"}, "Cancels blue-text containing messages.", false); public final Value green = new Value<>("GreenText", new String[]{"Green", "g"}, "Cancels green-text containing messages.", false); + public final Value 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 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); + } + } } } }