Skip to content

Commit

Permalink
Fix take items and sounds, bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSzabo committed Nov 6, 2024
1 parent 2ef6471 commit 77ef97e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 53 deletions.
52 changes: 24 additions & 28 deletions src/main/java/gg/auroramc/quests/api/quest/Quest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,7 +32,6 @@ public class Quest {
private final Set<String> taskTypes;
private final Map<String, Task> 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;
Expand All @@ -57,29 +52,22 @@ public String getId() {
return config.getId();
}

public CompletableFuture<Void> 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<CompletableFuture<Void>> 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<String, Object> params) {
Expand Down Expand Up @@ -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);
Expand Down
19 changes: 14 additions & 5 deletions src/main/java/gg/auroramc/quests/api/quest/QuestPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
25 changes: 8 additions & 17 deletions src/main/java/gg/auroramc/quests/api/quest/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> params) {
Expand All @@ -26,27 +25,20 @@ public void setProgress(Player player, double count, Map<String, Object> params)
.setProgress(pool.getId(), holder.getId(), id, count);
}

public CompletableFuture<Void> tryTakeItems(Player player) {
var future = new CompletableFuture<Void>();

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()) {
Expand All @@ -67,11 +59,10 @@ public CompletableFuture<Void> 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() {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/gg/auroramc/quests/menu/PoolMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ quest-complete-message:

quest-complete-sound:
enabled: true
sound: "ENTITY_PLAYER_LEVELUP"
sound: "entity.player.levelup"
volume: 1
pitch: 1

Expand All @@ -69,7 +69,7 @@ level-up-message:

level-up-sound:
enabled: true
sound: "ENTITY_PLAYER_LEVELUP"
sound: "entity.player.levelup"
volume: 1
pitch: 1

Expand Down

0 comments on commit 77ef97e

Please sign in to comment.