Buildosour_积木小恐龙

搬运工
U3版主
U4开发者认证
正式会员
UID
803
2024/02/24
98
勋章
2
红宝石
601个红宝石
红宝石
601
  • #1
分析一下下面的保险箱addon代码(大家看个乐子就行,有能纠正的大佬希望给个指导纠正qwq)
JSON:
{
   "format_version":"1.21.0",//这是版本

   "minecraft:entity":{//告诉mc这是实体

      "description":{//描述模块

         "identifier":"true:safe_basic",//也就是id(唯一标识符、身份证),只能是英文
         "spawn_category":"creature",/生成类别(生物)
         "is_spawnable":true,//是否允许自然生成,只有t或f
         "is_summonable":true//是否允许命令或者蛋生成,只有t或f
      },


      "component_groups":{//组件模块,用来罗列各个功能

         "true:unowned_safe":{//组件名字(未被驯服的保险箱)
            "minecraft:tameable":{//这里是调用mc原版自带的驯服功能(没座这个保险箱是和dog一样被钥匙驯服的,甚至还会冒爱心qwq)
               "tame_items":"true:safe_key",//驯服用的物品:保险箱钥匙(emm......)
               "probability":1,//几率:1(也就是一次就驯服了......)
               "tame_event":{//驯服事件
                  "event":"minecraft:on_tame",//事件:被驯服
                  "target":"self"//目标:自身
               }
            },//这一段就是保险箱实现解锁的原理和步骤了
            "minecraft:interact":{//交互,mc自带
               "interactions":[//交互选项
                  {
                     "on_interact":{//进入交互状态
                        "filters":{//定义一个条件过滤器,满足下列条件才能和保险箱成功交互
                           "all_of":[//表示下面的条件都必须满足,即为逻辑语言中的“与”
                              {
                                 "test":"is_sneaking",//检测:玩家是否为潜行状态
                                 "subject":"other",//对象:玩家(交互发起者)
                                 "operator":"==",//条件(也就是执行的取值范围,这里是=)
                                 "value":true//要求值
                              }
                           ]
                        },
                        "event":"true:pick_up",//事件:拾取自身(会掉落物品)
                        "target":"self"//对象:自身
                     },
                     "add_items":{//添加物品
                        "table":"loot_tables/safes/safe_basic.json"//这个是调用,暂时不管qwq
                     },
                     "interact_text":"action.interact.pick_up"//交互的文本(也就是和“上车”一个玩意的按钮)
                  }
               ]
            }//到这里写完了交互的内容(潜行时候右键收回保险箱等等)
         },


         "true:safe_inventory":{//组件名字(箱子功能)
            "minecraft:is_tamed":{//状态标记:被驯服
            },
            "minecraft:inventory":{ //mc自带的箱子功能
               "container_type":"minecart_chest",//界面类型:小箱子
               "inventory_size":28,//格子数
               "restrict_to_owner":true//是否限制访问权限
            }
         },//这里就是给保险箱箱子的功能


         "true:safe_closed":{//组件名字(保险箱关闭状态)
            "minecraft:interact":{//一样的内容,不过多赘述
               "interactions":[
                  {
                     "on_interact":{
                        "filters":{
                           "all_of":[
                              {
                                 "test":"is_sneaking",
                                 "subject":"other",
                                 "operator":"==",
                                 "value":true
                              },
                              {
                                 "test":"is_owner",
                                 "subject":"other",
                                 "value":true
                              }
                           ]
                        },
                        "event":"true:pick_up",
                        "target":"self"
                     },
                     "add_items":{
                        "table":"loot_tables/safes/safe_basic.json"
                     },
                     "interact_text":"action.interact.pick_up"
                  }
               ]
            },//这里貌似和上面代码一样?不知道为什么,猜测是具有相同的回收箱子功能

            "minecraft:entity_sensor":{//mc自带的实体传感器,用于检测实体是否满足特定条件
               "subsensors":[//子传感器列表,定义n组检测规则(这里为1)
                  {
                     "minimum_count":1,//满足条件的实体最低数量
                     "event_filters":{//条件过滤器(事件版)
                        "all_of":[//满足以下所有条件
                           {
                              "test":"is_owner",// 检查实体是否有所有者
                              "subject":"other",// 检测对象是除自身外的其他实体
                              "value":true// 要求检测结果必须为 true(即实体有所有者)
                           },//条件 1:检测是否为所有者
                           {
                              "subject":"other", // 检测其他实体
                              "test":"has_container_open",// 检查实体是否正在打开容器
                              "value":true// 要求检测结果为 true(即实体正在打开容器)
                           }//条件 2:检测是否打开容器
                        ]
                     },
                     "event":"true:open"//当满足上述所有条件时,触发名为 true:open 的自定义事件。
                  }
               ]
            }
         },//这里为:当附近存在至少 1 个同时满足以上条件的实体时,触发 true:open 事件(开箱事件)


         "true:safe_open":{//组件名字(保险箱开启状态)
            "minecraft:is_sheared":{//这里被复用为标记保险箱处于打开状态(无参数,不明白具体意图)
            },
            "minecraft:entity_sensor":{//实体传感器,不赘述
               "subsensors":[
                  {
                     "minimum_count":1,
                     "event_filters":{
                        "all_of":[
                           {
                              "test":"is_owner",
                              "subject":"other",
                              "value":true
                           },
                           {
                              "subject":"other",
                              "test":"has_container_open",
                              "value":false//就是正在关闭保险箱(退出界面)
                           }
                        ]
                     },
                     "event":"true:close"//触发关闭事件
                  }
               ]
            }
         },//这里就是关闭的全流程判定


         "true:despawn":{//组件名(消失)
            "minecraft:transformation":{//mc自带的转换功能,此处为转换实体
               "into":"true:despawn",//转换为true:despawn实体(实测为隐形实体,会自动死亡)
               "drop_inventory":true//是否掉落所有物品(包括保险箱)
            }
         }
      },


      "components":{//效果组件,为保险箱实体提供各种效果

         "minecraft:type_family":{// 实体家族
            "family":[
               "safe"
            ]
         },

         "minecraft:collision_box":{// 碰撞箱
            "width":0.5,
            "height":0.8
         },

         "minecraft:health":{ // 超高血量(几乎无敌)
            "value":999999,
            "max":999999
         },

         "minecraft:scale":{// 大小
            "value":1.0
         },

         "minecraft:movement":{// 移动功能,0表示禁用
            "value":0.0
         },

         "minecraft:navigation.generic":{//寻路功能,留空表示禁用
           
         },

         "minecraft:movement.basic":{//基本移动机制,留空表示默认机制,但上方的移动值设定导致其无法移动
           
         },
         "minecraft:behavior.float":{水中漂浮,0表示禁用
            "priority":0
         },

         "minecraft:fire_immune":{// 防火
           
         },

         "minecraft:knockback_resistance":{// 100%击退抗性
            "value":100,
            "max":100
         },

         "minecraft:environment_sensor":{// 环境传感器
            "triggers":[//检测规则或条件
               {
                  "filters":{
                     "all_of":[
                        {
                           "test":"has_mob_effect",// 检测药水效果
                           "subject":"self",// 检测对象是自身
                           "operator":"equals",// 判断方式:等于
                           "value":"any"// 检测值:任何效果
                        }
                     ]
                  },
                  "event":"true:immune"//当条件满足时触发自定义事件 true:immune(下面有)
               }
            ]
         },

         "minecraft:damage_sensor":{//伤害检测器
            "triggers":[
               {
                  "on_damage":{//受伤害时
                     "filters":{
                        "test":"is_family",
                        "subject":"other",
                        "value":"player"
                     }//当伤害来源属于"玩家"家族时条件成立
                  },
                  "deals_damage":false//玩家无法造成伤害
               }
            ]
         },

         "minecraft:pushable":{//推动效果
            "is_pushable":false,//是否能被实体推动
            "is_pushable_by_piston":false是否能被活塞推动
         },//实测能被水推动,还能上火车awa

         "minecraft:physics":{//物理组件,留空表示默认
           
         }
      },

      "events":{//事件(也就是上面那些自定义事件的详细功能与代码)
//过于简单,无非是执行后添加或者移除某些自定义组件,或者再添加命令、调用文件,这里就不一一介绍了qwq
         "minecraft:entity_spawned":{
            "add":{
               "component_groups":[
                  "true:unowned_safe"
               ]
            }
         },

         "minecraft:on_tame":{
            "remove":{
               "component_groups":[
                  "true:unowned_safe"
               ]
            },
            "add":{
               "component_groups":[
                  "true:safe_inventory",
                  "true:safe_closed"
               ]
            }
         },

         "true:open":{
            "add":{
               "component_groups":[
                  "true:safe_open"
               ]
            },
            "remove":{
               "component_groups":[
                  "true:safe_closed"
               ]
            },
            "queue_command":{
               "command":[
                  "playsound safe_open @a [r=4] ~ ~ ~"
               ]
            }
         },

         "true:close":{
            "add":{
               "component_groups":[
                  "true:safe_closed"
               ]
            },
            "remove":{
               "component_groups":[
                  "true:safe_open"
               ]
            },
            "queue_command":{
               "command":[
                  "playsound safe_close @a [r=4] ~ ~ ~"
               ]
            }
         },

         "true:pick_up":{
            "add":{
               "component_groups":[
                  "true:despawn"
               ]
            }
         },

         "true:immune":{
            "queue_command":{
               "command":[
                  "effect @s clear"//清除药水效果
               ]
            }
         }
      }
   }
}
从中学到的内容:
1.subject 字段选项:
"other" 表示检测除自身外的实体,还可设置为 "self"(自身)、"parent"(父实体)、"baby"(子实体)等。
2.过滤器逻辑扩展
除 all_of 外,还支持 any_of(逻辑或)、none_of(逻辑非)等组合条件。
3.基本的实体格式
a.基本介绍
b.功能组件
c.自身状态
d.补全漏洞
 

  • 赞
反馈: 被拉黑の友人

Buildosour_积木小恐龙

搬运工
U3版主
U4开发者认证
正式会员
UID
803
2024/02/24
98
勋章
2
红宝石
601个红宝石
红宝石
601
  • #3
总结:
总算编完了,果然只有自己总结过才能学会东西,特别是萌新小白qwq