From 03c18a98a1b8667eb0a02acbdc1d71899a483318 Mon Sep 17 00:00:00 2001 From: Zrips Date: Mon, 27 May 2019 11:46:09 +0300 Subject: [PATCH] Actual multi-objective quest thingy --- .../gamingmesh/jobs/commands/list/quests.java | 19 +++- .../gamingmesh/jobs/config/ConfigManager.java | 43 ++++++--- .../jobs/config/NameTranslatorManager.java | 21 +++-- .../gamingmesh/jobs/container/JobInfo.java | 1 - .../gamingmesh/jobs/container/JobsPlayer.java | 32 +++++-- .../com/gamingmesh/jobs/container/Quest.java | 87 ++++++++----------- .../jobs/container/QuestObjective.java | 58 +++++++++++++ .../jobs/container/QuestProgression.java | 56 ++++++++++-- 8 files changed, 227 insertions(+), 90 deletions(-) create mode 100644 src/main/java/com/gamingmesh/jobs/container/QuestObjective.java diff --git a/src/main/java/com/gamingmesh/jobs/commands/list/quests.java b/src/main/java/com/gamingmesh/jobs/commands/list/quests.java index b234c964..7165f64b 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/list/quests.java +++ b/src/main/java/com/gamingmesh/jobs/commands/list/quests.java @@ -2,6 +2,7 @@ package com.gamingmesh.jobs.commands.list; import java.util.ArrayList; import java.util.List; +import java.util.Map.Entry; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -11,9 +12,11 @@ import com.gamingmesh.jobs.commands.Cmd; import com.gamingmesh.jobs.commands.JobCommand; import com.gamingmesh.jobs.container.JobProgression; import com.gamingmesh.jobs.container.JobsPlayer; +import com.gamingmesh.jobs.container.QuestObjective; import com.gamingmesh.jobs.container.QuestProgression; import com.gamingmesh.jobs.CMILib.RawMessage; import com.gamingmesh.jobs.stuff.TimeManage; +import com.gamingmesh.jobs.stuff.Util; public class quests implements Cmd { @@ -53,13 +56,13 @@ public class quests implements Cmd { for (JobProgression jobProg : jPlayer.getJobProgression()) { List list = jPlayer.getQuestProgressions(jobProg.getJob()); for (QuestProgression q : list) { - String progressLine = Jobs.getCommandManager().jobProgressMessage(q.getQuest().getAmount(), q.getAmountDone()); + String progressLine = Jobs.getCommandManager().jobProgressMessage(q.getTotalAmountNeeded(), q.getTotalAmountDone()); if (q.isCompleted()) progressLine = Jobs.getLanguage().getMessage("command.quests.output.completed"); RawMessage rm = new RawMessage(); String msg = Jobs.getLanguage().getMessage("command.quests.output.questLine", "[progress]", - progressLine, "[questName]", q.getQuest().getQuestName(), "[done]", q.getAmountDone(), "[required]", q.getQuest().getAmount()); + progressLine, "[questName]", q.getQuest().getQuestName(), "[done]", q.getTotalAmountDone(), "[required]", q.getTotalAmountNeeded()); List hoverMsgs = Jobs.getLanguage().getMessageList("command.quests.output.hover"); List hoverList = new ArrayList<>(); @@ -74,6 +77,18 @@ public class quests implements Cmd { } } else hoverList.add(current); + + } + + for (Entry oneObjective : q.getQuest().getObjectives().entrySet()) { + + hoverList.add(Jobs.getLanguage().getMessage("command.info.output." + oneObjective.getValue().getAction().toString().toLowerCase() + ".info") + " " + + Jobs.getNameTranslatorManager().Translate(oneObjective.getKey(), oneObjective.getValue().getAction(), oneObjective.getValue().getTargetId(), oneObjective.getValue() + .getTargetMeta(), oneObjective.getValue().getTargetName()) + + " " + q.getAmountDone(oneObjective.getValue()) + "/" + + oneObjective.getValue().getAmount()); + + } String hover = ""; diff --git a/src/main/java/com/gamingmesh/jobs/config/ConfigManager.java b/src/main/java/com/gamingmesh/jobs/config/ConfigManager.java index b94573f1..2774d74b 100644 --- a/src/main/java/com/gamingmesh/jobs/config/ConfigManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/ConfigManager.java @@ -58,6 +58,7 @@ import com.gamingmesh.jobs.container.JobItems; import com.gamingmesh.jobs.container.JobLimitedItems; import com.gamingmesh.jobs.container.JobPermission; import com.gamingmesh.jobs.container.Quest; +import com.gamingmesh.jobs.container.QuestObjective; import com.gamingmesh.jobs.resources.jfep.Parser; import com.gamingmesh.jobs.stuff.ChatColor; import com.gamingmesh.jobs.CMILib.VersionChecker.Version; @@ -870,29 +871,47 @@ public class ConfigManager { ConfigurationSection sqsection = qsection.getConfigurationSection(one); String name = sqsection.getString("Name", one); + Quest quest = new Quest(name, job); - ActionType actionType = ActionType.getByName(sqsection.getString("Action")); KeyValues kv = null; if (sqsection.isString("Target")) { + ActionType actionType = ActionType.getByName(sqsection.getString("Action")); kv = getKeyValue(sqsection.getString("Target"), actionType, jobName); - } else if (sqsection.isList("Target")) { - List list = sqsection.getStringList("Target"); - for (int i = 0; i < list.size(); i++) { - kv = getKeyValue(list.get(i), actionType, jobName); + + if (kv != null) { + int amount = sqsection.getInt("Amount", 1); + QuestObjective objective = new QuestObjective(actionType, kv.getId(), kv.getMeta(), kv.getType() + kv.getSubType(), amount); + quest.addObjective(objective); } } - if (kv == null) - kv = getKeyValue("STONE", actionType, jobName); + if (sqsection.isList("Objectives")) { + List list = sqsection.getStringList("Objectives"); + for (String oneObjective : list) { + String[] split = oneObjective.split(";"); + if (split.length != 3) { + Jobs.getPluginLogger().warning("Job " + jobKey + " has incorrect quest objective (" + oneObjective + ")!"); + continue; + } + try { + ActionType actionType = ActionType.getByName(split[0]); + kv = getKeyValue(split[1], actionType, jobName); + if (kv != null) { + int amount = Integer.parseInt(split[2]); + QuestObjective objective = new QuestObjective(actionType, kv.getId(), kv.getMeta(), kv.getType() + kv.getSubType(), amount); + quest.addObjective(objective); + } + } catch (Exception | Error e) { + Jobs.getPluginLogger().warning("Job " + jobKey + " has incorrect quest objective (" + oneObjective + ")!"); + } + } + } - int amount = sqsection.getInt("Amount"); int chance = sqsection.getInt("Chance", 100); List commands = sqsection.getStringList("RewardCommands"); List desc = sqsection.getStringList("RewardDesc"); - Quest quest = new Quest(name, job, actionType); - if (sqsection.isInt("fromLevel")) quest.setMinLvl(sqsection.getInt("fromLevel")); @@ -900,11 +919,7 @@ public class ConfigManager { quest.setMaxLvl(sqsection.getInt("toLevel")); quest.setConfigName(one); - quest.setAmount(amount); quest.setChance(chance); - quest.setTargetId(kv.getId()); - quest.setTargetMeta(kv.getMeta()); - quest.setTargetName(kv.getType() + kv.getSubType()); quest.setRewardCmds(commands); quest.setDescription(desc); quests.add(quest); diff --git a/src/main/java/com/gamingmesh/jobs/config/NameTranslatorManager.java b/src/main/java/com/gamingmesh/jobs/config/NameTranslatorManager.java index 60ee411e..e5383327 100644 --- a/src/main/java/com/gamingmesh/jobs/config/NameTranslatorManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/NameTranslatorManager.java @@ -15,6 +15,7 @@ import com.gamingmesh.jobs.CMILib.ConfigReader; import com.gamingmesh.jobs.CMILib.ItemManager.CMIEntityType; import com.gamingmesh.jobs.CMILib.ItemManager.CMIMaterial; import com.gamingmesh.jobs.CMILib.ItemManager.CMIPotionType; +import com.gamingmesh.jobs.container.ActionType; import com.gamingmesh.jobs.container.JobInfo; import com.gamingmesh.jobs.container.NameList; import com.gamingmesh.jobs.stuff.Util; @@ -28,9 +29,13 @@ public class NameTranslatorManager { public ArrayList ListOfColors = new ArrayList<>(); public String Translate(String materialName, JobInfo info) { + return Translate(materialName, info.getActionType(), info.getId(), info.getMeta(), info.getName()); + } + + public String Translate(String materialName, ActionType action, Integer id, String meta, String mame) { // Translating name to user friendly if (Jobs.getGCManager().UseCustomNames) - switch (info.getActionType()) { + switch (action) { case BREAK: case TNTBREAK: case EAT: @@ -50,13 +55,13 @@ public class NameTranslatorManager { } for (NameList one : ListOfNames) { String ids = one.getId() + ":" + one.getMeta(); - if (!one.getMeta().equalsIgnoreCase("") && ids.equalsIgnoreCase(info.getId() + ":" + info.getMeta()) && !one.getId().equalsIgnoreCase("0")) { + if (!one.getMeta().equalsIgnoreCase("") && ids.equalsIgnoreCase(id + ":" + meta) && !one.getId().equalsIgnoreCase("0")) { return one.getName(); } } for (NameList one : ListOfNames) { String ids = one.getId(); - if (ids.equalsIgnoreCase(String.valueOf(info.getId())) && !one.getId().equalsIgnoreCase("0")) { + if (ids.equalsIgnoreCase(String.valueOf(id)) && !one.getId().equalsIgnoreCase("0")) { return one.getName(); } } @@ -67,15 +72,15 @@ public class NameTranslatorManager { case TAME: for (NameList one : ListOfEntities) { String ids = one.getId() + ":" + one.getMeta(); - if (!one.getMeta().equalsIgnoreCase("") && ids.equalsIgnoreCase(info.getId() + ":" + info.getMeta()) && !one.getId().equalsIgnoreCase("0")) { + if (!one.getMeta().equalsIgnoreCase("") && ids.equalsIgnoreCase(id + ":" + meta) && !one.getId().equalsIgnoreCase("0")) { return one.getName(); } ids = one.getId(); - if (ids.equalsIgnoreCase(String.valueOf(info.getId())) && !one.getId().equalsIgnoreCase("0")) { + if (ids.equalsIgnoreCase(String.valueOf(id)) && !one.getId().equalsIgnoreCase("0")) { return one.getName(); } ids = one.getMinecraftName(); - if (ids.equalsIgnoreCase(info.getName())) { + if (ids.equalsIgnoreCase(mame)) { return one.getName(); } } @@ -99,7 +104,7 @@ public class NameTranslatorManager { case SHEAR: for (NameList one : ListOfColors) { String ids = one.getMinecraftName(); - if (ids.equalsIgnoreCase(info.getName())) { + if (ids.equalsIgnoreCase(mame)) { return one.getName(); } } @@ -110,7 +115,7 @@ public class NameTranslatorManager { case DRINK: for (NameList one : ListOfPotionNames) { String ids = one.getMinecraftName(); - if (ids.equalsIgnoreCase(info.getName())) { + if (ids.equalsIgnoreCase(mame)) { return one.getName(); } } diff --git a/src/main/java/com/gamingmesh/jobs/container/JobInfo.java b/src/main/java/com/gamingmesh/jobs/container/JobInfo.java index 170b4d1a..13fca792 100644 --- a/src/main/java/com/gamingmesh/jobs/container/JobInfo.java +++ b/src/main/java/com/gamingmesh/jobs/container/JobInfo.java @@ -20,7 +20,6 @@ package com.gamingmesh.jobs.container; import com.gamingmesh.jobs.Jobs; import com.gamingmesh.jobs.resources.jfep.Parser; -import com.gamingmesh.jobs.stuff.Debug; public class JobInfo { private ActionType actionType; diff --git a/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java b/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java index 613656b6..36d8fe04 100644 --- a/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java +++ b/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java @@ -33,6 +33,7 @@ import com.gamingmesh.jobs.dao.JobsDAO; import com.gamingmesh.jobs.economy.PaymentData; import com.gamingmesh.jobs.resources.jfep.Parser; import com.gamingmesh.jobs.stuff.ChatColor; +import com.gamingmesh.jobs.stuff.Debug; import com.gamingmesh.jobs.stuff.FurnaceBrewingHandling; import com.gamingmesh.jobs.stuff.TimeManage; @@ -691,7 +692,7 @@ public class JobsPlayer { honorific = builder.toString().trim(); if (honorific.length() > 0) honorific = org.bukkit.ChatColor.translateAlternateColorCodes('&', - Jobs.getGCManager().getModifyChatPrefix() + honorific + Jobs.getGCManager().getModifyChatSuffix()); + Jobs.getGCManager().getModifyChatPrefix() + honorific + Jobs.getGCManager().getModifyChatSuffix()); } @@ -846,8 +847,15 @@ public class JobsPlayer { for (Entry one : qpl.entrySet()) { QuestProgression prog = one.getValue(); - if (!prog.isEnded() && (type == null || type.name().equals(prog.getQuest().getAction().name()))) - ls.add(prog.getQuest().getConfigName().toLowerCase()); + if (prog.isEnded()) + continue; + + for (Entry oneObjective : prog.getQuest().getObjectives().entrySet()) { + if (type == null || type.name().equals(oneObjective.getValue().getAction().name())) { + ls.add(prog.getQuest().getConfigName().toLowerCase()); + break; + } + } } return ls; @@ -899,12 +907,17 @@ public class JobsPlayer { g.put(qp.getQuest().getConfigName(), qp); } + if (qp.getQuest() == null) { g.remove(one.getKey()); continue; } - if (type == null || type.name().equals(qp.getQuest().getAction().name())) { - tmp.put(qp.getQuest().getConfigName(), qp); + + for (Entry oneObjective : qp.getQuest().getObjectives().entrySet()) { + if (type == null || type.name().equals(oneObjective.getValue().getAction().name())) { + tmp.put(qp.getQuest().getConfigName(), qp); + break; + } } } @@ -917,8 +930,13 @@ public class JobsPlayer { QuestProgression qp = new QuestProgression(q); g.put(qp.getQuest().getConfigName(), qp); - if (type == null || type.name().equals(qp.getQuest().getAction().name())) - tmp.put(qp.getQuest().getConfigName(), qp); + for (Entry oneObjective : qp.getQuest().getObjectives().entrySet()) { + if (type == null || type.name().equals(oneObjective.getValue().getAction().name())) { + tmp.put(qp.getQuest().getConfigName(), qp); + break; + } + } + } } diff --git a/src/main/java/com/gamingmesh/jobs/container/Quest.java b/src/main/java/com/gamingmesh/jobs/container/Quest.java index 8e4896cd..dcd45634 100644 --- a/src/main/java/com/gamingmesh/jobs/container/Quest.java +++ b/src/main/java/com/gamingmesh/jobs/container/Quest.java @@ -2,7 +2,11 @@ package com.gamingmesh.jobs.container; import java.util.ArrayList; import java.util.Calendar; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; +import java.util.Map.Entry; +import java.util.Set; import com.gamingmesh.jobs.Jobs; @@ -11,50 +15,21 @@ public class Quest { private String configName; private String questName; private Job job; - private ActionType action = null; private Long validUntil = 0L; - private int id; - private String meta; - private String name; - private int chance = 0; private Integer minLvl = null; private Integer maxLvl = null; - private int amount = Integer.MAX_VALUE; - private List rewardCmds = new ArrayList<>(); private List rewards = new ArrayList<>(); - public Quest(String questName, Job job, ActionType action) { + private HashMap objectives = new HashMap(); + private Set actions = new HashSet(); + + public Quest(String questName, Job job) { this.questName = questName; this.job = job; - this.action = action; - } - - public int getTargetId() { - return id; - } - - public void setTargetId(int id) { - this.id = id; - } - - public String getTargetMeta() { - return meta; - } - - public void setTargetMeta(String meta) { - this.meta = meta; - } - - public String getTargetName() { - return name; - } - - public void setTargetName(String name) { - this.name = name; } public List getRewardCmds() { @@ -73,14 +48,6 @@ public class Quest { this.rewards = rewards; } - public int getAmount() { - return amount; - } - - public void setAmount(int amount) { - this.amount = amount; - } - public Long getValidUntil() { if (validUntil < System.currentTimeMillis()) { int hour = Jobs.getGCManager().getResetTimeHour(); @@ -108,14 +75,6 @@ public class Quest { this.validUntil = validUntil; } - public ActionType getAction() { - return action; - } - - public void setAction(ActionType action) { - this.action = action; - } - public Job getJob() { return Jobs.getJob(this.job.getName()); } @@ -177,4 +136,34 @@ public class Quest { return true; } + public HashMap getObjectives() { + return objectives; + } + + public boolean hasObjective(QuestObjective objective) { + for (Entry one : this.objectives.entrySet()) { + if (one.getValue().getTargetId() == objective.getTargetId() && + one.getValue().getAction() == objective.getAction() && + objective.getAmount() == one.getValue().getAmount() && + objective.getTargetName() == one.getValue().getTargetName()) + return true; + } + return false; + } + + public void setObjectives(HashMap objectives) { + this.objectives = objectives; + for (Entry one : objectives.entrySet()) { + actions.add(one.getValue().getAction()); + } + } + + public void addObjective(QuestObjective objective) { + this.objectives.put(objective.getTargetName(), objective); + actions.add(objective.getAction()); + } + + public boolean hasAction(ActionType action) { + return this.actions.contains(action); + } } diff --git a/src/main/java/com/gamingmesh/jobs/container/QuestObjective.java b/src/main/java/com/gamingmesh/jobs/container/QuestObjective.java new file mode 100644 index 00000000..e69991ae --- /dev/null +++ b/src/main/java/com/gamingmesh/jobs/container/QuestObjective.java @@ -0,0 +1,58 @@ +package com.gamingmesh.jobs.container; + +public class QuestObjective { + + private int id; + private String meta; + private String name; + private int amount = Integer.MAX_VALUE; + 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; + } + + public int getTargetId() { + return id; + } + + public void setTargetId(int id) { + this.id = id; + } + + public String getTargetMeta() { + return meta; + } + + public void setTargetMeta(String meta) { + this.meta = meta; + } + + public String getTargetName() { + return name; + } + + public void setTargetName(String name) { + this.name = name; + } + + public int getAmount() { + return amount; + } + + public void setAmount(int amount) { + this.amount = amount; + } + + public ActionType getAction() { + return action; + } + + public void setAction(ActionType action) { + this.action = action; + } +} diff --git a/src/main/java/com/gamingmesh/jobs/container/QuestProgression.java b/src/main/java/com/gamingmesh/jobs/container/QuestProgression.java index a6f324d0..269c5a11 100644 --- a/src/main/java/com/gamingmesh/jobs/container/QuestProgression.java +++ b/src/main/java/com/gamingmesh/jobs/container/QuestProgression.java @@ -1,14 +1,18 @@ package com.gamingmesh.jobs.container; +import java.util.HashMap; +import java.util.Map.Entry; + import org.bukkit.Bukkit; import org.bukkit.event.server.ServerCommandEvent; public class QuestProgression { private Quest quest; - private int amountDone = 0; +// private int amountDone = 0; private Long validUntil; private boolean givenReward = false; + private HashMap done = new HashMap(); public QuestProgression(Quest quest) { this.quest = quest; @@ -23,12 +27,30 @@ public class QuestProgression { this.quest = quest; } - public int getAmountDone() { + public int getTotalAmountNeeded() { + int amountNeeded = 0; + for (Entry one : quest.getObjectives().entrySet()) { + amountNeeded += one.getValue().getAmount(); + } + return amountNeeded; + } + + public int getTotalAmountDone() { + int amountDone = 0; + for (Entry one : done.entrySet()) { + amountDone += one.getValue(); + } return amountDone; } - public void setAmountDone(int amountDone) { - this.amountDone = amountDone; + public int getAmountDone(QuestObjective objective) { + Integer amountDone = done.get(objective); + return amountDone == null ? 0 : amountDone; + } + + public void setAmountDone(QuestObjective objective, int amountDone) { + if (quest.hasObjective(objective)) + done.put(objective, amountDone); } public Long getValidUntil() { @@ -48,19 +70,35 @@ public class QuestProgression { } public boolean isCompleted() { - return amountDone >= quest.getAmount(); + for (Entry one : quest.getObjectives().entrySet()) { + Integer amountDone = this.done.get(one.getValue()); + if (amountDone == null || amountDone < one.getValue().getAmount()) + return false; + } + return true; } public void processQuest(JobsPlayer jPlayer, ActionInfo action) { - if (!quest.getAction().name().equals(action.getType().name())) + if (!quest.hasAction(action.getType())) return; - if (!quest.getTargetName().equalsIgnoreCase(action.getName()) && !quest.getTargetName().equalsIgnoreCase(action.getNameWithSub())) + if (!quest.getObjectives().containsKey(action.getName()) && !quest.getObjectives().containsKey(action.getNameWithSub())) return; - if (!isCompleted()) - amountDone++; + if (!isCompleted()) { + QuestObjective objective = quest.getObjectives().get(action.getName()); + if (objective == null) + objective = quest.getObjectives().get(action.getNameWithSub()); + Integer old = done.get(objective); + if (old == null) + old = 0; + if (old < objective.getAmount()) + done.put(objective, old + 1); + else { + done.put(objective, objective.getAmount()); + } + } if (!isCompleted()) return;