-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommands.js
254 lines (215 loc) · 7.78 KB
/
commands.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
'use strict';
const shellwords = require('shellwords');
const ItemPile = require('itempile');
module.exports = (game, opts) => new CommandsPlugin(game, opts);
module.exports.pluginInfo = {
loadAfter: ['voxel-console']
};
class CommandsPlugin {
constructor(game, opts) {
this.game = game;
this.console = this.game.plugins.get('voxel-console');
if (!this.console) throw new Error('voxel-commands requires voxel-console');
this.registry = this.game.plugins.get('voxel-registry');
if (!this.registry) throw new Error('voxel-commands requires voxel-registry');
// can be set to suppress 'Not connected to server' chat messages
this.isConnectedToServer = false;
this.usages = {
pos: "x y z",
home: "",
item: "name [count [tags]]",
clear: "",
block: "name [data]",
plugins: "",
enable: "plugin",
disable: "plugin"
};
this.handlers = {
undefined: (command, ...args) => {
this.console.log(`Invalid command ${command} ${args.join(' ')}`);
},
help: () => {
this.console.log("Available commands:");
//for name of this.handlers # TODO: include all commands, but this extraneously includes aliases, too
for (let name in this.usages) { // only documented commands
const usage = this.usages[name];
if (usage === undefined) usage = '';
this.console.log(`.${name} ${usage}`);
}
},
plugins: () => {
const list = this.game.plugins.list(); // TODO: listAll? show disabled in red
this.console.log(`Enabled plugins (${list.length}): ` + list.join(' '));
},
enable: (name) => {
if (this.game.plugins.enable(name)) {
this.console.log(`Enabled plugin: ${name}`);
} else {
this.console.log(`Failed to enable plugin: ${name}`);
}
},
disable: (name) => {
if (this.game.plugins.disable(name)) {
this.console.log(`Disabled plugin: ${name}`);
} else {
this.console.log(`Failed to disable plugin: ${name}`);
}
},
pos: (x, y, z) => {
const player = this.game.plugins.get('voxel-player');
if (player) {
player.moveTo(x, y, z);
this.console.log([player.position.x, player.position.y, player.position.z]);
}
},
home: () => {
if (this.game.plugins.get('voxel-player')) {
this.game.plugins.get('voxel-player').home();
}
},
item: (name, count, tagsStr) => {
const props = this.registry.getItemProps(name);
if (!props) {
this.console.log(`No such item: ${name}`);
return;
}
let tags;
if (tagsStr !== undefined) {
try {
tags = JSON.parse(tagsStr);
} catch(e) {
this.console.log(`Invalid JSON ${tagsStr}: ${e}`);
return;
}
} else {
tags = undefined;
}
if (count !== undefined) count = 1;
count = parseInt(count, 10);
if (isNaN(count)) count = 1;
const pile = new ItemPile(name, count, tags);
const carry = this.game.plugins.get('voxel-carry');
if (carry) {
carry.inventory.give(pile);
this.console.log(`Gave ${name} x ${count} ${ (tags !== undefined) ? JSON.stringify(tags) : ''}`);
}
// TODO: integrate with voxel-inventory-hotbar, move to current slot?
},
clear: () => { // TODO: optionally list item types to clear only those
const carry = this.game.plugins.get('voxel-carry');
if (carry) {
carry.inventory.clear();
this.console.log("Cleared inventory");
}
},
block: (name, data) => {
if (name !== undefined) {
const index = this.registry.getBlockIndex(name);
if (index !== undefined) {
this.console.log(`No such block: ${name}`);
return;
}
}
const reachDistance = 8;
const hit = this.game.raycastVoxels(this.game.cameraPosition(), this.game.cameraVector(), reachDistance); // TODO: refactor w/ voxel-highlight, voxel-reach?
if (!hit) {
this.console.log("No block targetted");
return;
}
const x = hit.voxel[0];
const y = hit.voxel[1];
const z = hit.voxel[2];
const oldIndex = hit.value;
const oldName = this.registry.getBlockName(oldIndex);
if (name !== undefined) {
this.game.setBlock(hit.voxel, index);
}
const blockdata = this.game.plugins.get('voxel-blockdata');
let oldData;
if (blockdata !== undefined) {
oldData = blockdata.get(x, y, z);
if (data !== undefined) {
blockdata.set(x, y, z, data);
}
}
let dataInfo = "";
if (oldData !== undefined) {
dataInfo = `${JSON.stringify(oldData)} -> `;
}
if (data === undefined) data = oldData;
if (oldData !== undefined) {
dataInfo += JSON.stringify(data);
}
if (name === undefined) name = oldName;
if (index === undefined) index = oldIndex;
this.console.log(`Set (${x}, ${y}, ${z}) ${oldName}/${oldIndex} -> ${name}/${index} ${dataInfo}`);
}
};
// aliases
this.handlers.p = this.handlers.position = this.handlers.tp = this.handlers.pos;
this.handlers.i = this.handlers.give = this.handlers.item;
this.handlers.b = this.handlers.setblock = this.handlers.set = this.handlers.block;
this.enable();
}
process(input) {
if (input.indexOf('.') !== 0) { // regular text
if (!this.isConnectedToServer) { // (or send to server)
this.console.log(input);
let connection;
if (this.game.plugins.get('voxel-client')) {
connection = this.game.plugins.get('voxel-client').connection;
}
if (connection !== undefined) {
connection.emit('chat', {message:input});
} else {
this.console.log('Not connected to server. Type .help for commands');
}
}
return; // no local echo
}
input = input.substring(1);
// split into tokens using shell-based rules (allows quoting)
// TODO: switch to https://github.com/substack/node-shell-quote?
const words = shellwords.split(input);
const command = words[0];
const args = words.slice(1);
let handler = this.handlers[command];
if (handler === undefined) {
handler = this.handlers.undefined;
args.unshift(command);
}
handler.apply(this, args);
}
enable() {
if (this.console.widget) {
this.console.widget.on('input', this.onInput = (input) => {
this.process(input);
});
}
if (this.game.plugins.get('voxel-client')) {
this.game.plugins.get('voxel-client').connection.on('chat', this.onChat = (input) => { // TODO: refresh if connection changes?
this.console.log(input.message !== undefined ? input.message : input);
});
}
}
disable() {
this.console.widget.removeListener('input', this.onInput);
if (this.game.plugins.get('voxel-client')) {
this.game.plugins.get('voxel-client').connection.removeListener('chat', this.onChat);
}
}
registerCommand(name, handler, usage, help) {
if (name in this.handlers) {
throw new Error(`voxel-commands duplicate command registration: ${name} for ${handler}`);
}
this.handlers[name] = handler;
this.usages[name] = `${usage} -- ${help}`;
}
unregisterCommand(name, handler) {
if (this.handlers[name] !== handler) {
throw new Error(`voxel-commands attempted to unregister mismatched command: ${name} was ${this.handlers[name]} not ${handler}`); // TODO: is this a good idea? like removeListener..
}
delete this.handlers[name];
delete this.usages[name];
}
}