Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the enchantment table "ready" event, which has been broken for years. #3120

Closed
wants to merge 14 commits into from
Prev Previous commit
Next Next commit
Update enchantment_table.js
zisis912 authored Sep 20, 2024
commit c700a21415cd918bf5bff6eb8bf1246c4ab2860f
39 changes: 33 additions & 6 deletions lib/plugins/enchantment_table.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const assert = require('assert')
const { once } = require('events')
const { once } = require('../promise_utils')

module.exports = inject

@@ -9,7 +9,7 @@ function inject (bot) {
let ready = false
const enchantmentTable = await bot.openBlock(enchantmentTableBlock)
if (!enchantmentTable.type.startsWith('minecraft:enchant')) {
throw new Error('This is not an enchantment table')
throw new Error('Expected minecraft:enchant when opening table but got ' + enchantmentTable.type)
}

resetEnchantmentOptions()
@@ -27,32 +27,55 @@ function inject (bot) {

return enchantmentTable


function onUpdateWindowProperty (packet) {
if (packet.windowId !== enchantmentTable.id) return
assert.ok(packet.property >= 0)

const slots = enchantmentTable.enchantments


if (packet.property === 4){
for (let i=0;i<3;i++) {
// console.log('setting appropriate slots')
if (slots[i].level === 0) {
slots[i].level = 0
slots[i].expected.enchant = null
slots[i].expected.level = 0
}
}
}

// console.log(packet.property)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented code

// console.log(packet.value)
if (packet.property < 3) {
const slot = slots[packet.property]
slot.level = packet.value
} else if (packet.property === 3) {
enchantmentTable.xpseed = packet.value
} else if (packet.property < 7) {
const slot = slots[packet.property - 4]
slot.expected.enchant = packet.value
if (packet.value === -1) { slot.expected.enchant = null }
else {
slot.expected.enchant = bot.registry.enchantments[packet.value].name
}
} else if (packet.property < 10) {
const slot = slots[packet.property - 7]
slot.expected.level = packet.value
}

if (slots[0].expected.level > 0 && slots[1].expected.level > 0 && slots[2].expected.level > 0) {


// console.log(slots)
if (slots[0].expected.level > 0 && slots[1].expected.level >= 0 && slots[2].expected.level >=0 ) {
if (!ready) {
ready = true
enchantmentTable.emit('ready')
//console.log('emitted ready')
//console.log(slots)
enchantmentTable.emit('ready')
}
} else {
ready = false
ready = false
}
}

@@ -75,6 +98,10 @@ function inject (bot) {
if (!ready) await once(enchantmentTable, 'ready')
choice = parseInt(choice, 10) // allow string argument
assert.notStrictEqual(enchantmentTable.enchantments[choice].level, -1)

// if (enchantmentTable.enchantments[choice].level>bot.experience.level) throw new Error('insufficient xp to enchant')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented code


// console.log('enchanting choice '+choice)
bot._client.write('enchant_item', {
windowId: enchantmentTable.id,
enchantment: choice