@miner-org/mineflayer-bot-war 0.0.2 → 0.1.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@miner-org/mineflayer-bot-war",
3
- "version": "0.0.2",
3
+ "version": "0.1.0",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "git-update": "git add . && git commit -m \"my butt hurts\" && git push",
@@ -3,8 +3,12 @@ const { WebSocket } = require("ws");
3
3
  const { v4: uuid } = require("uuid");
4
4
 
5
5
  class BotWarClient extends EventEmitter {
6
- constructor(options = {}) {
6
+ #bot;
7
+
8
+ constructor(bot, options = {}) {
7
9
  super();
10
+ this.#bot = bot;
11
+
8
12
  /**
9
13
  * @type {import ("./types/WarClientOptions").WarClientOptions}
10
14
  */
@@ -78,5 +82,9 @@ class BotWarClient extends EventEmitter {
78
82
  getPlayerTeam(playerName) {
79
83
  return this.request("getPlayerTeam", { playerName });
80
84
  }
85
+
86
+ getOwnTeam() {
87
+ return this.request("getPlayerTeam", { playerName: this.#bot.username });
88
+ }
81
89
  }
82
90
  module.exports = BotWarClient;
@@ -12,7 +12,7 @@ class BotWarPlugin {
12
12
  * @param {import("./types/WarClientOptions.js").WarClientOptions} options
13
13
  */
14
14
  connect(options) {
15
- this.client = new BotWarClient(options);
15
+ this.client = new BotWarClient(this.#bot, options);
16
16
  }
17
17
  }
18
18
 
@@ -1,45 +1,48 @@
1
1
  import TypedEmitter from "typed-emitter";
2
-
3
- interface SimpleVec3 {
4
- x: number;
5
- y: number;
6
- z: number;
7
- }
2
+ import {
3
+ PlayerKilledEvent,
4
+ TeamWinEvent,
5
+ StartCaptureEvent,
6
+ ControlCaptureEvent,
7
+ SimpleVec3,
8
+ } from "./Event";
8
9
 
9
10
  export interface BotWarClientEvents {
10
11
  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>;
12
+
22
13
  gameStarted(): void | Promise<void>;
23
14
  gameEnd(): void | Promise<void>;
24
15
 
25
- teamWin(team: string): void | Promise<void>;
16
+ /**
17
+ * Fired when a player is killed
18
+ */
19
+ playerKilled(data: PlayerKilledEvent): void | Promise<void>;
20
+
21
+ teamWin(data: TeamWinEvent): void | Promise<void>;
22
+
23
+ /**
24
+ * Fired when a team starts capturing a control point
25
+ */
26
+ startCapture(data: StartCaptureEvent): void | Promise<void>;
26
27
 
27
- startCapture(position: SimpleVec3, captureTeam: string): void | Promise<void>;
28
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
29
+ * Fired when a control point is fully captured
32
30
  */
33
- controlCapture(
34
- position: SimpleVec3,
35
- captureTeam: string,
36
- ): void | Promise<void>;
31
+ controlCapture(data: ControlCaptureEvent): void | Promise<void>;
37
32
  }
38
33
 
39
34
  export interface BotWarClient extends TypedEmitter<BotWarClientEvents> {
40
35
  authenticate(): void;
41
36
 
42
- request(action: string, data: object): Promise<any>;
37
+ request<T = unknown>(
38
+ action: string,
39
+ payload?: Record<string, unknown>,
40
+ ): Promise<T>;
43
41
 
44
- getTeams(): Promise<string[]>;
42
+ getTeams(): Promise<{ teams: string[] }>;
43
+ getControlPoints(): Promise<{ points: SimpleVec3[] }>;
44
+ getTeamScore(teamId: string): Promise<{ score: number }>;
45
+ getTeamPlayers(teamId: string): Promise<{ players: string[] }>;
46
+ getPlayerTeam(playerName: string): Promise<{ team: string }>;
47
+ getOwnTeam(): Promise<{ team: string }>;
45
48
  }
@@ -0,0 +1,25 @@
1
+ export interface SimpleVec3 {
2
+ x: number;
3
+ y: number;
4
+ z: number;
5
+ }
6
+
7
+ export interface PlayerKilledEvent {
8
+ killer: string;
9
+ dead: string;
10
+ team: string;
11
+ }
12
+
13
+ export interface TeamWinEvent {
14
+ team: string;
15
+ }
16
+
17
+ export interface StartCaptureEvent {
18
+ captureTeam: string;
19
+ position: SimpleVec3;
20
+ }
21
+
22
+ export interface ControlCaptureEvent {
23
+ captureTeam: string;
24
+ position: SimpleVec3;
25
+ }
package/test.js CHANGED
@@ -1,5 +1,6 @@
1
1
  const mineflayer = require("mineflayer");
2
2
  const botWarPlugin = require("./index.js");
3
+ const { Vec3 } = require("vec3");
3
4
 
4
5
  const bot = mineflayer.createBot({
5
6
  host: "localhost",
@@ -16,26 +17,53 @@ bot.once("spawn", async () => {
16
17
  url: "ws://localhost:8765",
17
18
  token: "92389d0ffe1547f1b41939abcc9f0b59",
18
19
  });
20
+ let teams = [];
19
21
 
20
22
  bot.botwar.client.on("ready", async () => {
21
- console.log("uwu");
22
- const teams = await bot.botwar.client.getTeams();
23
+ console.log("Authenticated!");
24
+ });
25
+
26
+ bot.on("messagestr", (msg, pos, chatMessage) => {
27
+ if (chatMessage.json.translate !== "chat.type.text") return;
28
+ const cleanName = username.replace(/[<>]/g, "").trim();
29
+ const usableMessage = Object.values(chatMessage.json.with[1])[0];
30
+ if (!usableMessage.startsWith(prefix)) return;
31
+
32
+ if (cleanName !== "AshLikesFood") return;
23
33
 
24
- console.log(teams);
34
+ const args = usableMessage.split(" ");
35
+ const command = args.shift();
36
+
37
+ switch (command) {
38
+ case "joinTeam": {
39
+ const team = args[0];
40
+
41
+ bot.chat(`/join-team ${team}`);
42
+ break;
43
+ }
44
+ }
25
45
  });
26
46
 
27
47
  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
- });
48
+ //make sure we are actually in a team;
49
+ const response = await bot.botwar.client.getOwnTeam();
50
+
51
+ if (!response || (response && response.team == null)) return;
52
+
53
+ //get nearest control point
54
+ const controlResponse = await bot.botwar.client.getControlPoints();
55
+
56
+ if (!controlResponse) return;
57
+
58
+ const points = controlResponse.points
59
+ .map((p) => new Vec3(p.x, p.y, p.z))
60
+ .sort(
61
+ (a, b) =>
62
+ bot.entity.position.distanceTo(a) - bot.entity.position.distanceTo(b),
63
+ );
64
+
65
+ const closest = points[0];
37
66
 
38
- bot.botwar.client.on("gameEnd", () => {
39
- console.log("A game has ended");
67
+
40
68
  });
41
69
  });