Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS
GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/lib/plugins/settings.js
1467 views
1
const assert = require('assert')
2
3
module.exports = inject
4
5
const chatToBits = {
6
enabled: 0,
7
commandsOnly: 1,
8
disabled: 2
9
}
10
11
const handToBits = {
12
left: 0,
13
right: 1
14
}
15
16
const viewDistanceToBits = {
17
far: 12,
18
normal: 10,
19
short: 8,
20
tiny: 6
21
}
22
23
function inject (bot, options) {
24
function setSettings (settings) {
25
extend(bot.settings, settings)
26
27
// chat
28
const chatBits = chatToBits[bot.settings.chat]
29
assert.ok(chatBits != null, `invalid chat setting: ${bot.settings.chat}`)
30
31
// view distance
32
let viewDistanceBits = null
33
if (typeof bot.settings.viewDistance === 'string') {
34
viewDistanceBits = viewDistanceToBits[bot.settings.viewDistance]
35
} else if (typeof bot.settings.viewDistance === 'number' && bot.settings.viewDistance > 0) { // Make sure view distance is a valid # || should be 2 or more
36
viewDistanceBits = bot.settings.viewDistance
37
}
38
assert.ok(viewDistanceBits != null, `invalid view distance setting: ${bot.settings.viewDistance}`)
39
40
// hand
41
const handBits = handToBits[bot.settings.mainHand]
42
assert.ok(handBits != null, `invalid main hand: ${bot.settings.mainHand}`)
43
44
// skin
45
// cape is inverted, not used at all (legacy?)
46
// bot.settings.showCape = !!bot.settings.showCape
47
const skinParts = bot.settings.skinParts.showCape << 0 |
48
bot.settings.skinParts.showJacket << 1 |
49
bot.settings.skinParts.showLeftSleeve << 2 |
50
bot.settings.skinParts.showRightSleeve << 3 |
51
bot.settings.skinParts.showLeftPants << 4 |
52
bot.settings.skinParts.showRightPants << 5 |
53
bot.settings.skinParts.showHat << 6
54
55
// write the packet
56
bot._client.write('settings', {
57
locale: bot.settings.locale || 'en_US',
58
viewDistance: viewDistanceBits,
59
chatFlags: chatBits,
60
chatColors: bot.settings.colorsEnabled,
61
skinParts,
62
mainHand: handBits,
63
enableTextFiltering: bot.settings.enableTextFiltering,
64
enableServerListing: bot.settings.enableServerListing
65
})
66
}
67
68
bot.settings = {
69
chat: options.chat || 'enabled',
70
colorsEnabled: options.colorsEnabled == null
71
? true
72
: options.colorsEnabled,
73
viewDistance: options.viewDistance || 'far',
74
difficulty: options.difficulty == null
75
? 2
76
: options.difficulty,
77
skinParts: options.skinParts == null
78
? {
79
showCape: true,
80
showJacket: true,
81
showLeftSleeve: true,
82
showRightSleeve: true,
83
showLeftPants: true,
84
showRightPants: true,
85
showHat: true
86
}
87
: options.skinParts,
88
mainHand: options.mainHand || 'right',
89
enableTextFiltering: options.enableTextFiltering || false,
90
enableServerListing: options.enableServerListing || true,
91
particleStatus: 'all'
92
}
93
94
bot._client.on('login', () => {
95
setSettings({})
96
})
97
98
bot.setSettings = setSettings
99
}
100
101
const hasOwn = {}.hasOwnProperty
102
function extend (obj, src) {
103
for (const key in src) {
104
if (hasOwn.call(src, key)) obj[key] = src[key]
105
}
106
return obj
107
}
108
109