Update util/core.js

This commit is contained in:
m_c_player 2024-10-27 05:11:51 -04:00
parent f2f1d9b2c3
commit 87a10ab00c

View file

@ -1,88 +1,117 @@
const Vec3 = require('vec3').Vec3; const Vec3 = require("vec3").Vec3;
const { MessageBuilder } = require("prismarine-chat")("1.19.4"); const { MessageBuilder } = require("prismarine-chat")("1.19.4");
const { Tellraw, Text } = require("./tellrawBuilder.js");
class CoreClass { class CoreClass {
constructor(client) { constructor(client) {
this.client = client; this.client = client;
this.commandBlocks = []; this.commandBlocks = [];
this.used = 0; this.used = 0;
this.corepos = { x: 0, y: 0, z: 0 }; this.corepos = { x: 0, y: 0, z: 0 };
this.totalCommandsRun = 0; this.totalCommandsRun = 0;
this.startTime = Date.now(); this.startTime = Date.now();
this.transaction_id = []; this.transaction_id = [];
this.coreSize = require('../config.json').core; this.coreSize = require("../config.json").core;
this.initialize(); this.initialize();
}
refill() {
const config = require("../config.json");
const pos = this.client.entity?.position; // Ensure entity exists before accessing position
this.commandBlocks = [];
// Check if the bot's position is defined
if (!pos || (pos.x === 0 && pos.y === 0 && pos.z === 0)) {
setTimeout(() => this.refill(), 650);
return;
} }
refill() { const core = {
const config = require('../config.json'); x: Math.floor(pos.x - (this.coreSize.width - 1) / 2),
const pos = this.client.entity?.position; // Ensure entity exists before accessing position y: Math.floor(config.core.y - (this.coreSize.height - 1) / 2),
this.commandBlocks = []; z: Math.floor(pos.z - (this.coreSize.depth - 1) / 2),
x2: Math.floor(pos.x + (this.coreSize.width - 1) / 2),
y2: Math.floor(config.core.y + (this.coreSize.height - 1) / 2),
z2: Math.floor(pos.z + (this.coreSize.depth - 1) / 2),
};
// Check if the bot's position is defined this.corepos = { x: core.x, y: core.y, z: core.z };
if (!pos || pos.x === 0 && pos.y === 0 && pos.z === 0) { this.client.chat(
setTimeout(() => this.refill(), 650); `/fill ${core.x} ${core.y} ${core.z} ${core.x2} ${core.y2} ${core.z2} minecraft:repeating_command_block{CustomName:'{"text":"Ibot core from ImGloriz","color":"cyan"}'} replace`
return; );
for (let x = core.x; x <= core.x2; x++) {
for (let y = core.y; y <= core.y2; y++) {
for (let z = core.z; z <= core.z2; z++) {
this.commandBlocks.push(new Vec3(x, y, z));
} }
}
}
const core = { setTimeout(
x: Math.floor(pos.x - (this.coreSize.width - 1) / 2), () =>
y: Math.floor(config.core.y - (this.coreSize.height - 1) / 2), this.run(
z: Math.floor(pos.z - (this.coreSize.depth - 1) / 2), `/minecraft:tp ${this.client.uuid} ${this.corepos.x} ${this.corepos.y} ${this.corepos.z}`
x2: Math.floor(pos.x + (this.coreSize.width - 1) / 2), ),
y2: Math.floor(config.core.y + (this.coreSize.height - 1) / 2), 85
z2: Math.floor(pos.z + (this.coreSize.depth - 1) / 2) );
}; }
this.corepos = { x: core.x, y: core.y, z: core.z }; initialize() {
this.client.chat(`/fill ${core.x} ${core.y} ${core.z} ${core.x2} ${core.y2} ${core.z2} minecraft:repeating_command_block{CustomName:'{"text":"Ibot core from ImGloriz","color":"cyan"}'} replace`); this.refill(); // Start the refill process
}
for (let x = core.x; x <= core.x2; x++) { run(cmd, amount = 1) {
for (let y = core.y; y <= core.y2; y++) { const iterations = Math.min(amount, this.commandBlocks.length);
for (let z = core.z; z <= core.z2; z++) { if (iterations > 10000) return this.tellraw("Invalid Amount of jobs");
this.commandBlocks.push(new Vec3(x, y, z)); const jobs = Math.ceil(iterations / 10); // Number of jobs to run concurrently
} const commandsPerJob = Math.ceil(iterations / jobs);
}
for (let job = 0; job < jobs; job++) {
const start = job * commandsPerJob;
const end = Math.min(start + commandsPerJob, iterations);
for (let i = start; i < end; i++) {
try {
this.client._client.write("update_command_block", {
command: cmd,
location: this.commandBlocks[this.used],
mode: 1,
flags: 4,
});
} catch (err) {
console.log(`ERROR: ${err}`);
return;
} }
this.used = (this.used + 1) % this.commandBlocks.length;
setTimeout(() => this.run(`/minecraft:tp ${this.client.uuid} ${this.corepos.x} ${this.corepos.y} ${this.corepos.z}`), 85); }
} }
this.totalCommandsRun += iterations;
}
initialize() { // Tellraw stuff
this.refill(); // Start the refill process tellraw(text, selector = "@a") {
} this.run(`minecraft:tellraw ${selector} ${JSON.stringify(text)}`);
};
run(cmd, amount = 1) {
const iterations = Math.min(amount, this.commandBlocks.length); fancyTellraw = (text, selector) => {
if (iterations > 10000) return this.tellraw('Invalid Amount of jobs'); const prefix = new Tellraw()
const jobs = Math.ceil(iterations / 10); // Number of jobs to run concurrently .add(new Text("iBot").setColor("blue"))
const commandsPerJob = Math.ceil(iterations / jobs); .add(new Text(" ").setColor("gray"))
.get(false)
for (let job = 0; job < jobs; job++) {
const start = job * commandsPerJob; if (typeof text === "object") {
const end = Math.min(start + commandsPerJob, iterations); let prf = [...prefix];
for (let i = start; i < end; i++) {
try { if (Array.isArray(text)) {
this.client._client.write('update_command_block', { text.forEach((t) => {
command: cmd, prf.push(t);
location: this.commandBlocks[this.used], });
mode: 1, } else prf.push(text);
flags: 4, return this.tellraw(prf, selector);
});
} catch (err) {
console.log(`ERROR: ${err}`);
// If an error occurs, break the loop to prevent further attempts
return;
}
this.used = (this.used + 1) % this.commandBlocks.length;
}
}
this.totalCommandsRun += iterations; // Update total commands run
}
tellraw(text) {
this.run(`/tellraw @a ${JSON.stringify(MessageBuilder.fromString(`&8[ &b&lI&c&lBot&8 ] &7> ${text}`))}`);
} }
this.tellraw(text, selector);
};
} }
module.exports = { CoreClass }; module.exports = { CoreClass };