Scarpet additions

模组

· 作者 replaceitem

为 Scarpet 语言添加了一些杂项新功能,例如 HTTP 请求。

0 本站下载 0 关注 更新于 7 个月前

需要前置:Carpet

最新 1.1.4 · 26.3 · Fabric

一键开服(送7天)

scarpet-additions

scarpet-additions

Carpet mod 的扩展,用于提供额外的 scarpet 函数

需要 Carpet mod

函数

convert_color(color,model,output)

将一种 model 格式的颜色转换为另一种。

color -> List: 根据指定的 model 而定

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

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

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

  • RGB: 红色、绿色和蓝色值的列表
  • RGBA: 红色、绿色、蓝色和透明度值的列表
  • 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 (String): 要请求的 URI
  • method (String, optional): HTTP 请求方法。例如 GET、POST、DELETE... 默认为 GET
  • headers (Map, optional): 每个映射项都是指向字符串或字符串列表的字符串键
  • body (String): POST 或其他请求的请求体

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

  • status_code (number): 请求的状态码
  • body (String): 从请求中返回的主体
  • headers (Map: string -> [strings]): 接收到的响应头
  • uri (String): 最初请求的 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?)

设置所有玩家的标签菜单中的页眉和页脚,或者如果提供了 player?,则更改单个玩家的页眉和页脚。

set_motd(motd)

设置服务器的每日消息(Message of the Day)。

你是这个资源的作者?登录后可认领此资源。

评论 0

0.0
暂无评分
5 星
0
4 星
0
3 星
0
2 星
0
1 星
0
我的评分

登录后参与评分

还没有评论,来抢沙发吧

评论配图

相似资源