MCFPP开发日志——列表的API
-
芝士MCFPP
MCFPP是一个能被编译为Minecraft数据包的全新的面向对象的语言。它旨在以类似C系语言的语法,进行数据包的编写,并引入编程中常用的概念,从而使数据包的编写更加的便利。
仓库地址:https://github.com/MinecraftFunctionPlusPlus/MCFPP
目前最新版本:SNAPSHOT 25m01a
要做什么
java.util.ArrayListmcfpp.lang.list 基于NBT列表的列表实现怎么做的
列表类型在源码中对应了
top.mcfpp.core.lang.nbt.NBTList
类。本来想把代码直接贴过来发现太长了还是贴github吧23333
https://github.com/MinecraftFunctionPlusPlus/MCFPP/blob/kotlin-latest/src/main/kotlin/top/mcfpp/core/lang/nbt/NBTList.kt参照Java中的
ArrayList
实现的API:函数名 参数 返回值 作用 add
E e
void
向列表中添加一个元素 addAll
list<E> l
void
向列表中添加一组元素 insert
int index, E e
void
在指定位置插入一个元素 removeAt
int index
void
移除指定位置的元素 indexOf
E e
int
返回指定元素的索引 lastIndexOf
E e
int
返回指定元素的最后一个索引 contains
E e
bool
判断列表中是否包含指定元素 clear
void
void
清空列表 add(E e)
直接用
data append
就好啦addAll(list<E> l)
用
data modify ... append from xxx[]
就能追加列表insert(int index, E e)
直接用
data insert
removeAt(int index)
直接用
data remove
indexOf(E e) -> int
调用库函数:
mcfpp.lang:list/index_of
# @param list 要检查的列表 # @param element 要检查的元素 # list.index mcfpp_temp当前的索引 # list.size mcfpp_temp列表的长度 # 检查索引是否越界 execute if score list.index mcfpp_temp >= list.size mcfpp_temp run return -1 # 检查第一个元素 execute store success score #if_success mcfpp_temp run data modify storage mcfpp:system list.list[0] set from storage mcfpp:system list.element # 如果元素符合 execute unless score #if_success mcfpp_temp matches 1 run return run scoreboard players get list.index mcfpp_temp # 否则,检查第二个元素 data remove storage mcfpp:system list.list[0] scoreboard players add list.index mcfpp_temp 1 function mcfpp.lang:list/index_of return -1
lastIndexOf(E e) -> int
调用库函数:
mcfpp.lang:list/last_index_of
#@param list 要检查的列表 #@param element 要检查的元素 #list.last_index_of.index mcfpp_temp当前的索引 #list.last_index_of.size mcfpp_temp列表的长度 #检查索引是否越界 execute if score list.index mcfpp_temp >= list.size mcfpp_temp run return -1 #检查第一个元素 execute store success score #if_success mcfpp_temp run data modify storage mcfpp:system list.list[-1] set from storage mcfpp:system list.element #如果元素符合 execute unless score #if_success mcfpp_temp matches 1 run return run scoreboard players get list.index mcfpp_temp #否则,检查第二个元素 data remove storage mcfpp:system list.list[-1] scoreboard players add list.index mcfpp_temp 1 function mcfpp.lang:list/last_index_of return -1
contains(E e) -> bool
调用库函数:
mcfpp.lang:list/contains
#@param list 要检查的列表 #@param element 要检查的元素 #list.index mcfpp_temp当前的索引 #list.size mcfpp_temp列表的长度 #检查索引是否越界 execute if score list.index mcfpp_temp >= list.size mcfpp_temp run return 0 #检查第一个元素 execute store success score #if_success mcfpp_temp run data modify storage mcfpp:system list.list[0] set from storage mcfpp:system list.element #如果元素符合 execute unless score #if_success mcfpp_temp matches 1 run return 1 #否则,检查第二个元素 data remove storage mcfpp:system list.list[0] scoreboard players add list.index mcfpp_temp 1 function mcfpp.lang:list/contains return 0
clear()
直接将当前变量的值设置为
[]
就好啦效果
源码:
namespace test; func main(){ dynamic list<int> l = [1,2,3,4]; dynamic list<int> l1 = [5,6,7]; print(l); l.addAll(l1); print(l); l.insert(0,114514); l.removeAt(1); print(l); print(l.indexOf(2)); print(l.contains(8)); print(l.contains(2)); l.clear(); print(l); }
dynamic
关键字表示变量不会被编译器追踪,不参与编译时优化编译结果:
(带注释)data modify storage mcfpp:system stack_frame prepend value {} #field: list<int> l = [1,2,3,4] data modify storage mcfpp:system stack_frame[0].l set value [1,2,3,4] #field: list<int> l1 = [5,6,7] data modify storage mcfpp:system stack_frame[0].l1 set value [5,6,7] #expression: print(l) #print(l) tellraw @a {"type":"nbt","nbt":"stack_frame[0].l", "storage":"mcfpp:system","interpret":false} #expression end: print(l) #expression: l.addAll(l1) #addAll(l1) data modify storage mcfpp:system stack_frame[0].l append from storage mcfpp:system stack_frame[0].l1[] #expression end: l.addAll(l1) #expression: print(l) #print(l) tellraw @a {"type":"nbt","nbt":"stack_frame[0].l", "storage":"mcfpp:system","interpret":false} #expression end: print(l) #expression: l.insert(0,114514) #insert(0,114514) data modify storage mcfpp:system stack_frame[0].l insert 0 value 114514 #expression end: l.insert(0,114514) #expression: l.removeAt(1) #removeAt(1) data remove storage mcfpp:system stack_frame[0].l[1] #expression end: l.removeAt(1) #expression: print(l) #print(l) tellraw @a {"type":"nbt","nbt":"stack_frame[0].l", "storage":"mcfpp:system","interpret":false} #expression end: print(l) #expression: print(l.indexOf(2)) #print(l.indexOf(2)) #indexOf(2) data modify storage mcfpp:system list.element set value 2 data modify storage mcfpp:system list.list set from storage mcfpp:system stack_frame[0].l 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 tellraw @a {"type":"score","score":{"name":"list.index","objective":"mcfpp_temp"}} #expression end: print(l.indexOf(2)) #expression: print(l.contains(8)) #print(l.contains(8)) #contains(8) data modify storage mcfpp:system list.element set value 8 data modify storage mcfpp:system list.list set from storage mcfpp:system stack_frame[0].l 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/contains execute store result score list.contains mcfpp_boolean run function mcfpp.lang:list/contains tellraw @a {"type":"score","score":{"name":"list.contains","objective":"mcfpp_boolean"}} #expression end: print(l.contains(8)) #expression: print(l.contains(2)) #print(l.contains(2)) #contains(2) data modify storage mcfpp:system list.element set value 2 data modify storage mcfpp:system list.list set from storage mcfpp:system stack_frame[0].l 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/contains execute store result score list.contains mcfpp_boolean run function mcfpp.lang:list/contains tellraw @a {"type":"score","score":{"name":"list.contains","objective":"mcfpp_boolean"}} #expression end: print(l.contains(2)) #expression: l.clear() #clear() #expression end: l.clear() #expression: print(l) #print(l) tellraw @a "[]" #expression end: print(l) data remove storage mcfpp:system stack_frame[0]
(不带注释)
data modify storage mcfpp:system stack_frame prepend value {} data modify storage mcfpp:system stack_frame[0].l set value [1,2,3,4] data modify storage mcfpp:system stack_frame[0].l1 set value [5,6,7] tellraw @a {"type":"nbt","nbt":"stack_frame[0].l", "storage":"mcfpp:system","interpret":false} data modify storage mcfpp:system stack_frame[0].l append from storage mcfpp:system stack_frame[0].l1[] tellraw @a {"type":"nbt","nbt":"stack_frame[0].l", "storage":"mcfpp:system","interpret":false} data modify storage mcfpp:system stack_frame[0].l insert 0 value 114514 data remove storage mcfpp:system stack_frame[0].l[1] tellraw @a {"type":"nbt","nbt":"stack_frame[0].l", "storage":"mcfpp:system","interpret":false} data modify storage mcfpp:system list.element set value 2 data modify storage mcfpp:system list.list set from storage mcfpp:system stack_frame[0].l 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 tellraw @a {"type":"score","score":{"name":"list.index","objective":"mcfpp_temp"}} data modify storage mcfpp:system list.element set value 8 data modify storage mcfpp:system list.list set from storage mcfpp:system stack_frame[0].l 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/contains execute store result score list.contains mcfpp_boolean run function mcfpp.lang:list/contains tellraw @a {"type":"score","score":{"name":"list.contains","objective":"mcfpp_boolean"}} data modify storage mcfpp:system list.element set value 2 data modify storage mcfpp:system list.list set from storage mcfpp:system stack_frame[0].l 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/contains execute store result score list.contains mcfpp_boolean run function mcfpp.lang:list/contains tellraw @a {"type":"score","score":{"name":"list.contains","objective":"mcfpp_boolean"}} tellraw @a "[]" data remove storage mcfpp:system stack_frame[0]
运行结果:
-
顶,非常好的项目