Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ The following examples are currently available:

- [translator.js](translator.js): Translates text through Google Translate with `.gtranslate <from> <to> <text>`.

- [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`.
133 changes: 133 additions & 0 deletions examples/autol/main.js
Original file line number Diff line number Diff line change
@@ -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();
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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"
Expand Down
Loading