Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS
GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/lib/plugins/command_block.js
1467 views
1
const assert = require('assert')
2
const { ProtoDef } = require('protodef')
3
4
module.exports = inject
5
6
function inject (bot) {
7
function setCommandBlock (pos, command, options = {}) {
8
assert.strictEqual(bot.player.gamemode, 1, new Error('The bot has to be in creative mode to open the command block window'))
9
assert.notStrictEqual(pos, null)
10
assert.notStrictEqual(command, null)
11
assert.strictEqual(bot.blockAt(pos).name.includes('command_block'), true, new Error("The block isn't a command block"))
12
13
// Default values when a command block is placed in vanilla minecraft
14
options.trackOutput = options.trackOutput ?? false
15
options.conditional = options.conditional ?? false
16
options.alwaysActive = options.alwaysActive ?? false
17
options.mode = options.mode ?? 2 // Possible values: 0: SEQUENCE, 1: AUTO and 2: REDSTONE
18
19
let flags = 0
20
flags |= +options.trackOutput << 0 // 0x01
21
flags |= +options.conditional << 1 // 0x02
22
flags |= +options.alwaysActive << 2 // 0x04
23
24
if (bot.supportFeature('usesAdvCmd') || bot.supportFeature('usesAdvCdm')) {
25
const pluginChannelName = bot.supportFeature('usesAdvCdm') ? 'MC|AdvCdm' : 'MC|AdvCmd'
26
27
const proto = new ProtoDef()
28
29
proto.addType('string', [
30
'pstring',
31
{
32
countType: 'varint'
33
}])
34
35
proto.addType(pluginChannelName, [
36
'container',
37
[
38
{
39
name: 'mode',
40
type: 'i8'
41
},
42
{
43
name: 'x',
44
type: [
45
'switch',
46
{
47
compareTo: 'mode',
48
fields: {
49
0: 'i32'
50
},
51
default: 'void'
52
}
53
]
54
},
55
{
56
name: 'y',
57
type: [
58
'switch',
59
{
60
compareTo: 'mode',
61
fields: {
62
0: 'i32'
63
},
64
default: 'void'
65
}
66
]
67
},
68
{
69
name: 'z',
70
type: [
71
'switch',
72
{
73
compareTo: 'mode',
74
fields: {
75
0: 'i32'
76
},
77
default: 'void'
78
}
79
]
80
},
81
{
82
name: 'eid',
83
type: [
84
'switch',
85
{
86
compareTo: 'mode',
87
fields: {
88
1: 'i32'
89
},
90
default: 'void'
91
}
92
]
93
},
94
{
95
name: 'command',
96
type: 'string'
97
},
98
{
99
name: 'trackOutput',
100
type: 'bool'
101
}
102
]
103
])
104
105
const buffer = proto.createPacketBuffer(pluginChannelName, {
106
mode: 0,
107
x: pos.x,
108
y: pos.y,
109
z: pos.z,
110
command,
111
trackOutput: options.trackOutput
112
})
113
bot._client.write('custom_payload', {
114
channel: pluginChannelName,
115
data: buffer
116
})
117
} else {
118
bot._client.write('update_command_block', {
119
location: pos,
120
command,
121
mode: options.mode,
122
flags
123
})
124
}
125
}
126
127
bot.setCommandBlock = setCommandBlock
128
}
129
130