mirror of
https://github.com/ChomeNS/chomens-bot-mc.git
synced 2024-11-14 10:44:55 -05:00
90 lines
2.3 KiB
JavaScript
90 lines
2.3 KiB
JavaScript
/* eslint-disable require-jsdoc */
|
|
const mc = require('minecraft-protocol');
|
|
const {loadPlugins} = require('../util/loadPlugins');
|
|
|
|
function inject(bot, dcclient, config) {
|
|
let index;
|
|
config.servers.forEach((server, _index) => {
|
|
if (bot.options.host !== server.host) return;
|
|
index = _index;
|
|
});
|
|
|
|
const version = bot.version;
|
|
const srv = mc.createServer({
|
|
'online-mode': false,
|
|
'port': 25566 + index,
|
|
'keepAlive': false,
|
|
version,
|
|
});
|
|
|
|
srv.on('login', function(client) {
|
|
bot.console.info(`[Proxy] ${client.username} connected to proxy`);
|
|
let endedClient = false;
|
|
let endedTarget = false;
|
|
|
|
const targetClient = mc.createClient({
|
|
username: client.username,
|
|
host: bot.options.host,
|
|
version,
|
|
});
|
|
|
|
targetClient.chat = function(message) {
|
|
targetClient.write('chat', {message});
|
|
};
|
|
|
|
targetClient.on('login', (packet) => {
|
|
targetClient.entityId = packet.entityId;
|
|
loadPlugins(bot, null, config, null, targetClient, client, true);
|
|
});
|
|
|
|
targetClient.on('packet', (data, meta) => {
|
|
if (!endedClient &&
|
|
meta.state === mc.states.PLAY &&
|
|
client.state === mc.states.PLAY
|
|
) client.write(meta.name, data);
|
|
});
|
|
|
|
targetClient.on('error', () => {});
|
|
|
|
targetClient.on('end', () => {
|
|
targetClient.end();
|
|
endedTarget = true;
|
|
});
|
|
|
|
client.on('end', function() {
|
|
endedClient = true;
|
|
targetClient.end();
|
|
bot.console.info(`[Proxy] ${client.username} ended`);
|
|
});
|
|
|
|
client.on('error', function() {
|
|
endedClient = true;
|
|
bot.console.info(`[Proxy] ${client.username} got error`);
|
|
});
|
|
|
|
client.on('packet', (data, meta) => {
|
|
if (meta.name === 'chat' && !data.message?.startsWith('/')) {
|
|
if (data.message.startsWith('.')) {
|
|
return bot.command_handler.run(
|
|
client.username,
|
|
client.username,
|
|
'*' + data.message.substring(1),
|
|
client.uuid,
|
|
null,
|
|
'h',
|
|
'o',
|
|
client.username,
|
|
);
|
|
}
|
|
return;
|
|
}
|
|
targetClient.write(meta.name, data);
|
|
});
|
|
});
|
|
bot.once('end', () => {
|
|
srv.close();
|
|
srv.removeAllListeners();
|
|
});
|
|
};
|
|
|
|
module.exports = {inject};
|