@miner-org/mineflayer-bot-war 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ import { BotWarClient } from "./src/types/BotWarClient";
2
+ import { WarClientOptions } from "./src/types/WarClientOptions";
3
+
4
+ interface BotWarPlugin {
5
+ client: BotWarClient;
6
+ connect(options: WarClientOptions): void;
7
+ }
8
+
9
+ declare module "@miner-org/mineflayer-bot-war" {
10
+ import { Bot } from "mineflayer";
11
+
12
+ export function loader(bot: Bot): void;
13
+ }
14
+
15
+ declare module "mineflayer" {
16
+ interface Bot {
17
+ botwar: BotWarPlugin;
18
+ }
19
+ }
package/index.js ADDED
@@ -0,0 +1,7 @@
1
+ const BotWarPlugin = require("./src/BotWarPlugin");
2
+
3
+ function loader(bot) {
4
+ bot.botwar = new BotWarPlugin(bot);
5
+ }
6
+
7
+ module.exports = loader;
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@miner-org/mineflayer-bot-war",
3
+ "version": "0.0.2",
4
+ "main": "index.js",
5
+ "scripts": {
6
+ "git-update": "git add . && git commit -m \"my butt hurts\" && git push",
7
+ "npm-publish-patch": "npm version patch && npm publish --access public && git push origin main --tags",
8
+ "npm-publish-minor": "npm version minor && npm publish --access public && git push origin main --tags",
9
+ "npm-publish-major": "npm version major && npm publish --access public && git push origin main --tags",
10
+ "update-all-patch": "npm run git-update && npm run npm-publish-patch",
11
+ "update-all-minor": "npm run git-update && npm run npm-publish-minor",
12
+ "update-all-major": "npm run git-update && npm run npm-publish-major"
13
+ },
14
+ "keywords": [],
15
+ "author": "Ash",
16
+ "license": "ISC",
17
+ "description": "A mineflayer plugin for the java plugin bot-war",
18
+ "dependencies": {
19
+ "ws": "^8.19.0"
20
+ },
21
+ "devDependencies": {
22
+ "mineflayer": "^4.25.0"
23
+ }
24
+ }
@@ -0,0 +1,82 @@
1
+ const EventEmitter = require("events");
2
+ const { WebSocket } = require("ws");
3
+ const { v4: uuid } = require("uuid");
4
+
5
+ class BotWarClient extends EventEmitter {
6
+ constructor(options = {}) {
7
+ super();
8
+ /**
9
+ * @type {import ("./types/WarClientOptions").WarClientOptions}
10
+ */
11
+ this.options = options;
12
+ this.ws = new WebSocket(options.url);
13
+ this.ready = false;
14
+ this.pending = new Map();
15
+
16
+ this.ws.on("open", () => this._authenticate());
17
+ this.ws.on("message", (data) => this._handleMessage(data.toString()));
18
+ this.ws.on("close", () => (this.ready = false));
19
+ }
20
+
21
+ _authenticate() {
22
+ this.ws.send(
23
+ JSON.stringify({
24
+ type: "auth",
25
+ token: this.options.token,
26
+ }),
27
+ );
28
+ }
29
+
30
+ _handleMessage(msg) {
31
+ const packet = JSON.parse(msg);
32
+ if (packet.type === "auth_ok") {
33
+ this.ready = true;
34
+ this.emit("ready");
35
+ return;
36
+ }
37
+
38
+ if (packet.type === "response" && packet.id) {
39
+ const cb = this.pending.get(packet.id);
40
+ if (cb) {
41
+ this.pending.delete(packet.id);
42
+ cb(packet.data);
43
+ }
44
+ return;
45
+ }
46
+
47
+ if (packet.type === "event") {
48
+ this.emit(packet.event, packet.data);
49
+ }
50
+ }
51
+
52
+ request(action, payload = {}) {
53
+ if (!this.ready) return Promise.reject("BotWar not authenticated");
54
+
55
+ return new Promise((resolve) => {
56
+ const id = uuid();
57
+ this.pending.set(id, resolve);
58
+ this.ws.send(JSON.stringify({ type: "request", id, action, payload }));
59
+ });
60
+ }
61
+
62
+ getTeams() {
63
+ return this.request("getTeams");
64
+ }
65
+
66
+ getControlPoints() {
67
+ return this.request("getControlPoints");
68
+ }
69
+
70
+ getTeamScore(teamId) {
71
+ return this.request("getTeamScore", { teamId });
72
+ }
73
+
74
+ getTeamPlayers(teamId) {
75
+ return this.request("getTeamPlayers", { teamId });
76
+ }
77
+
78
+ getPlayerTeam(playerName) {
79
+ return this.request("getPlayerTeam", { playerName });
80
+ }
81
+ }
82
+ module.exports = BotWarClient;
@@ -0,0 +1,19 @@
1
+ const BotWarClient = require("./BotWarClient.js");
2
+
3
+ class BotWarPlugin {
4
+ #bot;
5
+ constructor(bot) {
6
+ this.#bot = bot;
7
+ this.client = null;
8
+ }
9
+
10
+ /**
11
+ *
12
+ * @param {import("./types/WarClientOptions.js").WarClientOptions} options
13
+ */
14
+ connect(options) {
15
+ this.client = new BotWarClient(options);
16
+ }
17
+ }
18
+
19
+ module.exports = BotWarPlugin;
@@ -0,0 +1,45 @@
1
+ import TypedEmitter from "typed-emitter";
2
+
3
+ interface SimpleVec3 {
4
+ x: number;
5
+ y: number;
6
+ z: number;
7
+ }
8
+
9
+ export interface BotWarClientEvents {
10
+ ready(): void | Promise<void>;
11
+ /**
12
+ *
13
+ * @param killer The kilelr
14
+ * @param dead The one that died
15
+ * @param team The killer's team
16
+ */
17
+ playerKilled(
18
+ killer: string,
19
+ dead: string,
20
+ team: string,
21
+ ): void | Promise<void>;
22
+ gameStarted(): void | Promise<void>;
23
+ gameEnd(): void | Promise<void>;
24
+
25
+ teamWin(team: string): void | Promise<void>;
26
+
27
+ startCapture(position: SimpleVec3, captureTeam: string): void | Promise<void>;
28
+ /**
29
+ * @description Fires when a control point is captured
30
+ * @param position Position of the control point
31
+ * @param captureTeam The team that captured the point
32
+ */
33
+ controlCapture(
34
+ position: SimpleVec3,
35
+ captureTeam: string,
36
+ ): void | Promise<void>;
37
+ }
38
+
39
+ export interface BotWarClient extends TypedEmitter<BotWarClientEvents> {
40
+ authenticate(): void;
41
+
42
+ request(action: string, data: object): Promise<any>;
43
+
44
+ getTeams(): Promise<string[]>;
45
+ }
@@ -0,0 +1,4 @@
1
+ export interface WarClientOptions {
2
+ url: string,
3
+ token: string,
4
+ }
package/test.js ADDED
@@ -0,0 +1,41 @@
1
+ const mineflayer = require("mineflayer");
2
+ const botWarPlugin = require("./index.js");
3
+
4
+ const bot = mineflayer.createBot({
5
+ host: "localhost",
6
+ username: "Frisk",
7
+ port: 25565,
8
+ viewDistance: "tiny",
9
+ });
10
+
11
+ bot.loadPlugin(botWarPlugin);
12
+
13
+ bot.once("spawn", async () => {
14
+ await bot.waitForChunksToLoad();
15
+ bot.botwar.connect({
16
+ url: "ws://localhost:8765",
17
+ token: "92389d0ffe1547f1b41939abcc9f0b59",
18
+ });
19
+
20
+ bot.botwar.client.on("ready", async () => {
21
+ console.log("uwu");
22
+ const teams = await bot.botwar.client.getTeams();
23
+
24
+ console.log(teams);
25
+ });
26
+
27
+ bot.botwar.client.on("gameStarted", async () => {
28
+ console.log("A game has started");
29
+ bot.botwar.client.on("startCapture", (pos, team) => {
30
+ console.log(pos, team);
31
+ });
32
+
33
+ bot.botwar.client.on("controlCapture", (pos, team) => {
34
+ console.log("Captured", pos, team);
35
+ });
36
+ });
37
+
38
+ bot.botwar.client.on("gameEnd", () => {
39
+ console.log("A game has ended");
40
+ });
41
+ });