-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathput.js
60 lines (46 loc) · 1.83 KB
/
put.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
'use strict';
const { Broadcast: B, ItemType } = require('ranvier');
const ArgParser = require('../../bundle-example-lib/lib/ArgParser');
const dot = ArgParser.parseDot;
const ItemUtil = require('../../bundle-example-lib/lib/ItemUtil');
module.exports = {
usage: 'put <item> <container>',
command : (state) => (args, player) => {
args = args.trim();
if (!args.length) {
return B.sayAt(player, 'Put what where?');
}
// put 3.foo in bar -> put 3.foo bar -> put 3.foo into bar
const parts = args.split(' ').filter(arg => !arg.match(/^in$/) && !arg.match(/^into$/));
if (parts.length === 1) {
return B.sayAt(player, "Where do you want to put it?");
}
const fromList = player.inventory;
const fromArg = parts[0];
const toArg = parts[1];
const item = dot(fromArg, fromList);
const toContainer = dot(toArg, player.room.items) ||
dot(toArg, player.inventory) ||
dot(toArg, player.equipment);
if (!item) {
return B.sayAt(player, "You don't have that item.");
}
if (!toContainer) {
return B.sayAt(player, "You don't see anything like that here.");
}
if (toContainer.type !== ItemType.CONTAINER) {
return B.sayAt(player, `${ItemUtil.display(toContainer)} isn't a container.`);
}
if (toContainer.isInventoryFull()) {
return B.sayAt(player, `${ItemUtil.display(toContainer)} can't hold any more.`);
}
if (toContainer.closed) {
return B.sayAt(player, `${ItemUtil.display(toContainer)} is closed.`);
}
player.removeItem(item);
toContainer.addItem(item);
B.sayAt(player, `<green>You put </green>${ItemUtil.display(item)}<green> into </green>${ItemUtil.display(toContainer)}<green>.</green>`);
item.emit('put', player, toContainer);
player.emit('put', item, toContainer);
}
};