chomens-bot-js/plugins/proxy.js

108 lines
2.9 KiB
JavaScript
Raw Normal View History

2022-11-16 06:09:37 -05:00
/* eslint-disable require-jsdoc */
const mc = require('minecraft-protocol');
const {loadPlugins} = require('../util/loadPlugins');
2022-11-16 06:09:37 -05:00
function inject(bot, dcclient, config) {
config.servers.forEach((server, index) => {
if (server.host !== bot.options.host) return;
const {MessageBuilder} = require('prismarine-chat')(bot.version);
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);
});
2022-11-16 06:09:37 -05:00
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',
2022-11-16 06:41:30 -05:00
client.username,
2022-11-16 06:09:37 -05:00
);
}
bot.tellraw('@a', {
color: 'dark_gray',
translate: '[%s] [%s] %s \u203a %s',
with: [
{
text: 'Chat',
color: 'gray',
},
{
text: 'Proxy',
color: 'gray',
},
{
text: client.username,
color: 'green',
},
MessageBuilder.fromString('&7' + data.message),
],
});
return;
}
targetClient.write(meta.name, data);
});
2022-11-16 06:09:37 -05:00
bot.on('end', () => srv.close());
});
});
};
module.exports = {inject};