随身背包

By MCSugar_cane    2017/12/16

用命令方块实现背包,支持多人,已测试~代码适用版本为1.11.2-1.12,逻辑支持1.8.x以上版本

PS: 在写完代码后我又双叒叕惊喜的发现:我拓麻为什么要做这个模组,用原本有的潜影盒不好吗!(自己开的坑,哭着都要填完!)


我的世界随身背包我的世界随身背包

图片就介绍到这里,下面是思路以及代码部分~


整体思路:

首先我们来看单人里面的随身背包,无非就是用箱子或箱子矿车来实现,但箱子有个不好的地方就是它不是实体而无法被选择器选中。只能获取它的绝对位置进行clone才行,而不像箱子矿车可以被选择器选中。在单人中,我们只需要召唤一个箱子矿车,当玩家丢出掉落物后tp至掉落物,不要的时候再tp回来,十分简单。 有关检测指定掉落物可以查看此教程: 检测掉落物

按照单人的雏形,在多人中只需要一人有一个箱子矿车就可以了,难点就在于如何区分谁是谁的矿车,在这里我引用了之前做箱子商店区分主人的逻辑,为每一个矿车分配ID,并令其所有者ID=矿车ID,判定主人只需要令所有矿车ID减去玩家其ID,若差值等于0则为其所有者。


代码:

基础部分:

  1. /summon armor_stand ~ ~ ~ {CustomName:"bagIDStats"}
  2. /scoreboard objectives add gbag dummy
  3. /scoreboard objectives add gtemp dummy 信息匹配
  4. /scoreboard objectives add bagID dummy 背包ID

初始化:[高频循环]

  1. /scoreboard players add @a gbag 0
  2. /execute @a[score_gbag=0,score_gbag_min=0] ~ ~ ~ /summon minecraft:chest_minecart ~ ~-.6 ~ {CustomName:"随身背包 - www.Ganxiaozhe.com",NoGravity:1b,Invulnerable:1b,Tags:["newbag","bag"]}
  3. /tellraw @a[score_gbag=0,score_gbag_min=0] [{"text":"\n[随身背包] ","color":"dark_green","bold":"true"},{"text":"已为您配置您的随身背包。","color":"green","bold":"true"},{"text":" [\u00a7l了解更多\u00a78]\n","color":"dark_gray","clickEvent":{"action":"open_url","value":"https://www.ganxiaozhe.com/mc/?from=map"},"bold":"false"}]
  4. /scoreboard players set @a[score_gbag=0,score_gbag_min=0] gbag -1
  5. /clear @a[score_gbag=-1,score_gbag_min=-1] chest_minecart 0 64 {display:{Name:"随身箱子(按Q使用)"}}
  6. /replaceitem entity @a[score_gbag=-1,score_gbag_min=-1] slot.hotbar.8 chest_minecart 1 0 {display:{Name:"随身箱子(按Q使用)"},isbag:1,HideFlags:127,ench:[{id:34,lvl:10}]}
  7. /execute @e[tag=newbag,score_bagID_min=1] ~ ~ ~ scoreboard players operation @p[r=2] bagID = @e[r=0,c=1] bagID
  8. /scoreboard players set @e[tag=newbag,score_bagID_min=1] gbag -1
  9. /scoreboard players tag @e[tag=newbag,score_bagID_min=1] remove newbag
  10. /teleport @e[tag=bag,score_gbag=-1,score_gbag_min=-1] ~ ~1 ~
  11. /scoreboard players set @e[tag=bag,score_gbag=-1] gbag 1

ID分配:[高频循环]

  1. /scoreboard players add @e[tag=newbag] bagID 0
  2. /execute @e[tag=newbag,score_bagID_min=0,score_bagID=0,c=1] ~ ~ ~ scoreboard players add @e[type=armor_stand,name=bagIDStats] bagID 1
  3. /execute @e[tag=newbag,score_bagID_min=0,score_bagID=0,c=1] ~ ~ ~ scoreboard players operation @e[r=0,c=1] bagID = @e[type=armor_stand,name=bagIDStats] bagID

丢出出现箱子:[高频循环]

  1. /scoreboard players tag @e[type=Item] add isbag {Item:{id:"minecraft:chest_minecart",Damage:0s,tag:{isbag:1}},OnGround:1b}
  2. /execute @e[type=Item,tag=isbag] ~ ~ ~ scoreboard players set @p[r=6] gbag 1 判定开始
  3. /execute @p[score_gbag=1,score_gbag_min=1] ~ ~ ~ execute @e[tag=bag,score_gbag_min=1,score_gbag=1] ~ ~ ~ /scoreboard players operation @e[c=1,r=0] gtemp = @e[c=1,r=0] bagID
  4. /execute @p[score_gbag=1,score_gbag_min=1] ~ ~ ~ execute @e[tag=bag,score_gbag_min=1,score_gbag=1] ~ ~ ~ scoreboard players operation @e[c=1,r=0] gtemp -= @p[score_gbag=1,score_gbag_min=1] bagID
  5. 判定结束
  6. /execute @p[score_gbag=1,score_gbag_min=1] ~ ~ ~ /tp @e[type=chest_minecart,tag=bag,score_gtemp=0,score_gtemp_min=0] @e[type=item,tag=isbag,c=1]
  7. scoreboard players set @e[tag=bag,score_gtemp_min=0,score_gtemp=0] gbag 2
  8. /execute @e[tag=bag,score_gtemp_min=0,score_gtemp=0] ~ ~ ~ kill @e[type=Item,tag=isbag,r=1]
  9. /scoreboard players reset @e[type=chest_minecart,tag=bag,score_gtemp=0,score_gtemp_min=0] gtemp
  10. /scoreboard players set @a[score_gbag_min=1,score_gbag=1] gbag 2
  11. /clear @a[score_gbag_min=1] barrier 0 64 {display:{Name:"回收箱子(按Q使用)"}}
  12. /replaceitem entity @a[score_gbag_min=1] slot.hotbar.8 barrier 1 0 {display:{Name:"回收箱子(按Q使用)"},nobag:1,HideFlags:127,ench:[{id:34,lvl:10}]}

回收箱子:[高频循环]

  1. /scoreboard players tag @e[type=Item] add nobag {Item:{id:"minecraft:barrier",Damage:0s,tag:{nobag:1}},OnGround:1b}
  2. /execute @e[type=Item,tag=nobag] ~ ~ ~ scoreboard players set @e[tag=bag,score_gbag_min=2,score_gbag=2,c=1,r=2] gbag -1
  3. /execute @e[type=Item,tag=nobag] ~ ~ ~ scoreboard players set @p[score_gbag_min=2,score_gbag=2] gbag -1
  4. /kill @e[type=Item,tag=nobag]


OOC:

v1.11.2-v1.12
展开


欢迎加群交流划水:481528679