2023-08-06 18:07:12 +00:00
|
|
|
package com.alttd.altitudequests.util;
|
|
|
|
|
|
|
|
|
|
import com.alttd.altitudequests.AQuest;
|
|
|
|
|
import com.alttd.altitudequests.config.Config;
|
|
|
|
|
import com.alttd.altitudequests.objects.Variant;
|
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
|
import org.bukkit.NamespacedKey;
|
|
|
|
|
import org.bukkit.boss.BarColor;
|
|
|
|
|
import org.bukkit.boss.BarStyle;
|
|
|
|
|
import org.bukkit.boss.BossBar;
|
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
|
2023-08-06 18:57:59 +00:00
|
|
|
import java.time.Instant;
|
|
|
|
|
|
|
|
|
|
public class AutoHideBossBar implements Runnable {
|
2023-08-06 18:07:12 +00:00
|
|
|
|
|
|
|
|
private final BossBar bossBar;
|
2023-08-06 18:57:59 +00:00
|
|
|
private final Thread thread = new Thread(this);
|
|
|
|
|
private Instant endTime;
|
2023-08-06 18:07:12 +00:00
|
|
|
|
2023-08-06 18:57:59 +00:00
|
|
|
public AutoHideBossBar(Player player, Variant variant, String part, String title, BarColor barColor) throws Exception {
|
|
|
|
|
NamespacedKey namespacedKeyOne = NamespacedKey.fromString(player.getUniqueId() + variant.getInternalName() + part, AQuest.getInstance());
|
2023-08-06 18:07:12 +00:00
|
|
|
if (namespacedKeyOne == null) {
|
2023-08-06 18:57:59 +00:00
|
|
|
Logger.warning("Unable to create nameSpacedKey with suffix % for quest for %", part, player.getName());
|
2023-08-06 18:07:12 +00:00
|
|
|
throw new Exception("Failed to create namespace key");
|
|
|
|
|
}
|
|
|
|
|
this.bossBar = Bukkit.createBossBar(
|
|
|
|
|
namespacedKeyOne,
|
2023-08-06 18:57:59 +00:00
|
|
|
title,
|
|
|
|
|
barColor,
|
2023-08-06 18:07:12 +00:00
|
|
|
BarStyle.SOLID);
|
|
|
|
|
bossBar.setVisible(false);
|
|
|
|
|
bossBar.addPlayer(player);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-06 18:57:59 +00:00
|
|
|
private synchronized void schedule() {
|
|
|
|
|
endTime = Instant.now().plusSeconds(Config.BOSS_BAR_AUTO_HIDE.toSeconds());
|
|
|
|
|
if (!thread.isAlive())
|
|
|
|
|
thread.start();
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-06 18:07:12 +00:00
|
|
|
public void show(double progress) {
|
|
|
|
|
bossBar.setVisible(true);
|
|
|
|
|
bossBar.setProgress(progress);
|
2023-08-06 18:57:59 +00:00
|
|
|
schedule();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private synchronized boolean waitOrRunTask() {
|
|
|
|
|
if (Instant.now().isBefore(endTime))
|
|
|
|
|
return true;
|
|
|
|
|
bossBar.setVisible(false);
|
|
|
|
|
return false;
|
2023-08-06 18:07:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
2023-08-06 18:57:59 +00:00
|
|
|
while (waitOrRunTask()) {
|
|
|
|
|
try {
|
|
|
|
|
wait(100);
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-06 18:07:12 +00:00
|
|
|
}
|
|
|
|
|
}
|