猫史档案馆


【米粒教程】背包折叠代码详细使用说明

用户:FML饭米粒FML饭米粒查看:2 回复:6 评论:2 创建时间:2022-03-04T17:04:14


world.onPlayerJoin(async({entity})=>{entity.tlist=[]//用于储存玩家背包的列表entity.hasThings=function(name,number){//检测背包内是否有number个name(有返回true,没有name或数量不足返回false)for(num=0;num<entity.tlist.length;num++){if(entity.tlist[num].name==name){if(entity.tlist[num].number>=number){returntrue}else{returnfalse}}}returnfalse}entity.getThings=function(name,number){//给玩家背包内增喵namefor(num=0;num<entity.tlist.length;num++){if(entity.tlist[num].name==name){entity.tlist[num].number+=numberreturnentity.tlist}}entity.tlist.push({name:name,number:number})returnentity.tlist}entity.delThings=function(name,number){//删除玩家背包内number个name(数量不足不会更改)for(num=0;num<entity.tlist.length;num++){if(entity.tlist[num].name==name){if(entity.tlist[num].number<number){world.say('Error:bagdeletionerror')returnentity.tlist}entity.tlist[num].number-=numberif(entity.tlist[num].number==0){consttarget=entity.tlistentity.tlist=[]target.forEach((object)=>{if(object.name!=name){entity.tlist.push(object)}})}returnentity.tlist}}}entity.showBag=function(){//显示玩家背包(返回格式:列表,如['a×1','b×1','c×2'...]varshowList=[]for(num=0;num<entity.tlist.length;num++){showList.push(`${entity.tlist[num].name}×${entity.tlist[num].number}`)}returnshowList}entity.bagLength=function(){//物品总量(返回玩家背包内物品总数,格式:数字)varthingsNumber=0for(num=0;num<entity.tlist.length;num++){thingsNumber+=entity.tlist[num].number}returnthingsNumber}})world.onChat(async({entity,message})=>{switch(message){case'1'://玩家发送“1”时给玩家增加一个“空气”entity.getThings('空气',1)breakcase'2'://玩家发送“2”时给玩家删除一个“空气”entity.delThings('空气',1)breakcase'3'://玩家发送“3”时告诉玩家背包里有没有至少一个“空气”entity.player.directMessage(entity.hasThings('空气',1))breakcase'4'://玩家发送“4”时告诉玩家背包里有多少物品entity.player.directMessage(entity.bagLength())breakcase'5'://玩家发送“5”时显示玩家背包并告诉玩家选择了什么物品,有几个varchoose=awaitentity.player.dialog({type:Box3DialogType.SELECT,title:'背包',options:entity.showBag()})if(!choose||choose==null)return//没有选择就跳过entity.player.directMessage(choose.value.split('×')[0])//选择了什么物品entity.player.directMessage(choose.value.split('×')[1])//选择的物品有多少break}})


回复

上一页1 页 / 共 1下一页
FML饭米粒FML饭米粒

world.onPlayerJoin(async ({ entity }) => {
    entity.tlist = []
    entity.hasThings = function (name, number) {
        for (num = 0; num < entity.tlist.length; num++) {
            if (entity.tlist[num].name == name) {
                if (entity.tlist[num].number >= number) {
                    return true
                } else {
                    return false
                }
            }
        }
        return false
    }
    entity.getThings = function (name, number) {
        for (num = 0; num < entity.tlist.length; num++) {
            if (entity.tlist[num].name == name) {
                entity.tlist[num].number += number
                return entity.tlist
            }
        }
        entity.tlist.push({ name: name, number: number })
        return entity.tlist
    }
    entity.delThings = function (name, number) {
        for (num = 0; num < entity.tlist.length; num++) {
            if (entity.tlist[num].name == name) {
                if (entity.tlist[num].number < number) {
                    world.say('Error:bag deletion error')
                    return entity.tlist
                }
                entity.tlist[num].number -= number
                if (entity.tlist[num].number == 0) {
                    c喵t target = entity.tlist
                    entity.tlist = []
                    target.forEach((object) => {
                        if (object.name != name) {
                            entity.tlist.push(object)
                        }
                    })
                }
                return entity.tlist
            }
        }
    }
    entity.showBag = function () {
        var showList = []
        for (num = 0; num < entity.tlist.length; num++) {
            showList.push(`${entity.tlist[num].name}×${entity.tlist[num].number}`)
        }
        return showList
    }
    entity.bagLength = function () {
        var thingsNumber = 0
        for (num = 0; num < entity.tlist.length; num++) {
            thingsNumber += entity.tlist[num].number
        }
        return thingsNumber
    }
})
world.onChat(async ({ entity, message }) => {
    switch (message) {
        case '1': 
            entity.getThings('空气', 1)
            break
        case '2': 
            entity.delThings('空气', 1)
            break
        case '3':
            entity.player.directMessage(entity.hasThings('空气', 1))
            break
        case '4': 
            entity.player.directMessage(entity.bagLength())
            break
        case '5':
            var choose = await entity.player.dialog({
                type: Box3DialogType.SELECT,
                title: '背包',
                opti喵: entity.showBag()
            })
            if (!choose || choose == null) return
            entity.player.directMessage(choose.value.split('×')[0])
            entity.player.directMessage(choose.value.split('×')[1])
            break
    }
})

点赞0


评论


一个STUB用户_14302596一个STUB用户_14302596

顶顶顶

点赞0


评论


Accumulating丶謀Accumulating丶謀

这是一点也没看懂

点赞0


评论


灵清帝王吃爆一切厕所灵清帝王吃爆一切厕所

收藏

点赞0


评论


全能代码师全能代码师

喵d

点赞0


评论


yee剑走天下yee剑走天下

喵d

点赞0


评论