From 181f4e0dec381966e2041834e3699ae58aeacbdf Mon Sep 17 00:00:00 2001 From: PikaMug <2267126+PikaMug@users.noreply.github.com> Date: Tue, 14 May 2024 18:16:24 -0400 Subject: [PATCH] Expose potion level for 1.9+, part 3. See #2205 --- .../pikamug/quests/player/BukkitQuester.java | 35 +++++++++---------- .../pikamug/quests/util/BukkitItemUtil.java | 27 ++++++++++++-- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/core/src/main/java/me/pikamug/quests/player/BukkitQuester.java b/core/src/main/java/me/pikamug/quests/player/BukkitQuester.java index c2e68ac42..c64bd895f 100644 --- a/core/src/main/java/me/pikamug/quests/player/BukkitQuester.java +++ b/core/src/main/java/me/pikamug/quests/player/BukkitQuester.java @@ -74,7 +74,6 @@ import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.EnchantmentStorageMeta; -import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.material.Crops; import org.jetbrains.annotations.NotNull; @@ -1316,6 +1315,12 @@ public class BukkitQuester implements Quester { if (formatNames) { message = message.replace("", BukkitItemUtil.getName(goal)); } + final String level = BukkitItemUtil.getPrettyPotionLevel(goal.getItemMeta()); + if (level.isEmpty()) { + message = message.replace(" ", level); + } else { + message = message.replace("", level); + } objectives.add(new BukkitObjective(ObjectiveType.BREW_ITEM, message, progress, goal)); } brewIndex++; @@ -1603,16 +1608,12 @@ public class BukkitQuester implements Quester { } else if (localeManager != null && settings.canTranslateNames() && goal.getItemMeta() != null && !goal.getItemMeta().hasDisplayName() && !goal.getType().equals(Material.WRITTEN_BOOK)) { // Bukkit version is 1.9+ and item lacks custom name - if (goal.getType().equals(Material.POTION) && localeManager.hasBasePotionData()) { - final PotionMeta meta = (PotionMeta) goal.getItemMeta(); - if (meta != null && meta.getBasePotionData().isUpgraded()) { - final int level = meta.getBasePotionData().getType().name().contains("SLOWNESS") ? 4 : 2; - message = message.replace("", ChatColor.GREEN + RomanNumeral.getNumeral(level) - + ChatColor.RESET); - } else if (meta != null && meta.getBasePotionData().isExtended()) { - message = message.replace("", ChatColor.GREEN + "+" + ChatColor.RESET); + if (goal.getType().name().contains("POTION") && localeManager.hasBasePotionData()) { + final String level = BukkitItemUtil.getPrettyPotionLevel(goal.getItemMeta()); + if (level.isEmpty()) { + message = message.replace(" ", level); } else { - message = message.replace(" ", ""); + message = message.replace("", level); } } localeManager.sendMessage(quester.getPlayer(), message, goal); @@ -3433,16 +3434,12 @@ public class BukkitQuester implements Quester { String message = formatCompletedObjectiveMessage("brewItem", goal.getAmount()); if (plugin.getConfigSettings().canTranslateNames() && is.hasItemMeta() && !is.getItemMeta().hasDisplayName()) { // Bukkit version is 1.9+ - if (goal.getType().equals(Material.POTION) && plugin.getLocaleManager().hasBasePotionData()) { - final PotionMeta meta = (PotionMeta) goal.getItemMeta(); - if (meta != null && meta.getBasePotionData().isUpgraded()) { - final int level = meta.getBasePotionData().getType().name().contains("SLOWNESS") ? 4 : 2; - message = message.replace("", ChatColor.GREEN + RomanNumeral.getNumeral(level) - + ChatColor.RESET); - } else if (meta != null && meta.getBasePotionData().isExtended()) { - message = message.replace("", ChatColor.GREEN + "+" + ChatColor.RESET); + if (goal.getType().name().contains("POTION") && plugin.getLocaleManager().hasBasePotionData()) { + final String level = BukkitItemUtil.getPrettyPotionLevel(goal.getItemMeta()); + if (level.isEmpty()) { + message = message.replace(" ", level); } else { - message = message.replace(" ", ""); + message = message.replace("", level); } } if (!plugin.getLocaleManager().sendMessage(p, message, goal.getType(), goal.getDurability(), diff --git a/core/src/main/java/me/pikamug/quests/util/BukkitItemUtil.java b/core/src/main/java/me/pikamug/quests/util/BukkitItemUtil.java index bd98f98ab..6e4d4c84e 100644 --- a/core/src/main/java/me/pikamug/quests/util/BukkitItemUtil.java +++ b/core/src/main/java/me/pikamug/quests/util/BukkitItemUtil.java @@ -668,11 +668,11 @@ public class BukkitItemUtil { /** * Gets player-friendly name from enchantment. 'FIRE_ASPECT' becomes 'Fire Aspect' * - * @param e Enchantment to get pretty localized name of + * @param enchant Enchantment to get pretty localized name of * @return pretty localized name */ - public static String getPrettyEnchantmentName(final Enchantment e) { - final String baseString = e.getName(); + public static String getPrettyEnchantmentName(final Enchantment enchant) { + final String baseString = enchant.getName(); final String[] substrings = baseString.split("_"); String prettyString = ""; int size = 1; @@ -775,4 +775,25 @@ public class BukkitItemUtil { } return getEnchantmentFromProperName(enchant); } + + /** + * Gets player-friendly level from 1.9+ Potion meta. Upgrade returns numeral, extended returns '+' + * + * @param itemMeta Potion meta to get pretty localized level of + * @return pretty localized level or empty + */ + public static String getPrettyPotionLevel(final ItemMeta itemMeta) { + String prettyString = ""; + if (Material.getMaterial("LINGERING_POTION") == null) { + return prettyString; + } + final PotionMeta meta = (PotionMeta) itemMeta; + if (meta != null && meta.getBasePotionData().isUpgraded()) { + final int level = meta.getBasePotionData().getType().name().contains("SLOWNESS") ? 4 : 2; + prettyString = ChatColor.GREEN + RomanNumeral.getNumeral(level) + ChatColor.RESET; + } else if (meta != null && meta.getBasePotionData().isExtended()) { + prettyString = ChatColor.GREEN + "+" + ChatColor.RESET; + } + return prettyString; + } }