#Converting excess effect intensity into duration

63 messages · Page 1 of 1 (latest)

sinful lagoon
#

I want to be able to make it so if you have an effect whose intensity surpasses a certain number, it's intensity is set to that number and the duration is increased based on how much you lost

Is this even possible with kubeJS?

sullen urchinBOT
#

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

sinful lagoon
#

-# bump

restive pollen
#

yes it should be possible but it might need tick events

#

if there's an event when an effect is applied or changed then that makes things a little easier

sinful lagoon
restive pollen
#

I don't even use the wiki when making scripts lol

sinful lagoon
restive pollen
#

it's gives code hints/autocompelte

#

only for vscode editor

sinful lagoon
#

huh
thanks

restive pollen
#

it's highly highly recommended

#

However it is not 100% accurate on what is possible to autocomplete so check the forge packages to make sure

sinful lagoon
#

yeah it also says it requires 1.21

mossy sky
#

the latest version does I'm sure, but there are older versions

sinful lagoon
#

nvm

sinful lagoon
#

okay anything seem wrong with this script before i test it?

const maxIntensity = 9;
const durationPer = 30

PlayerEvents.tick(event => {
    const player = event.player;
    player.activeEffects.forEach(effect => {
        //Return if the potion effect is below the bound
        if (effect.amplifier <= maxIntensity) return
        let nbt = effect.save({})
        console.log("Old: " + nbt)
        //new duration and level
        nbt.Duration = NBT.b(nbt.Duration + durationPer * (nbt.Amplifier - maxIntensity))
        //apply effect
        nbt.Amplifier = NBT.b(maxIntensity)
        let newInstance = effect.load(nbt)
        console.log("New: " + newInstance.save({}))
        player.forceAddEffect(newInstance, player)
    })
});
#

side question, does forceAddEffect() override current effect or do i need to clear the current one first because itll have higher intensity?

restive pollen
#

note that using tick events will cause lag on high population servers

sinful lagoon
restive pollen
#

did you see if there was an event for effects being applied?

sinful lagoon
restive pollen
#

if you realy can't find any then your script should be fine

mossy sky
#

You can use these forge events to have more granular control over the effect life cycle

    event => {
        global.mob_effect_added(event);
    }
)

ForgeEvents.onEvent("net.minecraftforge.event.entity.living.MobEffectEvent$Applicable",
    event => {
        global.mob_effect_applicable(event);
    }
)

ForgeEvents.onEvent("net.minecraftforge.event.entity.living.MobEffectEvent$Expired",
    event => {
        global.mob_effect_expired(event);
    }
)

ForgeEvents.onEvent("net.minecraftforge.event.entity.living.MobEffectEvent$Remove",
    event => {
        global.mob_effect_remove(event);
    }
)```
sinful lagoon
mossy sky
sinful lagoon
#

like do this?
replace the event with that one and do event.entity instead of event.player?

const maxIntensity = 9;
const durationPer = 30

ForgeEvents.onEvent("net.minecraftforge.event.entity.living.MobEffectEvent$Applicable", event => {
    const player = event.entity;
    player.activeEffects.forEach(effect => {
        //Return if the potion effect is below the bound
        if (effect.amplifier <= maxIntensity) return
        let nbt = effect.save({})
        console.log("Old: " + nbt)
        //new duration and level
        nbt.Duration = NBT.b(nbt.Duration + durationPer * (nbt.Amplifier - maxIntensity))
        //apply effect
        nbt.Amplifier = NBT.b(maxIntensity)
        let newInstance = effect.load(nbt)
        console.log("New: " + newInstance.save({}))
        player.forceAddEffect(newInstance, player)
    })
});
sinful lagoon
mossy sky
#

the main difference in this case is just when in the chain of events they happen. the way it could matter in a use case like this is that if you use Added instead of Applicable, technically an effect with an amplifier greater than the max allowed value will still be applied for ~a tick, whereas if you use Applicable then you can ensure that effects that actually get applied to the player are only ever within the range you want

#

functionally it might not matter, but it could

sinful lagoon
#

probably want that then yeah

mossy sky
sinful lagoon
mossy sky
#

event.entity.isPlayer() should do

sinful lagoon
#

so this?

const maxIntensity = 9;
const durationPer = 30

ForgeEvents.onEvent("net.minecraftforge.event.entity.living.MobEffectEvent$Applicable", event => {
    const player = event.player;
    if (!player.isPlayer()) return
    player.activeEffects.forEach(effect => {
        //Return if the potion effect is below the bound
        if (effect.amplifier <= maxIntensity) return
        let nbt = effect.save({})
        console.log("Old: " + nbt)
        //new duration and level
        nbt.Duration = NBT.b(nbt.Duration + durationPer * (nbt.Amplifier - maxIntensity))
        //apply effect
        nbt.Amplifier = NBT.b(maxIntensity)
        let newInstance = effect.load(nbt)
        console.log("New: " + newInstance.save({}))
        player.forceAddEffect(newInstance, player)
    })
});
mossy sky
#

oh also I should mention that when you hook into forge events like this the code has to go in a startup script. that's why I pass the event to a global method so that you can put that global method in a server script and reload / test without restarting the whole game

sinful lagoon
mossy sky
#

yep 🙂‍↕️ that looks like it should do

sinful lagoon
#

also i forget how for each works in js,
should i be using a continue instead of return for the if <= maxIntensity?

#

would return end the forEach early or no?

restive pollen
#

continue you want to end one loop

#

return is stop entire loop

sinful lagoon
#

yes and does forEach count as a loop?

#

or no

restive pollen
#

yes

sinful lagoon
#

uhh probeJS says thats not right

mossy sky
#

you can't continue in forEach

#

in JS

restive pollen
#

oh yeah forgor about that

#

if you were to use a normal for loop then yes you would use continue

sinful lagoon
#

so then how do i make it skip that effect but still parse the others?

restive pollen
#

return

mossy sky
#

return, or set up a traditional loop

sinful lagoon
#

okay so it isnt a loop

mossy sky
#

yeah it's technically different

sinful lagoon
#

last question then i think im good for now

#

is the duration recorded in ticks or seconds?