Skip to content

Instantly share code, notes, and snippets.

@Flemmli97
Last active January 26, 2022 01:47
Show Gist options
  • Save Flemmli97/dfb5d50115a083a50049617797d4a536 to your computer and use it in GitHub Desktop.
Save Flemmli97/dfb5d50115a083a50049617797d4a536 to your computer and use it in GitHub Desktop.
Taterzen Type Changer
__give_tool() -> (
run('/give @p paper{NPCChanger:1,Unbreakable:1,Enchantments:[{}],display:{Name:\'[{"text":"NPC type changer", "italic":false,"color":"gold"}]\',Lore:[\'[{"text":"Rename in anvil to following format: <entity>-<name>","italic":false}]\', \'[{"text":"For example (without |): |iron_golem-fancyName|","italic":false}]\', \'[{"text":"After that left click a trader npc","italic":false}]\']},HideFlags:4} 1');
return(null);
);
__config() -> {
'stay_loaded' -> true,
'allow_command_conflicts' -> true,
'command_permission' -> 2,
};
//Gives the spawning item
__command() -> __give_tool();
__on_player_attacks_entity(player, entity) ->
(
item_tuple = query(player, 'holds', 'mainhand');
if(!__is_item(item_tuple) || entity~'type'!='taterzens:npc', return());
tag = query(entity, 'nbt', 'TaterzenNPCTag');
prof = get(tag, 'Professions');
if(for(prof:[], get(_, 'ProfessionType') == 'tradernpcs:survival_trader' && __uuid_to_string(get(get(_, 'ProfessionData'), 'OwnerUUID')) == query(player, 'uuid')) == 0, return());
name = __item_name(item_tuple);
if(name == 'NPC type changer', return());
ename = replace(name, '^([a-z]*-)');
entity = replace(name, '-' + ename);
if(entity == 'player', (print('Players are buggy');return()));
run('/npc select');
run(str('/npc edit type %s', entity));
//run(str('npc edit skin %s', playername));
run(str('npc edit name %s', ename));
run('/npc deselect');
slot = player ~ 'selected_slot';
l(item, count, nbt) = inventory_get(player, slot);
inventory_set(player, slot, count-1);
);
__is_item(item_tuple) -> item_tuple && item_tuple:0 == 'paper' && get(item_tuple:2,'NPCChanger') == 1;
__item_name(item_tuple) -> (
get(parse_nbt(replace(get(get(item_tuple:2, 'display'), 'Name'), '[\\[\\]]')), 'text')
);
// function from UUIDS.scl by BisUmTo
// Converts UUID int array to UUID string
__uuid_to_string(uuid_nbt) -> (
uuid = uuid_nbt:[];
if( type(uuid) != 'list' ||
length(uuid) != 4 ||
reduce(uuid, _a || type(_) != 'number', false),
// then
exit('Invalid UUID: ' + uuid)
);
// hex formatting is silly rn so quick workaround
str1 = slice(str('%x',uuid:0),-8,-1);
str2 = slice(str('%x',floor(uuid:1 / 2^16)),-4,-1);
str3 = slice(str('%x',uuid:1 % 2^16),-4,-1);
str4 = slice(str('%x',floor(uuid:2 / 2^16)),-4,-1);
str5 = slice(str('%x',uuid:2 % 2^16),-4,-1);
str6 = slice(str('%x',uuid:3),-8,-1);
str('%s-%s-%s-%s-%s%s',str1,str2,str3,str4,str5,str6);
// str('%08x-%04x-%04x-%04x-%04x%08x', uuid:0, floor(uuid:1 / 2^16), uuid:1 % 2^16, floor(uuid:2 / 2^16), uuid:2 % 2^16, uuid:3)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment