Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const mineflayer = require('mineflayer')
- const pvp = require('mineflayer-pvp').plugin
- const { pathfinder, Movements, goals} = require('mineflayer-pathfinder')
- const armorManager = require('mineflayer-armor-manager')
- const AutoAuth = require('mineflayer-auto-auth')
- const autoeat = require("mineflayer-auto-eat")
- function createBot(){
- const bot = mineflayer.createBot({
- host: process.argv[2] ||"wolfxmc.org",
- port: "25565",
- username: 'Hcat_lbS68Y2k',
- logErrors: true,
- hideErrors: false,
- version: "1.16.5",
- plugins: [AutoAuth],
- AutoAuth: {
- logging: true,
- password: 'AdminHcat',
- ignoreRepeat: true
- }
- })
- bot.on('error', (err) => console.log(err))
- bot.on('end', createBot)
- // const bot = mineflayer.createBot({
- // host: process.argv[2],
- // port: process.argv[3],
- // username: process.argv[4] || 'Hcat_SuperBot',
- // password: process.argv[5],
- // logErrors: false,
- // plugins: [AutoAuth],
- // AutoAuth: {
- // logging: true,
- // password: 'AdminHcat',
- // ignoreRepeat: true
- // }
- // })
- bot.loadPlugin(pvp)
- bot.loadPlugin(armorManager)
- bot.loadPlugin(pathfinder)
- bot.loadPlugin(autoeat)
- bot.on('serverAuth', function() {
- // 授权登录成功
- bot.chat("授权登录成功")
- });
- bot.on('playerCollect', (collector, itemDrop) => {
- if (collector !== bot.entity) return
- setTimeout(() => {
- const sword = bot.inventory.items().find(item => item.name.includes('sword'))
- if (sword) bot.equip(sword, 'hand')
- }, 150)
- })
- bot.on('playerCollect', (collector, itemDrop) => {
- if (collector !== bot.entity) return
- bot.armorManager.equipAll()
- setTimeout(() => {
- const shield = bot.inventory.items().find(item => item.name.includes('shield'))
- if (shield) bot.equip(shield, 'off-hand')
- }, 250)
- })
- let guardPos = null
- function guardArea (pos) {
- guardPos = pos.clone()
- if (!bot.pvp.target) {
- moveToGuardPos()
- }
- }
- function stopGuarding () {
- guardPos = null
- bot.pvp.stop()
- bot.pathfinder.setGoal(null)
- }
- function moveToGuardPos () {
- const mcData = require('minecraft-data')(bot.version)
- bot.pathfinder.setMovements(new Movements(bot, mcData))
- bot.pathfinder.setGoal(new goals.GoalBlock(guardPos.x, guardPos.y, guardPos.z))
- }
- function moveToEntityPos (entity) {
- const mcData = require('minecraft-data')(bot.version)
- bot.pathfinder.setMovements(new Movements(bot, mcData))
- bot.pathfinder.setGoal(new goals.GoalBlock(entity.position.x, entity.position.y, entity.position.z))
- }
- bot.on('stoppedAttacking', () => {
- if (guardPos) {
- moveToGuardPos()
- }
- })
- bot.on('physicTick', () => {
- if (bot.pvp.target) return
- if (bot.pathfinder.isMoving()) return
- const entity = bot.nearestEntity()
- if(!entity)return
- if (entity.displayName !=="undefined" && entity.displayName !=="Arrow") bot.lookAt(entity.position.offset(0, entity.height, 0))
- // console.log("跟踪到实体:" + entity.displayName)
- if (entity.displayName === "Item") moveToEntityPos(entity)
- })
- bot.on('physicTick', () => {
- if (!guardPos) return
- const filter = e => e.type === 'mob' && e.position.distanceTo(bot.entity.position) < 16 &&
- e.mobType !== 'Armor Stand' // Mojang classifies armor stands as mobs for some reason?
- const entity = bot.nearestEntity(filter)
- if (entity) {
- bot.pvp.attack(entity)
- }
- })
- bot.on('chat', (username, message) => {
- if (message === 'guard' || message === '守护') {
- const player = bot.players[username]
- if (!player) {
- bot.chat("我看不到你")
- return
- }
- if (!player.entity){
- bot.chat("无法找到实体")
- return
- }
- bot.chat('我会保护好这个地方哒')
- guardArea(player.entity.position)
- }
- if (message === 'fight me') {
- const player = bot.players[username]
- if (!player) {
- bot.chat("我看不到你")
- return
- }
- bot.chat('准备战斗!')
- bot.pvp.attack(player.entity)
- }
- if (message === 'stop') {
- bot.chat('我将不再守卫这个地方')
- stopGuarding()
- }
- })
- //控制台打印消息
- bot.on('message', (message) => {
- console.log(message.toAnsi())
- })
- //控制台打印生命状态
- bot.on('health', () => {
- // console.log(bot.food)
- })
- bot.once('spawn', () => {
- bot.autoEat.options = {
- priority: 'foodPoints',
- startAt: 18,
- setTimeout: 10,
- bannedFood: ["golden_apple", "enchanted_golden_apple", "rotten_flesh"]
- }
- // bot.physics.sprintSpeed = 15;
- })
- bot.on('autoeat_started', () => {
- console.log('自动进食已开始!')
- })
- bot.on('autoeat_stopped', () => {
- console.log('自动进食已停止!')
- })
- bot.on('entityHurt', (entity) => {
- if (entity===bot.entity){
- guardArea(bot.entity.position)
- const sword = bot.inventory.items().find(item => item.name.includes('sword'))
- if (sword) bot.equip(sword, 'hand')
- console.log("焯!我被打了")
- }
- })
- bot.on('entityEffect', (entity,effect) => {
- console.log("获得buff:"+ entity.type+"BUFF:"+effect.id)
- })
- var resend = false
- var tpaccept = false
- bot.on('chat', (username, message) => {
- if (message === "BotTpME") {
- if (username !== "Hcat") {
- bot.chat("机器人暂仅支持Hcat使用哦")
- return
- }
- bot.chat("/tpa " + username)
- bot.chat("已发送传送请求")
- }
- if (message === "切换复读机") {
- if (username !== "Hcat") {
- bot.chat("机器人暂仅支持Hcat使用哦")
- return
- }
- resend = !resend
- bot.chat("复读机开关:" + resend)
- }
- if (message === "切换自动请求") {
- if (username !== "Hcat") {
- bot.chat("机器人暂仅支持Hcat使用哦")
- return
- }
- tpaccept = !tpaccept
- bot.chat("自动接受传送:" + tpaccept)
- }
- // if (tpaccept) {
- // bot.chat("/tpaccept")
- // } else {
- // bot.chat("/tpdeny")
- // }
- if (message === '手动接受TP') {
- bot.chat('/tpaccept')
- }
- if (message === '传送小黑塔') {
- bot.chat('/res tp black_tower')
- }
- if (username === bot.username) return
- if (resend === true) bot.chat(message)
- })
- bot.on('health', () => {
- if (bot.food === 20 || bot.pvp.target) {
- bot.autoEat.disable()//如果机器人处于20个食物点,则自动进食
- }else bot.autoEat.enable() // 否则再次启用插件
- })
- bot.on('chat', async (username, message) => {
- if (username === bot.username) return
- switch (message) {
- case 'loaded':
- await bot.waitForChunksToLoad()
- bot.chat('Ready!')
- break
- case 'list':
- sayItems()
- break
- case 'dig':
- dig()
- break
- case 'build':
- build()
- break
- case 'equip dirt':
- equipDirt()
- break
- }
- })
- function sayItems (items = bot.inventory.items()) {
- const output = items.map(itemToString).join(', ')
- if (output) {
- bot.chat(output)
- } else {
- bot.chat('背包内无物品')
- }
- }
- function dig () {
- let target
- if (bot.targetDigBlock) {
- bot.chat(`already digging ${bot.targetDigBlock.name}`)
- } else {
- target = bot.blockAt(bot.entity.position.offset(0, -1, 0))
- if (target && bot.canDigBlock(target)) {
- bot.chat(`正在开始采集 ${target.name}`)
- bot.dig(target, onDiggingCompleted)
- } else {
- bot.chat('cannot dig')
- }
- }
- function onDiggingCompleted (err) {
- if (err) {
- console.log(err.stack)
- return
- }
- bot.chat(`finished digging ${target.name}`)
- }
- }
- function build () {
- const referenceBlock = bot.blockAt(bot.entity.position.offset(0, -1, 0))
- const jumpY = Math.floor(bot.entity.position.y) + 1.0
- bot.setControlState('jump', true)
- bot.on('move', placeIfHighEnough)
- let tryCount = 0
- function placeIfHighEnough () {
- if (bot.entity.position.y > jumpY) {
- bot.placeBlock(referenceBlock, vec3(0, 1, 0), (err) => {
- if (err) {
- tryCount++
- if (tryCount > 10) {
- bot.chat(err.message)
- bot.setControlState('jump', false)
- bot.removeListener('move', placeIfHighEnough)
- return
- }
- return
- }
- bot.setControlState('jump', false)
- bot.removeListener('move', placeIfHighEnough)
- bot.chat('Placing a block was successful')
- })
- }
- }
- }
- function equipDirt () {
- const mcData = require('minecraft-data')(bot.version)
- let itemsByName
- if (bot.supportFeature('itemsAreNotBlocks')) {
- itemsByName = 'itemsByName'
- } else if (bot.supportFeature('itemsAreAlsoBlocks')) {
- itemsByName = 'blocksByName'
- }
- bot.equip(mcData[itemsByName].dirt.id, 'hand', (err) => {
- if (err) {
- bot.chat(`unable to equip dirt: ${err.message}`)
- } else {
- bot.chat('equipped dirt')
- }
- })
- }
- function itemToString (item) {
- if (item) {
- return `${item.name} x ${item.count}`
- } else {
- return '(nothing)'
- }
- }
- }
- createBot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement