PrismarineJS / mineflayer

Create Minecraft bots with a powerful, stable, and high level JavaScript API.
https://prismarinejs.github.io/mineflayer/
MIT License
5.11k stars 923 forks source link

throw new Error(`Server rejected transaction for clicking on slot ${slot}, on window with id ${window?.id}.`) #3048

Closed MisingN closed 1 year ago

MisingN commented 1 year ago

Server rejected transaction for clicking on slot 9, on window with id 0.\

at clickWindow (C:\Users\Admin\Desktop\project\NodejsConsoleApp2\node_modules\mineflayer\lib\plugins\inventory.js:549:15) at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

at async EventEmitter.moveSlotItem(C:\Users\Admin\Desktop\project\NodejsConsoleApp2\node_modules\mineflayer\lib\plugins\inventory.js:567:5)

at async EventEmitter.equip (C:\Users\Admin\Desktop\project\NodejsConsoleApp2\node_modules\mineflayer\lib\plugins\simple_inventory.js:125:5)

All my code:

const mineflayer = require('mineflayer') const pathfinder = require('mineflayer-pathfinder').pathfinder const Movements = require('mineflayer-pathfinder').Movements const { GoalNear } = require('mineflayer-pathfinder').goals const armorManager = require('mineflayer-armor-manager') const pvp = require('mineflayer-pvp').plugin const autoeat = require('mineflayer-auto-eat').plugin const toolPlugin = require('mineflayer-tool').plugin const vec3 = require('vec3') const fs = require("fs");

const bot = mineflayer.createBot({ host: "Misingpley1.aternos.me", version: "1.16.4", username: "JsBot", })//Create bot

bot.loadPlugin(pathfinder) bot.loadPlugin(armorManager) bot.loadPlugin(pvp) bot.loadPlugin(autoeat) bot.loadPlugin(toolPlugin)//Plugins

bot.once('spawn', function () { bot.chat('Hi World') })//Hello world

bot.on('chat', function Hi(_username, message) { ; if (message === "Info") { bot.chat("...") } })//info

bot.once('spawn', () => {
    const defaultMove = new Movements(bot)

    bot.on('chat', function (username, message) {

        if (username === bot.username) return

        const target = bot.players[username] ? bot.players[username].entity : null
        if (message === 'come') {
            if (!target) {
                bot.chat('I don\'t see you !')
                return
            }
            const p = target.position

            bot.pathfinder.setMovements(defaultMove)
            bot.pathfinder.setGoal(new GoalNear(p.x, p.y, p.z, 1))
        }
    })
})//Come to player

bot.armorManager.equipAll()//Equip Armor

bot.on('chat', (username, message) => { if (message === 'fight me') { const player = bot.players[username]

    if (!player) {
        bot.chat("I can't see you.")
        return
    }

    bot.pvp.attack(player.entity)
}

if (message === 'stop') {
    bot.pvp.stop()
}

})//pvp

bot.on('autoeat_started', (item, offhand) => { console.log(Eating ${item.name} in ${offhand ? 'offhand' : 'hand'}) })

bot.on('autoeat_finished', (item, offhand) => { console.log(Finished eating ${item.name} in ${offhand ? 'offhand' : 'hand'}) })

bot.on('autoeat_error', console.error)//Auto-eat

bot.on('spawn', () => { const totemId = bot.registry.itemsByName.totem_of_undying.id // Get the correct id if (bot.registry.itemsByName.totem_of_undying) { setInterval(() => { const totem = bot.inventory.findInventoryItem(totemId, null) if (totem) { bot.equip(totem, 'hand') } }, 50) } })//auto-totem

function blockToSow() { return bot.findBlock({ point: bot.entity.position, matching: bot.registry.blocksByName.farmland.id, maxDistance: 6, useExtraInfo: (block) => { const blockAbove = bot.blockAt(block.position.offset(0, 1, 0)) return !blockAbove || blockAbove.type === 0 } }) }

function blockToHarvest() { return bot.findBlock({ point: bot.entity.position, maxDistance: 6, matching: (block) => { return block && block.type === bot.registry.blocksByName.wheat.id && block.metadata === 7 } }) }

async function loop() { try { while (1) { const toHarvest = blockToHarvest() if (toHarvest) { await bot.dig(toHarvest) } else { break } } while (1) { const toSow = blockToSow() if (toSow) { await bot.equip(bot.registry.itemsByName.wheat_seeds.id, 'hand') await bot.placeBlock(toSow, new vec3(0, 1, 0)) } else { break } } } catch (e) { console.log(e) }

// No block to harvest or sow. Postpone next loop a bit
setTimeout(loop, 1000)

}

bot.once('login', loop)

setInterval(() => { const sword = bot.inventory.items().find(item => item.name.includes('sword')) if (sword) bot.equip(sword, 'hand') const entityFilter = e => e.type === 'mob' && e.mobType === 'Zombie' && e.position.distanceTo(bot.entity.position) < 6 const entity = bot.nearestEntity(entityFilter)

if (!entity) return;

if (entity) {
    // Start attacking
    bot.pvp.attack(entity)
}

}, 1000);//Zombie

Help me please!

WhoTho commented 1 year ago

You are switching slots too fast. My suggestion is to get rid of the auto totem function or change it so that it equips to the offhand only if there isnt a totem there already. Same type of thing with the auto zombie killer. You are trying to equip a sword every second and when your script does find a zombie, it will try to kill it while still trying to farm. You should also add checks to see if youve run out of seeds before trying to equip more. All in all, learn to code large projects with multiple states before taking a bunch of scripts from online and smacking them into a file.

Also join the prismarinejs discord for quicker and better help.

MisingN commented 1 year ago

Thank you, I changed the function of the auto-totem and check the sword in my hand and I succeeded