Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS
GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/lib/plugins/furnace.js
1467 views
1
const assert = require('assert')
2
3
module.exports = inject
4
5
function inject (bot) {
6
const allowedWindowTypes = ['minecraft:furnace', 'minecraft:blast_furnace', 'minecraft:smoker']
7
8
function matchWindowType (window) {
9
for (const type of allowedWindowTypes) {
10
if (window.type.startsWith(type)) return true
11
}
12
return false
13
}
14
15
async function openFurnace (furnaceBlock) {
16
const furnace = await bot.openBlock(furnaceBlock)
17
if (!matchWindowType(furnace)) {
18
throw new Error('This is not a furnace-like window')
19
}
20
21
furnace.totalFuel = null
22
furnace.fuel = null
23
furnace.fuelSeconds = null
24
furnace.totalProgress = null
25
furnace.progress = null
26
furnace.progressSeconds = null
27
furnace.takeInput = takeInput
28
furnace.takeFuel = takeFuel
29
furnace.takeOutput = takeOutput
30
furnace.putInput = putInput
31
furnace.putFuel = putFuel
32
furnace.inputItem = function () { return this.slots[0] }
33
furnace.fuelItem = function () { return this.slots[1] }
34
furnace.outputItem = function () { return this.slots[2] }
35
36
bot._client.on('craft_progress_bar', onUpdateWindowProperty)
37
furnace.once('close', () => {
38
bot._client.removeListener('craft_progress_bar', onUpdateWindowProperty)
39
})
40
41
return furnace
42
43
function onUpdateWindowProperty (packet) {
44
if (packet.windowId !== furnace.id) return
45
46
switch (packet.property) {
47
case 0: // Current fuel
48
furnace.fuel = 0
49
furnace.fuelSeconds = 0
50
if (furnace.totalFuel) {
51
furnace.fuel = packet.value / furnace.totalFuel
52
furnace.fuelSeconds = furnace.fuel * furnace.totalFuelSeconds
53
}
54
break
55
case 1: // Total fuel
56
furnace.totalFuel = packet.value
57
furnace.totalFuelSeconds = ticksToSeconds(furnace.totalFuel)
58
break
59
case 2: // Current progress
60
furnace.progress = 0
61
furnace.progressSeconds = 0
62
if (furnace.totalProgress) {
63
furnace.progress = packet.value / furnace.totalProgress
64
furnace.progressSeconds = furnace.totalProgressSeconds - (furnace.progress * furnace.totalProgressSeconds)
65
}
66
break
67
case 3: // Total progress
68
furnace.totalProgress = packet.value
69
furnace.totalProgressSeconds = ticksToSeconds(furnace.totalProgress)
70
}
71
72
furnace.emit('update')
73
}
74
75
async function takeSomething (item) {
76
assert.ok(item)
77
await bot.putAway(item.slot)
78
return item
79
}
80
81
async function takeInput () {
82
return takeSomething(furnace.inputItem())
83
}
84
85
async function takeFuel () {
86
return takeSomething(furnace.fuelItem())
87
}
88
89
async function takeOutput () {
90
return takeSomething(furnace.outputItem())
91
}
92
93
async function putSomething (destSlot, itemType, metadata, count) {
94
const options = {
95
window: furnace,
96
itemType,
97
metadata,
98
count,
99
sourceStart: furnace.inventoryStart,
100
sourceEnd: furnace.inventoryEnd,
101
destStart: destSlot,
102
destEnd: destSlot + 1
103
}
104
await bot.transfer(options)
105
}
106
107
async function putInput (itemType, metadata, count) {
108
await putSomething(0, itemType, metadata, count)
109
}
110
111
async function putFuel (itemType, metadata, count) {
112
await putSomething(1, itemType, metadata, count)
113
}
114
}
115
116
function ticksToSeconds (ticks) {
117
return ticks * 0.05
118
}
119
120
bot.openFurnace = openFurnace
121
}
122
123