Skip to content

Commit

Permalink
Merge pull request #157 from LordTocs/tocs
Browse files Browse the repository at this point in the history
0.2.1 Fixes
  • Loading branch information
LordTocs authored Jun 20, 2022
2 parents c245b5c + 543fc4f commit 063ee5b
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 44 deletions.
125 changes: 83 additions & 42 deletions src/core/plugins/minecraft.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,67 @@ module.exports = {
icon: "mdi-minecraft",
color: "#66A87B",
async init() {
this.connectionState = "Disconnected";
this.startConnectLoop();
},
methods: {
tryConnect() {
return new Promise((resolve, reject) => {
try {
this.rcon = new Rcon(
this.settings.host,
Number(this.settings.port),
this.secrets.password,
);

this.logger.info(`Trying MC Connection ${this.settings.host}:${this.settings.port}`)

this.rcon.connect();

this.rcon.on('auth', () => {
this.logger.info("Connected to Minecraft!");
this.analytics.set({ usesMinecraft: true });
resolve(true);
})

this.rcon.on('response', (str) => {
this.logger.info(`MC Resp: ${str}`)
})

this.rcon.on('end', async () => {
this.logger.info("Minecraft Connection Ended.");
await sleep(5000);
this.tryConnect();
resolve(false)
})

this.rcon.on("error", (err) => {
this.logger.info("Error!");
this.logger.error(String(err));
resolve(false);
})
}
catch (err) {
this.logger.error(err);
resolve(false)
}
});
try {
this.rcon = new Rcon(
this.settings.host,
Number(this.settings.port),
this.secrets.password,
);

this.logger.info(`Trying MC Connection ${this.settings.host}:${this.settings.port}`)

this.connectionState = "Connecting";
this.rcon.connect();

this.rcon.on('auth', () => {
this.logger.info("Connected to Minecraft!");
this.analytics.set({ usesMinecraft: true });
this.connectionState = "Connected";
})

this.rcon.on('response', (str) => {
this.logger.info(`MC Resp: ${str}`)
})

this.rcon.on('end', async () => {
this.logger.info("Minecraft Connection Ended.");
this.connectionState = "Disconnected";
if (this.disconnectFunc)
this.disconnectFunc();
})

this.rcon.on("error", (err) => {
this.logger.info("Error!");
this.logger.error(String(err));
this.connectionState = "Disconnected";
if (this.errorFunc)
this.errorFunc(err)
})
}
catch (err) {
this.connectionState = "Disconnected";
this.logger.error(err);
if (this.errorFunc)
this.errorFunc(err)
}
},

async retry() {
if (this.connectionState != "Disconnected")
return;
await sleep(5000);
this.tryConnect();
},


async startConnectLoop() {
this.rcon = null;


if (!this.settings.host)
return;
if (!this.settings.port)
Expand All @@ -67,9 +78,39 @@ module.exports = {
return;

this.logger.info("Starting MC Connection Loop");
await this.tryConnect();

this.disconnectFunc = () => this.retry();
this.errorFunc = () => this.retry();

this.tryConnect();
},
async shutdown() {
if (this.connectionState == "Disconnected")
return;

try {
await new Promise((resolve, reject) => {
this.disconnectFunc = () => resolve();
this.errorFunc = (e) => reject(e);
this.rcon.disconnect()
});
}
catch(err)
{
this.logger.error(`Error disconnecting ${err}`);
}
}
},
async onSettingsReload() {
await this.shutdown();

await this.startConnectLoop();
},
async onSecretsReload() {
await this.shutdown();

await this.startConnectLoop();
},
settings: {
host: { type: String, name: "Server" },
port: { type: Number, name: "Port" },
Expand Down
4 changes: 2 additions & 2 deletions src/core/plugins/obs.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,8 @@ module.exports = {
name: "Muted",
default: true,
leftLabel: "Un-Muted",
trueIcon: "mdi-volume-high",
falseIcon: "mdi-volume-off",
trueIcon: "mdi-volume-off",
falseIcon: "mdi-volume-high",
}
}
},
Expand Down

0 comments on commit 063ee5b

Please sign in to comment.