CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/examples/anvil.js
Views: 789
1
/**
2
* This example demonstrates how to use anvils w/ mineflayer
3
* the options are: (<Option> are required, [<Option>] are optional)
4
* 1. "anvil combine <itemName1> <itemName2> [<name>]"
5
* 2. "anvil rename <itemName> <name>"
6
*
7
* to use this:
8
* /op anvilman
9
* /gamemode anvilman creative
10
* /xp set anvilman 999 levels
11
*
12
* Put an anvil near the bot
13
* Give him a sword and an enchanted book
14
* say list
15
* say xp
16
* say anvil combine diamond_sword enchanted_book
17
*/
18
const mineflayer = require('mineflayer')
19
20
if (process.argv.length < 4 || process.argv.length > 6) {
21
console.log('Usage : node anvil.js <host> <port> [<name>] [<password>]')
22
process.exit(1)
23
}
24
25
const bot = mineflayer.createBot({
26
host: process.argv[2],
27
port: parseInt(process.argv[3]),
28
username: process.argv[4] ? process.argv[4] : 'anvilman',
29
password: process.argv[5]
30
})
31
32
bot.on('chat', async (username, message) => {
33
const command = message.split(' ')
34
35
switch (true) {
36
case /^list$/.test(message):
37
sayItems()
38
break
39
case /^toss \w+$/.test(message):
40
// toss name
41
// ex: toss diamond
42
tossItem(command[1])
43
break
44
case /^xp$/.test(message):
45
bot.chat(bot.experience.level)
46
break
47
case /^gamemode$/.test(message):
48
bot.chat(bot.game.gameMode)
49
break
50
case /^anvil combine \w+ \w+$/.test(message): // anvil firstSlot secondSlot
51
combine(bot, command[2], command[3])
52
break
53
case /^anvil combine \w+ \w+ (.+)$/.test(message): // anvil firstSlot secondSlot name
54
combine(bot, command[2], command[3], command.slice(4).join(' '))
55
break
56
case /^anvil rename \w+ (.+)/.test((message)):
57
rename(bot, command[2], command.slice(3).join(' '))
58
break
59
}
60
})
61
62
async function tossItem (name, amount) {
63
amount = parseInt(amount, 10)
64
const item = itemByName(name)
65
if (!item) {
66
bot.chat(`I have no ${name}`)
67
} else {
68
try {
69
if (amount) {
70
await bot.toss(item.type, null, amount)
71
bot.chat(`tossed ${amount} x ${name}`)
72
} else {
73
await bot.tossStack(item)
74
bot.chat(`tossed ${name}`)
75
}
76
} catch (err) {
77
bot.chat(`unable to toss: ${err.message}`)
78
}
79
}
80
}
81
82
function itemByName (name) {
83
return bot.inventory.items().filter(item => item.name === name)[0]
84
}
85
86
function itemToString (item) {
87
if (item) {
88
return `${item.name} x ${item.count}`
89
} else {
90
return '(nothing)'
91
}
92
}
93
94
function sayItems (items = bot.inventory.items()) {
95
const output = items.map(itemToString).join(', ')
96
if (output) {
97
bot.chat(output)
98
} else {
99
bot.chat('empty')
100
}
101
}
102
103
function getAnvilIds () {
104
const matchingBlocks = [bot.registry.blocksByName.anvil.id]
105
if (bot.registry.blocksByName?.chipped_anvil) {
106
matchingBlocks.push(bot.registry.blocksByName.chipped_anvil.id)
107
matchingBlocks.push(bot.registry.blocksByName.damaged_anvil.id)
108
}
109
return matchingBlocks
110
}
111
112
async function rename (bot, itemName, name) {
113
const anvilBlock = bot.findBlock({
114
matching: getAnvilIds()
115
})
116
const anvil = await bot.openAnvil(anvilBlock)
117
try {
118
await anvil.rename(itemByName(itemName), name)
119
bot.chat('Anvil used successfully.')
120
} catch (err) {
121
bot.chat(err.message)
122
}
123
anvil.close()
124
}
125
126
async function combine (bot, itemName1, itemName2, name) {
127
const anvilBlock = bot.findBlock({
128
matching: getAnvilIds()
129
})
130
const anvil = await bot.openAnvil(anvilBlock)
131
try {
132
bot.chat('Using the anvil...')
133
await anvil.combine(itemByName(itemName1), itemByName(itemName2), name)
134
bot.chat('Anvil used successfully.')
135
} catch (err) {
136
bot.chat(err.message)
137
}
138
anvil.close()
139
}
140
141
bot.on('error', console.log)
142
143