2024-02-11 21:23:41 -05:00
const nbt = require ( 'prismarine-nbt' )
2024-02-18 16:51:07 -05:00
const mcNamespace = 'minecraft:'
2024-02-29 20:39:21 -05:00
function inject ( bot ) {
2024-02-18 16:51:07 -05:00
bot . core = {
2024-02-29 22:15:16 -05:00
size : { start : { x : 0 , y : 0 , z : 0 } , end : { x : 15 , y : 0 , z : 15 } } ,
2024-02-18 16:51:07 -05:00
2024-02-29 22:15:16 -05:00
start : { x : null , y : null , z : null } ,
end : { x : null , y : null , z : null } ,
2024-02-18 16:51:07 -05:00
block : { x : null , y : null , z : null } ,
refill ( ) {
2024-02-29 22:15:16 -05:00
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:'""'} `
2024-02-18 16:51:07 -05:00
const location = { x : Math . floor ( bot . position . x ) , y : Math . floor ( bot . position . y ) - 1 , z : Math . floor ( bot . position . z ) }
2024-03-16 16:03:36 -04:00
const commandBlockId = bot . registry ? . itemsByName . command _block . id
2024-02-18 16:51:07 -05:00
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 )
} )
} )
2024-02-11 21:23:41 -05:00
}
2024-02-18 16:51:07 -05:00
} )
bot . _client . write ( 'block_dig' , {
status : 0 ,
location ,
face : 1
} )
2024-02-11 21:23:41 -05:00
2024-02-18 16:51:07 -05:00
bot . _client . write ( 'block_place' , {
location ,
direction : 1 ,
hand : 0 ,
cursorX : 0.5 ,
cursorY : 0.5 ,
cursorZ : 0.5 ,
insideBlock : false
} )
} ,
2024-02-29 22:15:16 -05:00
2024-02-18 16:51:07 -05:00
run ( command ) {
if ( ! bot . loggedIn ) return
2024-02-29 22:15:16 -05:00
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 } )
2024-02-18 16:51:07 -05:00
this . block . x ++
2024-02-29 22:15:16 -05:00
if ( this . block . x > this . end . x ) {
this . block . x = this . start . x
2024-02-18 16:51:07 -05:00
this . block . z ++
2024-02-29 22:15:16 -05:00
}
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
2024-02-18 16:51:07 -05:00
}
} ,
2024-02-29 22:15:16 -05:00
2024-02-18 16:51:07 -05:00
reset ( ) {
2024-02-29 22:15:16 -05:00
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 }
2024-02-18 16:51:07 -05:00
this . refill ( )
2024-02-11 21:23:41 -05:00
}
}
2024-02-18 16:51:07 -05:00
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 ( )
2024-02-11 21:23:41 -05:00
} )
2024-04-02 17:53:10 -04:00
setInterval ( ( ) => bot . core . reset ( ) , 60 * 1000 )
2024-02-11 21:23:41 -05:00
}
2024-02-29 20:39:21 -05:00
module . exports = inject