|
| 1 | +package nge.lk.mods.simpletabs; |
| 2 | + |
| 3 | +import lombok.Getter; |
| 4 | +import lombok.Setter; |
| 5 | +import net.minecraft.client.Minecraft; |
| 6 | +import net.minecraft.client.gui.GuiNewChat; |
| 7 | +import net.minecraft.util.text.ITextComponent; |
| 8 | + |
| 9 | +import java.util.regex.Matcher; |
| 10 | +import java.util.regex.Pattern; |
| 11 | + |
| 12 | +/** |
| 13 | + * Represents a chat tab. |
| 14 | + */ |
| 15 | +public class ChatTab extends GuiNewChat { |
| 16 | + |
| 17 | + /** |
| 18 | + * The filter which selects the messages to accept. |
| 19 | + */ |
| 20 | + private Matcher filter; |
| 21 | + |
| 22 | + /** |
| 23 | + * Whether this tab has unread messages. |
| 24 | + */ |
| 25 | + @Getter private boolean unread; |
| 26 | + |
| 27 | + /** |
| 28 | + * The pattern string, for saving. |
| 29 | + */ |
| 30 | + @Getter private String pattern; |
| 31 | + |
| 32 | + /** |
| 33 | + * Whether the pattern is literal, for saving. |
| 34 | + */ |
| 35 | + @Getter private boolean literal; |
| 36 | + |
| 37 | + /** |
| 38 | + * The prefix for sent messages in this tab. |
| 39 | + */ |
| 40 | + @Getter @Setter private String prefix; |
| 41 | + |
| 42 | + /** |
| 43 | + * Constructor. |
| 44 | + * |
| 45 | + * @param mc The minecraft reference. |
| 46 | + */ |
| 47 | + public ChatTab(final Minecraft mc, final String pattern, final boolean literal, final String prefix) { |
| 48 | + super(mc); |
| 49 | + this.pattern = pattern; |
| 50 | + this.literal = literal; |
| 51 | + this.prefix = prefix; |
| 52 | + filter = Pattern.compile(pattern, literal ? Pattern.LITERAL : 0).matcher(""); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Updates the filter pattern. |
| 57 | + * |
| 58 | + * @param pattern The new pattern. |
| 59 | + * @param literal Whether the pattern will be escaped. |
| 60 | + */ |
| 61 | + public void updatePattern(final String pattern, final boolean literal) { |
| 62 | + this.pattern = pattern; |
| 63 | + this.literal = literal; |
| 64 | + filter = Pattern.compile(pattern, literal ? Pattern.LITERAL : 0).matcher(""); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void printChatMessageWithOptionalDeletion(final ITextComponent chatComponent, final int chatLineId) { |
| 69 | + super.printChatMessageWithOptionalDeletion(chatComponent, chatLineId); |
| 70 | + unread = true; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Whether this tab accepts the given message for display. |
| 75 | + * |
| 76 | + * @param message The message. |
| 77 | + * |
| 78 | + * @return Whether it is accepted. |
| 79 | + */ |
| 80 | + public boolean acceptsMessage(final CharSequence message) { |
| 81 | + filter.reset(message); |
| 82 | + return filter.find(); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Marks this tab as read. |
| 87 | + */ |
| 88 | + public void markRead() { |
| 89 | + unread = false; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Returns the export part of this tab. |
| 94 | + * |
| 95 | + * @return The export string. |
| 96 | + */ |
| 97 | + public String getExport() { |
| 98 | + return pattern + "§" + Boolean.toString(literal) + "§" + prefix; |
| 99 | + } |
| 100 | +} |
0 commit comments