#Living Hurt Event on Multiplayer server using player specific persistent data

27 messages · Page 1 of 1 (latest)

fervent tundra
#

Since I drew inspiration from the help you gave that one random person, I wanted to know how this type of script would play out on a multiplayer server? @tropic steppe

ForgeEvents.onEvent('net.minecraftforge.event.entity.living.LivingHurtEvent', event => {
    global.hurtEvent(event)
})

global.hurtEvent = event => {
    const { amount, source: { actual }, source, entity } = event
    if (entity.type == "minecraft:player" || entity.isMonster()) {
        
        Utils.server.players.forEach(player => {
            if (['inFire', 'lava', 'onFire', 'stalagmite', 'thorns', 'explosion.player', 'fall', 'explosion', 'starve', 'dehydrate', 'witherSkull', 'wither', 'magic', 'drown'].includes(source.getType())) {
                let baseMultiplier = 1;
                let healthMultiplier = entity.maxHealth;
                baseMultiplier = player.persistentData?.coef ?? 1;
                baseMultiplier = 1 + (baseMultiplier - 1) / 10;
                let scaledDamage = ((Math.pow(amount, 0.4)) * baseMultiplier * (Math.pow(healthMultiplier, 0.29))).toFixed(1);
                event.setAmount(scaledDamage);
            }
        })
    }
}

For example, how would thinks play out, if 2 players were in the same world/server but they both had individual values of persistentData.coef which is a number between 1 - 20. Would the highest number have a higher priority, therefore always being in charge of the total scaledDamage?

How would I be able to make this only per-player?

misty dragonBOT
#

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

tropic steppe
#

persistent data is an nbt tag unique to the player themselves so even if they were the same tag name and same tag value it will be read separately anyways

fervent tundra
#

What if I wanted the nearest entity being hurt, snatch from the nearest player's baseMultiplier.
Is this possible.

tropic steppe
#

whats the end goal with that? i think i need more context to understand what you mean

fervent tundra
#

When an entity is hurt, be it a isMonster() , then the damage is conform with the player's baseMultiplier meaning player.persistentData.coef - the data being send to the mob by the nearest player around.
If no player is present, baseMultiplier defaults back to 1

#

I wanted to handle this using server.persistentData but I've never used this before. But at the same time, this would mess things up on servers.

tropic steppe
#

instead you could just detect if the target entity is a player right?

#

and if its not a player then put it at 1

fervent tundra
#

is target included in the hurt event?

tropic steppe
#

the target is the event.entity

#

the player whos getting hurt

#

the attacker is event.source.actual

#

if there is an entity attacker that is

fervent tundra
#

Whenever I use source.actual the game crashes

ForgeEvents.onEvent('net.minecraftforge.event.entity.living.LivingHurtEvent', event => {
    global.hurtEvent(event)
})

global.hurtEvent = event => {
    const { amount, source, entity } = event
    
    Utils.server.players.forEach(player => {
        if (entity.type != "minecraft:player") {
            if (['inFire', 'lava', 'onFire', 'stalagmite', 'thorns', 'explosion.player', 'fall', 'explosion', 'starve', 'dehydrate', 'witherSkull', 'wither', 'magic', 'drown'].includes(source.getType())) {
                let baseMultiplier = 1.2;
                let healthMultiplier = entity.maxHealth;
                baseMultiplier = source.persistentData?.coef ?? 1;
                console.log(source.persistentData?.coef ?? 1)
                //console.log(source.actual)
                baseMultiplier = 1 + (baseMultiplier - 1) / 10;
                let scaledDamage = ((Math.pow(amount, 0.4)) * baseMultiplier * (Math.pow(healthMultiplier, 0.29))).toFixed(1);
                event.setAmount(scaledDamage);
            }
        }
        if (entity.type == "minecraft:player") {
            if (['inFire', 'lava', 'onFire', 'stalagmite', 'thorns', 'explosion.player', 'fall', 'explosion', 'starve', 'dehydrate', 'witherSkull', 'wither', 'drown'].includes(source.getType())) {
                let baseMultiplier = player.persistentData?.coef ?? 1;
                let healthMultiplier = entity.maxHealth;
                baseMultiplier = 1 + (baseMultiplier - 1) / 10;
                let scaledDamage = ((Math.pow(amount, 0.4)) * baseMultiplier * (Math.pow(healthMultiplier, 0.29))).toFixed(1);
                event.setAmount(scaledDamage);
            }
        }
    })
}
#

For example, when a creeper gets hit by a poison splash potion, it should get the source (the player) which is what I'm trying to do here.

tropic steppe
#

yeah thats because you have to null check it, js if (!event.source.actual) return

fervent tundra
frank fossilBOT
#

Paste version of scaling_dmg_sources.js from @fervent tundra

tropic steppe
#

why are you doing players.foreach?

#

theres no need if you're already filtering by player

fervent tundra
frank fossilBOT
#

Paste version of scaling_dmg_sources2.js from @fervent tundra

fervent tundra
#

The part it's not logging is the entity hit by player

global.hurtEvent = event => {
    const { amount, source, entity } = event
    
    if (source.actual) console.log('SOURCE: '+source.actual)
}
fervent tundra
#

I figured it out now:

frank fossilBOT
#

Paste version of scaling_dmg_sources3.js from @fervent tundra