#Creating mob class
1 messages · Page 1 of 1 (latest)
Can you provide the latest code?
so then you dont need db
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
There are errors in this [code](#1079052452264935485 message):
[36m<repl>.js[0m:[33m32[0m:[33m47[0m - [31merror[0m[30m TS2304: [0mCannot find name 'MobList'.
[7m32[0m const random = Math.floor(Math.random() * MobList.length);
[7m [0m [31m ~~~~~~~[0m
[36m<repl>.js[0m:[33m33[0m:[33m20[0m - [31merror[0m[30m TS2304: [0mCannot find name 'MobList'.
[7m33[0m const entity = MobList[random].id,
[7m [0m [31m ~~~~~~~[0m
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
There are errors in this [code](#1079052452264935485 message):
[36m<repl>.js[0m:[33m15[0m:[33m9[0m - [31merror[0m[30m TS2322: [0mType 'void' is not assignable to type 'string'.
[7m15[0m this.nameTag = this.update();
[7m [0m [31m ~~~~~~~~~~~~[0m
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
read messages above
and send me your update function
it seems like you giving to it previous name
yes
you dont need it
you editing tag and pasting previous tag to it lol
oh wait
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
...