#can't .replace() parenthesis from a string output by class 'DamageSource' using regex

24 messages · Page 1 of 1 (latest)

dense grove
#

Accidentally made the post previously without the modloader tag, but 2nd times the charm

EntityEvents.hurt('minecraft:player', event => {
    var plr = event.entity.username;

    //set source and type to be read as null to avoid type mismatch
    var source = null
    var sourceType = null

    //debug check damage types
    //event.server.tell(event.source)

    
    
    //Check if damage source has a name, if not, do not define.
    if (event.source.toString() == 'DamageSource (mob)' || event.source.toString() == 'DamageSource (player)') {
        source = event.source.getActual().getName().getString();
        sourceType = event.source.getType().toString();
    }
    else if (event.source.toString() != 'DamageSource (mob)' || event.source.toString() != 'DamageSource (player)') {
        var rawsource = event.source.toString();
        source = rawsource.replace(`DamageSource`,'').replace(' ', '').replace(/[{()}]/g, '');
        sourceType = event.source.getType.toString();
    }

  event.server.tell(Text.red(`${plr} was put into combat by ${source}!`));
  event.server.tell(Text.red(`${plr} was put into combat by type ${sourceType}`));
}
limpid pierBOT
#

Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!

dense grove
#

perferably I'd like to remove both the extra space and parenthesis in DamageSource's output with one .replace but I don't know how I'd manage it

for reference: if player is attacked by mob it'll send the message:
"DamageSource (mob)"

dense grove
#

ex: minecraft:skeleton

#

DamageSource has a really weird generalized pool it pulls from I haven't be able to find yet

#

but it does suit my purposes soooo

quiet haven
#

what is your purpose? I am kinda confused

dense grove
#

differentiating between attacks from players and mobs, and setting a combat tag timer according to what hit

#

this is a just a small snippet

quiet haven
# dense grove ex: minecraft:skeleton

I think you might want to check it again. In my test, event.source.getType() returns player, mob, fall, explosion and things like that. event.source.actual.getType() returns the entity type like minecraft:skeleton instead.

dense grove
dense grove
#

yea it works

#

before I close the ticket, I have a question @quiet haven , do you know how to resolve projectile/explosive attacks? (Witch, Skeleton, Creeper, etc.), because they return null when trying to use actual, their types either return 'magic', 'indirectMagic', 'arrow', or 'player.Explosion' (not in respective order)

quiet haven
#

But that was me using forge, I'm not sure if that's a forge/fabric difference.

dense grove
dense grove
#

I'll have to trial and error some of it but eh

#

as for now I think I'm gonna try and get some rest and try again in a bit

#

thank you for your help

#

if I need anything I'll make a new ticket and ping you