查看: 920|回复: 1
打印 上一主题 下一主题

[交流] 随便分享几个函数

[复制链接]

23

主题

31

回帖

37

积分

Lv.2 石器学徒

UID
7551983
小麦
0
金锭
229
下界之星
0
跳转到指定楼层
楼主
发表于 2021-12-7 22:30:42 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式 IP:山东
随便分享几个函数
def random_index(self,intt):
        """概率函数"""
        #
        # 参数intt为int,输入1到100数如输入40那么返回0的概率为40% 返回1的概率为60%
        #
        rate = [intt,100-intt]
        start = 0
        randnum = random.randint(1, sum(rate))

        for index, item in enumerate(rate):
            start += item
            if randnum <= start:
                break
        return index
def effect(self, entityId, effect, time, level,boolc):
        """效果函数 1,id  2,效果名字 3,时间 4,效果等级,5是否显示粒子"""
        comp = serverApi.GetEngineCompFactory().CreateEffect(entityId)
        res = comp.AddEffectToEntity(effect, time, level, boolc)
        return res
def Hurt2(self, idd, entityId, hurt,knocked,y):
        """模拟攻击多个实体 1,创造者ID 2,要攻击的对象ID列表 3,攻击伤害 4,Y轴的瞬时移动"""
        for value in entityId:
            if value != idd:
                # comp = serverApi.GetEngineCompFactory().CreateGame(serverApi.GetLevelId())
                # entityitemName = comp.GetEntityIdentifier(value)
                comp = serverApi.GetEngineCompFactory().CreateEngineType(value)
                entityitemName = comp.GetEngineTypeStr()
                comp = serverApi.GetEngineCompFactory().CreatePos(value)
                pos = comp.GetPos()
                if entityitemName != "minecraft:item" and value != idd and pos != None:
                    comp = serverApi.GetEngineCompFactory().CreateHurt(value)
                    comp.Hurt(hurt, serverApi.GetMinecraftEnum().ActorDamageCause.EntityAttack, idd, None, knocked)
                    motionComp = serverApi.GetEngineCompFactory().CreateActorMotion(value)
                    motionComp.SetMotion((0,y, 0))
def zhidingfaswu(self, idd, pos1, pos2,item):
        """指定一个位置发射抛射物 到指定目标位置 1,发射人ID 3,发射物创建位置 3,目标位置"""
        x = pos1[0] - pos2[0]
        y = pos1[1] - pos2[1]
        z = pos1[2] - pos2[2]
        ros = (-x,-y,-z)

        comp = serverApi.GetEngineCompFactory().CreateProjectile(serverApi.GetLevelId())
        param = {
                # 'position': (pos[0]+random.randint(-5,5),pos[1]+random.randint(2,8),pos[2]+random.randint(-5,5)),                  #发射位置
                'position': pos1,                  #发射位置
                'direction': ros,              #朝向
                'power': 1,                       #投掷的力量值,越大发射的速度越快
                'gravity': 0.01,                      #抛射物重力因子,默认为json配置中的值
                'damage':  20,                #抛射物伤害值,默认为json配置中的值
                # 'targetId': str(item)                   #抛射物目标(指定了target之后,会和潜影贝生物发射的跟踪导弹的那个投掷物是一个效果),默认不指定
                # 'isDamageOwner': False          #对创建者是否造成伤害,默认不造成伤害  
                # 火焰弹minecraft:fireball(ok)          魔影住minecraft:ender_pearl(ok)    箭minecraft:arrow
                }
        ectileEntityid = comp.CreateProjectileEntity(idd, "zhuzhizi:fireball", param)

        return ectileEntityid
def Get_PlayerItemPos(self, idd):
        """返回背包为None的槽位 如果槽位都有物品返回None"""
        a = 0
        while a < 36:
            comp = serverApi.GetEngineCompFactory().CreateItem(idd)
            PlayerItem = comp.GetPlayerItem(serverApi.GetMinecraftEnum().ItemPosType.INVENTORY, a)
            if PlayerItem == None:
                return a
            if a >= 35:
                return None
            a = a +1










13

主题

501

回帖

145

积分

Lv.4 怪物猎人

UID
7945930
小麦
0
金锭
422
下界之星
0
沙发
发表于 2021-12-9 11:06:35 | 只看该作者 IP:广东
感谢大佬分享
试试回复里<代码>格式效果,大佬下次用那个代码格式列出来可能会方便看一点
  1. def Get_PlayerItemPos(self, idd):
  2.         """返回背包为None的槽位 如果槽位都有物品返回None"""
  3.         a = 0
  4.         while a < 36:
  5.             comp = serverApi.GetEngineCompFactory().CreateItem(idd)
  6.             PlayerItem = comp.GetPlayerItem(serverApi.GetMinecraftEnum().ItemPosType.INVENTORY, a)
  7.             if PlayerItem == None:
  8.                 return a
  9.             if a >= 35:
  10.                 return None
  11.             a = a +1
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

返回顶部