Skip to content

Commit

Permalink
options splitted in plugins.
Browse files Browse the repository at this point in the history
  • Loading branch information
RubenNL committed Jul 28, 2020
1 parent 46cea6c commit d26c2fc
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 58 deletions.
1 change: 0 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"password": "",
"messagesOnJoin":["/su","/afk"],
"version":"1.15.2",
"hopInMinecart":false,
"activateButton":false,
"activateLever":false,
"maxConnectAttempts":3,
Expand Down
15 changes: 15 additions & 0 deletions plugins/autoEat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports=(bot,options)=>{
let loginTime;
bot.on('spawn',()=>loginTime=new Date);
bot.on('health',()=>{
if(((new Date)-loginTime)<10000) return;
if(bot.food==20) return;
bot.equip(mcData.itemsByName.cooked_beef.id, 'hand', (err) => {
if (err) {
console.log("LOOKS LIKE I HAVE RUN OUT OF FOOD! err:",err,"health:",bot.health,"food level:",bot.food)
return;
}
bot.consume(err=>console.log("CONSUME ERR:",err))
})
})
}
11 changes: 11 additions & 0 deletions plugins/autoLeaveLowHealth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports=(bot,options)=>{
let loginTime;
bot.on('spawn',()=>loginTime=new Date);
bot.on('health',()=>{
if(((new Date)-loginTime)<10000) return;
if(bot.health>4) return;
bot.chat("AFK session end, health is below 4. disconnecting!");
console.log('Health too low, disconnecting!');
bot.quit();
})
}
12 changes: 12 additions & 0 deletions plugins/clickBlockOnJoin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports=(bot,options)=>{
moveTimeout=setTimeout(()=>{});
bot.on('forcedMove',()=>{
clearTimeout(moveTimeout)
moveTimeout=setTimeout(()=>{
if(options.exact) block=bot.findBlock({matching:block=>block.name==options.name})
else block=bot.findBlock({matching:(block)=>block.name.includes(options.name)})
if(!block) console.log("BLOCK NOT FOUND!");
else bot.activateBlock(block);
},5000)
})
}
8 changes: 8 additions & 0 deletions plugins/sendOnJoin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports=(bot,options)=>{
let switched=false;
bot.on('spawn',()=>{
if(switched) return;
switched=true;
options.messagesOnJoin.forEach(bot.chat);
})
}
3 changes: 3 additions & 0 deletions plugins/showChat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports=(bot,options)=>{
bot.on('chat',(username,message)=>console.log((new Date).toTimeString(),'CHAT MESSAGE',username,':',message));
}
61 changes: 4 additions & 57 deletions start.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,56 +13,13 @@ var bot
var moveTimeout
function connect() {
bot= mc.createBot(botConfig);
bot.world="lobby"
bot.on('spawn',()=>{
if(bot.world=="afkWorld") return;
bot.world="afkWorld"
loginTime=new Date
console.log('logged in!',loginTime.toTimeString());
botConfig.messagesOnJoin.forEach(bot.chat);
})
moveTimeout=setTimeout(()=>{});
bot.on('chat',(username,message)=>console.log((new Date).toTimeString(),'CHAT MESSAGE',username,':',message));
bot.on('forcedMove',()=>{
clearTimeout(moveTimeout)
moveTimeout=setTimeout(()=>{
if(botConfig.hopInMinecart) {
minecarts=Object.values(bot.entities).filter(entity=>{
distance=bot.entity.position.distanceTo(entity.position);
console.log(entity.objectType,entity.position,distance);
return entity.objectType=='Minecart' && distance<5
})
if(minecarts.length==0) console.log("NO MINECARTS FOUND!")
else if(minecarts.length>1) console.log("MORE THAN 1 MINECART FOUND! NOT HOPPING IN!")
else bot.mount(minecarts[0])
}
if(botConfig.activateButton) {
button=bot.findBlock({matching:(block)=>block.name.includes('button')})
if(!button) console.log("NO BUTTON FOUND!");
else bot.activateBlock(button);
}
if(botConfig.activateLever) {
lever=bot.findBlock({matching:block=>block.name=="lever"})
if(!lever) console.log("NO LEVER FOUND!");
else bot.activateBlock(lever);
}
},5000)
})
bot.on('health',()=>{
if(((new Date)-loginTime)<10000) {
console.log('HEALTH CHANGE, JUST JOINED');
return
}
console.log("HEALTH CHANGED! ITS NOW",bot.health,"FOOD:",bot.food)
if(bot.food<20) eat();
if(bot.health>4) return;
bot.chat("AFK session end, health is below 4. disconnecting!")
botConfig.maxConnectAttempts=0;
bot.quit()
})
bot.loadPlugins(['autoEat.js','autoLeaveLowHealth.js','clickBlockOnJoin.js','sendOnJoin.js','showChat.js'].map(line=>'./plugins/'+line).map(require))
bot.on('kicked',(reason,loggedIn)=>{
console.log('GOT KICKED WHILE',loggedIn?'CONNECTED':'LOGGING IN',reason)
})
bot.on('spawn',()=>{
loginTime=new Date
})
bot.on('end',()=>{
clearInterval(displayInterval)
endTime=new Date;
Expand Down Expand Up @@ -93,16 +50,6 @@ function connect() {

}
connect();
function eat () {
bot.equip(mcData.itemsByName.cooked_beef.id, 'hand', (err) => {
if (err) {
console.log("LOOKS LIKE I HAVE RUN OUT OF FOOD! err:",err,"health:",bot.health,"food level:",bot.food)
return;
}

bot.consume(err=>console.log("CONSUME ERR:",err))
})
}
function logMillis(millis) {
seconds=Math.floor(millis/1000);
minutes=Math.floor(seconds/60);
Expand Down

0 comments on commit d26c2fc

Please sign in to comment.