红石中继站
    • 资源
    • 新帖
    • 版块
    • 热门
    • 登录

    MCFPP开发日志——列表的API

    矿工茶馆
    2
    2
    59
    正在加载更多帖子
    • 从旧到新
    • 从新到旧
    • 最多赞同
    回复
    登录后回复
    此主题已被删除。只有拥有主题管理权限的用户可以查看。
    • AlumopperA
      Alumopper
      最后由 编辑 · 陕西省

      芝士MCFPP

      MCFPP是一个能被编译为Minecraft数据包的全新的面向对象的语言。它旨在以类似C系语言的语法,进行数据包的编写,并引入编程中常用的概念,从而使数据包的编写更加的便利。

      仓库地址:https://github.com/MinecraftFunctionPlusPlus/MCFPP

      项目官网:https://www.mcfpp.top

      目前最新版本:SNAPSHOT 25m01a

      要做什么

      java.util.ArrayList mcfpp.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]
      

      运行结果:
      04c056a3-dc22-4b05-bf42-ccd4b75f2053-cc8b76fcdba865a332e2bd8ebdf468a5.png

      业余代码雪狐一只~
      支持Project MCFPP项目谢谢喵

      1 条回复 最后回复 回复 引用
      • BaimoQilinB
        BaimoQilin
        最后由 编辑 · 江苏省

        顶,非常好的项目 b2604dc7-9240-488a-9ed6-8e857d2e8c5a-image.png

        初二在读 | LLM/AI 爱好者
        GitHub CyniaAI 系列开源 LLM Agents 项目维护者
        Hypiworld 机械筑梦腐竹 现已停运,另见 #492

        个人主页 | GitHub主页 | 微信公众号 | 日志随笔 | 知乎专栏

        ✨ 用冰冷的理性温暖世界

        我的最新文章: 评“煤炭压力版”事件:观研报告网们敲响的 LLM 信息污染警钟 | 评“姚北实验学校食堂惊现活蛆”事件 | BukkitGPT 开发日志: 添加 AI 编辑现有 Bukkit 插件的功能 | ...

        1 条回复 最后回复 回复 引用
        • 第一个帖子
          最后一个帖子
          "Minecraft" 以及 "我的世界" 为 Mojang Synergies AB 的商标,本站与 Mojang 以及 Microsoft 没有从属关系
          © 2024-2025 红石中继站 版权所有 本站原创图文内容版权属于原创作者,未经许可不得转载
          侵权投诉邮箱:[email protected]
          由 长亭雷池WAF 提供安全检测与防护 由 WAFPRO 提供 SCDN 安全加速
          苏公网安备32050902102328号 苏ICP备2023043601号-8