#import player problem

1 messages Β· Page 1 of 1 (latest)

somber moth
#
import * as mc from "@minecraft/server"
import * as lib from '../template'

mc.world.afterEvents.entitySpawn.subscribe(({entity}) => {
  if (entity.typeId === "ssak:leopard_hibachi") {

    const callback = mc.world.afterEvents.projectileHit.subscribe((arg) => {
    const { source, projectile } = arg;
    const hitInfo = arg.getEntityHit();

      if (hitInfo?.entity instanceof Player && source instanceof Player && projectile === entity) {

        hitInfo.runCommandAsync("damage @s " + (lib.scoreboard.get(source, "dmg")  * lib.scoreboard.get(player, "dmg_multiply") * 2) + " entity_attack entity " + player.nameTag)
        mc.world.afterEvents.projectileHit.unsubscribe(callback)
      }
    })
  }
})
#

Line 11

stark lodge
#

change that to source

somber moth
#

Ok

somber moth
#

The log says the problem is here:

      if (hitInfo?.entity instanceof Player && source instanceof Player && projectile === entity) {
stark lodge
#

whats the error

somber moth
stark lodge
#

do mc.Player

#

do it work

somber moth
stark lodge
#

you did mc.Player rigth

somber moth
#

Ok

stark lodge
#

did it work

somber moth
stark lodge
#

What now

somber moth
stark lodge
#

Player is not Defined

somber moth
#
import * as mc from "@minecraft/server"
import * as lib from '../template'

mc.world.afterEvents.entitySpawn.subscribe(({entity}) => { 
  if (entity.typeId === "ssak:leopard_hibachi") {

    const callback = mc.world.afterEvents.projectileHit.subscribe((arg) => {
    const { source, projectile } = arg;
    const hitInfo = arg.getEntityHit();

      if (hitInfo?.entity instanceof Player && source instanceof mc.Player && projectile === entity) {

        hitInfo.runCommandAsync("damage @s " + (lib.scoreboard.get(source, "dmg")  * lib.scoreboard.get(source, "dmg_multiply") * 2) + " entity_attack entity " + source.nameTag)
        mc.world.afterEvents.projectileHit.unsubscribe(callback)
      }
    })
  }
})
somber moth
stark lodge
#
import * as mc from "@minecraft/server"
import * as lib from '../template'

mc.world.afterEvents.entitySpawn.subscribe(({entity}) => {
  if (entity.typeId === "ssak:leopard_hibachi") {

    const callback = mc.world.afterEvents.projectileHit.subscribe((arg) => {
    const { source, projectile } = arg;
    const hitInfo = arg.getEntityHit();

      if (hitInfo?.entity instanceof mc.Player && source instanceof mc.Player && projectile === entity) {

        hitInfo.runCommandAsync("damage @s " + (lib.scoreboard.get(source, "dmg")  * lib.scoreboard.get(source, "dmg_multiply") * 2) + " entity_attack entity " + source.nameTag)
        mc.world.afterEvents.projectileHit.unsubscribe(callback)
      }
    })
  }
}) ```
#

did it work

somber moth
#

I didn't realize there was more than one Player

stark lodge
#

oh

somber moth
stark lodge
#

sure

somber moth
#

I tried to make this code work with different TypeId

#

But testing now, I saw that I couldn't

stark lodge
#

what were you trying to do

somber moth
#

This here is the original code that was working normally:

#
mc.world.afterEvents.entitySpawn.subscribe(({ entity }) => {
  if (entity.typeId !== "minecraft:arrow") return;

  const callback = world.afterEvents.projectileHit.subscribe((arg) => {
    const { source, projectile } = arg;
    const hitInfo = arg.getEntityHit();
  
    if (hitInfo?.entity instanceof Player && source instanceof Player && projectile === entity) {
      hitInfo.runCommandAsync("damage @s " + (lib.scoreboard.get(source, "dmg") * 1) + " entity_attack entity " + player.nameTag)
      world.afterEvents.projectileHit.unsubscribe(callback);
    };
  });
});
somber moth
#

The TypeId identifies the projectile

#

I wanted for more other funcutions identifying other projectiles

#

But I can not

stark lodge
#

oh if the player gets hit by a arrow he gets damage

somber moth
#

Yea

#

I need to make it with other projectiles

stark lodge
#

oki give me a sec

slim sable
#

why put projectileHit in entitySpawn?

#

projectileHit only fires if the entity is a projectile

somber moth
#

Think out of the box

#

Is it possible to combine things

slim sable
#

trying to get the entity instance since you can't get the projectile entity using projectileHit afterEvent?

slim sable
#

Here's my stupid idea: make an array of possible projectile IDs.
( if it's stupid, but it works, it's not stupid )

#
const projectiles = [
  "minecraft:arrow",
  "minecraft:snowball",
  "minecraft:thrown_trident",
  "minecraft:potion",
  "minecraft:fireball"
]

//if (projectiles.includes(entity.typeId))```
somber moth
#

Each projectile needs a unique command

slim sable
#

switch statement

#
switch (entity.typeId) {
  case "minecraft:arrow":
    //code for arrow
    break;
  case "minecraft:snowball":
    //code for snowball
    break;
}
somber moth
#

Let's say there are more than 50

slim sable
#

And you want each of them to run a different commands correct?

somber moth
#

I think minecraft updated

#

The script seems to have stopped working and that was out of nowhere

thorn grove
#
data.getEntityHit().entity
somber moth
stark lodge
#

yo @thorn grove i never used projectileHit what did i do wrong ```js
import { world, Player } from '@minecraft/server';

const database = [
['arrow', 11],
];

world.afterEvents.projectileHit.subscribe((event) => {
const { projectile } = event;
const hitEntity = event.getEntityHit()?.entity;
if (typeof hitEntity === "undefined" || !(hitEntity instanceof Player)) return;
for (const [projectileName, damageValue] of database) {
const projectileType = projectile?.typeId?.replace('minecraft:', '');
if (projectileName === projectileType) {
hitEntity.applyDamage(damageValue);
break;
}
}
}); ```

somber moth
#

Oh my

stark lodge
#

@thorn grove

somber moth
#

Why did you make so many changes?

thorn grove
#

;-;

somber moth
#

;-;

stark lodge
#

what?

thorn grove
somber moth
stark lodge
#

mm idk

stark lodge
somber moth
#

You even removed the essence of the system πŸ˜…πŸ˜…πŸ˜…

stark lodge
#

can you fix the code for me im working on something eles

thorn grove
#

;-;

somber moth
#

That command was to identify the player's score to deal damage to the entity hit

stark lodge
#

bro you are a Scripting Leader be nice

somber moth
#

The hit is according to the player's "dmg" score

stark lodge
#

M9 will help you now i allready made i code he can fix it

somber moth
#

🫠

thorn grove
#

ya, i do have some preferences..

somber moth
#

But that command cannot be withdrawn because it is the essence of the system.

#

I will show you

slim sable
#

You used a scoreboard to determine the damage done by the projectile?

thorn grove
#

thats what the projectile gona do?

somber moth
#

I basically repeated this on the projectiles, so that they have the same strength as the player...

somber moth
#

The projectile replicates the player's strength

slim sable
#

ProjectileHitAfterEvent
return both the source (player who shot it) and the entity that got hit.

I think both of these are enough to replicate what you are trying to do

thorn grove
#

projectile event have source

somber moth
#

If this is simpler and performs the same thing it will be much better

#

But I don't know how to do it

somber moth
#

It will be something that improves the quality of my addon

#

i think 🫠

slim sable
#

Step 1: steal borrow a function that grabs the player's score from the specified scoreboard objective.

somber moth
#

The "dmg_multiply" score is no big deal

#

It's just to multiply the damage whenever I want

slim sable
#

Step 2: code

  • get the player who shot the projectile
  • get the entity that got hit
  • get the 'player' scoreboard 'dmg'
  • <idk> I have no idea what you are trying to do
somber moth
#

I never messed with projectilehitevent

thorn grove
#

const projectiles = {
    "minecraft:arrow"(projectile, hitEntity, source) {

        hitEntity.applyDamage(lib.scoreboard.get(source, "dmg") * 1, {
            cause: 'entity_attack',
            damagingEntity: source
        })
    },
    "minecraft:thrown_trident"(projectile, hitEntity, source) {

    }
}

world.afterEvents.projectileHit.subscribe(arg => {
    const { source, projectile } = arg;
    const hitInfo = arg.getEntityHit();
    if (projectile.typeId in projectiles && hitInfo && source instanceof Player) {
        projectiles[projectile.typeId](projectile, hitInfo.entity, source);
    };
})
thorn grove
#

dunno if that'd even work

slim sable
#

he did import * as mc

#

idk

somber moth
#

That's why I put a multiplication in front

thorn grove
#

ok

#

so what type of data r u gona differenciate the projectiles

somber moth
slim sable
#

entity.typeId ?

somber moth
#

ssak:leopard_hibachi
ssak:leopard_bigan

#

ssak:leopard_rokougan

somber moth
#

Each entity, in addition to adding damage, multiplies it in a different way

slim sable
#

I would be using an array and do .find()

somber moth
thorn grove
#

/**@type {{[typeId:string]:(a:mc.Entity,b:mc.Entity,c:mc.Player)=>any}} */
const projectiles = {
    "minecraft:arrow"(projectile, hitEntity, source) {
        hitEntity.applyImpulse({x:0,y:0,z:100})
        hitEntity.applyDamage(lib.scoreboard.get(source, "dmg") * 1, {
            cause: 'entity_attack',
            damagingEntity: source
        })
    },
    "minecraft:thrown_trident"(projectile, hitEntity, source) {

        hitEntity.applyDamage(lib.scoreboard.get(source, "dmg") * 255, {
            cause: 'anvil',
            damagingEntity: source
        })
    }
}
slim sable
#

does the 'cause' changes?

slim sable
#

or you just want to change the *multiplier?

slim sable
thorn grove
#

kind of applyKnockback

somber moth
thorn grove
#

u can do whatever in those entities there

somber moth
#

They are special attacks, it will always be the same entities

#

But each player will have a different strength, so the damage given by entities will be different...

thorn grove
#

ye...

slim sable
#

I get what you are trying to do.
playerDmg * projectileDmg

thorn grove
#

each projectile.typeId have their own methods there

somber moth
#

These projectiles do the same damage that the player can currently do

thorn grove
#
projectiles[projectile.typeId](projectile, hitInfo.entity, source);
somber moth
slim sable
slim sable
#

Forget the formula, lets try and solve this issue.

somber moth
#

Projectiles are a way to get attacks with more range

thorn grove
#

so projectile type is just for dmg multipliers?

slim sable
#

from my observation of this conversation, yes.

somber moth
#

I'll show

slim sable
#

Then what is this multiplier based on?

somber moth
slim sable
#

Thanks, that doesn't help at all.

thorn grove
#

so different methods/type

somber moth
#

These projectiles should take the player's strength and multiply

somber moth
#

I wanted to show that projectiles shouldn't just multiply

#

They are cast by the player

slim sable
#

YES, each type of projectile has a different multiplier

thorn grove
#

thats why i made that const projectiles for different methods

somber moth
#

And when they hit the entity, they add multiplied damage

somber moth
#

I haven't tested it yet

#

I'm trying to explain the system πŸ˜…

slim sable
#

it uses the player 'dmg' (which is a scoreboard) and multiplies it using the corresponding projectile multiplier dmg (that's used)

somber moth
#

The player's strength is defined by the "dmg" score

slim sable
#

ok

#

happy?

somber moth
#

Eita

somber moth
#

What are you talking about?

#

All I know is this system is crazy

somber moth
#

But that's because I don't know how to apply what Learn Why did

thorn grove
somber moth
#

Are they not const?

thorn grove
#

wdym?

somber moth
#

Is that

thorn grove
#

the entities are passed in the function from the hitevent

somber moth
#

Is that, from what I understood

#

are const

thorn grove
#

its an object of methods

somber moth
#

I'm not understanding anything 😭

thorn grove
#
const projectiles = {
    "minecraft:arrow"(projectile, hitEntity, source) {
        hitEntity.applyImpulse()
        hitEntity.applyDamage(lib.scoreboard.get(source, "dmg") * 1, {
            cause: 'entity_attack',
            damagingEntity: source
        })
    },
    "minecraft:thrown_trident"(projectile, hitEntity, source) {
        //code
    },
    "custom:trident"(projectile, hitEntity, source) {
        //code
    },
    "custom:arrow"(projectile, hitEntity, source) {
        //code
    }
}
somber moth
#

I thought what you did was the script for projectiles to deal player damage

#

AAAAAAAAA

#

Now I understand

#

How cool in

#

you are really smart

thorn grove
#

well, uhh

thorn grove
#

the applydamage might be tricky

slim sable
#

cannot apply damage when players have iframe?

thorn grove
#

ig so

somber moth
slim sable
#

why not just, set their current health? (ignore this - bad idea)

thorn grove
somber moth
#

I just put the /damage thing one more time

somber moth
thorn grove
#

depending on projectile typeId, yes

somber moth
#

Projectilhit?

#

HitEntity?

somber moth
somber moth
#

I can kill myself

thorn grove
#

;-;

slim sable
#

No! don't kill your self!
At Least do it with styles!

somber moth
somber moth
#

πŸ’€πŸ’€

somber moth
thorn grove
#

cus putting it in the event means that it'd generate that same object repeatedly

somber moth
thorn grove
#

so it's constant...

#

generating an object takes performace

#

so making it once should be fine

somber moth
#

I'm sorry for all this trouble

#

But I never studied anything about scripting

thorn grove
#

it's alr, as long as youre learning

somber moth
#

I don't know anything about minecraft scripting actually

#

What I know, was tinkering with some scripts around

thorn grove
#

what i sent is mostly javascript

#

object and methods

somber moth
#

I'll try

#

I will test what you did

thorn grove
#

u can see it like /function projectiles/minecraft:arrow

somber moth
#

Because these const are outside

#

Is it just me putting the "impot * as mc..."?

thorn grove
#

just paste it in the same file, so no need to import

#

oh u need the whole file

#
import * as mc from '@minecraft/server';
import * as lib from '../template'

const projectiles = {
    "ssak:leopard_hibachi"(projectile, hitEntity, source) {
        hitEntity.applyImpulse
        hitEntity.applyDamage(lib.scoreboard.get(source, "dmg") * 1, {
            cause: 'entity_attack',
            damagingEntity: source
        })
    },
    "minecraft:thrown_trident"(projectile, hitEntity, source) {
        source.runCommand('say hi')
    },
    "custom:trident"(projectile, hitEntity, source) {
        hitEntity.runCommand('say hi')
        
    },
    "custom:arrow"(projectile, hitEntity, source) {
        let location = hitEntity.location
        projectile.runCommand('say hi')
        
    }
}
mc.world.afterEvents.entitySpawn.subscribe(({ entity }) => {
    const type = entity.typeId
    if (type in projectiles) {

        const callback = mc.world.afterEvents.projectileHit.subscribe((arg) => {
            mc.world.afterEvents.projectileHit.unsubscribe(callback)
            const { source, projectile } = arg
            const hitInfo = arg.getEntityHit()

            if (hitInfo?.entity instanceof mc.Player && source instanceof mc.Player && projectile === entity) {
                projectiles[type](projectile, hitInfo.entity, source)
            }
        })
    }
})
somber moth
somber moth
#

But there in "\code"

#

If I needed for a function

#

Wouldn't I need to import?

thorn grove
#

treat it likejs mc.world.afterEvents.projectileHit.subscribe(arg => { //code })

#

//code is js commant

somber moth
thorn grove
#

u can remove it

thorn grove
somber moth
#

I confused the languages

somber moth
#

If I were to make a command

#

How would I put a command there using one of these const?

#

Does not make sense

#

If I put the function in projectilehit, how would it identify the const?

#

Does not make sense

thorn grove
#

there

somber moth
#

The command works, even outside of an event

#

I didn't know this was possible

thorn grove
#

prjectilehit event triggers it

somber moth
#

I think I understand

somber moth
#

Get player scores?

thorn grove
#

source is the player

somber moth
#

Take the player's score to add the damage

thorn grove
#

its in applyDamage there

#

thats the sample

somber moth
#

Ok

#

I will try

somber moth
#

The Minecraft:Arrow in the case

thorn grove
#

did u add the ```js

import * as lib from '../template'

thorn grove
#

hmm

#

so it needs in nested event then

somber moth
somber moth
slim sable
#

cant get entity(projectile) instance in projectileHit afterEvent?

somber moth
#

Only the one that applies the damage according to the player's score did not work

slim sable
#

Yeah it's because the arrow disappeared when it hit the player

#

That's why it's undefined (I think)

somber moth
#

Didn't you see it in the video?

slim sable
#

yes typeId is undefined

somber moth
#

Damage is not added

#

Damage must be added according to the player's "dmg" score

#

This is the problem

slim sable
#

because typeId is undefined, it stops the other script from running?

somber moth
thorn grove
#

use this event

mc.world.afterEvents.entitySpawn.subscribe(({ entity }) => {
    const type = entity.typeId
    if (type in projectiles) {

        const callback = mc.world.afterEvents.projectileHit.subscribe((arg) => {
            mc.world.afterEvents.projectileHit.unsubscribe(callback)
            const { source, projectile } = arg
            const hitInfo = arg.getEntityHit()

            if (hitInfo?.entity instanceof Player && source instanceof Player && projectile === entity) {
                projectiles[type](projectile, hitInfo.entity, source)
            }
        })
    }
})
somber moth
#

Ok

#

Substitute?

thorn grove
#

yeah

slim sable
#

Does that work? if it does, I'm gonna yoink it.

somber moth
#

For some reason, even having everything added to "MC"

somber moth
slim sable
#

yea, do that. that should fix it

somber moth
#

But the script I used worked

#

However, it stopped working out of nowhere

#

And there was no way for other entities

slim sable
#

Confirmation please, does the new provided script work? Does it show any errors? I hope not.

somber moth
#

Still not adding damage, that's the problem

somber moth
#

I don't understand how

#

But before

#
mc.world.afterEvents.entitySpawn.subscribe(({ entity }) => {
  if (entity.typeId !== "minecraft:arrow") return;

  const callback = world.afterEvents.projectileHit.subscribe((arg) => {
    const { source, projectile } = arg;
    const hitInfo = arg.getEntityHit();
  
    if (hitInfo?.entity instanceof Player && source instanceof Player && projectile === entity) {
      hitInfo.runCommandAsync("damage @s " + (lib.scoreboard.get(source, "dmg") * 1) + " entity_attack entity " + player.nameTag)
      world.afterEvents.projectileHit.unsubscribe(callback);
    };
  });
});
#

it worked

slim sable
#

Show me the script you're using

thorn grove
#

did u have this

slim sable
somber moth
#

The code I used

slim sable
#

weird

somber moth
#

It was this:

mc.world.afterEvents.entitySpawn.subscribe(({ entity }) => {
  if (entity.typeId !== "ssak:leopard_hibachi") return;

  const callback = world.afterEvents.projectileHit.subscribe((arg) => {
    const { source, projectile } = arg;
    const hitInfo = arg.getEntityHit();
  
    if (hitInfo?.entity instanceof Player && source instanceof Player && projectile === entity) {
      hitInfo.runCommandAsync("damage @s " + (lib.scoreboard.get(source, "dmg") * 1) + " entity_attack entity " + player.nameTag)
      world.afterEvents.projectileHit.unsubscribe(callback);
    };
  });
});
#

As I said at the beginning

#

it worked

#

But it stopped out of nowhere

#

And besides, I can't do the commands with different entities

thorn grove
#

whats the whole file

somber moth
somber moth
# thorn grove whats the whole file
import * as mc from "@minecraft/server"
import * as lib from './template'

mc.world.afterEvents.entitySpawn.subscribe(({ entity }) => {
  if (entity.typeId !== "ssak:leopard_hibachi") return;

  const callback = mc.world.afterEvents.projectileHit.subscribe((arg) => {
    const { source, projectile } = arg;
    const hitInfo = arg.getEntityHit();
  
    if (hitInfo?.entity instanceof mc.Player && source instanceof mc.Player && projectile === entity) {
      hitInfo.runCommandAsync("damage @s " + (lib.scoreboard.get(source, "dmg") * 1) + " entity_attack entity " + player.nameTag)
      mc.world.afterEvents.projectileHit.unsubscribe(callback);
    };
  });
});
somber moth
somber moth
#

But another bug appeared

#

🫠🫠🫠🫠🫠🫠🫠

thorn grove
#

it works fine for me tho

somber moth
#

That is the problem

somber moth
#

It's like it's not identifying the player's score

thorn grove
#

cus player is in immunity state

somber moth
somber moth
somber moth
#

That is the problem

somber moth
#

@thorn grove @slim sable
I achieved

#

Now the projectile consults the score of its source and executes a command when reaching its target

#
mc.world.afterEvents.entityHurt.subscribe(data => {
  const
    hurt = data.hurtEntity,
    damage = data.damage,
    damaging = data.damageSource.damagingProjectile;
  if (damaging?.typeId != "ssak:leopard_hibachi") return;
  const player= data.damageSource.damagingEntity
  hurt.runCommandAsync(`damage @s ${lib.score(player, "dmg") * 2} entity_attack entity "${player.nameTag}"`)
})
slim sable
#

congrats

#

I will now yoink it for scientific purposes.

thorn grove
#

use the one thats nested in entitySpawn

#

projectileHit throws errors still

slim sable
#

nesting projectileHit in entitySpawn makes it run more than once every time it fires. (for me)
So, I think this one is better

somber moth
#

I used only damageSource variables

thorn grove
#

ok

somber moth
#

Thank you everyone