From 608f8c2547a1b24097fa3e409ea33b1c69ce7c7f Mon Sep 17 00:00:00 2001 From: Izuna Seikatsu Date: Tue, 10 Nov 2026 05:06:01 +0000 Subject: [PATCH] feat(examples): AutoL and AutoGG --- .github/workflows/build.yml | 2 + examples/README.md | 2 + examples/autol/main.js | 133 ++++++++++++++++++ .../script/gametest/ExampleScriptsGameTest.kt | 26 ++++ 4 files changed, 163 insertions(+) create mode 100644 examples/autol/main.js diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b8387b1..102ba80 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -151,6 +151,8 @@ jobs: include: - script: nes_emulator item-id: ${{ vars.NES_EMULATOR_ITEM_ID }} + - script: autol + item-id: ${{ vars.AUTOL_ITEM_ID }} steps: - name: Checkout repository uses: actions/checkout@v7 diff --git a/examples/README.md b/examples/README.md index b791520..88875ee 100644 --- a/examples/README.md +++ b/examples/README.md @@ -12,4 +12,6 @@ The following examples are currently available: - [translator.js](translator.js): Translates text through Google Translate with `.gtranslate `. +- [autol](autol): Writes a message to every player you kill (AutoL) and says gg when a game ends (AutoGG). + - [nes_emulator](nes_emulator): Plays NES games in the client. ROMs go in `LiquidBounce/scripts/nes_emulator/roms`. diff --git a/examples/autol/main.js b/examples/autol/main.js new file mode 100644 index 0000000..9fcc746 --- /dev/null +++ b/examples/autol/main.js @@ -0,0 +1,133 @@ +/* +AutoL writes a message to every player you kill, AutoGG says "gg" when a game ends. + +AutoL listens to the playerKill event, which reads kills from death messages in chat and +needs a LiquidBounce build that has it. {name} in a message is the name of the killed player. + +AutoGG matches chat lines and titles against regular expressions. The defaults cover the +usual end-of-game summaries ("1st Killer - ...", "Winner: ...") and titles ("VICTORY!"). +*/ +const script = registerScript({ + name: "AutoL", + version: "1.0.0", + authors: ["Tsikyng Kirisame", "CzechHek", "1zuna"] +}); + +function plain(text) { + return String(text).replace(/ยง./g, ""); +} + +function pick(list) { + const entries = Java.from(list).filter((entry) => entry.trim().length > 0); + return entries[Math.floor(Math.random() * entries.length)]; +} + +function matchesAny(patterns, text) { + return Java.from(patterns).some((pattern) => { + try { + return new RegExp(pattern, "i").test(text); + } catch (e) { + return false; + } + }); +} + +script.registerModule({ + name: "AutoL", + category: "Misc", + description: "Writes a message to the player you killed.", + settings: { + messages: Setting.textArray({ + name: "Messages", + default: ["L {name}"] + }), + cooldown: Setting.int({ + name: "Cooldown", + default: 1000, + range: [0, 5000], + suffix: "ms" + }) + } +}, (mod) => { + let lastSent = 0; + + mod.on("playerKill", (event) => { + const now = Date.now(); + const message = pick(mod.settings.messages.value); + if (!message || now - lastSent < mod.settings.cooldown.value) { + return; + } + + lastSent = now; + NetworkUtil.sendChatMessage(message.replaceAll("{name}", event.getVictim())); + }); +}); + +script.registerModule({ + name: "AutoGG", + category: "Misc", + description: "Says gg when a game ends.", + settings: { + message: Setting.text({ + name: "Message", + default: "gg" + }), + delay: Setting.int({ + name: "Delay", + default: 20, + range: [0, 100], + suffix: "ticks" + }), + chatTriggers: Setting.textArray({ + name: "ChatTriggers", + default: [ + "^ +1st Killer ?- ", + "^ *1st (Place )?[-:] ", + "^ +Winners?(: | - )", + "^ +Winning Team ?[-:] " + ] + }), + titleTriggers: Setting.textArray({ + name: "TitleTriggers", + default: [ + "^(VICTORY|GAME OVER|GAME END|YOU WIN|YOU WON)!?$" + ] + }) + } +}, (mod) => { + // One gg per game, the next world is the next game + let said = false; + + async function gameEnded() { + if (said) { + return; + } + said = true; + + await AsyncUtil.ticks(mod.settings.delay.value); + if (mod.enabled && mc.player) { + NetworkUtil.sendChatMessage(mod.settings.message.value); + } + } + + mod.on("enable", () => { + said = false; + }); + + mod.on("worldChange", () => { + said = false; + }); + + mod.on("chatReceive", (event) => { + if (matchesAny(mod.settings.chatTriggers.value, plain(event.getMessage()))) { + gameEnded(); + } + }); + + mod.on("title", (event) => { + const text = event.getText(); + if (text && matchesAny(mod.settings.titleTriggers.value, plain(text.getString()).trim())) { + gameEnded(); + } + }); +}); diff --git a/src/gametest/kotlin/net/ccbluex/liquidbounce/script/gametest/ExampleScriptsGameTest.kt b/src/gametest/kotlin/net/ccbluex/liquidbounce/script/gametest/ExampleScriptsGameTest.kt index 0cef36f..3ab91e7 100644 --- a/src/gametest/kotlin/net/ccbluex/liquidbounce/script/gametest/ExampleScriptsGameTest.kt +++ b/src/gametest/kotlin/net/ccbluex/liquidbounce/script/gametest/ExampleScriptsGameTest.kt @@ -21,6 +21,8 @@ package net.ccbluex.liquidbounce.script.gametest import com.google.gson.JsonParser import com.mojang.blaze3d.platform.InputConstants import com.mojang.blaze3d.platform.NativeImage +import net.ccbluex.liquidbounce.event.EventManager +import net.ccbluex.liquidbounce.event.events.TitleEvent import net.ccbluex.liquidbounce.features.addon.AddonManager import net.ccbluex.liquidbounce.features.addon.AddonState import net.ccbluex.liquidbounce.features.command.CommandManager @@ -29,6 +31,7 @@ import net.ccbluex.liquidbounce.features.module.ModuleManager import net.ccbluex.liquidbounce.script.ScriptManager import net.fabricmc.fabric.api.client.gametest.v1.FabricClientGameTest import net.fabricmc.fabric.api.client.gametest.v1.context.ClientGameTestContext +import net.minecraft.network.chat.Component import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.async @@ -61,6 +64,7 @@ class ExampleScriptsGameTest : FabricClientGameTest { harness.case("AutoJump-2.js") { autoJump("AutoJump-2.js") } harness.case("macros.js") { macros() } harness.case("translator.js") { translator() } + harness.case("autol") { autoL() } harness.case("nes_emulator") { nesEmulator() } harness.runCases() } @@ -203,6 +207,28 @@ private fun Case.translator(): String { error("translation failed: $reply") } +private fun Case.autoL(): String { + load("autol/main.js") + val player = client { it.user.name } + + val autoGG = module("AutoGG") + client { autoGG.enabled = true } + client { EventManager.callEvent(TitleEvent.Title(Component.literal("VICTORY!"))) } + awaitChat(100) { it.endsWith("<$player> gg") } ?: error("no gg after the victory title") + client { autoGG.enabled = false } + + val autoL = module("AutoL") + // Up to 0.42.0 the client does not tell scripts about kills + val killEvent = client { EventManager.eventClassByName("playerKill") } + ?: return "gg after the victory title; this client has no playerKill event" + client { autoL.enabled = true } + client { EventManager.callEvent(killEvent.getConstructor(String::class.java).newInstance("Enemy")) } + awaitChat(40) { it.endsWith("<$player> L Enemy") } ?: error("no message after the kill") + client { autoL.enabled = false } + + return "gg after the victory title, L after the kill" +} + private const val NESTEST_URL = "https://raw.githubusercontent.com/christopherpow/nes-test-roms/" + "95d8f621ae55cee0d09b91519a8989ae0e64753b/other/nestest.nes" private const val NESTEST_SHA256 = "f67d55fd6b3cf0bad1cc85f1df0d739c65b53e79cecb7fea8f77ec0eadab0004"