-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdecoratedpotsherdswapper.sc
66 lines (61 loc) · 2.75 KB
/
decoratedpotsherdswapper.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
//////
// decoratedpotsherdswapper by BisUmTo
// Allows players to swap decorated potsherds while placed, by shift right clicking with a sherd or brick
//
// Known issues:
// - If sherd is present in both hands and mainhand has only one item, offhand will be used overwriting the mainhand sherd
//////
__config() -> {'stay_loaded' -> true};
global_sherds = item_list('decorated_pot_sherds');
global_sherd_facing = {
'south' -> ['south', 'east', 'west', 'north'],
'north' -> ['north', 'west', 'east', 'south'],
'east' -> ['east', 'north', 'south', 'west'],
'west' -> ['west', 'south', 'north', 'east'],
};
__on_player_right_clicks_block(player, item_tuple, hand, block, face, hitvec) -> (
if(!item_tuple || !player ~ 'sneaking' || player ~ 'gamemode' == 'spectator' || block != 'decorated_pot' || face == 'up' || face == 'down', return());
[item, count, nbt] = item_tuple;
rotation = block_state(block):'facing';
index_face = (global_sherd_facing:rotation)~face;
if(hand == 'offhand' && _main_hand_would_fail(player, block, index_face), return());
if(item == 'brick'|| global_sherds ~ item != null,
if((old_item = _swap(player, block, index_face, item)) && player ~ 'gamemode' != 'creative',
spawn('item', pos(block)+[0.5,1,0.5], str('{Item:{id:"%s",Count:1b},PickupDelay:10,Motion:[%f,.2,%f]}', old_item, rand(0.2) - 0.1, rand(0.2) - 0.1));
inventory_set(player, if(hand == 'mainhand', player~'selected_slot', -1), count - 1, item, nbt);
);
modify(player, 'swing', hand);
);
);
_swap(player, block, index_face, item) -> (
nbt = block_data(block);
if(!nbt:'sherds',
nbt:'sherds' = ['brick', 'brick', 'brick', 'brick'];
);
old_item = nbt:('sherds['+index_face+']');
if (old_item ~ item, return(null));
nbt:('sherds['+index_face+']') = item;
force_update = all(parse_nbt(nbt:'sherds'), _~'brick') && !inventory_has_items(block);
without_updates(set(block, 'air'));
if(force_update,
schedule(0, _(outer(block)) -> set(block, 'decorated_pot', block_state(block))),
// else
set(block, 'decorated_pot', block_state(block), nbt);
);
sound('block.decorated_pot.hit', pos(block), 1.0, 1.5, 'block');
old_item
);
// Workaround for offhand usage when main hand succeded
_main_hand_would_fail(player, block, index_face) -> (
item_tuple = player ~ 'holds';
if(!item_tuple, return(false));
[item, count, nbt] = item_tuple;
if(item != 'brick' && global_sherds ~ item == null, return(false));
block_nbt = block_data(block);
if(!block_nbt:'sherds',
block_nbt:'sherds' = ['brick', 'brick', 'brick', 'brick'];
);
old_item = block_nbt:('sherds['+index_face+']');
if(old_item ~ item, return(true));
false
);