Scarpet 新增内容

Scarpet 新增内容

向scarpet语言添加一些杂项新功能,如http请求

基础库

scarpet-additions

Available for fabric See me on GitHub Available on Modrinth Chat on Discord

scarpet-additions

一个 Carpet 模组 扩展,提供一些额外的 scarpet 函数

需要 Carpet 模组

函数

convert_color(color,model,output)

将一个颜色从一种 model 转换为另一种。

color -> 列表:取决于指定的 model

model -> 字符串:输入颜色模型,可以是 RGB、RGBA 或 HSB

  • RGB:红、绿、蓝值的列表
  • RGBA:红、绿、蓝、alpha 值的列表
  • HSB:色相、饱和度、亮度值的列表

output -> 字符串:输出颜色模型,可以是 RGB、RGBA、HEX 或 NUM

  • RGB:红、绿、蓝值的列表
  • RGBA:红、绿、蓝、alpha 值的列表
  • HEX:十六进制字符组成的字符串(不带前导 '#')可用于 format()
  • NUM:以 4 字节表示颜色的数字:0xRRGGBBAA。可用于 draw_shape() 中的 'color' 参数

示例:

convert_color([255,128,0],'rgb','hex'); -> 'FF8000'

convert_color([255,128,0],'rgb','num'); -> 0xff7f00ff

convert_color([0,255,255],'hsb','hex'); -> 'FF0000'

convert_color([120,255,255],'hsb','hex'); -> '00FF00'

示例:

__on_tick() -> (
    if((tick_time() % 2) == 0,
        headerHue = tick_time()%360;
        headerGlossIndex = (floor(tick_time()/3)%40)-10;
        header = [];
        title = 'MinecraftServer';
        for(range(length(title)),
            if(abs(_-headerGlossIndex) < 3,
                c = convert_color([headerHue,abs(_-headerGlossIndex)/3*255,255],'hsb','hex');
            ,
                if(_ < 7,
                    c = convert_color([headerHue,255,190],'hsb','hex');
                ,
                    c = convert_color([headerHue,255,255],'hsb','hex');
                );
            );
            put(header,null,str('b#%s %s',c,slice(title,_,_+1)));
        );
        header = format(header);
        footer = format('r to the server!');
        set_tab_text(header,footer);
  )
);

http_request(options)

执行由给定 options 指定的 http 请求。

此调用是阻塞的,因此你应该在 task() 中使用它!

options 参数是一个带有以下键的映射值:

  • uri(字符串):要请求的 URI
  • method(字符串,可选):http 请求方法。例如 GET、POST、DELETE……默认为 GET
  • headers(映射,可选):每个映射条目是一个字符串键,指向一个字符串或字符串列表
  • body(字符串):用于 POST 或其他请求的请求体

该函数返回一个包含以下条目的映射:

  • status_code(数字):请求的状态码
  • body(字符串):请求返回的响应体
  • headers(映射:字符串 -> [字符串]):接收到的响应头
  • uri(字符串):最初请求的 URI

注意响应体不会按 json 解析,也不会进行 html 转义。 请使用 escape_html 和 unescape_html 函数, 以及 scarpet 内置的 encode_json 和 decode_json。

使用示例:

// simple get request and parsing

response = http_request({
    'uri'->'https://opentdb.com/api.php?amount=1'
});

print('Response: ' + response);

if(response:'status_code' != 200,
    print('Request failed: ' + response:'status_code');
,
    body = decode_json(response:'body');
    print('\n\nBody: ' + body);

    question_data = body:'results':0;
    question = unescape_html(question_data:'question');
    answer = unescape_html(question_data:'correct_answer');

    print('\n\n\n' + question + '\n' + answer);
);

escape_html(html)

返回转义后的 html 字符串(例如 " -> &quot;)

unescape_html(html)

返回未转义的 html 字符串(例如 &quot; -> ")

list_text(header, footer, player?)

设置所有玩家的 tab 菜单中的页眉和页脚,如果给出了 player?,则更改某一个玩家的。

set_motd(motd)

设置服务器的每日消息(motd)。