#Tone down difficulty when dying too much

66 messages · Page 1 of 1 (latest)

sacred heath
vivid roostBOT
#

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

olive wharf
#

It seems difficulty effects all based on position? Weird, but it’s possible to decrease via DifficultyData.

I’ll check more into this later and provide code on how to do so

sacred heath
#

Alrighty

olive wharf
# sacred heath Alrighty

am i correct that difficulty effect all players, or is it position based? i'm still confused
ahh difficulty is based on position from spawn

olive wharf
#

we can try this one if it doesn't work then position based.

const $DifficultyData = Java.loadClass('io.github.flemmli97.improvedmobs.difficulty.DifficultyData');

EntityEvents.death('minecraft:player', event => {
  let player = event.getEntity();
  let difficultyData = $DifficultyData.get(event.server);
  let difficultyLevel = difficultyData.getDifficulty(event.level, player);
  let difficultyDistance = $DifficultyData.getDifficultyFromDist(event.level, player.position);
  difficultyData.setDifficulty(Math.max(0, difficultyLevel - difficultyDistance), event.server);
  // difficultyData.save();
})
#

confusing because you can get the difficulty from a specific level, but you can't set the difficulty to that level, only overworld

#

at first i was getting the difficulty and difficulty distance from event.server but replaced it with event.level.
i'm not sure what to use.

#

hopefully this works. i haven't tested but the code is what it should be

#

and not sure how much of the difficulty should be decreased. set it back to 0 if died? feel fre to change.

sacred heath
#

so the mod has a difficulty for each player

#

my goal was to make it so whenever you died, you would decrease the difficulty a little bit

#

@olive wharf

olive wharf
#

i just don't see a way to do it per player

sacred heath
#

i have wrote this but its kind of shitty i wont lie

#
// Persistent storage for tracking deaths
let death_tracker = {};
function initializePlayerData(player_name) {
    return {
        deaths: 0,
        difficulty: -4 // Initial difficulty
    };
}

// Function to update the player's difficulty based on death count
function updatePlayerDifficulty(player, player_name) {
    let player_data = death_tracker[player_name];

    // Increment death count
    player_data.deaths++;

    // Calculate the exponential difficulty multiplier and decrease amount
    const death_multiplier = Math.pow(2, player_data.deaths - 1);
    const decrease_amount = -1 / death_multiplier;

    // Update difficulty, ensuring it doesn't go below -2
    const new_difficulty = Math.max(-2, player_data.difficulty - decrease_amount);
    player_data.difficulty = new_difficulty;

    // Run the command to update the player's difficulty
    player.server.runCommandSilent(`/improvedmobs difficulty player ${player_name} add ${new_difficulty.toFixed(2)}`);

    // Notify the player (for debugging purposes) 
    player.tell(`Your death has decreased your Improved Mobs difficulty to ${new_difficulty.toFixed(2)}.`);
}

// Event listener for player death
EntityEvents.death(event => {
    let player = event.player;

    // Ensure the player exists before continuing
    if (!player || !player.username) {
        return;  // Exit the function if the player is null or username is not available
    }

    let player_name = player.username;

    // Initialize player data if it doesn't exist
    if (!death_tracker[player_name]) {
        death_tracker[player_name] = initializePlayerData(player_name);
    }

    // Update the player's difficulty after death
    updatePlayerDifficulty(player, player_name);
});```
#

like this works, but its going below 0 difficulty, like into the negatives

#

and i dont want that, i want the difficulty to stop decreasing alltogether when it reaches 0

olive wharf
#

"ensuring it doesn't go below -2"

#

that was your intent to not go below -2 lol

#

Math.max(0, player_data.difficulty - decrease_amount) in that case

sacred heath
#

that doesnt even work

#

i kept that there to see if it even worked

#

and it didnt

#

it goes even below -2

olive wharf
#

ok looking at the command source i kindof get it now

sacred heath
olive wharf
#
const $CrossPlatformStuff = Java.loadClass('io.github.flemmli97.improvedmobs.platform.CrossPlatformStuff');

EntityEvents.death('minecraft:player', event => {
  let player = event.getEntity();
  if (!death_tracker[player.username]) {
    death_tracker[player.username] = initializePlayerData(player.username);
  }
  
  death_tracker[player.username].deaths += 1;
  let death_multiplier = Math.pow(2, death_tracker[player.username].deaths - 1);
  let decrease_amount = -1 / death_multiplier;
  death_tracker[player.username].difficulty = Math.max(0, death_tracker[player.username].difficulty - decrease_amount)

  let iplayer = $CrossPlatformStuff.INSTANCE.getPlayerDifficultyData(player);
  // let difficultyLevel = iplayer.getDifficultyLevel()
  iplayer.setDifficultyLevel(death_tracker[player.username].difficulty)
  iplayer.save();

  player.tell(`Your death has decreased your Improved Mobs difficulty to ${death_tracker[player.username].difficulty.toFixed(2)}.`);
})
#

apparently via config the difficulty can be global or per player.

sacred heath
#

im aware of that

#

the issue however is that once it increases, it cant be decreased whatsoever

olive wharf
#

that's how you get and set the difficulty. now how much to decrease it by?

#

do you want to decrease based on the position from spawn still?

#

decreased multiplier?

#

fraction of difficulty?

sacred heath
#

how i made it is basically starts from a flat 2, to then decrease for each death

#

so 2, 1.5, 1.25 and so on

#

doesnt take into consideration anything

olive wharf
#

right so the way you have it is -1 / playerDeaths

sacred heath
#

approx, yes

olive wharf
#

-1, -0.5, -0.3, etc

sacred heath
#

yeah

olive wharf
#

there i updated code above. is that good?

#

updated again

#

i took account for the original code you had

#

should also not go negative either and properly decreases.

#

oh and last thing

#

i see why it goes negative. you're storing your own difficulty (-4 initially) instead of taking into account the current difficulty.
this will constantly keep going negative below -4. (well with the new code, difficulty will always be 0 since it's death_tracker.death will always be negative)

#

think you want to initializePlayerData with the current difficulty level (not -4) before decreasing

#

i don't know how much the level increases overtime to decide how much it needs to be decreased when player dies.

#

that's why i thought a decrease multiplier from the difficulty level the mod has would be better than using your own difficulty level?

#

getting rid of death_tracker entirely

#

instead maybe something like this? this will half the difficulty level each death and will not go below 0

const $CrossPlatformStuff = Java.loadClass('io.github.flemmli97.improvedmobs.platform.CrossPlatformStuff');

EntityEvents.death('minecraft:player', event => {
  let player = event.getEntity();
  let iplayer = $CrossPlatformStuff.INSTANCE.getPlayerDifficultyData(player);
  let new_difficulty = Math.max(0, iplayer.getDifficultyLevel() * 0.5)
  iplayer.setDifficultyLevel(new_difficulty);
  iplayer.save();
  player.tell(`Your death has decreased your Improved Mobs difficulty to ${new_difficulty}.`);
})
#

and also maybe check if the player actually dies by a mob before decreasing?

#

but you have the code now to get and set difficulty level. you can adjust to your likings how you want.

sacred heath
#

so which one should i be testing exactly?

sacred heath
#

alright, i fixed it

#

thank you, this code does exactly what i want it to do

#

i appreciate the help immensely!

sacred heath