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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
49 changes: 43 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

Expand All @@ -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()
Expand All @@ -27,32 +27,65 @@ function inject (bot) {

return enchantmentTable


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

const slots = enchantmentTable.enchantments

if (bot.majorVersion !== "1.8"){
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)
// 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 (bot.majorVersion === "1.8" ) { slot.expected.enchant = packet.value }
else {

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].level >= 0 && slots[1].level >= 0 && slots[2].level >= 0) {
let readycheck;

if (bot.majorVersion === "1.8") {
readyCheck = slots[0].level >= 0 && slots[1].level >= 0 && slots[2].level >= 0
} else
{
readyCheck = slots[0].expected.level > 0 && slots[1].expected.level >= 0 && slots[2].expected.level >=0
}

// console.log(slots)
if (readyCheck) {
if (!ready) {
ready = true
enchantmentTable.emit('ready')
//console.log('emitted ready')
//console.log(slots)
enchantmentTable.emit('ready')
}
} else {
ready = false
ready = false
}
}

Expand All @@ -75,6 +108,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')

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