@Su37Erich: Here is an example on how to create a hp reset after 40 seconds. It is important that you only have one instance of a timer for each player active. Because when the player dies twice in these 40 seconds, the timer will be started multiple times. It is also important that you check that the player is still connected after these 40 seconds. In this example, I will use the PlayerLifecycleHolder:
PHP код:
class UserData extends PlayerLifecycleObject {
private static final int HEALTH_RESET_INTERVAL = 40000; //40 secs
private Timer healthReset;
public UserData(EventManager eventManager, Player player) {
super(eventManager, player);
}
@Override
protected void onInit() {
eventManagerNode.registerHandler(PlayerDeathEvent.class, HandlerPriority.NORMAL,
Attentions.create().object(player), deathEvent -> {
if(deathEvent.getPlayer() != player) return; //The event will maybe fire when the killer is the player, and we don't want that. We only want this event if the died player is the player.
if (healthReset != null && healthReset.isRunning())
healthReset.stop(); //Stop previous timer if it was running
healthReset = Timer.create(HEALTH_RESET_INTERVAL, 1, i -> {
if (player.isOnline()) {
player.setHealth(100f);
}
healthReset = null;
});
healthReset.start();
EventManagerNode spawnNode = eventManagerNode.createChildNode();
spawnNode.registerHandler(PlayerSpawnEvent.class, HandlerPriority.NORMAL,
Attentions.create().object(player), spawnEvent -> {
player.setHealth(100000f);
spawnNode.destroy();
});
});
}
@Override
protected void onDestroy() {
}
}
Let me explain what this code is doing:
When the UserData class gets initialized by the PlayerLifecycleHolder, a new event handler for the PlayerDeath event is registered, but only for the specific player (look at the Attentions.create().object(player) part. The "player" field gets inherited from the PlayerLifecycleObject superclass). When the event gets fired, it will check if a healthReset timer is already running, and if it is, it will be stopped. After that, a new timer will be created that will fire once after 40 seconds. When the timer gets fired, it will check if the player is still connected, and if it is, it will set the health of the player to 100 HP. After that, the timer will set itself to null, just to mark it as "not in use". When the timer has been setted up in the DeathEvent, it will be started. But we also want that the player's health will be set to 10000 HP when he spawns again, so we register a new event manager node with a new handler for the PlayerSpawnEvent but only for the specific player (see Attentions.create()....). In the event, the health of the player will be set to 10000 HP and after that, the handler will destroy itself.