From 7c7b269e7d1e1d05a3521c58693fb84eca2310af Mon Sep 17 00:00:00 2001 From: Zrips Date: Mon, 8 Aug 2022 12:51:46 +0300 Subject: [PATCH] Quest detailed progress being reset after plugin reload fix --- .../gamingmesh/jobs/container/JobItems.java | 230 +++++++++--------- .../jobs/container/QuestObjective.java | 34 +-- .../jobs/container/QuestProgression.java | 225 ++++++++++------- 3 files changed, 265 insertions(+), 224 deletions(-) diff --git a/src/main/java/com/gamingmesh/jobs/container/JobItems.java b/src/main/java/com/gamingmesh/jobs/container/JobItems.java index b77e8247..d0e91490 100644 --- a/src/main/java/com/gamingmesh/jobs/container/JobItems.java +++ b/src/main/java/com/gamingmesh/jobs/container/JobItems.java @@ -58,188 +58,188 @@ public class JobItems { private int untilLevel = Integer.MAX_VALUE; public JobItems(String node, CMIMaterial mat, int amount, String name, List lore, Map enchants, BoostMultiplier boostMultiplier, List jobs) { - this(node, mat, amount, name, lore, enchants, boostMultiplier, jobs, null, null); + this(node, mat, amount, name, lore, enchants, boostMultiplier, jobs, null, null); } public JobItems(String node, CMIMaterial mat, int amount, String name, List lore, Map enchants, BoostMultiplier boostMultiplier, List jobs, - Object potion, Color leatherColor) { - if (mat == null) { - mat = CMIMaterial.STONE; - } + Object potion, Color leatherColor) { + if (mat == null) { + mat = CMIMaterial.STONE; + } - if (enchants != null) { - this.enchants.putAll(enchants); - } + if (enchants != null) { + this.enchants.putAll(enchants); + } - this.node = node; + this.node = node; - if (boostMultiplier != null) { - this.boostMultiplier = boostMultiplier; - } + if (boostMultiplier != null) { + this.boostMultiplier = boostMultiplier; + } - setJobs(jobs); + setJobs(jobs); - ItemMeta meta = (item = mat.newItemStack()).getItemMeta(); - if (potion != null && meta instanceof PotionMeta && CMIMaterial.isPotion(mat.getMaterial())) { - PotionMeta potionMeta = (PotionMeta) meta; + ItemMeta meta = (item = mat.newItemStack()).getItemMeta(); + if (potion != null && meta instanceof PotionMeta && CMIMaterial.isPotion(mat.getMaterial())) { + PotionMeta potionMeta = (PotionMeta) meta; - if (Version.isCurrentEqualOrHigher(Version.v1_10_R1) && potion instanceof org.bukkit.potion.PotionData) { - potionMeta.setBasePotionData((org.bukkit.potion.PotionData) potion); - } else if (potion instanceof org.bukkit.potion.Potion) { - PotionEffectType effectType = ((org.bukkit.potion.Potion) potion).getType().getEffectType(); + if (Version.isCurrentEqualOrHigher(Version.v1_10_R1) && potion instanceof org.bukkit.potion.PotionData) { + potionMeta.setBasePotionData((org.bukkit.potion.PotionData) potion); + } else if (potion instanceof org.bukkit.potion.Potion) { + PotionEffectType effectType = ((org.bukkit.potion.Potion) potion).getType().getEffectType(); - if (effectType != null) { - potionMeta.setMainEffect(effectType); - } - } + if (effectType != null) { + potionMeta.setMainEffect(effectType); + } + } - meta = potionMeta; - } else if (leatherColor != null && meta instanceof LeatherArmorMeta && CMIMaterial.isLeatherArmor(mat.getMaterial())) { - LeatherArmorMeta armorMeta = (LeatherArmorMeta) meta; - armorMeta.setColor(this.leatherColor = leatherColor); - meta = armorMeta; - } + meta = potionMeta; + } else if (leatherColor != null && meta instanceof LeatherArmorMeta && CMIMaterial.isLeatherArmor(mat.getMaterial())) { + LeatherArmorMeta armorMeta = (LeatherArmorMeta) meta; + armorMeta.setColor(this.leatherColor = leatherColor); + meta = armorMeta; + } - if (meta != null) { - if (name != null) - meta.setDisplayName(CMIChatColor.translate(name)); + if (meta != null) { + if (name != null) + meta.setDisplayName(CMIChatColor.translate(name)); - if (lore != null) - meta.setLore(lore); + if (lore != null) + meta.setLore(lore); - if (enchants != null) { - if (mat == CMIMaterial.ENCHANTED_BOOK) { - EnchantmentStorageMeta bookMeta = (EnchantmentStorageMeta) meta; - for (Entry oneEnch : enchants.entrySet()) { - bookMeta.addStoredEnchant(oneEnch.getKey(), oneEnch.getValue(), true); - } - } else { - for (Entry oneEnchant : enchants.entrySet()) { - meta.addEnchant(oneEnchant.getKey(), oneEnchant.getValue(), true); - } - } - } + if (enchants != null) { + if (mat == CMIMaterial.ENCHANTED_BOOK) { + EnchantmentStorageMeta bookMeta = (EnchantmentStorageMeta) meta; + for (Entry oneEnch : enchants.entrySet()) { + bookMeta.addStoredEnchant(oneEnch.getKey(), oneEnch.getValue(), true); + } + } else { + for (Entry oneEnchant : enchants.entrySet()) { + meta.addEnchant(oneEnchant.getKey(), oneEnchant.getValue(), true); + } + } + } - item.setItemMeta(meta); - } + item.setItemMeta(meta); + } - item.setAmount(amount); - CMINBT nbt = new CMINBT(item); - item = (ItemStack) nbt.setString("JobsItemBoost", node); + item.setAmount(amount); + CMINBT nbt = new CMINBT(item); + item = (ItemStack) nbt.setString("JobsItemBoost", node); } public String getNode() { - return node; + return node; } public ItemStack getItemStack(Player player) { - if (player == null) - return item; + if (player == null) + return item; - ItemStack item = this.item.clone(); - ItemMeta meta = item.getItemMeta(); - if (meta == null) { - return item; - } + ItemStack item = this.item.clone(); + ItemMeta meta = item.getItemMeta(); + if (meta == null) { + return item; + } - if (potion != null && CMIMaterial.isPotion(item.getType()) && meta instanceof PotionMeta) { - PotionMeta potionMeta = (PotionMeta) meta; + if (potion != null && CMIMaterial.isPotion(item.getType()) && meta instanceof PotionMeta) { + PotionMeta potionMeta = (PotionMeta) meta; - if (Version.isCurrentEqualOrHigher(Version.v1_10_R1) && potion instanceof org.bukkit.potion.PotionData) { - potionMeta.setBasePotionData((org.bukkit.potion.PotionData) potion); - } else if (potion instanceof org.bukkit.potion.Potion) { - PotionEffectType effectType = ((org.bukkit.potion.Potion) potion).getType().getEffectType(); + if (Version.isCurrentEqualOrHigher(Version.v1_10_R1) && potion instanceof org.bukkit.potion.PotionData) { + potionMeta.setBasePotionData((org.bukkit.potion.PotionData) potion); + } else if (potion instanceof org.bukkit.potion.Potion) { + PotionEffectType effectType = ((org.bukkit.potion.Potion) potion).getType().getEffectType(); - if (effectType != null) { - potionMeta.setMainEffect(effectType); - } - } + if (effectType != null) { + potionMeta.setMainEffect(effectType); + } + } - meta = potionMeta; - } else if (leatherColor != null && CMIMaterial.isLeatherArmor(item.getType()) && meta instanceof LeatherArmorMeta) { - LeatherArmorMeta armorMeta = (LeatherArmorMeta) meta; - armorMeta.setColor(leatherColor); - meta = armorMeta; - } + meta = potionMeta; + } else if (leatherColor != null && CMIMaterial.isLeatherArmor(item.getType()) && meta instanceof LeatherArmorMeta) { + LeatherArmorMeta armorMeta = (LeatherArmorMeta) meta; + armorMeta.setColor(leatherColor); + meta = armorMeta; + } - if (meta.hasDisplayName()) - meta.setDisplayName(CMIChatColor.translate(meta.getDisplayName().replace("[player]", player.getName()))); + if (meta.hasDisplayName()) + meta.setDisplayName(CMIChatColor.translate(meta.getDisplayName().replace("[player]", player.getName()))); - if (meta.hasLore()) { - List translatedLore = meta.getLore(); + if (meta.hasLore()) { + List translatedLore = meta.getLore(); - for (int a = 0; a < translatedLore.size(); a++) { - translatedLore.set(a, CMIChatColor.translate(translatedLore.get(a).replace("[player]", player.getName()))); - } + for (int a = 0; a < translatedLore.size(); a++) { + translatedLore.set(a, CMIChatColor.translate(translatedLore.get(a).replace("[player]", player.getName()))); + } - meta.setLore(translatedLore); - } + meta.setLore(translatedLore); + } - if (enchants != null) { - if (item.getType() == CMIMaterial.ENCHANTED_BOOK.getMaterial()) { - EnchantmentStorageMeta bookMeta = (EnchantmentStorageMeta) meta; - for (Entry oneEnch : enchants.entrySet()) { - bookMeta.addStoredEnchant(oneEnch.getKey(), oneEnch.getValue(), true); - } - } else { - for (Entry oneEnchant : enchants.entrySet()) { - meta.addEnchant(oneEnchant.getKey(), oneEnchant.getValue(), true); - } - } - } + if (enchants != null) { + if (item.getType() == CMIMaterial.ENCHANTED_BOOK.getMaterial()) { + EnchantmentStorageMeta bookMeta = (EnchantmentStorageMeta) meta; + for (Entry oneEnch : enchants.entrySet()) { + bookMeta.addStoredEnchant(oneEnch.getKey(), oneEnch.getValue(), true); + } + } else { + for (Entry oneEnchant : enchants.entrySet()) { + meta.addEnchant(oneEnchant.getKey(), oneEnchant.getValue(), true); + } + } + } - item.setItemMeta(meta); - return item; + item.setItemMeta(meta); + return item; } public BoostMultiplier getBoost() { - return boostMultiplier.clone(); + return boostMultiplier.clone(); } public BoostMultiplier getBoost(JobProgression job) { - if (job == null || !jobs.contains(job.getJob()) || job.getLevel() < getFromLevel() || job.getLevel() > getUntilLevel()) - return new BoostMultiplier(); + if (job == null || !jobs.contains(job.getJob()) || job.getLevel() < getFromLevel() || job.getLevel() > getUntilLevel()) + return new BoostMultiplier(); - return boostMultiplier.clone(); + return boostMultiplier.clone(); } public List getJobs() { - return jobs; + return jobs; } public void setJobs(List jobs) { - this.jobs.clear(); + this.jobs.clear(); - if (jobs != null) { - this.jobs.addAll(jobs); - } + if (jobs != null) { + this.jobs.addAll(jobs); + } } public Map getEnchants() { - return enchants; + return enchants; } public int getFromLevel() { - return fromLevel; + return fromLevel; } public void setFromLevel(int fromLevel) { - this.fromLevel = fromLevel; + this.fromLevel = fromLevel; } public int getUntilLevel() { - return untilLevel; + return untilLevel; } public void setUntilLevel(int untilLevel) { - this.untilLevel = untilLevel; + this.untilLevel = untilLevel; } public String getLegacyKey() { - return legacyKey; + return legacyKey; } public void setLegacyKey(String legacyKey) { - this.legacyKey = legacyKey; + this.legacyKey = legacyKey; } } diff --git a/src/main/java/com/gamingmesh/jobs/container/QuestObjective.java b/src/main/java/com/gamingmesh/jobs/container/QuestObjective.java index e69991ae..f9c71f40 100644 --- a/src/main/java/com/gamingmesh/jobs/container/QuestObjective.java +++ b/src/main/java/com/gamingmesh/jobs/container/QuestObjective.java @@ -9,50 +9,54 @@ public class QuestObjective { private ActionType action = null; public QuestObjective(ActionType action, int id, String meta, String name, int amount) { - this.action = action; - this.id = id; - this.meta = meta; - this.name = name; - this.amount = amount; + this.action = action; + this.id = id; + this.meta = meta; + this.name = name; + this.amount = amount; } public int getTargetId() { - return id; + return id; } public void setTargetId(int id) { - this.id = id; + this.id = id; } public String getTargetMeta() { - return meta; + return meta; } public void setTargetMeta(String meta) { - this.meta = meta; + this.meta = meta; } public String getTargetName() { - return name; + return name; } public void setTargetName(String name) { - this.name = name; + this.name = name; } public int getAmount() { - return amount; + return amount; } public void setAmount(int amount) { - this.amount = amount; + this.amount = amount; } public ActionType getAction() { - return action; + return action; } public void setAction(ActionType action) { - this.action = action; + this.action = action; + } + + public boolean same(QuestObjective obj) { + return obj.id == this.id && obj.meta.equals(this.meta) && obj.name.equals(this.name) && obj.amount == this.amount && obj.action == this.action; } } diff --git a/src/main/java/com/gamingmesh/jobs/container/QuestProgression.java b/src/main/java/com/gamingmesh/jobs/container/QuestProgression.java index 0fe33bde..4e59859e 100644 --- a/src/main/java/com/gamingmesh/jobs/container/QuestProgression.java +++ b/src/main/java/com/gamingmesh/jobs/container/QuestProgression.java @@ -24,168 +24,205 @@ public class QuestProgression { private final Map done = new HashMap<>(); public QuestProgression(Quest quest) { - this.quest = quest; + this.quest = quest; - validUntil = quest.getValidUntil(); + validUntil = quest.getValidUntil(); } public Quest getQuest() { - if (quest == null) - return null; + if (quest == null) + return null; - Job job = quest.getJob(); - return job == null ? null : job.getQuest(quest.getConfigName()); + Job job = quest.getJob(); + return job == null ? null : job.getQuest(quest.getConfigName()); } public void setQuest(Quest quest) { - this.quest = quest; + this.quest = quest; } public int getTotalAmountNeeded() { - int amountNeeded = 0; - for (Map oneA : quest.getObjectives().values()) { - for (QuestObjective one : oneA.values()) { - amountNeeded += one.getAmount(); - } - } - return amountNeeded; + int amountNeeded = 0; + for (Map oneA : quest.getObjectives().values()) { + for (QuestObjective one : oneA.values()) { + amountNeeded += one.getAmount(); + } + } + return amountNeeded; } public void reset() { - validUntil = quest.getValidUntil(); - givenReward = false; - done.clear(); + validUntil = quest.getValidUntil(); + givenReward = false; + done.clear(); } public int getTotalAmountDone() { - int amountDone = 0; - for (Integer one : done.values()) { - amountDone += one; - } + int amountDone = 0; + for (Integer one : done.values()) { + amountDone += one; + } - return amountDone; + return amountDone; } public int getAmountDone(QuestObjective objective) { - return done.getOrDefault(objective, 0); + + Integer result = done.get(objective); + + if (result != null) + return result; + + // We might need to readd questObjectives in case config file got reloaded and new objective objects have been created which no longer match previous cache + Map objectives = quest.getJob().getQuest(quest.getConfigName()).getObjectives().get(objective.getAction()); + + QuestObjective globalObj = null; + QuestObjective replaceableObj = null; + + for (QuestObjective one : objectives.values()) { + if (!one.same(objective)) + continue; + globalObj = one; + break; + } + + for (QuestObjective one : done.keySet()) { + if (!one.same(objective)) + continue; + replaceableObj = one; + break; + } + + if (globalObj == null || replaceableObj == null) + return 0; + + Integer record = done.get(replaceableObj); + + if (record == null) + return 0; + + done.remove(replaceableObj); + done.put(globalObj, record); + return record; + } public void setAmountDone(QuestObjective objective, int amountDone) { - if (quest.hasObjective(objective)) { - done.put(objective, amountDone); - } + if (quest.hasObjective(objective)) { + done.put(objective, amountDone); + } } public long getValidUntil() { - return validUntil; + return validUntil; } public void setValidUntil(long validUntil) { - this.validUntil = validUntil; + this.validUntil = validUntil; } public boolean isValid() { - return !isEnded(); + return !isEnded(); } public boolean isEnded() { - return validUntil < System.currentTimeMillis(); + return validUntil < System.currentTimeMillis(); } public boolean isCompleted() { - for (Map oneA : quest.getObjectives().values()) { - for (QuestObjective one : oneA.values()) { - Integer amountDone = done.get(one); - if (amountDone == null || amountDone < one.getAmount()) - return false; - } - } - return true; + for (Map oneA : quest.getObjectives().values()) { + for (QuestObjective one : oneA.values()) { + Integer amountDone = done.get(one); + if (amountDone == null || amountDone < one.getAmount()) + return false; + } + } + return true; } public void processQuest(JobsPlayer jPlayer, ActionInfo action) { - if (quest.isStopped() || !quest.hasAction(action.getType())) - return; + if (quest.isStopped() || !quest.hasAction(action.getType())) + return; - Map byAction = quest.getObjectives().get(action.getType()); - QuestObjective objective = objectiveForAction(action); + Map byAction = quest.getObjectives().get(action.getType()); + QuestObjective objective = objectiveForAction(action); - if (byAction != null && objective == null) - return; + if (byAction != null && objective == null) + return; - org.bukkit.entity.Player player = jPlayer.getPlayer(); + org.bukkit.entity.Player player = jPlayer.getPlayer(); - for (String area : quest.getRestrictedAreas()) { - for (Entry a : Jobs.getRestrictedAreaManager().getRestrictedAreas().entrySet()) { - if (a.getKey().equalsIgnoreCase(area) && a.getValue().inRestrictedArea(player.getLocation())) { - return; - } - } - } + for (String area : quest.getRestrictedAreas()) { + for (Entry a : Jobs.getRestrictedAreaManager().getRestrictedAreas().entrySet()) { + if (a.getKey().equalsIgnoreCase(area) && a.getValue().inRestrictedArea(player.getLocation())) { + return; + } + } + } - Job questJob = quest.getJob(); - if (questJob != null) { - int maxQuest = jPlayer.getPlayerMaxQuest(questJob.getName()); - if (maxQuest > 0 && jPlayer.getDoneQuests() >= maxQuest) { - return; - } - } + Job questJob = quest.getJob(); + if (questJob != null) { + int maxQuest = jPlayer.getPlayerMaxQuest(questJob.getName()); + if (maxQuest > 0 && jPlayer.getDoneQuests() >= maxQuest) { + return; + } + } - if (!isCompleted() && - objective != null) { - Integer old = done.getOrDefault(objective, 0); - done.put(objective, old < objective.getAmount() ? old + 1 : objective.getAmount()); - } + if (!isCompleted() && + objective != null) { + Integer old = done.getOrDefault(objective, 0); + done.put(objective, old < objective.getAmount() ? old + 1 : objective.getAmount()); + } - jPlayer.setSaved(false); + jPlayer.setSaved(false); - if (!isCompleted() || !player.isOnline() || givenReward) - return; + if (!isCompleted() || !player.isOnline() || givenReward) + return; - givenReward = true; + givenReward = true; - jPlayer.addDoneQuest(questJob); + jPlayer.addDoneQuest(questJob); - for (String one : quest.getRewardCmds()) { - ServerCommandEvent ev = new ServerCommandEvent(Bukkit.getConsoleSender(), one.replace("[playerName]", player.getName())); - Bukkit.getPluginManager().callEvent(ev); - if (!ev.isCancelled()) { - Bukkit.dispatchCommand(Bukkit.getConsoleSender(), ev.getCommand().startsWith("/") ? ev.getCommand().substring(1) : ev.getCommand()); - } - } + for (String one : quest.getRewardCmds()) { + ServerCommandEvent ev = new ServerCommandEvent(Bukkit.getConsoleSender(), one.replace("[playerName]", player.getName())); + Bukkit.getPluginManager().callEvent(ev); + if (!ev.isCancelled()) { + Bukkit.dispatchCommand(Bukkit.getConsoleSender(), ev.getCommand().startsWith("/") ? ev.getCommand().substring(1) : ev.getCommand()); + } + } } public boolean isGivenReward() { - return givenReward; + return givenReward; } public void setGivenReward(boolean givenReward) { - this.givenReward = givenReward; + this.givenReward = givenReward; } private static boolean objectiveKeyMatches(String objectiveKey, ActionInfo actionInfo) { - if (actionInfo instanceof EnchantActionInfo) { - return Util.enchantMatchesActionInfo(objectiveKey, (EnchantActionInfo) actionInfo); - } + if (actionInfo instanceof EnchantActionInfo) { + return Util.enchantMatchesActionInfo(objectiveKey, (EnchantActionInfo) actionInfo); + } - return (objectiveKey.equalsIgnoreCase(actionInfo.getNameWithSub()) || - objectiveKey.equalsIgnoreCase(actionInfo.getName())); + return (objectiveKey.equalsIgnoreCase(actionInfo.getNameWithSub()) || + objectiveKey.equalsIgnoreCase(actionInfo.getName())); } private QuestObjective objectiveForAction(ActionInfo actionInfo) { - Map byAction = quest.getObjectives().get(actionInfo.getType()); - if (byAction == null) { - return null; - } + Map byAction = quest.getObjectives().get(actionInfo.getType()); + if (byAction == null) { + return null; + } - for (Map.Entry objectiveEntry : byAction.entrySet()) { - String objectiveKey = objectiveEntry.getKey(); + for (Map.Entry objectiveEntry : byAction.entrySet()) { + String objectiveKey = objectiveEntry.getKey(); - if (objectiveKeyMatches(objectiveKey, actionInfo)) { - return objectiveEntry.getValue(); - } - } + if (objectiveKeyMatches(objectiveKey, actionInfo)) { + return objectiveEntry.getValue(); + } + } - return null; + return null; } }