chipmunkbot3/plugins/core.js

96 lines
3 KiB
JavaScript

const nbt = require('prismarine-nbt')
const mcNamespace = 'minecraft:'
function inject (bot) {
let mcData = require('minecraft-data')('1.17.1')
bot.on('login', () => (mcData = require('minecraft-data')(bot._client.version)))
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:'""'}`
const location = { x: Math.floor(bot.position.x), y: Math.floor(bot.position.y) - 1, z: Math.floor(bot.position.z) }
const commandBlockId = mcData?.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.refill(), 60 * 1000)
}
module.exports = inject