chomens-bot-js/plugins/core.js

100 lines
2.9 KiB
JavaScript
Raw Normal View History

2022-08-14 05:51:45 -04:00
/* eslint-disable max-len */
/* eslint-disable require-jsdoc */
const config = require('../config.json');
2022-08-16 07:38:50 -04:00
const chatMessage = require('prismarine-chat')('1.18.2');
2022-08-14 05:51:45 -04:00
const Vec3 = require('vec3');
const relativePosition = new Vec3(0, 0, 0);
function inject(bot) {
bot.createCore = (layers = config.core.layers) => {
const core = {
isCore(position) {
return position.x >= core.start.x && position.x <= core.end.x && position.y >= core.start.y && position.y <= core.end.y && position.z >= core.start.z && position.z <= core.end.z;
},
run(command) {
2022-08-17 07:47:22 -04:00
try {
if (config.useChat && command.startsWith('minecraft:tellraw @a ') && !command.includes('Console') && !command.includes('Discord')) {
bot.chat(chatMessage.fromNotch(command.replace('minecraft:tellraw @a ', '')).toMotd().replaceAll('§', '&'));
return;
}
2022-08-16 07:38:50 -04:00
2022-08-17 07:47:22 -04:00
relativePosition.x++;
2022-08-14 05:51:45 -04:00
2022-08-17 07:47:22 -04:00
if (relativePosition.x >= 16) {
relativePosition.x = 0;
relativePosition.y++;
}
2022-08-14 05:51:45 -04:00
2022-08-17 07:47:22 -04:00
if (relativePosition.y >= layers) {
relativePosition.y = 0;
relativePosition.z++;
}
2022-08-14 05:51:45 -04:00
2022-08-17 07:47:22 -04:00
if (relativePosition.z >= 16) {
relativePosition.z = 0;
}
2022-08-14 05:51:45 -04:00
2022-08-17 07:47:22 -04:00
bot.write('update_command_block', {
location: {
x: core.start.x + relativePosition.x,
y: core.start.y + relativePosition.y,
z: core.start.z + relativePosition.z,
},
command: command.substring(0, 32767),
mode: 1,
flags: 0b100,
});
} catch (e) {
return;
}
2022-08-14 05:51:45 -04:00
},
fillCore(useCore) {
const fillCommand = `/minecraft:fill ${core.start.x} ${core.start.y} ${core.start.z} ${core.end.x} ${core.end.y} ${core.end.z} command_block{CustomName:'{"text":"ChomeNS Bot Core","color":"yellow"}'}`;
if (useCore==true) {
bot.core.run(fillCommand);
} else {
bot.chat(fillCommand);
}
bot.emit('core_filled');
},
};
bot._client.on('position', (position) => {
bot.position = position;
bot.emit('position', position);
});
bot._client.on('block_change', (packet) => {
if (core.isCore(packet.location) && packet.type===0) fillCore();
});
2022-08-18 21:39:59 -04:00
bot._client.on('multi_block_change', (packet) => {
if (core.isCore(packet.chunkCoordinates)) fillCore();
});
2022-08-14 05:51:45 -04:00
bot.on('position', fillCore);
fillCore();
bot.core = core;
return core;
function fillCore() {
core.start = new Vec3(
Math.floor(bot.position.x / 16) * 16,
0 /* bot.position.y */,
Math.floor(bot.position.z / 16) * 16,
).floor();
core.end = core.start.clone().translate(16, layers, 16).subtract(new Vec3(1, 1, 1));
core.fillCore();
}
};
}
module.exports = {inject};