diff options
author | Wyvest <45589059+Wyvest@users.noreply.github.com> | 2021-12-25 14:13:27 +0700 |
---|---|---|
committer | Wyvest <45589059+Wyvest@users.noreply.github.com> | 2021-12-25 14:13:27 +0700 |
commit | 4927d585ec72cb045a70718423228fa2cf55d071 (patch) | |
tree | 32714efb0c1a3a3aa3a01cadfc7a8ba5a9c784f1 /src/main | |
parent | f720ca0a074411be7cbbdc1b9629b14d06e3b26c (diff) | |
download | Chatting-4927d585ec72cb045a70718423228fa2cf55d071.tar.gz Chatting-4927d585ec72cb045a70718423228fa2cf55d071.tar.bz2 Chatting-4927d585ec72cb045a70718423228fa2cf55d071.zip |
new: rewrite chat searching
fix: fix chat tabs / shortcuts not initializing when turning on
Diffstat (limited to 'src/main')
13 files changed, 171 insertions, 69 deletions
diff --git a/src/main/java/com/raeids/stratus/hook/GuiNewChatHook.java b/src/main/java/com/raeids/stratus/hook/GuiNewChatHook.java index 93abcdf..e37995a 100644 --- a/src/main/java/com/raeids/stratus/hook/GuiNewChatHook.java +++ b/src/main/java/com/raeids/stratus/hook/GuiNewChatHook.java @@ -9,4 +9,6 @@ public interface GuiNewChatHook { default ChatLine getFullMessage(ChatLine line) { throw new AssertionError("getFullMessage not overridden on GuiNewChat"); } + String getPrevText(); + void setPrevText(String prevText); } diff --git a/src/main/java/com/raeids/stratus/mixin/GuiChatMixin.java b/src/main/java/com/raeids/stratus/mixin/GuiChatMixin.java index 397cb06..4b8142c 100644 --- a/src/main/java/com/raeids/stratus/mixin/GuiChatMixin.java +++ b/src/main/java/com/raeids/stratus/mixin/GuiChatMixin.java @@ -21,10 +21,13 @@ import java.awt.datatransfer.StringSelection; @Mixin(GuiChat.class) public abstract class GuiChatMixin extends GuiScreen { + private CleanSearchButton searchButton; + @Inject(method = "initGui", at = @At("TAIL")) private void init(CallbackInfo ci) { if (StratusConfig.INSTANCE.getChatSearch()) { - ChatSearchingKt.initGui(); + searchButton = new CleanSearchButton(); + buttonList.add(searchButton); } if (StratusConfig.INSTANCE.getChatTabs()) { for (ChatTab chatTab : ChatTabs.INSTANCE.getTabs()) { @@ -33,43 +36,28 @@ public abstract class GuiChatMixin extends GuiScreen { } } - @Inject(method = "drawScreen", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiTextField;drawTextBox()V", shift = At.Shift.AFTER)) - private void yeah(int mouseX, int mouseY, float partialTicks, CallbackInfo ci) { - if (ChatSearchingKt.getInputField() != null) { - ChatSearchingKt.getInputField().drawTextBox(); - } - } - - @Inject(method = "onGuiClosed", at = @At("TAIL")) - private void onGuiClosed(CallbackInfo ci) { - ChatSearchingKt.setInputField(null); - ChatSearchingKt.setPrevText(""); - } - @Inject(method = "updateScreen", at = @At("HEAD")) private void updateScreen(CallbackInfo ci) { - ChatSearchingKt.updateScreen(); + if (StratusConfig.INSTANCE.getChatSearch() && searchButton.isEnabled()) { + searchButton.getInputField().updateCursorCounter(); + } } @Inject(method = "keyTyped", at = @At("HEAD"), cancellable = true) private void keyTyped(char typedChar, int keyCode, CallbackInfo ci) { - if (ChatSearchingKt.getInputField() != null) { - if (ChatSearchingKt.getInputField().isFocused()) { - ci.cancel(); - if (keyCode == 1 && ChatSearchingKt.getInputField().isFocused()) { - ChatSearchingKt.getInputField().setFocused(false); - return; - } - ChatSearchingKt.getInputField().textboxKeyTyped(typedChar, keyCode); + if (StratusConfig.INSTANCE.getChatSearch() && searchButton.isEnabled()) { + ci.cancel(); + if (keyCode == 1) { + searchButton.onMousePress(); + return; } + searchButton.getInputField().textboxKeyTyped(typedChar, keyCode); + ChatSearchingKt.setPrevText(searchButton.getInputField().getText()); } } @Inject(method = "mouseClicked", at = @At("HEAD")) private void mouseClicked(int mouseX, int mouseY, int mouseButton, CallbackInfo ci) { - if (ChatSearchingKt.getInputField() != null) { - ChatSearchingKt.getInputField().mouseClicked(mouseX, mouseY, mouseButton); - } GuiNewChatHook hook = ((GuiNewChatHook) Minecraft.getMinecraft().ingameGUI.getChatGUI()); float f = mc.ingameGUI.getChatGUI().getChatScale(); int x = MathHelper.floor_float((float) mouseX / f); diff --git a/src/main/java/com/raeids/stratus/mixin/GuiNewChatMixin.java b/src/main/java/com/raeids/stratus/mixin/GuiNewChatMixin.java index c831528..8d2602b 100644 --- a/src/main/java/com/raeids/stratus/mixin/GuiNewChatMixin.java +++ b/src/main/java/com/raeids/stratus/mixin/GuiNewChatMixin.java @@ -36,6 +36,7 @@ public abstract class GuiNewChatMixin extends Gui implements GuiNewChatHook { @Shadow @Final private List<ChatLine> drawnChatLines; @SuppressWarnings({"FieldCanBeLocal", "unused"}) private float percentComplete; + private String stratus$previousText = ""; @Shadow public abstract boolean getChatOpen(); @@ -65,6 +66,7 @@ public abstract class GuiNewChatMixin extends Gui implements GuiNewChatHook { @Inject(method = "setChatLine", at = @At("HEAD"), cancellable = true) private void handleSetChatLine(IChatComponent chatComponent, int chatLineId, int updateCounter, boolean displayOnly, CallbackInfo ci) { + ChatSearchingKt.getCache().invalidateAll(); handleChatTabMessage(chatComponent, chatLineId, updateCounter, displayOnly, ci); } @@ -88,7 +90,6 @@ public abstract class GuiNewChatMixin extends Gui implements GuiNewChatHook { : linesToDraw; } - //TODO: fix with betterchat @ModifyArgs(method = "drawChat", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiNewChat;drawRect(IIIII)V"), slice = @Slice(from = @At(value = "INVOKE", target = "Lnet/minecraft/util/MathHelper;clamp_double(DDD)D"), to = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/GlStateManager;enableBlend()V"))) private void captureDrawRect(Args args) { int left = args.get(0); @@ -110,7 +111,7 @@ public abstract class GuiNewChatMixin extends Gui implements GuiNewChatHook { @Redirect(method = "drawChat", at = @At(value = "FIELD", target = "Lnet/minecraft/client/gui/GuiNewChat;drawnChatLines:Ljava/util/List;", opcode = Opcodes.GETFIELD)) private List<ChatLine> injected(GuiNewChat instance) { - return ChatSearchingKt.filterMessages(drawnChatLines); + return ChatSearchingKt.filterMessages(stratus$previousText, drawnChatLines); } @Inject(method = "drawChat", at = @At("RETURN")) @@ -199,4 +200,14 @@ public abstract class GuiNewChatMixin extends Gui implements GuiNewChatHook { } return null; } + + @Override + public String getPrevText() { + return stratus$previousText; + } + + @Override + public void setPrevText(String prevText) { + stratus$previousText = prevText; + } } diff --git a/src/main/kotlin/com/raeids/stratus/Stratus.kt b/src/main/kotlin/com/raeids/stratus/Stratus.kt index eafff31..d771915 100644 --- a/src/main/kotlin/com/raeids/stratus/Stratus.kt +++ b/src/main/kotlin/com/raeids/stratus/Stratus.kt @@ -12,6 +12,7 @@ import gg.essential.universal.UDesktop import net.minecraft.client.Minecraft import net.minecraft.client.gui.FontRenderer import net.minecraft.client.gui.GuiChat +import net.minecraft.client.renderer.GlStateManager import net.minecraft.client.settings.KeyBinding import net.minecraft.client.shader.Framebuffer import net.minecraftforge.common.MinecraftForge.EVENT_BUS @@ -108,6 +109,8 @@ object Stratus { w = fr.getStringWidth(chatLines.stream().max(Comparator.comparingInt { obj: String -> obj.length }).get()) } val fb: Framebuffer = RenderHelper.createBindFramebuffer(w, h) + GlStateManager.translate(-2f, (160 - (180 - h)).toFloat(), 0f) + chat.drawChat(hud.updateCounter) val file = File(Minecraft.getMinecraft().mcDataDir, "screenshots/chat/" + fileFormatter.format(Date())) RenderHelper.screenshotFramebuffer(fb, file) Minecraft.getMinecraft().entityRenderer.setupOverlayRendering() diff --git a/src/main/kotlin/com/raeids/stratus/config/StratusConfig.kt b/src/main/kotlin/com/raeids/stratus/config/StratusConfig.kt index 8841700..e9d2401 100644 --- a/src/main/kotlin/com/raeids/stratus/config/StratusConfig.kt +++ b/src/main/kotlin/com/raeids/stratus/config/StratusConfig.kt @@ -2,6 +2,7 @@ package com.raeids.stratus.config import com.raeids.stratus.Stratus import com.raeids.stratus.gui.ChatShortcutViewGui +import com.raeids.stratus.hook.ChatShortcuts import com.raeids.stratus.hook.ChatTab import com.raeids.stratus.hook.ChatTabs import com.raeids.stratus.updater.DownloadGui @@ -107,7 +108,18 @@ object StratusConfig : Vigilant(File(Stratus.modDir, "${Stratus.ID}.toml"), Stra initialize() registerListener("chatTabs") { funny: Boolean -> chatTabs = funny - ChatTabs.currentTab = ChatTab("ALL", false, null, null, null, null, null, "") + ChatTabs.initialize() + if (!funny) { + val dummy = ChatTab("ALL", false, null, null, null, null, null, "") + dummy.initialize() + ChatTabs.currentTab = dummy + } else { + ChatTabs.currentTab = ChatTabs.tabs[0] + } + } + registerListener("chatShortcuts") { funny: Boolean -> + chatShortcuts = funny + ChatShortcuts.initialize() } } }
\ No newline at end of file diff --git a/src/main/kotlin/com/raeids/stratus/gui/ChatShortcutEditGui.kt b/src/main/kotlin/com/raeids/stratus/gui/ChatShortcutEditGui.kt index ef7baf5..ce64c6a 100644 --- a/src/main/kotlin/com/raeids/stratus/gui/ChatShortcutEditGui.kt +++ b/src/main/kotlin/com/raeids/stratus/gui/ChatShortcutEditGui.kt @@ -49,6 +49,8 @@ class ChatShortcutEditGui(private var alias: String, private var command: String } childOf window } ButtonComponent("Save") { + alias = alias.substringAfter("/") + command = alias.substringAfter("/") if (editing) { ChatShortcuts.removeShortcut(initialAlias) } @@ -67,7 +69,7 @@ class ChatShortcutEditGui(private var alias: String, private var command: String } childOf window } - inner class ChatShortcutConfirmGui(var alias: String, var command: String): WindowScreen(restoreCurrentGuiOnClose = true) { + inner class ChatShortcutConfirmGui(private var alias: String, private var command: String): WindowScreen(restoreCurrentGuiOnClose = true) { override fun initScreen(width: Int, height: Int) { super.initScreen(width, height) EssentialAPI.getEssentialComponentFactory().buildConfirmationModal { diff --git a/src/main/kotlin/com/raeids/stratus/hook/ChatSearching.kt b/src/main/kotlin/com/raeids/stratus/hook/ChatSearching.kt index c056161..57cfc93 100644 --- a/src/main/kotlin/com/raeids/stratus/hook/ChatSearching.kt +++ b/src/main/kotlin/com/raeids/stratus/hook/ChatSearching.kt @@ -1,41 +1,41 @@ package com.raeids.stratus.hook +import gg.essential.lib.caffeine.cache.Cache +import gg.essential.lib.caffeine.cache.Caffeine import gg.essential.universal.wrappers.message.UTextComponent import net.minecraft.client.Minecraft import net.minecraft.client.gui.ChatLine -import net.minecraft.client.gui.GuiTextField -import net.minecraft.client.gui.ScaledResolution +import java.util.concurrent.LinkedBlockingQueue +import java.util.concurrent.ThreadPoolExecutor +import java.util.concurrent.TimeUnit +import java.util.concurrent.atomic.AtomicInteger -var inputField: GuiTextField? = null -var sr: ScaledResolution? = null -var prevText = "" - -fun initGui() { - sr = ScaledResolution(Minecraft.getMinecraft()) - inputField = GuiTextField( - 694209000, - Minecraft.getMinecraft().fontRendererObj, - sr!!.scaledWidth * 4 / 5 - 1, - sr!!.scaledHeight - 13, - sr!!.scaledWidth / 5, - 12 +private var counter: AtomicInteger = AtomicInteger(0) +private var POOL: ThreadPoolExecutor = ThreadPoolExecutor( + 50, 50, + 0L, TimeUnit.SECONDS, + LinkedBlockingQueue() +) { r -> + Thread( + r, + "Chat Filter Cache Thread ${counter.incrementAndGet()}" ) - inputField!!.maxStringLength = 100 - inputField!!.enableBackgroundDrawing = true - inputField!!.isFocused = false - inputField!!.text = "" - inputField!!.setCanLoseFocus(true) - prevText = "" } -fun updateScreen() { - inputField?.updateCursorCounter() -} +val cache: Cache<String, List<ChatLine?>> = Caffeine.newBuilder().executor(POOL).maximumSize(5000).build() -fun filterMessages(list: List<ChatLine?>?): List<ChatLine?>? { - if (inputField == null || list == null || inputField?.text.isNullOrBlank()) return list - return list.filter { - it != null && UTextComponent.stripFormatting(it.chatComponent.unformattedText).lowercase() - .contains(inputField!!.text!!.lowercase()) +fun filterMessages(text: String, list: List<ChatLine?>?): List<ChatLine?>? { + if (list.isNullOrEmpty() || text.isBlank()) return list + val cached = cache.getIfPresent(text) + return cached ?: run { + cache.put(text, list.filter { + it != null && UTextComponent.stripFormatting(it.chatComponent.unformattedText).lowercase() + .contains(text.lowercase()) + }) + cache.getIfPresent(text) } +} + +fun setPrevText(text: String) { + (Minecraft.getMinecraft().ingameGUI.chatGUI as GuiNewChatHook).prevText = text }
\ No newline at end of file diff --git a/src/main/kotlin/com/raeids/stratus/hook/ChatShortcuts.kt b/src/main/kotlin/com/raeids/stratus/hook/ChatShortcuts.kt index dfea873..10a2af9 100644 --- a/src/main/kotlin/com/raeids/stratus/hook/ChatShortcuts.kt +++ b/src/main/kotlin/com/raeids/stratus/hook/ChatShortcuts.kt @@ -9,10 +9,17 @@ object ChatShortcuts { private val shortcutsFile = File(Stratus.modDir, "chatshortcuts.json") private val PARSER = JsonParser() + private var initialized = false + val shortcuts = mutableSetOf<Pair<String, String>>() fun initialize() { + if (initialized) { + return + } else { + initialized = true + } if (!shortcutsFile.exists()) { shortcutsFile.createNewFile() shortcutsFile.writeText( diff --git a/src/main/kotlin/com/raeids/stratus/hook/ChatTab.kt b/src/main/kotlin/com/raeids/stratus/hook/ChatTab.kt index 3609fdc..2f3a729 100644 --- a/src/main/kotlin/com/raeids/stratus/hook/ChatTab.kt +++ b/src/main/kotlin/com/raeids/stratus/hook/ChatTab.kt @@ -16,18 +16,18 @@ data class ChatTab( @SerializedName("regex") val uncompiledRegex: List<String>?, val prefix: String ) { - lateinit var button: CleanButton + lateinit var button: CleanTabButton lateinit var compiledRegex: ChatRegexes //Ugly hack to make GSON not make button / regex null fun initialize() { compiledRegex = ChatRegexes(uncompiledRegex) val width = Minecraft.getMinecraft().fontRendererObj.getStringWidth(name) - button = CleanButton(653452, runBlocking { + button = CleanTabButton(653452, runBlocking { val returnValue = x - 2 x += 6 + width return@runBlocking returnValue - }, 0, width + 4, 12, this) + }, width + 4, 12, this) } fun shouldRender(chatComponent: IChatComponent): Boolean { diff --git a/src/main/kotlin/com/raeids/stratus/hook/ChatTabs.kt b/src/main/kotlin/com/raeids/stratus/hook/ChatTabs.kt index 0c371d1..a994f3d 100644 --- a/src/main/kotlin/com/raeids/stratus/hook/ChatTabs.kt +++ b/src/main/kotlin/com/raeids/stratus/hook/ChatTabs.kt @@ -19,10 +19,16 @@ object ChatTabs { } } } + private var initialized = false private val tabFile = File(Stratus.modDir, "chattabs.json") fun initialize() { + if (initialized) { + return + } else { + initialized = true + } if (!tabFile.exists()) { generateNewFile() } else { diff --git a/src/main/kotlin/com/raeids/stratus/hook/CleanButton.kt b/src/main/kotlin/com/raeids/stratus/hook/CleanButton.kt index 38e44ad..bc4db34 100644 --- a/src/main/kotlin/com/raeids/stratus/hook/CleanButton.kt +++ b/src/main/kotlin/com/raeids/stratus/hook/CleanButton.kt @@ -2,7 +2,6 @@ package com.raeids.stratus.hook import club.sk1er.patcher.config.PatcherConfig import com.raeids.stratus.Stratus -import gg.essential.universal.UResolution import net.minecraft.client.Minecraft import net.minecraft.client.gui.GuiButton import net.minecraft.client.renderer.GlStateManager @@ -13,21 +12,30 @@ import java.awt.Color * https://github.com/P0keDev/ChatShortcuts/blob/master/LICENSE * @author P0keDev */ -class CleanButton(buttonId: Int, x: Int, y: Int, widthIn: Int, heightIn: Int, private val chatTab: ChatTab) : - GuiButton(buttonId, x, y, widthIn, heightIn, chatTab.name) { +open class CleanButton(buttonId: Int, private val x: () -> Int, private val y: () -> Int, widthIn: Int, heightIn: Int, name: String) : + GuiButton(buttonId, x.invoke(), 0, widthIn, heightIn, name) { + + open fun isEnabled(): Boolean { + return false + } + + open fun onMousePress() { + + } override fun mousePressed(mc: Minecraft, mouseX: Int, mouseY: Int): Boolean { val isPressed = - enabled && visible && mouseX >= xPosition && mouseY >= yPosition && mouseX < xPosition + width && mouseY < yPosition + height + visible && mouseX >= xPosition && mouseY >= yPosition && mouseX < xPosition + width && mouseY < yPosition + height if (isPressed) { - ChatTabs.currentTab = chatTab + onMousePress() } return isPressed } override fun drawButton(mc: Minecraft, mouseX: Int, mouseY: Int) { - enabled = chatTab != ChatTabs.currentTab - yPosition = UResolution.scaledHeight - 26 + enabled = isEnabled() + xPosition = x.invoke() + yPosition = y.invoke() if (visible) { val fontrenderer = mc.fontRendererObj GlStateManager.color(1.0f, 1.0f, 1.0f, 1.0f) diff --git a/src/main/kotlin/com/raeids/stratus/hook/CleanSearchButton.kt b/src/main/kotlin/com/raeids/stratus/hook/CleanSearchButton.kt new file mode 100644 index 0000000..4b7ba5a --- /dev/null +++ b/src/main/kotlin/com/raeids/stratus/hook/CleanSearchButton.kt @@ -0,0 +1,46 @@ +package com.raeids.stratus.hook + +import gg.essential.universal.UResolution +import net.minecraft.client.Minecraft +import net.minecraft.client.gui.GuiTextField + +class CleanSearchButton: CleanButton(3993935, {UResolution.scaledWidth - 42}, {UResolution.scaledHeight - 27}, 40, 12, "Search") { + val inputField = SearchTextField() + private var chatBox = false + + override fun isEnabled(): Boolean { + return chatBox + } + + override fun onMousePress() { + println("hi") + chatBox = !chatBox + inputField.setEnabled(chatBox) + inputField.isFocused = chatBox + (Minecraft.getMinecraft().ingameGUI.chatGUI as GuiNewChatHook).prevText = "" + inputField.text = "" + } + + override fun drawButton(mc: Minecraft, mouseX: Int, mouseY: Int) { + inputField.drawTextBox() + super.drawButton(mc, mouseX, mouseY) + } + + inner class SearchTextField: GuiTextField(69420, Minecraft.getMinecraft().fontRendererObj, UResolution.scaledWidth * 4 / 5 - 60, UResolution.scaledHeight - 27, UResolution.scaledWidth / 5, 12) { + + init { + maxStringLength = 100 + enableBackgroundDrawing = true + isFocused = false + text = "" + setCanLoseFocus(true) + } + + override fun drawTextBox() { + if (isEnabled()) { + if (!isFocused) isFocused = true + super.drawTextBox() + } + } + } +}
\ No newline at end of file diff --git a/src/main/kotlin/com/raeids/stratus/hook/CleanTabButton.kt b/src/main/kotlin/com/raeids/stratus/hook/CleanTabButton.kt new file mode 100644 index 0000000..0277dd8 --- /dev/null +++ b/src/main/kotlin/com/raeids/stratus/hook/CleanTabButton.kt @@ -0,0 +1,17 @@ +package com.raeids.stratus.hook + +import gg.essential.universal.UResolution + +class CleanTabButton(buttonId: Int, x: Int, widthIn: Int, heightIn: Int, private val chatTab: ChatTab) : + CleanButton(buttonId, { x }, { + UResolution.scaledHeight - 26 + }, widthIn, heightIn, chatTab.name) { + + override fun onMousePress() { + ChatTabs.currentTab = chatTab + } + + override fun isEnabled(): Boolean { + return chatTab != ChatTabs.currentTab + } +}
\ No newline at end of file |