[Scarpet] set() function fails to set properties and NBT from variables
chililisoup opened this issue ยท 1 comments
This happens in 1.4.20, did not happen in 1.4.18
This issue popped up for me when working on a command to lock containers. When used, it now wipes the inventory of the block you're trying to lock.
__config() -> {
'commands' -> {
'<key>' -> 'lockBlock',
},
'arguments' -> {
'key' -> {
'type' -> 'term',
'suggest' -> ['password']
}
},
['stay_loaded','true']
};
lockBlock(key) -> (
p = player();
current_gamemode = p~'gamemode';
blok = query(p, 'trace', 4.5, 'blocks'); //Gets blocked currently looked at
if (blok,
if (isInArray([ //Makes sure it's a supported block
'chest',
'trapped_chest',
'barrel',
'hopper',
'furnace',
'blast_furnace',
'smoker'
], blok),
if (name != replace(name, '[^A-Za-z0-9-_+.]'), //Ensures that the lock password will work
print(format('w [','d Lock','w ] ', 'y Invalid characters in key!'));
exit();
);
blok_pos = pos(blok);
blok_props = [];
for (keys(block_state(blok_pos)), //Makes an array of the block properties, ex. [open, true, facing, east]
put(blok_props, _i*2, _);
put(blok_props, _i*2 + 1, block_state(blok_pos, _));
);
blok_data = block_data(blok_pos);
if (blok_data:'Lock', //Removes the lock if the key provided is the same as the key the block already has
if (blok_data:'Lock' == key,
delete(blok_data, 'Lock');
set(blok_pos, blok, blok_props, blok_data); //Broken function!
print(format('w [','d Lock','w ] ', 'y Successfully unlocked ', 'wb ' + blok, 'y .')),
print(format('w [','d Lock','w ] ', 'y This ', 'wb ' + blok, 'y is locked! Input correct password to unlock.'));
), //Sets a new lock if the block is unlocked
put(blok_data, 'Lock', key);
set(blok_pos, blok, blok_props, blok_data); //Broken function!
print(format('w [','d Lock','w ] ', 'y Successfully locked ', 'wb ' + blok, 'y with key ', 'wb ' + key, 'y .'));
),
print(format('w [','d Lock','w ] ', 'y This block can\'t be locked!'));
),
print(format('w [','d Lock','w ] ', 'y You\'re not looking at anything in range.'));
);
exit();
);
isInArray(arr, val) -> (
for (arr,
if (val == _,
r = true;
break();
);
);
r
)
Thanks for catching that regression. Should be fixed next update.
I also noticed how you rewrite state map into a list. set
has been added at the very beginning, way before scarpet had support for map types. Since block_state
now returns a map of block properties, it just makes sense to allow set
to accept a map as well. This should be in 1.4.21 as well.
Also - noticed you are checking in a weird way an item membership in the list. why not define a list of acceptable blocks in a set and use has
// outside
global_blocks = {'chest', 'barrel', ...};
// inside function
if (has(global_blocks:str(blok)), ...)