parent
47a944fe2a
commit
8e4eb7748f
3 changed files with 234 additions and 234 deletions
@ -1,29 +1,29 @@ |
|||||||
{ |
{ |
||||||
// Use IntelliSense to learn about possible attributes. |
// Use IntelliSense to learn about possible attributes. |
||||||
// Hover to view descriptions of existing attributes. |
// Hover to view descriptions of existing attributes. |
||||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 |
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 |
||||||
"version": "0.2.0", |
"version": "0.2.0", |
||||||
"configurations": [ |
"configurations": [ |
||||||
{ |
{ |
||||||
"type": "node", |
"type": "node", |
||||||
"request": "launch", |
"request": "launch", |
||||||
"name": "protospace", |
"name": "protospace", |
||||||
"skipFiles": [ |
"skipFiles": [ |
||||||
"<node_internals>/**" |
"<node_internals>/**" |
||||||
], |
], |
||||||
"program": "${workspaceFolder}/lib/index.js", |
"program": "${workspaceFolder}/lib/index.js", |
||||||
"args": ["games.protospace.ca"] |
"args": ["games.protospace.ca"] |
||||||
}, |
}, |
||||||
{ |
{ |
||||||
"type": "node", |
"type": "node", |
||||||
"request": "launch", |
"request": "launch", |
||||||
"name": "Launch Program", |
"name": "Launch Program", |
||||||
"skipFiles": [ |
"skipFiles": [ |
||||||
"<node_internals>/**" |
"<node_internals>/**" |
||||||
], |
], |
||||||
"program": "${workspaceFolder}/lib/index.js", |
"program": "${workspaceFolder}/lib/index.js", |
||||||
// port may need to be changed for each session |
// port may need to be changed for each session |
||||||
"args": ["localhost", "56901"] |
"args": ["localhost", "56901"] |
||||||
} |
} |
||||||
] |
] |
||||||
} |
} |
@ -1,113 +1,113 @@ |
|||||||
let cfg = {} |
let cfg = {} |
||||||
let bot = {} |
let bot = {} |
||||||
let isEating = false |
let isEating = false |
||||||
|
|
||||||
function callbackHandle(err) { |
function callbackHandle(err) { |
||||||
if (err) console.error(err) |
if (err) console.error(err) |
||||||
} |
} |
||||||
|
|
||||||
function eat(callback) { |
function eat(callback) { |
||||||
isEating = true |
isEating = true |
||||||
|
|
||||||
const foodNames = require('minecraft-data')(bot.version).foodsArray.map((item) => item.name) |
const foodNames = require('minecraft-data')(bot.version).foodsArray.map((item) => item.name) |
||||||
|
|
||||||
let available_food = bot.inventory |
let available_food = bot.inventory |
||||||
.items() |
.items() |
||||||
.filter((item) => foodNames.includes(item.name)) |
.filter((item) => foodNames.includes(item.name)) |
||||||
|
|
||||||
if (available_food.length === 0 || !available_food) { |
if (available_food.length === 0 || !available_food) { |
||||||
isEating = false |
isEating = false |
||||||
return callback(new Error('No food found.')) |
return callback(new Error('No food found.')) |
||||||
} |
} |
||||||
|
|
||||||
if (cfg.eat.bannedFood.length > 0) { |
if (cfg.eat.bannedFood.length > 0) { |
||||||
available_food = available_food.filter( |
available_food = available_food.filter( |
||||||
(item) => !cfg.eat.bannedFood.includes(item.name) |
(item) => !cfg.eat.bannedFood.includes(item.name) |
||||||
) |
) |
||||||
} |
} |
||||||
let priority = cfg.eat.priority |
let priority = cfg.eat.priority |
||||||
let best_food = available_food.reduce((prev, current) => (prev[priority] > current[priority]) ? prev : current) |
let best_food = available_food.reduce((prev, current) => (prev[priority] > current[priority]) ? prev : current) |
||||||
|
|
||||||
if (!best_food) { |
if (!best_food) { |
||||||
isEating = false |
isEating = false |
||||||
return callback(new Error('No best food has been found.')) |
return callback(new Error('No best food has been found.')) |
||||||
} |
} |
||||||
|
|
||||||
bot.emit('eat_start') |
bot.emit('eat_start') |
||||||
|
|
||||||
bot.equip(best_food, 'hand', function (error) { |
bot.equip(best_food, 'hand', function (error) { |
||||||
if (error) { |
if (error) { |
||||||
console.error(error) |
console.error(error) |
||||||
isEating = false |
isEating = false |
||||||
bot.emit('eat_stop') |
bot.emit('eat_stop') |
||||||
} else { |
} else { |
||||||
bot.consume(function (err) { |
bot.consume(function (err) { |
||||||
if (err) { |
if (err) { |
||||||
console.error(err) |
console.error(err) |
||||||
isEating = false |
isEating = false |
||||||
bot.emit('eat_stop') |
bot.emit('eat_stop') |
||||||
return callback(err) |
return callback(err) |
||||||
} else { |
} else { |
||||||
isEating = false |
isEating = false |
||||||
bot.emit('eat_stop') |
bot.emit('eat_stop') |
||||||
callback(null) |
callback(null) |
||||||
if (!bot.food === 20) eat(callbackHandle) |
if (!bot.food === 20) eat(callbackHandle) |
||||||
} |
} |
||||||
}) |
}) |
||||||
} |
} |
||||||
}) |
}) |
||||||
} |
} |
||||||
|
|
||||||
function checkFood() { |
function checkFood() { |
||||||
console.info("eater: " |
console.info("eater: " |
||||||
// , " status: ", !isEating
|
// , " status: ", !isEating
|
||||||
, cfg.eat.auto && "auto" |
, cfg.eat.auto && "auto" |
||||||
, bot.food < cfg.eat.startAt && "hungry" |
, bot.food < cfg.eat.startAt && "hungry" |
||||||
, "hunger:", bot.food |
, "hunger:", bot.food |
||||||
, "at:", cfg.eat.startAt) |
, "at:", cfg.eat.startAt) |
||||||
if ( |
if ( |
||||||
!isEating |
!isEating |
||||||
&& cfg.eat.auto |
&& cfg.eat.auto |
||||||
&& bot.food < cfg.eat.startAt |
&& bot.food < cfg.eat.startAt |
||||||
) { |
) { |
||||||
if ( |
if ( |
||||||
(bot.pathfinder |
(bot.pathfinder |
||||||
&& !(bot.pathfinder.isMining() || bot.pathfinder.isBuilding()) |
&& !(bot.pathfinder.isMining() || bot.pathfinder.isBuilding()) |
||||||
// TODO implement better idle state
|
// TODO implement better idle state
|
||||||
) || true // idle most likely
|
) || true // idle most likely
|
||||||
) { |
) { |
||||||
eat(callbackHandle) |
eat(callbackHandle) |
||||||
} |
} |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
function resetEat(value) { |
function resetEat(value) { |
||||||
// to prevent the plugin from breaking if the bot gets killed while eating btw
|
// to prevent the plugin from breaking if the bot gets killed while eating btw
|
||||||
isEating = !!value // false
|
isEating = !!value // false
|
||||||
} |
} |
||||||
|
|
||||||
const load = (config) => { |
const load = (config) => { |
||||||
cfg = config |
cfg = config |
||||||
bot = cfg.bot |
bot = cfg.bot |
||||||
cfg.eat = { |
cfg.eat = { |
||||||
priority: 'saturation', //'foodPoints', //
|
priority: 'saturation', //'foodPoints', //
|
||||||
// startAt: 19, //anarchy
|
// startAt: 19, //anarchy
|
||||||
// startAt: 18,
|
// startAt: 18,
|
||||||
startAt: 14, |
startAt: 14, |
||||||
bannedFood: [ |
bannedFood: [ |
||||||
"enchanted_golden_apple", "golden_apple", "pufferfish", "chorus_fruit" |
"enchanted_golden_apple", "golden_apple", "pufferfish", "chorus_fruit" |
||||||
], |
], |
||||||
auto: true |
auto: true |
||||||
} |
} |
||||||
|
|
||||||
bot.on('health', checkFood) |
bot.on('health', checkFood) |
||||||
bot.on('spawn', resetEat) |
bot.on('spawn', resetEat) |
||||||
|
|
||||||
} |
} |
||||||
|
|
||||||
const unload = () => { |
const unload = () => { |
||||||
bot.off('health', checkFood) |
bot.off('health', checkFood) |
||||||
bot.off('spawn', resetEat) |
bot.off('spawn', resetEat) |
||||||
} |
} |
||||||
|
|
||||||
module.exports = { load, unload, eat, resetEat } |
module.exports = { load, unload, eat, resetEat } |
@ -1,95 +1,95 @@ |
|||||||
// import { EntityFilters } from "mineflayer-statemachine"
|
// import { EntityFilters } from "mineflayer-statemachine"
|
||||||
// import v from "vec3"
|
// import v from "vec3"
|
||||||
|
|
||||||
// import { Movements } from "mineflayer-pathfinder"
|
// import { Movements } from "mineflayer-pathfinder"
|
||||||
|
|
||||||
// const mineflayer = require('mineflayer')
|
// const mineflayer = require('mineflayer')
|
||||||
const { Movements } = require('mineflayer-pathfinder') |
const { Movements } = require('mineflayer-pathfinder') |
||||||
// const { GoalBLah } = require('mineflayer-pathfinder').goals
|
// const { GoalBLah } = require('mineflayer-pathfinder').goals
|
||||||
const v = require('vec3') |
const v = require('vec3') |
||||||
|
|
||||||
let cfg = {} |
let cfg = {} |
||||||
let bot = {} |
let bot = {} |
||||||
// let moving
|
// let moving
|
||||||
let pathfinder |
let pathfinder |
||||||
let movements = [] |
let movements = [] |
||||||
|
|
||||||
|
|
||||||
function initMoves(bot = bot, mcData = require('minecraft-data')(bot.version)) { |
function initMoves(bot = bot, mcData = require('minecraft-data')(bot.version)) { |
||||||
let defaultMove = new Movements(bot, mcData) |
let defaultMove = new Movements(bot, mcData) |
||||||
defaultMove.canDig = false |
defaultMove.canDig = false |
||||||
defaultMove.scafoldingBlocks.push(mcData.blocksByName.slime_block.id) |
defaultMove.scafoldingBlocks.push(mcData.blocksByName.slime_block.id) |
||||||
// defaultMove.blocksCantBreak.add(mcData.blocksByName.glass.id)
|
// defaultMove.blocksCantBreak.add(mcData.blocksByName.glass.id)
|
||||||
// defaultMove.blocksToAvoid.add(mcData.blocksByName.magma.id)
|
// defaultMove.blocksToAvoid.add(mcData.blocksByName.magma.id)
|
||||||
movements.defaultMove = defaultMove |
movements.defaultMove = defaultMove |
||||||
|
|
||||||
bot.pathfinder.setMovements(defaultMove) |
bot.pathfinder.setMovements(defaultMove) |
||||||
} |
} |
||||||
|
|
||||||
|
|
||||||
function moveNear(pos, distance = 3) { |
function moveNear(pos, distance = 3) { |
||||||
const { GoalNear } = require('mineflayer-pathfinder').goals |
const { GoalNear } = require('mineflayer-pathfinder').goals |
||||||
cfg.quiet || bot.chat(`moving to ${pos}`) |
cfg.quiet || bot.chat(`moving to ${pos}`) |
||||||
|
|
||||||
pos = v(pos) |
pos = v(pos) |
||||||
bot.pathfinder.setMovements(movements.defaultMove) |
bot.pathfinder.setMovements(movements.defaultMove) |
||||||
bot.pathfinder.setGoal(new GoalNear(pos.x, pos.y, pos.z, distance)) |
bot.pathfinder.setGoal(new GoalNear(pos.x, pos.y, pos.z, distance)) |
||||||
} |
} |
||||||
|
|
||||||
function follow(entity, dynamic = true) { |
function follow(entity, dynamic = true) { |
||||||
console.assert(entity) |
console.assert(entity) |
||||||
const { GoalFollow } = require('mineflayer-pathfinder').goals |
const { GoalFollow } = require('mineflayer-pathfinder').goals |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
cfg.quiet && console.log(entity) |
cfg.quiet && console.log(entity) |
||||||
|| bot.chat( |
|| bot.chat( |
||||||
`following ${entity.type |
`following ${entity.type |
||||||
}: ${entity.username || entity.displayName |
}: ${entity.username || entity.displayName |
||||||
}${dynamic ? "" : " once"}` |
}${dynamic ? "" : " once"}` |
||||||
) |
) |
||||||
|
|
||||||
entity = entity.entity ? entity.entity : entity |
entity = entity.entity ? entity.entity : entity |
||||||
|
|
||||||
// console.log(entity)
|
// console.log(entity)
|
||||||
|
|
||||||
bot.pathfinder.setMovements(movements.defaultMove) |
bot.pathfinder.setMovements(movements.defaultMove) |
||||||
bot.pathfinder.setGoal(new GoalFollow(entity, 3), dynamic) |
bot.pathfinder.setGoal(new GoalFollow(entity, 3), dynamic) |
||||||
} |
} |
||||||
|
|
||||||
function hit(blockOrEntity) { |
function hit(blockOrEntity) { |
||||||
bot.chat(`hitting ${entity.name || entity.type}`) |
bot.chat(`hitting ${entity.name || entity.type}`) |
||||||
} |
} |
||||||
|
|
||||||
function stop() { |
function stop() { |
||||||
bot.pathfinder.setGoal(null) |
bot.pathfinder.setGoal(null) |
||||||
bot.stopDigging() |
bot.stopDigging() |
||||||
} |
} |
||||||
|
|
||||||
const load = (config) => { |
const load = (config) => { |
||||||
cfg = config |
cfg = config |
||||||
bot = cfg.bot |
bot = cfg.bot |
||||||
cfg.move = { |
cfg.move = { |
||||||
// auto: true,
|
// auto: true,
|
||||||
canDig: false, |
canDig: false, |
||||||
// list: ["hello", "wassup"],
|
// list: ["hello", "wassup"],
|
||||||
quiet: !!cfg.quiet, |
quiet: !!cfg.quiet, |
||||||
movements: [] |
movements: [] |
||||||
} |
} |
||||||
|
|
||||||
pathfinder = bot.pathfinder || bot.loadPlugin(require('mineflayer-pathfinder').pathfinder) |
pathfinder = bot.pathfinder || bot.loadPlugin(require('mineflayer-pathfinder').pathfinder) |
||||||
|
|
||||||
// initMoves(bot, mcData)
|
// initMoves(bot, mcData)
|
||||||
setTimeout(initMoves, 500, bot) |
setTimeout(initMoves, 500, bot) |
||||||
|
|
||||||
// bot.loadPlugin(pathfinder)
|
// bot.loadPlugin(pathfinder)
|
||||||
|
|
||||||
// bot.on('time', hello)
|
// bot.on('time', hello)
|
||||||
|
|
||||||
} |
} |
||||||
|
|
||||||
const unload = () => { |
const unload = () => { |
||||||
// TODO stop pathfinding maybe?
|
// TODO stop pathfinding maybe?
|
||||||
} |
} |
||||||
|
|
||||||
module.exports = { load, unload, stop, initMoves, moveNear, follow } |
module.exports = { load, unload, stop, initMoves, moveNear, follow } |
Loading…
Reference in new issue