Attempting to fix thread issues, temporarily disabled them

This commit is contained in:
Teriuihi 2023-08-06 20:57:59 +02:00
parent b8114ba0d3
commit e93a922cc8
2 changed files with 41 additions and 23 deletions

View File

@ -13,6 +13,7 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.bukkit.Bukkit;
import org.bukkit.boss.BarColor;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
@ -50,10 +51,8 @@ public abstract class Quest {
private boolean isDone;
private boolean rewardReceived;
private final int amount;
// private final BossBar barProgressOne;
// private final BossBar barProgressTwo;
private final AutoHideBossBar barProgressOne;
private final AutoHideBossBar barProgressTwo;
// private final AutoHideBossBar barProgressOne;
// private final AutoHideBossBar barProgressTwo;
public static synchronized void putDailyQuest(UUID uuid, Quest quest) {
dailyQuests.put(uuid, quest);
@ -94,8 +93,8 @@ public abstract class Quest {
this.amount = variant.calculateAmount(loadQuestsDoneThisMonth(uuid));
else
this.amount = amount;
this.barProgressOne = new AutoHideBossBar(player, variant, "1");
this.barProgressTwo = new AutoHideBossBar(player, variant, "2");
// this.barProgressOne = new AutoHideBossBar(player, variant, "1", "Step One Progress", BarColor.GREEN);
// this.barProgressTwo = new AutoHideBossBar(player, variant, "2", "Step Two Progress", BarColor.PURPLE);
}
private int loadQuestsDoneThisMonth(UUID uuid) {
@ -184,6 +183,7 @@ public abstract class Quest {
Class<? extends Quest> aClass = any.get();
Constructor<? extends Quest> constructor;
try {
Player player = Bukkit.getPlayer(uuid);
if (Config.DEBUG) {
Logger.info("quest: %, uuid: %, step1: %, step2: %, variant: %, amount: %, turnedIn:%",
quest,
@ -194,8 +194,8 @@ public abstract class Quest {
String.valueOf(amount),
String.valueOf(turnedIn));
}
constructor = aClass.getConstructor(UUID.class, int.class, int.class, String.class, int.class, boolean.class);
Quest quest1 = constructor.newInstance(uuid, step_1_progress, step_2_progress, quest_variant, amount, turnedIn);
constructor = aClass.getConstructor(Player.class, int.class, int.class, String.class, int.class, boolean.class);
Quest quest1 = constructor.newInstance(player, step_1_progress, step_2_progress, quest_variant, amount, turnedIn);
putDailyQuest(uuid, quest1);
} catch (NoSuchMethodException | IllegalAccessException | InstantiationException |
InvocationTargetException e) {
@ -303,7 +303,7 @@ public abstract class Quest {
public void addStep1(int step1) {
this.step1 += step1;
barProgressOne.show(((double) getAmount()) / getStep1());
// barProgressOne.show(((double) getStep1()) / getAmount());
}
public int getStep2() {
@ -312,7 +312,7 @@ public abstract class Quest {
public void addStep2(int step2) {
this.step2 += step2;
barProgressTwo.show(((double) getAmount()) / getStep2());
// barProgressTwo.show(((double) getStep2()) / getAmount());
}
public void setDone(boolean done) {

View File

@ -9,39 +9,57 @@ import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitScheduler;
public class AutoHideBossBar extends BukkitRunnable {
import java.time.Instant;
public class AutoHideBossBar implements Runnable {
private final BossBar bossBar;
private final Thread thread = new Thread(this);
private Instant endTime;
public AutoHideBossBar(Player player, Variant variant, String suffix) throws Exception {
NamespacedKey namespacedKeyOne = NamespacedKey.fromString(player.getUniqueId() + variant.getInternalName() + suffix, AQuest.getInstance());
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());
if (namespacedKeyOne == null) {
Logger.warning("Unable to create nameSpacedKey with suffix % for quest for %", suffix, player.getName());
Logger.warning("Unable to create nameSpacedKey with suffix % for quest for %", part, player.getName());
throw new Exception("Failed to create namespace key");
}
this.bossBar = Bukkit.createBossBar(
namespacedKeyOne,
"Step One Progress",
BarColor.GREEN,
title,
barColor,
BarStyle.SOLID);
bossBar.setVisible(false);
bossBar.addPlayer(player);
}
private synchronized void schedule() {
endTime = Instant.now().plusSeconds(Config.BOSS_BAR_AUTO_HIDE.toSeconds());
if (!thread.isAlive())
thread.start();
}
public void show(double progress) {
BukkitScheduler scheduler = Bukkit.getScheduler();
if (scheduler.isQueued(this.getTaskId()))
scheduler.cancelTask(this.getTaskId());
bossBar.setVisible(true);
bossBar.setProgress(progress);
this.runTaskLater(AQuest.getInstance(), Config.BOSS_BAR_AUTO_HIDE.getSeconds() * 20);
schedule();
}
private synchronized boolean waitOrRunTask() {
if (Instant.now().isBefore(endTime))
return true;
bossBar.setVisible(false);
return false;
}
@Override
public void run() {
bossBar.setVisible(false);
while (waitOrRunTask()) {
try {
wait(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}