#Saving potion effects on death and giving them back on respawn

1 messages · Page 1 of 1 (latest)

narrow lodge
#

so im creating a plugin that saves potion effects on death and gives them back when you respawn
my current code:


@EventHandler
    public void onPlayerDeath(EntityDeathEvent event) {
        if (event.getEntity() instanceof Player) {
            Player player = (Player) event.getEntity();
            List<PotionEffect> effects = player.getActivePotionEffects().stream().collect(Collectors.toList());
            playerEffects.put(player.getUniqueId(), effects);
            getLogger().info("Saved potion effects for player " + player.getName() + ": " + effects);
        }
    }

    @EventHandler
    public void onPlayerRespawn(PlayerRespawnEvent event) {
        Player player = event.getPlayer();
        List<PotionEffect> effects = playerEffects.getOrDefault(player.getUniqueId(), new ArrayList<>());
        for (PotionEffect effect : effects) {
            player.addPotionEffect(effect);
        }
        playerEffects.remove(player.getUniqueId());
        getLogger().info("Restored potion effects for player " + player.getName() + ": " + effects);
    }

it does print that it restored effects in the console but i don't get the effects in game

mossy vine
#

apply the effect one tick later

#

try if that works

narrow lodge
#

ok

muted marlin
#

for me it was 2 ticks

#

one should work tho

mossy vine
#

the respawnevent gets called before the player is actually respawned

#

so yeah, delaying it should fix it

#

please report back whether delaying it worked @narrow lodge

narrow lodge
#

ty it works

#

didn't knew the event is executed before the player respawns, ty for your time

mossy vine
#

great! np

mossy vine
#

but yeah that's how it is

narrow lodge
#

i made it to wait for 1 second just in case

mossy vine
#

one tick should be enough

#

the player is respawned in the same tick that the event gets called