Compare commits
9 Commits
f8df1fa319
...
nogameplay
Author | SHA1 | Date | |
---|---|---|---|
|
bf45a53f08 | ||
|
a7ccb08d43 | ||
|
8e4eb7748f | ||
|
47a944fe2a | ||
|
1a3c345017 | ||
|
de0af4d2ac | ||
|
73d5f43ad3 | ||
|
b970231519 | ||
|
787d08dd31 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -13,6 +13,7 @@
|
||||
|
||||
# misc
|
||||
.DS_Store
|
||||
.env
|
||||
.env.local
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
|
22
lib/index.js
22
lib/index.js
@@ -1,4 +1,4 @@
|
||||
// TODO reload
|
||||
const env = require("dotenv-packed").parseEnv().parsed
|
||||
const fs = require('fs');
|
||||
let cfg = {
|
||||
admin: "Applezaus",
|
||||
@@ -12,14 +12,14 @@ const mineflayer = require("mineflayer");
|
||||
const bot =
|
||||
!isNaN(parseInt(process.argv[3])) && parseInt(process.argv[3]) > 1e2 ?
|
||||
mineflayer.createBot({
|
||||
host: process.argv[2] || process.env.MINECRAFT_HOST || 'localhost', // Change this to the ip you want.
|
||||
port: parseInt(process.argv[3]) || process.env.MINECRAFT_PORT // || 58471,
|
||||
host: process.argv[2] || process.env.MINECRAFT_HOST || env.MINECRAFT_HOST || 'localhost', // Change this to the ip you want.
|
||||
port: parseInt(process.argv[3]) || process.env.MINECRAFT_PORT || env.MINECRAFT_PORT // || 58471,
|
||||
})
|
||||
:
|
||||
mineflayer.createBot({
|
||||
host: process.argv[2] || process.env.MINECRAFT_HOST || 'localhost', // Change this to the ip you want.
|
||||
username: process.argv[3] || process.env.MINECRAFT_USER,
|
||||
password: process.argv[4] || process.env.MINECRAFT_PASS,
|
||||
host: process.argv[2] || process.env.MINECRAFT_HOST || env.MINECRAFT_HOST || 'localhost', // Change this to the ip you want.
|
||||
username: process.argv[3] || process.env.MINECRAFT_USER || env.MINECRAFT_USER,
|
||||
password: process.argv[4] || process.env.MINECRAFT_PASS || env.MINECRAFT_PASS,
|
||||
// port: process.argv[5] || process.env.MINECRAFT_PORT || 58471,
|
||||
})
|
||||
|
||||
@@ -29,7 +29,7 @@ let plugins = {}
|
||||
function loadplugin(pluginname, pluginpath) {
|
||||
try {
|
||||
plugins[pluginname] = require(pluginpath)
|
||||
plugins[pluginname]?.load(cfg)
|
||||
plugins[pluginname].load(cfg)
|
||||
} catch (error) {
|
||||
if (error.code == 'MODULE_NOT_FOUND') {
|
||||
console.warn('plugin not used:', pluginpath)
|
||||
@@ -44,7 +44,7 @@ function unloadplugin(pluginname, pluginpath) {
|
||||
const plugin = require.resolve(pluginpath)
|
||||
try {
|
||||
if (plugin && require.cache[plugin]) {
|
||||
require.cache[plugin].exports?.unload()
|
||||
require.cache[plugin].exports.unload()
|
||||
delete plugins[pluginname]
|
||||
delete require.cache[plugin]
|
||||
}
|
||||
@@ -94,12 +94,12 @@ bot.once("spawn", () => {
|
||||
command: require('./plugins/command'),
|
||||
sleeper: require('./plugins/sleeper'),
|
||||
armor: require('./plugins/armor'),
|
||||
// mover: require('./plugins/mover'),
|
||||
mover: require('./plugins/mover'),
|
||||
guard: require('./plugins/guard'),
|
||||
inventory: require('./plugins/inventory'),
|
||||
// eater: require('./plugins/eater'),
|
||||
eater: require('./plugins/eater'),
|
||||
finder: require('./plugins/finder'),
|
||||
miner: require('./plugins/miner'),
|
||||
// miner: require('./plugins/miner.js'),
|
||||
// statemachine: require('./plugins/statemachine'),
|
||||
}
|
||||
|
||||
|
113
lib/plugins/eater.js
Normal file
113
lib/plugins/eater.js
Normal file
@@ -0,0 +1,113 @@
|
||||
let cfg = {}
|
||||
let bot = {}
|
||||
let isEating = false
|
||||
|
||||
function callbackHandle(err) {
|
||||
if (err) console.error(err)
|
||||
}
|
||||
|
||||
function eat(callback) {
|
||||
isEating = true
|
||||
|
||||
const foodNames = require('minecraft-data')(bot.version).foodsArray.map((item) => item.name)
|
||||
|
||||
let available_food = bot.inventory
|
||||
.items()
|
||||
.filter((item) => foodNames.includes(item.name))
|
||||
|
||||
if (available_food.length === 0 || !available_food) {
|
||||
isEating = false
|
||||
return callback(new Error('No food found.'))
|
||||
}
|
||||
|
||||
if (cfg.eat.bannedFood.length > 0) {
|
||||
available_food = available_food.filter(
|
||||
(item) => !cfg.eat.bannedFood.includes(item.name)
|
||||
)
|
||||
}
|
||||
let priority = cfg.eat.priority
|
||||
let best_food = available_food.reduce((prev, current) => (prev[priority] > current[priority]) ? prev : current)
|
||||
|
||||
if (!best_food) {
|
||||
isEating = false
|
||||
return callback(new Error('No best food has been found.'))
|
||||
}
|
||||
|
||||
bot.emit('eat_start')
|
||||
|
||||
bot.equip(best_food, 'hand', function (error) {
|
||||
if (error) {
|
||||
console.error(error)
|
||||
isEating = false
|
||||
bot.emit('eat_stop')
|
||||
} else {
|
||||
bot.consume(function (err) {
|
||||
if (err) {
|
||||
console.error(err)
|
||||
isEating = false
|
||||
bot.emit('eat_stop')
|
||||
return callback(err)
|
||||
} else {
|
||||
isEating = false
|
||||
bot.emit('eat_stop')
|
||||
callback(null)
|
||||
if (!bot.food === 20) eat(callbackHandle)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function checkFood() {
|
||||
console.info("eater: "
|
||||
// , " status: ", !isEating
|
||||
, cfg.eat.auto && "auto"
|
||||
, bot.food < cfg.eat.startAt && "hungry"
|
||||
, "hunger:", bot.food
|
||||
, "at:", cfg.eat.startAt)
|
||||
if (
|
||||
!isEating
|
||||
&& cfg.eat.auto
|
||||
&& bot.food < cfg.eat.startAt
|
||||
) {
|
||||
if (
|
||||
(bot.pathfinder
|
||||
&& !(bot.pathfinder.isMining() || bot.pathfinder.isBuilding())
|
||||
// TODO implement better idle state
|
||||
) || true // idle most likely
|
||||
) {
|
||||
eat(callbackHandle)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function resetEat(value) {
|
||||
// to prevent the plugin from breaking if the bot gets killed while eating btw
|
||||
isEating = !!value // false
|
||||
}
|
||||
|
||||
const load = (config) => {
|
||||
cfg = config
|
||||
bot = cfg.bot
|
||||
cfg.eat = {
|
||||
priority: 'saturation', //'foodPoints', //
|
||||
// startAt: 19, //anarchy
|
||||
// startAt: 18,
|
||||
startAt: 14,
|
||||
bannedFood: [
|
||||
"enchanted_golden_apple", "golden_apple", "pufferfish", "chorus_fruit"
|
||||
],
|
||||
auto: true
|
||||
}
|
||||
|
||||
bot.on('health', checkFood)
|
||||
bot.on('spawn', resetEat)
|
||||
|
||||
}
|
||||
|
||||
const unload = () => {
|
||||
bot.off('health', checkFood)
|
||||
bot.off('spawn', resetEat)
|
||||
}
|
||||
|
||||
module.exports = { load, unload, eat, resetEat }
|
95
lib/plugins/mover.js
Normal file
95
lib/plugins/mover.js
Normal file
@@ -0,0 +1,95 @@
|
||||
// import { EntityFilters } from "mineflayer-statemachine"
|
||||
// import v from "vec3"
|
||||
|
||||
// import { Movements } from "mineflayer-pathfinder"
|
||||
|
||||
// const mineflayer = require('mineflayer')
|
||||
const { Movements } = require('mineflayer-pathfinder')
|
||||
// const { GoalBLah } = require('mineflayer-pathfinder').goals
|
||||
const v = require('vec3')
|
||||
|
||||
let cfg = {}
|
||||
let bot = {}
|
||||
// let moving
|
||||
let pathfinder
|
||||
let movements = []
|
||||
|
||||
|
||||
function initMoves(bot = bot, mcData = require('minecraft-data')(bot.version)) {
|
||||
let defaultMove = new Movements(bot, mcData)
|
||||
defaultMove.canDig = false
|
||||
defaultMove.scafoldingBlocks.push(mcData.blocksByName.slime_block.id)
|
||||
// defaultMove.blocksCantBreak.add(mcData.blocksByName.glass.id)
|
||||
// defaultMove.blocksToAvoid.add(mcData.blocksByName.magma.id)
|
||||
movements.defaultMove = defaultMove
|
||||
|
||||
bot.pathfinder.setMovements(defaultMove)
|
||||
}
|
||||
|
||||
|
||||
function moveNear(pos, distance = 3) {
|
||||
const { GoalNear } = require('mineflayer-pathfinder').goals
|
||||
cfg.quiet || bot.chat(`moving to ${pos}`)
|
||||
|
||||
pos = v(pos)
|
||||
bot.pathfinder.setMovements(movements.defaultMove)
|
||||
bot.pathfinder.setGoal(new GoalNear(pos.x, pos.y, pos.z, distance))
|
||||
}
|
||||
|
||||
function follow(entity, dynamic = true) {
|
||||
console.assert(entity)
|
||||
const { GoalFollow } = require('mineflayer-pathfinder').goals
|
||||
|
||||
|
||||
|
||||
cfg.quiet && console.log(entity)
|
||||
|| bot.chat(
|
||||
`following ${entity.type
|
||||
}: ${entity.username || entity.displayName
|
||||
}${dynamic ? "" : " once"}`
|
||||
)
|
||||
|
||||
entity = entity.entity ? entity.entity : entity
|
||||
|
||||
// console.log(entity)
|
||||
|
||||
bot.pathfinder.setMovements(movements.defaultMove)
|
||||
bot.pathfinder.setGoal(new GoalFollow(entity, 3), dynamic)
|
||||
}
|
||||
|
||||
function hit(blockOrEntity) {
|
||||
bot.chat(`hitting ${entity.name || entity.type}`)
|
||||
}
|
||||
|
||||
function stop() {
|
||||
bot.pathfinder.setGoal(null)
|
||||
bot.stopDigging()
|
||||
}
|
||||
|
||||
const load = (config) => {
|
||||
cfg = config
|
||||
bot = cfg.bot
|
||||
cfg.move = {
|
||||
// auto: true,
|
||||
canDig: false,
|
||||
// list: ["hello", "wassup"],
|
||||
quiet: !!cfg.quiet,
|
||||
movements: []
|
||||
}
|
||||
|
||||
pathfinder = bot.pathfinder || bot.loadPlugin(require('mineflayer-pathfinder').pathfinder)
|
||||
|
||||
// initMoves(bot, mcData)
|
||||
setTimeout(initMoves, 500, bot)
|
||||
|
||||
// bot.loadPlugin(pathfinder)
|
||||
|
||||
// bot.on('time', hello)
|
||||
|
||||
}
|
||||
|
||||
const unload = () => {
|
||||
// TODO stop pathfinding maybe?
|
||||
}
|
||||
|
||||
module.exports = { load, unload, stop, initMoves, moveNear, follow }
|
@@ -1,13 +1,6 @@
|
||||
|
||||
let pathfinder
|
||||
//TODO replace with simple pathfinder motions
|
||||
const {
|
||||
gameplay,
|
||||
MoveTo,
|
||||
MoveToInteract,
|
||||
ObtainItem,
|
||||
// Craft
|
||||
} = require('prismarine-gameplay')
|
||||
|
||||
let cfg = {}
|
||||
let bot = {}
|
||||
@@ -33,15 +26,8 @@ function sleep(quiet) {
|
||||
}
|
||||
if (bed && bedstatus == "") {
|
||||
bot.lookAt(bed.position)
|
||||
// const nearbed =
|
||||
bot.gameplay.solveFor(
|
||||
new MoveTo((bed.position.range = 2) && bed.position), (err) => {
|
||||
// new MoveTo(bed.position), (err) => {
|
||||
// new MoveToInteract(bed.position), (err) => {
|
||||
if (err) {
|
||||
!quiet && bot.chat(`can't reach bed: ${err.message}`)
|
||||
} else {
|
||||
bot.waitForChunksToLoad(() => {
|
||||
cfg.plugins.moveNear(bed.position)
|
||||
bot.sleep(bed, (err) => {
|
||||
if (err) {
|
||||
!quiet && bot.chat(`can't sleep: ${err.message}`)
|
||||
@@ -54,21 +40,19 @@ function sleep(quiet) {
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
// } else if (bed){
|
||||
} else if (inv && inv.equipItem("red_bed", "hand", true)) {
|
||||
// doesn't work fortunately
|
||||
// FIXME: DONT IMPLEMENT until it is detected as NOT NETHER
|
||||
bot.placeBlock()
|
||||
// bot.placeBlock()
|
||||
} else {
|
||||
bot.gameplay.solveFor(
|
||||
new ObtainItem("bed"), (err) => {
|
||||
if (err) {
|
||||
!quiet && bot.chat(`need a${bedstatus} bed: may not see if just placed`)
|
||||
}
|
||||
}
|
||||
)
|
||||
// TODO: use mover
|
||||
// bot.gameplay.solveFor(
|
||||
// new ObtainItem("bed"), (err) => {
|
||||
// if (err) {
|
||||
// !quiet && bot.chat(`need a${bedstatus} bed: may not see if just placed`)
|
||||
// }
|
||||
// }
|
||||
// )
|
||||
// bot.chat('/afk')
|
||||
}
|
||||
bot.pathfinder.movements
|
||||
@@ -104,7 +88,6 @@ const load = (config) => {
|
||||
|
||||
pathfinder = bot.pathfinder || require('mineflayer-pathfinder').pathfinder
|
||||
// bot.loadPlugin(pathfinder)
|
||||
bot.loadPlugin(gameplay)
|
||||
inv = cfg.plugins["inventory"]
|
||||
bot.on("time", autoSleep)
|
||||
|
||||
|
@@ -42,7 +42,6 @@
|
||||
"prismarine-block": "^1",
|
||||
"prismarine-chat": "^1",
|
||||
"prismarine-entity": "^1.1.0",
|
||||
"prismarine-gameplay": "github:TheDudeFromCI/prismarine-gameplay#crafting",
|
||||
"prismarine-item": "^1.5.0",
|
||||
"prismarine-nbt": "^1.3",
|
||||
"prismarine-recipe": "^1",
|
||||
|
Reference in New Issue
Block a user