chipmunkbot3/plugins/core.js
Chipmunk 1a8d4a12e8 Support legacy versions
Hypothetically, the bot should now work on 1.14 and newer
2024-08-03 16:26:00 -04:00

100 lines
3.1 KiB
JavaScript

const nbt = require('prismarine-nbt')
const mcNamespace = 'minecraft:'
function inject (bot) {
bot.core = {
size: { start: { x: 0, y: 0, z: 0 }, end: { x: 15, y: 0, z: 15 } },
start: { x: null, y: null, z: null },
end: { x: null, y: null, z: null },
block: { x: null, y: null, z: null },
refill () {
const refillCommand = `fill ${this.start.x} ${this.start.y} ${this.start.z} ${this.end.x} ${this.end.y} ${this.end.z} repeating_command_block{CustomName:'""'}`
if (bot.proxy?.client || bot._client.version === '20w13b') {
// Use chat commands instead of blocks
bot.chat.command(refillCommand)
return
}
const location = { x: Math.floor(bot.position.x), y: Math.floor(bot.position.y) - 1, z: Math.floor(bot.position.z) }
const commandBlockId = bot.registry?.itemsByName.command_block.id
bot._client.write('set_creative_slot', {
slot: 36,
item: {
present: true,
itemId: commandBlockId,
itemCount: 1,
nbtData: nbt.comp({
BlockEntityTag: nbt.comp({
auto: nbt.byte(1),
Command: nbt.string(refillCommand)
})
})
}
})
bot._client.write('block_dig', {
status: 0,
location,
face: 1
})
bot._client.write('block_place', {
location,
direction: 1,
hand: 0,
cursorX: 0.5,
cursorY: 0.5,
cursorZ: 0.5,
insideBlock: false
})
},
run (command) {
if (!bot.loggedIn) return
if (!bot.features.commandNamespaces && command.startsWith(mcNamespace)) command = command.substring(mcNamespace.length)
if (bot.features.amnesicCommandBlocks) bot._client.write('update_command_block', { location: this.block, command: '', mode: 0, flags: 0b000 })
bot._client.write('update_command_block', { location: this.block, command: String(command).substring(0, 32767), mode: bot.features.amnesicCommandBlocks ? 1 : 2, flags: 0b100 })
this.block.x++
if (this.block.x > this.end.x) {
this.block.x = this.start.x
this.block.z++
}
if (this.block.z > this.end.z) {
this.block.z = this.start.z
this.block.y++
}
if (this.block.y > this.end.y) {
this.block.x = this.start.x
this.block.y = this.start.y
this.block.z = this.start.z
}
},
reset () {
const x = (bot.position.x >> 4) << 4
const y = 0
const z = (bot.position.z >> 4) << 4
this.start = { x: this.size.start.x + x, y: this.size.start.y + y, z: this.size.start.z + z }
this.end = { x: this.size.end.x + x, y: this.size.end.y + y, z: this.size.end.z + z }
this.block = { ...this.start }
this.refill()
}
}
bot.on('move', oldPos => {
bot.core.run(`minecraft:setblock ${Math.floor(oldPos.x)} ${Math.floor(oldPos.y - 1)} ${Math.floor(oldPos.z)} minecraft:air replace mincecraft:command:block`) // Clean up after refills
bot.core.reset()
})
setInterval(() => bot.core.reset(), 60 * 1000)
}
module.exports = inject