forked from BisUmTo/scarpet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswapitem.sc
102 lines (95 loc) · 4.28 KB
/
swapitem.sc
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
global_slots = ['mainhand', 'feet', 'legs', 'chest', 'head', 'offhand'];
__config() -> {
'stay_loaded' -> true,
'commands' -> {
'<fromslot>' -> _(fromSlot) -> swap(player(), fromSlot, player(), 'mainhand'),
'<fromslot> <toslot>' -> _(fromSlot, toSlot) -> swap(player(), fromSlot, player(), toSlot),
'<fromslot> entity' -> _(fromSlot) -> swap(player(), fromSlot, player() ~ ['trace',5,'entities'], 'mainhand'),
'<fromslot> entity <toslot>' -> _(fromSlot, toSlot) -> swap(player(), fromSlot, player() ~ ['trace',5,'entities'], toSlot),
'entity' -> _() -> swap(player() ~ ['trace',5,'entities'], 'mainhand', player(), 'mainhand'),
'entity <fromslot>' -> _(fromslot) -> swap(player() ~ ['trace',5,'entities'], fromslot, player(), 'mainhand'),
'entity <fromslot> <toslot>' -> _(fromSlot, toSlot) -> swap(player() ~ ['trace',5,'entities'], fromSlot, player(), toSlot),
'entity <fromslot> entity' -> _(fromSlot) -> swap(player() ~ ['trace',5,'entities'], fromSlot, player() ~ ['trace',5,'entities'], 'mainhand'),
'entity <fromslot> entity <toslot>' -> _(fromSlot, toSlot) -> swap(player() ~ ['trace',5,'entities'], fromSlot, player() ~ ['trace',5,'entities'], toSlot),
'entity entity <toslot>' -> _(toSlot) -> swap(player() ~ ['trace',5,'entities'], 'mainhand', player() ~ ['trace',5,'entities'], toSlot),
},
'arguments' -> {
'fromslot' -> {
'type' -> 'term',
'options' -> global_slots
},
'toslot' -> {
'type' -> 'term',
'options' -> global_slots
}
}
};
swap(fromEntity, fromSlot, toEntity, toSlot) -> (
if(!fromEntity || !toEntity,
return(print(player(), format('n Look an entity to run this command.')))
);
if(!player() ~ 'permission_level' && (isOtherPlayer(player(), fromEntity) || isOtherPlayer(player(), toEntity)),
return(print(player(), format('n You can\'t swap items to another player.')))
);
if(!isValid(fromEntity) || !isValid(toEntity),
return(print(player(), format('n The entity you\'re looking at is not a valid entity.')))
);
if(trade(fromEntity) || trade(toEntity),
return(print(player(), format('n You can\'t swap items that are villager trades to villager')))
);
if(fromEntity == toEntity && fromSlot == toSlot,
return(print(player(), format('n You can\'t swap items on the same slot of the same entity.')))
);
toItem = toEntity ~ ['holds', toSlot] || [null, null, null];
fromItem = fromEntity ~ ['holds', fromSlot] || [null, null, null];
if(toItem == fromItem,
return(print(player(), format('n You can\'t swap two identical items.')))
);
success = (!fromItem:0 || fromItem:1 <= stack_limit(fromItem:0)) &&
(!toItem:0 || toItem:1 <= stack_limit(toItem:0)) &&
_inventory_set(toEntity, toSlot, fromItem:1, fromItem:0, fromItem:2) &&
_inventory_set(fromEntity, fromSlot, toItem:1, toItem:0, toItem:2);
if(!success,
_inventory_set(fromEntity, fromSlot, fromItem:1, fromItem:0, fromItem:2);
_inventory_set(toEntity, toSlot, toItem:1, toItem:0, toItem:2);
return(print(player(), format('n You can\'t swap this two items.')))
);
sound('minecraft:item.armor.equip_generic', pos(fromEntity), 1, 1, 'player');
);
isOtherPlayer(player1, player2) -> player2 ~ 'type' == 'player' && player1 != player2;
isValid(entity) ->
entity ~ 'type' == 'player' || (
entity ~ 'nbt' : 'ArmorItems' &&
entity ~ 'nbt' : 'HandItems'
);
getSlotNumber(entity, slot) ->
if(entity ~ 'type' == 'player',
if(
slot == 'mainhand', entity ~ 'selected_slot',
slot == 'offhand', -1,
35 + global_slots ~ slot
),
// else
global_slots ~ slot
);
_inventory_set(entity, slot, count, item, nbt) -> (
item = if(!count || !item, 'air', item);
count = count || 1;
nbt = nbt || '';
slot = if(slot ~ 'hand', 'weapon.' + slot, 'armor.' + slot);
run(str('item replace entity %s %s with %s%s %d',
entity ~ 'command_name',
slot,
item,
nbt,
count
)):0
);
trade(entity) -> (
if( entity ~ 'type' == 'villager', (
hold = entity ~ 'holds';
trades = entity ~ 'nbt':'Offers':'Recipes';
return(trades ~ hold);
)
)
);