#Creating mob class

1 messages · Page 1 of 1 (latest)

unkempt sand
#

so

#

where do you will use this class?

#

ohhh

quick summit
#

Can you provide the latest code?

unkempt sand
#

so then you dont need db

unkempt sand
# quick summit Can you provide the latest code?
class Mob {
    /**
     * List of all mobs
     * @type {Record<string, Mob>}
     */
    static mobs = {};

    constructor(entity, index, health, damage, speed) {
        this.entity = entity;
        this.index = index;
        this.health = health;
        this.damage = damage;
        this.speed = speed;
        this.level = this.health + this.damage + this.speed;
        this.mobList = MobList[index];
    }
    summon() {
        if (!this.entity) return;
        const location = new Location(0, 0, 0);

        world
            .getDimension("overworld")
            .spawnEntity(
                this.entity,
                location
            ).nameTag = `${this.mobList.nameTag} | Lvl: ${this.level}`;
    }
    apply() {
        for (const player of world.getPlayers()) {
            for (const entity of player.dimension.getEntities()) {
                if (entity.nameTag === `${this.mobList.nameTag} | Lvl: ${this.level}`) {
                    entity.getComponent("minecraft:health").setCurrent(this.health);
                    entity.nameTag = `${this.mobList.nameTag} | Lvl: ${
                        this.level
                    } | Health: ${entity.getComponent("health").current}`;
                }
            }
        }
    }
    update() {
        this.entity.nameTag = `${this.mobList.nameTag} | Lvl: ${
            this.level
        } | Health: ${this.entity.getComponent("minecraft:health").current}`;
    }
}
#
for (let i = 0; i < 20; i++) {
    const random = Math.floor(Math.random() * MobList.length);
    const entity = MobList[random].id,
        index = random,
        health = Math.floor(Math.random() * 100 + 20),
        damage = Math.floor(Math.random() * 10 + 1),
        speed = Math.floor(Math.random() * 5 + 1);
    const mob = new Mob(entity, index, health, damage, speed);
    mob.summon();
    mob.apply();
}

world.events.entityHurt.subscribe((event) => {
    const entity = event.hurtEntity;
  const MobEntity = Mob.mobs[entity.id];
  
    if (!MobEntity) return

  MobEntity.update()
});
#

then i think i can do it better

#

will you use methods like summon and apply more than one time?

#

so you can simply

#

made them called in constructor

#

also you dont need apply method

#

ye, wait a minute

#

no

#

no

#

nameTag inst a function

#

wait ill do it

#
class Mob {
    /**
     * List of all mobs
     * @type {Record<string, Mob>}
     */
    static mobs = {};

    constructor(MobListEntity, health, damage, speed) {
        this.health = health;
        this.damage = damage;
        this.speed = speed;
        this.entity = this.summon();
        this.level = this.health + this.damage + this.speed;
        this.mlEntity = MobListEntity;

        Mob.mobs[this.entity.id] = this;
    }
    summon() {
        const location = new Location(0, 0, 0);

        return world.getDimension("overworld").spawnEntity(this.entity, location);
    }

    update() {
        this.entity.nameTag = `${this.mlEntity.nameTag} | Lvl: ${
            this.level
        } | Health: ${this.entity.getComponent("minecraft:health").current}`;
    }
}

for (let i = 0; i < 20; i++) {
    const random = Math.floor(Math.random() * MobList.length);
    const entity = MobList[random].id,
        health = Math.floor(Math.random() * 100 + 20),
        damage = Math.floor(Math.random() * 10 + 1),
        speed = Math.floor(Math.random() * 5 + 1);

    new Mob(entity, health, damage, speed);
}

world.events.entityHurt.subscribe((event) => {
    const entity = event.hurtEntity;
    const MobEntity = Mob.mobs[entity.id];

    if (!MobEntity) return;

    MobEntity.update();
});
#

oh wiat

#

now done

#

you can call update in constructor too

#

so now you got good template to update

#

huh, this.mlEntity.typeId

red oracleBOT
#
Debug Result

There are errors in this [code](#1079052452264935485 message):

<repl>.js:32:47 - error TS2304: Cannot find name 'MobList'.

32     const random = Math.floor(Math.random() * MobList.length);
                                                 ~~~~~~~
<repl>.js:33:20 - error TS2304: Cannot find name 'MobList'.

33     const entity = MobList[random].id,
                      ~~~~~~~

unkempt sand
#

id

#

ye

#

huh

#

i think

#

bc it was called

#

before initializing

#
        this.nameTag = this.update();
#

why do you need that

#

this.update() returns void

#

no, i mean

#

you can make const in update

#

then set this const to entity nameTag

#

and return it

#

or

#

better way

#

is just get it by

#

new Mob().entity.nameTag

#

or if you like new Mob().nameTag

#

you can add getter property to class

#
class Mob {
   get nameTag() {
      return this.entity.nameTag
   }
   set nameTag(name) {
      this.entity.nameTag = name
   } 
}
#

uh

#

where it sets mlEntity.nameTag

#

why

red oracleBOT
#
Debug Result

There are errors in this [code](#1079052452264935485 message):

<repl>.js:15:9 - error TS2322: Type 'void' is not assignable to type 'string'.

15         this.nameTag = this.update();
           ~~~~~~~~~~~~

unkempt sand
#

you dont need that line

#

also

#

you spawning entity one time

#

so this.mlEntity makes no sense

#
summon(id) {
        return world.getDimension("overworld").spawnEntity(id, new Location(0, 0, 0));
    }
#
        this.health = health;
        this.damage = damage;
        this.speed = speed;
        this.level = this.health + this.damage + this.speed;
        this.entity = this.summon(MobListEntity.id);
        this.customName = MobListEntity.nameTag
#

@wise prism

#

and then every this.mlEntity.nameTag need to be replaced to this.customName

#

and id of entity you still can get by new Mob().entity.typeId

unkempt sand
#

read messages above

#

and send me your update function

#

it seems like you giving to it previous name

#

you dont need it

#

you editing tag and pasting previous tag to it lol

#

oh wait

fast falcon
#

what's problem here?

#

@wise prism

fast falcon
#
set nameTag(name) {
   let { entity: { nameTag } } = this
   nameTag = nameTag.split(' | ')
   nameTag[0] = name
   this.entity.nameTag = nameTag.join(' | ')
}
#

the update one?

#

what it's like now?

#

which line tho

#

i changed a few things

#

works for me

#

works now?

#

cool

fast falcon
#

...