From 77ef97e75e8d9bdcc04d93d884df94a5a87a6a69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Szab=C3=B3?= Date: Wed, 6 Nov 2024 15:21:40 +0100 Subject: [PATCH] Fix take items and sounds, bump version --- .../gg/auroramc/quests/api/quest/Quest.java | 52 +++++++++---------- .../auroramc/quests/api/quest/QuestPool.java | 19 +++++-- .../gg/auroramc/quests/api/quest/Task.java | 25 +++------ .../gg/auroramc/quests/menu/PoolMenu.java | 3 +- src/main/resources/config.yml | 4 +- 5 files changed, 50 insertions(+), 53 deletions(-) diff --git a/src/main/java/gg/auroramc/quests/api/quest/Quest.java b/src/main/java/gg/auroramc/quests/api/quest/Quest.java index 8f71af1..b269e5f 100644 --- a/src/main/java/gg/auroramc/quests/api/quest/Quest.java +++ b/src/main/java/gg/auroramc/quests/api/quest/Quest.java @@ -18,15 +18,11 @@ import lombok.Getter; import net.kyori.adventure.text.Component; import org.bukkit.Bukkit; -import org.bukkit.Sound; +import org.bukkit.NamespacedKey; +import org.bukkit.Registry; import org.bukkit.entity.Player; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.atomic.AtomicBoolean; +import java.util.*; import java.util.stream.Collectors; @Getter @@ -36,7 +32,6 @@ public class Quest { private final Set taskTypes; private final Map tasks = Maps.newHashMap(); private final QuestPool holder; - private final AtomicBoolean isTakingItems = new AtomicBoolean(false); public Quest(QuestPool holder, QuestConfig config, RewardFactory rewardFactory) { this.holder = holder; @@ -57,29 +52,22 @@ public String getId() { return config.getId(); } - public CompletableFuture tryTakeItems(Player player) { - if (!taskTypes.contains(TaskType.TAKE_ITEM)) return CompletableFuture.completedFuture(null); + public void tryTakeItems(Player player) { + if (!taskTypes.contains(TaskType.TAKE_ITEM)) return; - if (!holder.isUnlocked(player)) return CompletableFuture.completedFuture(null); - if (!isUnlocked(player)) return CompletableFuture.completedFuture(null); - if (isCompleted(player)) return CompletableFuture.completedFuture(null); - - if (isTakingItems.getAndSet(true)) return CompletableFuture.completedFuture(null); - - List> futures = new ArrayList<>(); + if (!holder.isUnlocked(player)) return; + if (!isUnlocked(player)) return; + if (isCompleted(player)) return; for (var task : tasks.values()) { if (task.getTaskType().equals(TaskType.TAKE_ITEM)) { - futures.add(task.tryTakeItems(player)); + task.tryTakeItems(player); } } - return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).thenRun(() -> { - if (canComplete(player)) { - complete(player); - } - isTakingItems.set(false); - }); + if (canComplete(player)) { + complete(player); + } } public void progress(Player player, String taskType, double count, Map params) { @@ -260,10 +248,18 @@ private void reward(Player player) { if (gConfig.getQuestCompleteSound().getEnabled()) { var sound = gConfig.getQuestCompleteSound(); - player.playSound(player.getLocation(), - Sound.valueOf(sound.getSound().toUpperCase()), - sound.getVolume(), - sound.getPitch()); + var key = NamespacedKey.fromString(sound.getSound()); + if (key != null) { + var realSound = Registry.SOUNDS.get(key); + if (realSound != null) { + player.playSound(player.getLocation(), + realSound, + sound.getVolume(), + sound.getPitch()); + } + } else { + AuroraQuests.logger().warning("Invalid sound key: " + sound.getSound()); + } } RewardExecutor.execute(rewards.values().stream().toList(), player, 1, placeholders); diff --git a/src/main/java/gg/auroramc/quests/api/quest/QuestPool.java b/src/main/java/gg/auroramc/quests/api/quest/QuestPool.java index 5c8c17b..09c82ac 100644 --- a/src/main/java/gg/auroramc/quests/api/quest/QuestPool.java +++ b/src/main/java/gg/auroramc/quests/api/quest/QuestPool.java @@ -16,7 +16,8 @@ import gg.auroramc.quests.util.RomanNumber; import lombok.Getter; import net.kyori.adventure.text.Component; -import org.bukkit.Sound; +import org.bukkit.NamespacedKey; +import org.bukkit.Registry; import org.bukkit.entity.Player; import java.time.Duration; @@ -214,10 +215,18 @@ public void reward(Player player, int level) { if (mc.getLevelUpSound().getEnabled()) { var sound = mc.getQuestCompleteSound(); - player.playSound(player.getLocation(), - Sound.valueOf(sound.getSound().toUpperCase()), - sound.getVolume(), - sound.getPitch()); + var key = NamespacedKey.fromString(sound.getSound()); + if (key != null) { + var realSound = Registry.SOUNDS.get(key); + if (realSound != null) { + player.playSound(player.getLocation(), + realSound, + sound.getVolume(), + sound.getPitch()); + } + } else { + AuroraQuests.logger().warning("Invalid sound key: " + sound.getSound()); + } } RewardExecutor.execute(rewards, player, level, placeholders); diff --git a/src/main/java/gg/auroramc/quests/api/quest/Task.java b/src/main/java/gg/auroramc/quests/api/quest/Task.java index 778bbaa..21d3385 100644 --- a/src/main/java/gg/auroramc/quests/api/quest/Task.java +++ b/src/main/java/gg/auroramc/quests/api/quest/Task.java @@ -9,7 +9,6 @@ import org.bukkit.entity.Player; import java.util.Map; -import java.util.concurrent.CompletableFuture; public record Task(QuestPool pool, Quest holder, TaskConfig config, String id) { public void progress(Player player, double count, Map params) { @@ -26,27 +25,20 @@ public void setProgress(Player player, double count, Map params) .setProgress(pool.getId(), holder.getId(), id, count); } - public CompletableFuture tryTakeItems(Player player) { - var future = new CompletableFuture(); - + public void tryTakeItems(Player player) { if (!TaskManager.getEvaluator(config.getTask()).evaluate(player, config, Map.of())) { - future.complete(null); - return future; + return; } - var itemId = config.getArgs().getString("item"); final var currentAmount = (int) AuroraAPI.getUser(player.getUniqueId()).getData(QuestData.class).getProgression(pool.getId(), holder.getId(), id); final var requiredAmount = config.getArgs().getInt("amount", 1); final var remainingAmount = requiredAmount - currentAmount; - if (itemId == null || remainingAmount <= 0) { - future.complete(null); - return future; + return; } final var typeId = TypeId.fromString(itemId); - - player.getScheduler().run(AuroraQuests.getInstance(), (st) -> { + try { var amountNeeded = remainingAmount; for (var invItem : player.getInventory().getContents()) { @@ -67,11 +59,10 @@ public CompletableFuture tryTakeItems(Player player) { AuroraAPI.getUser(player.getUniqueId()).getData(QuestData.class) .progress(pool.getId(), holder.getId(), id, remainingAmount - amountNeeded); - - future.complete(null); - }, () -> future.complete(null)); - - return future; + } catch (Exception e) { + AuroraQuests.logger().severe("Failed to take items"); + e.printStackTrace(); + } } public String getTaskType() { diff --git a/src/main/java/gg/auroramc/quests/menu/PoolMenu.java b/src/main/java/gg/auroramc/quests/menu/PoolMenu.java index 6e71d99..4651585 100644 --- a/src/main/java/gg/auroramc/quests/menu/PoolMenu.java +++ b/src/main/java/gg/auroramc/quests/menu/PoolMenu.java @@ -129,7 +129,8 @@ private AuroraMenu createMenu() { if (quest.getTaskTypes().contains(TaskType.TAKE_ITEM) && (quest.isUnlocked(player) || !pool.isGlobal()) && !quest.isCompleted(player)) { menu.addItem(builder.build(player), (e) -> { - quest.tryTakeItems(player).thenAccept((result) -> createMenu().open()); + quest.tryTakeItems(player); + createMenu().open(player); }); } else { menu.addItem(builder.build(player)); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 7582da8..516f001 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -51,7 +51,7 @@ quest-complete-message: quest-complete-sound: enabled: true - sound: "ENTITY_PLAYER_LEVELUP" + sound: "entity.player.levelup" volume: 1 pitch: 1 @@ -69,7 +69,7 @@ level-up-message: level-up-sound: enabled: true - sound: "ENTITY_PLAYER_LEVELUP" + sound: "entity.player.levelup" volume: 1 pitch: 1