Alumopper 发表于 2025-2-16 18:44:49

MCFPP开发日志——dict和map

本帖最后由 Alumopper 于 2025-2-16 18:43 编辑

芝士MCFPPMCFPP是一个能被编译为Minecraft数据包的全新的面向对象的语言。它旨在以类似C系语言的语法,进行数据包的编写,并引入编程中常用的概念,从而使数据包的编写更加的便利。仓库地址:https://github.com/MinecraftFunctionPlusPlus/MCFPP项目官网:https://www.mcfpp.top目前最新版本:SNAPSHOT 25m01a
要做什么java.util.HashMap(划去) mcfpp.lang.dict和mcfpp.lang.map,基于NBT复合标签的字典实现,以及可遍历的简单包装版本
怎么做的神圣NBT让命令天然就有了字典这种数据类型。但是可惜的是,原版的复合标签只提供了基本的读写能力,甚至完全不具备遍历能力。这也是为什么MCFPP提供了两个类型,dict和map,分别对应的是最基本的最原版的复合标签,以及维护了一个键列表从而能提供简单遍历功能的包装版本。
dict<E>dict类型是最基本的复合标签,实现于top.mcfpp.core.lang.nbt.NBTDictionary。它有一个泛型参数E,决定了这个字典里面能存什么类型的数据。使用索引访问字典中的内容。

函数名参数返回值作用
containsKeystring keybool判断字典中是否包含指定键
mergedict dictvoid合并两个字典
removestring keyvoid移除指定键的键值对
clear-void清空字典

以下是一个使用例
namespace test;

func main(){
    dynamic dict<any> qwq = {
      a:1,
      b:"test",
      c:
    };
    print(qwq);
    print((nbt)qwq["a"]);
    qwq["d"] = {a:"b"};
    print(qwq);
    print(qwq.containsKey("a"));
    print(qwq.containsKey("x"));
    dict<any> pwp = {
      e: 1,
      f: 2,
      g: 3
    };
    qwq.merge(pwp);
    print(qwq);
    qwq.remove("a");
    print(qwq);
}
map<E>map类型维护了一个键的列表。在map类型的数据中,有两个子成员,一个keys,即键列表,一个keyValueSet,也就是一个复合标签。换句话说,map类型是一个列表加一个字典。map同样拥有一个泛型参数E,决定了map中能存的内容。可以直接访问keys和keyValueSet两个成员。

函数名参数返回值作用
clearvoidvoid清空map
containsKeystring keybool判断map中是否包含指定键
isEmptyvoidbool判断map是否为空
removestring keyvoid移除指定键的键值对
mergemap<T> mapvoid合并两个map
sizevoidint获取map的大小

使用例:
namespace test;

func main(){
    dynamic map<any> qwq = {
      a:1,
      b:"test",
      c:
    };
    print(qwq);
    print((nbt)qwq["a"]);
    qwq["d"] = {a:"b"};
    print(qwq);
    print(qwq.containsKey("a"));
    print(qwq.containsKey("x"));
    map<any> pwp = {
      e: 1,
      f: 2,
      g: 3
    };
    qwq.merge(pwp);
    print(qwq);
    qwq.remove("a");
    print(qwq);
}
编译结果:
test:maindata modify storage mcfpp:system stack_frame prepend value {}
#field: map<any> qwq = {a:1,b:"test",c:}
data modify storage mcfpp:system stack_frame.qwq.keys set value
data modify storage mcfpp:system stack_frame.qwq.keyValueSet set value {a:1,b:test,c:}
#expression: print(qwq)
#print(qwq)
tellraw @a {"type":"nbt","nbt":"stack_frame.qwq", "storage":"mcfpp:system","interpret":false}
#expression end: print(qwq)
#expression: print((nbt)qwq["a"])
#print((nbt)qwq["a"])
tellraw @a {"type":"nbt","nbt":"stack_frame.qwq.keyValueSet.a", "storage":"mcfpp:system","interpret":false}
#expression end: print((nbt)qwq["a"])
#expression: qwq["d"]={a:"b"}
data modify storage mcfpp:system stack_frame.qwq.keyValueSet.d set value {a:b}
data modify storage mcfpp:system stack_frame.qwq.keys append value d
#expression end: qwq["d"]={a:"b"}
#expression: print(qwq)
#print(qwq)
tellraw @a {"type":"nbt","nbt":"stack_frame.qwq", "storage":"mcfpp:system","interpret":false}
#expression end: print(qwq)
#expression: print(qwq.containsKey("a"))
#print(qwq.containsKey("a"))
#containsKey("a")
execute store result score test_func_containsKey_return mcfpp_boolean if data storage mcfpp:system stack_frame.qwq.keyValueSet.a
tellraw @a {"type":"score","score":{"name":"test_func_containsKey_return","objective":"mcfpp_boolean"}}
#expression end: print(qwq.containsKey("a"))
#expression: print(qwq.containsKey("x"))
#print(qwq.containsKey("x"))
#containsKey("x")
execute store result score test_func_containsKey_return mcfpp_boolean if data storage mcfpp:system stack_frame.qwq.keyValueSet.x
tellraw @a {"type":"score","score":{"name":"test_func_containsKey_return","objective":"mcfpp_boolean"}}
#expression end: print(qwq.containsKey("x"))
#field: map<any> pwp = {e:1,f:2,g:3}
#expression: qwq.merge(pwp)
#merge(pwp)
data modify storage mcfpp:system stack_frame.pwp.keys set value
data modify storage mcfpp:system stack_frame.qwq.keys append from storage mcfpp:system stack_frame.pwp.keys[]
data modify storage mcfpp:system stack_frame.qwq.keyValueSet merge value {e:1,f:2,g:3}
#expression end: qwq.merge(pwp)
#expression: print(qwq)
#print(qwq)
tellraw @a {"type":"nbt","nbt":"stack_frame.qwq", "storage":"mcfpp:system","interpret":false}
#expression end: print(qwq)
#expression: qwq.remove("a")
#remove("a")
data remove storage mcfpp:system stack_frame.qwq.keyValueSet.a
data modify storage mcfpp:system list.element set value a
scoreboard players set list.index mcfpp_temp 0
execute store result score list.size mcfpp_temp run data get storage mcfpp:system list.list
function mcfpp.lang:list/index_of
execute unless score list_index mcfpp_temp matches -1 run function test:remove_temp_33
#expression end: qwq.remove("a")
#expression: print(qwq)
#print(qwq)
tellraw @a {"type":"nbt","nbt":"stack_frame.qwq", "storage":"mcfpp:system","interpret":false}
#expression end: print(qwq)
data remove storage mcfpp:system stack_frame

test:remove_temp_33execute store result storage mcfpp:system stack_frame.list_index int 1 run scoreboard players get list_index mcfpp_temp
function mcfpp:dynamic/macro_34 with storage mcfpp:system stack_frame
mcfpp:dynamic/macro_34$data remove storage mcfpp:system stack_frame.qwq.keys[$(list_index)]
结果:

然后然后
在写map.remove(E e)的时候,我们顺便给list加上了remove函数~

函数名参数返回值作用
removeE evoid删除列表中对应的第一个元素,若没有这个元素则不改变列表

同时对any进行了大幅度重构,现在可以用any类型实现类似弱类型语言的功能~
页: [1]
查看完整版本: MCFPP开发日志——dict和map