Skip to content

Commit

Permalink
add example macros
Browse files Browse the repository at this point in the history
  • Loading branch information
CheatCod committed Jun 18, 2024
1 parent 9914ce6 commit e192634
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
7 changes: 7 additions & 0 deletions example-macros/README.md
Original file line number Diff line number Diff line change
@@ -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)
48 changes: 48 additions & 0 deletions example-macros/auto-backup/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { format } from "https://deno.land/[email protected]/datetime/format.ts";
import { copy } from "https://deno.land/[email protected]/fs/copy.ts";
import { sleep } from "https://deno.land/x/[email protected]/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);
}

0 comments on commit e192634

Please sign in to comment.