#Run commands at entity death

1 messages · Page 1 of 1 (latest)

forest lintel
#

Not all event callback parameters have the same property structure.

In this case, the entityDie event class provided two properties:

  1. damageSource (Object)
  2. deadEntity (Entity Class)

with damageSource have the following properties:

  1. cause (Enum)
  2. damagingEntity
  3. damagingProjectile

damagingEntity and damagingProjectile are optional properties, they may return undefined if the entity dies from natural causes or was killed with melee attacks.

#

if you wish for the code to only work when a player is the one killing the entity, do the following checks:```js
const player = event.damageSource.damagingEntity //get the attacking entity
if (!player) return; //check if it's undefined
//check if it's a player class (it could be another mob)
if (!(player instanceof Player)) return;

`Import { Player } from "@minecraft/server"`
upper warren
#

Thanks again Gega, you are helping a lot!

I am still a complete noob to javascript, I will try and get my head around what you have suggested and see if I can implement it, but I may be back soon 😄

#

I am now getting the error:
[main.js] ran with error: [SyntaxError: Could not find export 'player' in module '@minecraft/server']

I can't use any beta scripting due to marketplace restrictions, is the player module in beta?

forest lintel
#

It's case sensitive

upper warren
#

ahh, my bad, I have amended that, but have an error 'deadEntity' is not defined, let me check again

#

Okay, so here is what I have now:

import { world, Player } from "@minecraft/server";

const mobTypes = [
  "minecraft:chicken"
];
const scoreboard = world.scoreboard
const score = scoreboard.getObjective("job_hunter_lvl")

world.afterEvents.entityDie.subscribe((event) => {
  const player = event.damageSource.damagingEntity;
  const scores = score.getScore(player);


  if (!player) return;
  if (!(player instanceof Player)) return;

  mobTypes.forEach((id) => {
    if (mobTypes.matches(id) && scores < 6) {
      event.player.dimension.runCommand(
        `particle minecraft:basic_flame_particle ${event.mobTypes.x} ${event.mobTypes.y} ${event.mobTypes.z}`
      );
              let players = world.getAllPlayers();
        for (let player of players) {
            player.runCommand("scoreboard players add @s job_hunter_xp " + 1);
            player.runCommand("playsound random.orb @s ~~~ 1 1");
            player.runCommand("function level_up")
        }

      return;
    }
  });
});

But I am now getting the error

[Scripting][error]-TypeError: not a function at <anonymous> (HunterTest.js:18)
at forEach (native)
at <anonymous> (HunterTest.js:31)

and I am still getting

[Scripting][error]-TypeError: Native variant type conversion failed. at <anonymous> (HunterTest.js:11)

I am sorry for being such a noob, just really struggling to get my head round this

forest lintel
#

That came from the matches() method.

The appropriate is to use object:```js
entity.matches({
type: "namespace:name" //entity identifier
}); //return Boolean

upper warren
#

but would this mean I cant use the array in

const mobTypes = [
"minecraft:chicken"
];

I want to eventually have a list of mobs in there

forest lintel
#

you're iterating the array with forEach, and id is the string from your array.

#

It's fine.

#

But I would switch to for...loop to avoid unnecessary iteration when only checking for a single entity.

upper warren
#

bao_foxxo_crying I feel so confused right now lol

I think I need to read more on how js works maybe 😄

forest lintel
#

Here's a for...of loop:```js
let mobId; //this is undefined
for (const typeId of mobTypes) {
if (deadEntity.matches({type: typeId})) {
mobId = typeId; //reassignment
break; //exit loop
};
};
if (!mobId) return; //stop code if entity id is not on the mobTypes list
//code

upper warren
#

thanks, I am just taking a break right now as my brain hurts, but I will give this a go, thanks so much Gega!!

forest lintel
#

yeah, let's ignore the part where you tried to access mobTypes from event inside the runCommand, attempting to get the XYZ location. Have a good break!