Skip to content

Commit 8ab2232

Browse files
authored
Merge pull request #25 from HyPixelic/allow-plugin-options
Allow Plugin Options
2 parents 3b4d27f + bec8764 commit 8ab2232

3 files changed

Lines changed: 57 additions & 45 deletions

File tree

src/client.ts

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
unmuteGuildMember,
1818
} from "./modules/index.js";
1919

20-
import type { Bot, GuildMuteDurations } from "../types/index.d.ts";
20+
import type { Bot, GuildMuteDurations, PluginOptions } from "../types/index.d.ts";
2121

2222
/**
2323
* Injects the HyFlayer Plugin into a Mineflayer Bot
@@ -33,52 +33,58 @@ import type { Bot, GuildMuteDurations } from "../types/index.d.ts";
3333
* version: "1.8.9",
3434
* }) as HyFlayerBot
3535
*
36-
* bot.loadPlugin(HyFlayer);
36+
* const hyflayer = HyFlayer()
37+
*
38+
* bot.loadPlugin(hyflayer);
3739
* ```
3840
*/
39-
export const HyFlayer = (bot: Bot): void => {
40-
/* Structures */
41-
bot.mowojang = new MowojangClient();
42-
bot.hypixel = {
43-
proxy: {
44-
ip: undefined,
45-
port: undefined,
46-
latency: undefined,
47-
},
48-
location: {},
49-
} as Bot["hypixel"];
41+
export const HyFlayer = (options?: PluginOptions) => {
42+
const mowojang = options?.mowojang ?? new MowojangClient();
43+
44+
return (bot: Bot): void => {
45+
/* Structures */
46+
bot.mowojang = mowojang;
47+
bot.hypixel = {
48+
proxy: {
49+
ip: undefined,
50+
port: undefined,
51+
latency: undefined,
52+
},
53+
location: {},
54+
} as Bot["hypixel"];
5055

51-
/* Functions */
52-
bot.sendGuildMessage = (msg: string) => sendGuildMessage(bot, msg);
53-
bot.sendGuildOfficerMessage = (msg: string) => sendGuildOfficerMessage(bot, msg);
54-
bot.toggleGuildSlowChat = () => toggleGuildSlowChat(bot);
55-
bot.muteGuildChat = (duration: GuildMuteDurations) => muteGuildChat(bot, duration);
56-
bot.muteGuildMember = (member: string, duration: GuildMuteDurations) => muteGuildMember(bot, member, duration);
57-
bot.unmuteGuildChat = () => unmuteGuildChat(bot);
58-
bot.unmuteGuildMember = (member: string) => unmuteGuildMember(bot, member);
59-
bot.sendPrivateMessage = (player: string, msg: string) => sendPrivateMessage(bot, player, msg);
60-
bot.sendSkyblockCoopMessage = (msg: string) => sendSkyblockCoopMessage(bot, msg);
56+
/* Functions */
57+
bot.sendGuildMessage = (msg: string) => sendGuildMessage(bot, msg);
58+
bot.sendGuildOfficerMessage = (msg: string) => sendGuildOfficerMessage(bot, msg);
59+
bot.toggleGuildSlowChat = () => toggleGuildSlowChat(bot);
60+
bot.muteGuildChat = (duration: GuildMuteDurations) => muteGuildChat(bot, duration);
61+
bot.muteGuildMember = (member: string, duration: GuildMuteDurations) => muteGuildMember(bot, member, duration);
62+
bot.unmuteGuildChat = () => unmuteGuildChat(bot);
63+
bot.unmuteGuildMember = (member: string) => unmuteGuildMember(bot, member);
64+
bot.sendPrivateMessage = (player: string, msg: string) => sendPrivateMessage(bot, player, msg);
65+
bot.sendSkyblockCoopMessage = (msg: string) => sendSkyblockCoopMessage(bot, msg);
6166

62-
/* Proxy Parsing */
63-
bot.once("login", () => {
64-
bot.hypixel.proxy = {
65-
ip: bot._client?.socket?.remoteAddress,
66-
port: bot._client?.socket?.remotePort,
67-
latency: bot?.player?.ping || undefined,
68-
};
69-
setInterval(() => {
70-
bot.hypixel.proxy.latency = bot?.player?.ping || undefined;
71-
}, 60000);
72-
});
67+
/* Proxy Parsing */
68+
bot.once("login", () => {
69+
bot.hypixel.proxy = {
70+
ip: bot._client?.socket?.remoteAddress,
71+
port: bot._client?.socket?.remotePort,
72+
latency: bot?.player?.ping || undefined,
73+
};
74+
setInterval(() => {
75+
bot.hypixel.proxy.latency = bot?.player?.ping || undefined;
76+
}, 60000);
77+
});
7378

74-
/* Location Parsing */
75-
parseLocation(bot);
76-
bot.once("spawn", () => getLocation(bot));
77-
bot.on("respawn", () => getLocation(bot));
79+
/* Location Parsing */
80+
parseLocation(bot);
81+
bot.once("spawn", () => getLocation(bot));
82+
bot.on("respawn", () => getLocation(bot));
7883

79-
/* Chat Parsers */
80-
parsePrivateChat(bot);
81-
parseSkyblockCoopChat(bot);
82-
parseGuildChat(bot);
83-
parseGuildEvents(bot);
84+
/* Chat Parsers */
85+
parsePrivateChat(bot);
86+
parseSkyblockCoopChat(bot);
87+
parseGuildChat(bot);
88+
parseGuildEvents(bot);
89+
};
8490
};

test/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import mineflayer from "mineflayer";
2-
import { HyFlayer } from "../dist/index.js";
2+
import { HyFlayer } from "../dist/src/index.js";
33

44
const bot = mineflayer.createBot({
55
host: "hypixel.net",
@@ -8,7 +8,9 @@ const bot = mineflayer.createBot({
88
version: "1.8.9",
99
});
1010

11-
bot.loadPlugin(HyFlayer);
11+
const hyflayer = HyFlayer();
12+
13+
bot.loadPlugin(hyflayer);
1214

1315
setTimeout(() => {
1416
bot.chat("/limbo");

types/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import type { Bot as MineflayerBot, BotEvents as MineflayerBotEvents } from "mineflayer";
22
import type { Client as MowojangClient } from "mowojang";
33

4+
export interface PluginOptions {
5+
mowojang?: MowojangClient;
6+
}
7+
48
/**
59
* HyFlayer Location Event, this Event is emitted everytime the Mineflayer Bot detects a "spawn" or "respawn" Event.
610
*

0 commit comments

Comments
 (0)