diff --git a/example-macros/README.md b/example-macros/README.md new file mode 100644 index 00000000..09102a61 --- /dev/null +++ b/example-macros/README.md @@ -0,0 +1,7 @@ +This folder contains example macros that can be used as a starting point for creating your own macros. + +Note: make sure your macro is in the correct format. The macro should be a folder with the `index.ts` or `index.js` file at the root of the folder as the entry point. + +PRs are welcome! If you have a macro you'd like to share, feel free to create a PR to add it to this folder. + +Read more about creating macros [here](https://github.com/Lodestone-Team/lodestone/wiki/Intro-to-Macro-and-Task) \ No newline at end of file diff --git a/example-macros/auto-backup/index.ts b/example-macros/auto-backup/index.ts new file mode 100644 index 00000000..3d3e4c9f --- /dev/null +++ b/example-macros/auto-backup/index.ts @@ -0,0 +1,48 @@ +import { format } from "https://deno.land/std@0.177.1/datetime/format.ts"; +import { copy } from "https://deno.land/std@0.191.0/fs/copy.ts"; +import { sleep } from "https://deno.land/x/sleep@v1.2.1/mod.ts"; +import { EventStream } from "https://raw.githubusercontent.com/Lodestone-Team/lodestone-macro-lib/main/events.ts"; +import { lodestoneVersion } from "https://raw.githubusercontent.com/Lodestone-Team/lodestone-macro-lib/main/prelude.ts"; +import { MinecraftJavaInstance } from "https://raw.githubusercontent.com/Lodestone-Team/lodestone-macro-lib/main/instance.ts"; + +const currentInstance = await MinecraftJavaInstance.current(); + +const eventStream = new EventStream( + currentInstance.getUUID(), + await currentInstance.name() +); + +// Lodestone will parse the configuration class and inject the configuration into the macro +class LodestoneConfig { + // Where to store the backups relative to the instance path + backupFolderRelative: string = "backups"; + // How long to wait between backups in seconds + delaySec: number = 3600; +} + +// not technically necessary, but it's a good practice to appease the linter +declare const config: LodestoneConfig; + +// make sure the config is injected properly +console.log(config); + +const instancePath = await currentInstance.path(); +const backupFolder = `${instancePath}/${config.backupFolderRelative}`; +EventStream.emitDetach(); +while (true) { + eventStream.emitConsoleOut("[Backup Macro] Backing up world..."); + if ((await currentInstance.state()) == "Stopped") { + eventStream.emitConsoleOut("[Backup Macro] Instance stopped, exiting..."); + break; + } + + const now = new Date(); + const now_str = format(now, "yy-MM-dd_HH"); + try { + await copy(`${instancePath}/world`, `${backupFolder}/backup_${now_str}`); + } catch (e) { + console.log(e); + } + + await sleep(config.delaySec); +}