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