diff options
| author | Krakenied <Krakenied1@gmail.com> | 2024-02-23 15:08:57 +0100 |
|---|---|---|
| committer | Leonardo Bishop <13875753+LMBishop@users.noreply.github.com> | 2024-03-09 17:01:12 +0000 |
| commit | 7c8618f960df6b180e10e5edf057f6d22b703ff5 (patch) | |
| tree | abdd4e3fb5ca609c9cbcaf9ffaea55d9b21f360a /bukkit | |
| parent | 7cef6c62ff599ece66398c766308a281c3a67481 (diff) | |
Improve QItemStack placeholder processing
Diffstat (limited to 'bukkit')
| -rw-r--r-- | bukkit/src/main/java/com/leonardobishop/quests/bukkit/menu/itemstack/QItemStack.java | 100 | ||||
| -rw-r--r-- | bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/TaskUtils.java | 7 |
2 files changed, 54 insertions, 53 deletions
diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/menu/itemstack/QItemStack.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/menu/itemstack/QItemStack.java index 3a4171c7..31ff26be 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/menu/itemstack/QItemStack.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/menu/itemstack/QItemStack.java @@ -131,69 +131,67 @@ public class QItemStack { return is; } - public static String processPlaceholders(String s, TaskProgress taskProgress) { - Matcher m = Pattern.compile("\\{([^}]+)}").matcher(s); - while (m.find()) { - String[] parts = m.group(1).split(":"); - if (parts.length > 1) { - if (taskProgress == null) { - continue; - } - if (parts[1].equals("progress")) { - Object progress = taskProgress.getProgress(); - String str; - if (progress instanceof Float || progress instanceof Double || progress instanceof BigDecimal) { - str = String.format("%.2f", progress); - } else { - str = String.valueOf(progress); - } + private static final Pattern taskPlaceholderPattern = Pattern.compile("\\{([^}]+):([^:}]+)}"); - s = s.replace("{" + m.group(1) + "}", (progress == null ? String.valueOf(0) : str)); - } - if (parts[1].equals("complete")) { - String str; - if (taskProgress.isCompleted()) { - str = Chat.legacyColor(Messages.UI_PLACEHOLDERS_TRUE.getMessageLegacyColor()); - } else { - str = Chat.legacyColor(Messages.UI_PLACEHOLDERS_FALSE.getMessageLegacyColor()); - } - s = s.replace("{" + m.group(1) + "}", str); - } - } - } - return s; + public static String processPlaceholders(String s, QuestProgress questProgress) { + return processPlaceholders(s, questProgress, null); } - public static String processPlaceholders(String s, QuestProgress questProgress) { - Matcher m = Pattern.compile("\\{([^}]+)}").matcher(s); - while (m.find()) { - String[] parts = m.group(1).split(":"); - if (parts.length > 1) { - if (questProgress.getTaskProgress(parts[0]) == null) { - continue; - } - if (parts[1].equals("progress")) { - Object progress = questProgress.getTaskProgress(parts[0]).getProgress(); - String str; + public static String processPlaceholders(String s, QuestProgress questProgress, TaskProgress taskProgress) { + Matcher matcher = taskPlaceholderPattern.matcher(s); + + while (matcher.find()) { + TaskProgress matchedTaskProgress; + + String taskIdPart = matcher.group(1); + if (taskProgress != null && taskIdPart.equals("this")) { + matchedTaskProgress = taskProgress; + } else { + matchedTaskProgress = questProgress.getTaskProgress(taskIdPart); + } + + if (matchedTaskProgress == null) { + continue; + } + + String placeholderPart = matcher.group(2); + String replacement; + + switch (placeholderPart) { + // formatted progress placeholders + case "progress" -> { + Object progress = matchedTaskProgress.getProgress(); if (progress instanceof Float || progress instanceof Double || progress instanceof BigDecimal) { - str = String.format("%.2f", progress); + replacement = String.format("%.2f", ((Number) progress).floatValue()); } else { - str = String.valueOf(progress); + replacement = String.valueOf(progress); } - - s = s.replace("{" + m.group(1) + "}", (progress == null ? String.valueOf(0) : str)); } - if (parts[1].equals("complete")) { - String str; - if (questProgress.getTaskProgress(parts[0]).isCompleted()) { - str = Chat.legacyColor(Messages.UI_PLACEHOLDERS_TRUE.getMessageLegacyColor()); + + // completion placeholders + case "complete" -> { + boolean completed = matchedTaskProgress.isCompleted(); + if (completed) { + replacement = Messages.UI_PLACEHOLDERS_TRUE.getMessageLegacyColor(); } else { - str = Chat.legacyColor(Messages.UI_PLACEHOLDERS_FALSE.getMessageLegacyColor()); + replacement = Messages.UI_PLACEHOLDERS_FALSE.getMessageLegacyColor(); } - s = s.replace("{" + m.group(1) + "}", str); } + + // may be particularly useful when using PAPI placeholders in boss bars + case "id" -> replacement = matchedTaskProgress.getTaskId(); + + // set it to null so we can check if there is need to update the matcher + default -> replacement = null; + } + + // update the matcher only if something needs to be replaced + if (replacement != null) { + s = s.substring(0, matcher.start()) + replacement + s.substring(matcher.end()); + matcher = taskPlaceholderPattern.matcher(s); } } + return s; } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/TaskUtils.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/TaskUtils.java index a71d1026..5444b03c 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/TaskUtils.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/TaskUtils.java @@ -153,7 +153,9 @@ public class TaskUtils { return progress; } - public static void sendTrackAdvancement(Player player, Quest quest, Task task, TaskProgress taskProgress, Number amount) { + public static void sendTrackAdvancement(Player player, Quest quest, Task task, PendingTask pendingTask, Number amount) { + TaskProgress taskProgress = pendingTask.taskProgress(); + boolean useActionBar = plugin.getConfig().getBoolean("options.actionbar.progress", false) || (taskProgress.isCompleted() && plugin.getConfig().getBoolean("options.actionbar.complete", false)); boolean useBossBar = plugin.getConfig().getBoolean("options.bossbar.progress", false) @@ -194,7 +196,8 @@ public class TaskUtils { return; // no valid title format found } - title = QItemStack.processPlaceholders(title, taskProgress); + QuestProgress questProgress = pendingTask.questProgress(); + title = QItemStack.processPlaceholders(title, questProgress, taskProgress); boolean usePlaceholderAPI = plugin.getQuestsConfig().getBoolean("options.progress-use-placeholderapi", false); if (usePlaceholderAPI) { |
