From 666b9bd8bb0801849894fd3474534400422fe51c Mon Sep 17 00:00:00 2001 From: PikaMug Date: Sat, 20 Apr 2019 00:41:59 -0400 Subject: [PATCH] Improve support for multiple custom objectives per stage --- .../me/blackvein/quests/CustomObjective.java | 19 ++++--- .../me/blackvein/quests/QuestFactory.java | 19 +++++-- .../java/me/blackvein/quests/Quester.java | 3 +- .../main/java/me/blackvein/quests/Quests.java | 45 ++++++++------- .../quests/prompts/CreateStagePrompt.java | 56 +++++++++++++------ 5 files changed, 91 insertions(+), 51 deletions(-) diff --git a/main/src/main/java/me/blackvein/quests/CustomObjective.java b/main/src/main/java/me/blackvein/quests/CustomObjective.java index 1849501e1..8ef116605 100644 --- a/main/src/main/java/me/blackvein/quests/CustomObjective.java +++ b/main/src/main/java/me/blackvein/quests/CustomObjective.java @@ -166,22 +166,23 @@ public abstract class CustomObjective implements Listener { if (currentStage == null) { return null; } - int index = -1; - int tempIndex = 0; + CustomObjective found = null; for (me.blackvein.quests.CustomObjective co : currentStage.customObjectives) { if (co.getName().equals(obj.getName())) { - index = tempIndex; + found = co; break; } - tempIndex++; } - if (index > -1) { + if (found != null) { Map m = new HashMap(); - for (int i = index; i < index + data.size(); i++) { - Entry e = currentStage.customObjectiveData.get(i); - m.put(e.getKey(), e.getValue()); + for (Entry datamap : found.getData()) { + for (Entry e : currentStage.customObjectiveData) { + if (e.getKey().equals(datamap.getKey())) { + m.put(e.getKey(), e.getValue()); + return m; + } + } } - return m; } } return null; diff --git a/main/src/main/java/me/blackvein/quests/QuestFactory.java b/main/src/main/java/me/blackvein/quests/QuestFactory.java index 834ec42b9..c0ce629e8 100644 --- a/main/src/main/java/me/blackvein/quests/QuestFactory.java +++ b/main/src/main/java/me/blackvein/quests/QuestFactory.java @@ -763,7 +763,7 @@ public class QuestFactory implements ConversationAbandonedListener { } @SuppressWarnings("unchecked") - public static void saveQuest(ConversationContext cc, ConfigurationSection cs) { + public void saveQuest(ConversationContext cc, ConfigurationSection cs) { String edit = null; if (cc.getSessionData(CK.ED_QUEST_EDIT) != null) { edit = (String) cc.getSessionData(CK.ED_QUEST_EDIT); @@ -1301,9 +1301,20 @@ public class QuestFactory implements ConversationAbandonedListener { ConfigurationSection sec2 = sec.createSection("custom" + (index + 1)); sec2.set("name", customObjs.get(index)); sec2.set("count", customObjCounts.get(index)); + CustomObjective found = null; + for (CustomObjective co : plugin.getCustomObjectives()) { + if (co.getName().equals(customObjs.get(index))) { + found = co; + break; + } + } ConfigurationSection sec3 = sec2.createSection("data"); - for (Entry e : customObjsData) { - sec3.set(e.getKey(), e.getValue()); // if anything goes wrong it's probably here + for (Entry datamap : found.getData()) { + for (Entry e : customObjsData) { + if (e.getKey().equals(datamap.getKey())) { + sec3.set(e.getKey(), e.getValue()); // if anything goes wrong it's probably here + } + } } } } @@ -1666,8 +1677,8 @@ public class QuestFactory implements ConversationAbandonedListener { for (int i = 0; i < stage.customObjectives.size(); i++) { list.add(stage.customObjectives.get(i).getName()); countList.add(stage.customObjectiveCounts.get(i)); - datamapList.add(stage.customObjectiveData.get(i)); } + datamapList.addAll(stage.customObjectiveData); cc.setSessionData(pref + CK.S_CUSTOM_OBJECTIVES, list); cc.setSessionData(pref + CK.S_CUSTOM_OBJECTIVES_COUNT, countList); cc.setSessionData(pref + CK.S_CUSTOM_OBJECTIVES_DATA, datamapList); diff --git a/main/src/main/java/me/blackvein/quests/Quester.java b/main/src/main/java/me/blackvein/quests/Quester.java index 7616f2334..fc00ef3b3 100644 --- a/main/src/main/java/me/blackvein/quests/Quester.java +++ b/main/src/main/java/me/blackvein/quests/Quester.java @@ -1701,7 +1701,8 @@ public class Quester { break; } } - List> sub = getCurrentStage(quest).customObjectiveData.subList(index, getCurrentStage(quest).customObjectiveData.size()); + List> sub = new LinkedList<>(); + sub.addAll(getCurrentStage(quest).customObjectiveData.subList(index, getCurrentStage(quest).customObjectiveData.size())); List> end = new LinkedList>(sub); sub.clear(); // since sub is backed by end, this removes all sub-list items from end for (Entry datamap : end) { diff --git a/main/src/main/java/me/blackvein/quests/Quests.java b/main/src/main/java/me/blackvein/quests/Quests.java index c6a3f378e..bc5f03a50 100644 --- a/main/src/main/java/me/blackvein/quests/Quests.java +++ b/main/src/main/java/me/blackvein/quests/Quests.java @@ -1135,42 +1135,45 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener for (CustomObjective co : stage.customObjectives) { int countsIndex = 0; String display = co.getDisplay(); - boolean addUnfinished = false; - boolean addFinished = false; + List unfinished = new LinkedList(); + List finished = new LinkedList(); for (Entry entry : data.customObjectiveCounts.entrySet()) { if (co.getName().equals(entry.getKey())) { - int dataIndex = 0; for (Entry prompt : co.getData()) { String replacement = "%" + prompt.getKey() + "%"; try { - if (display.contains(replacement)) - display = display.replace(replacement, ((String) stage.customObjectiveData.get(dataIndex).getValue())); + for (Entry e : stage.customObjectiveData) { + if (e.getKey().equals(prompt.getKey())) { + if (display.contains(replacement)) { + display = display.replace(replacement, ((String) e.getValue())); + } + } + } } catch (NullPointerException ne) { getLogger().severe("Unable to fetch display for " + co.getName() + " on " + quest.getName()); ne.printStackTrace(); } - dataIndex++; } if (entry.getValue() < stage.customObjectiveCounts.get(countsIndex)) { if (co.canShowCount()) { display = display.replace("%count%", entry.getValue() + "/" + stage.customObjectiveCounts.get(countsIndex)); } - addUnfinished = true; + unfinished.add(display); } else { if (co.canShowCount()) { display = display.replace("%count%", stage.customObjectiveCounts.get(countsIndex) + "/" + stage.customObjectiveCounts.get(countsIndex)); } - addFinished = true; + finished.add(display); } } countsIndex++; } - if (addUnfinished) { - quester.getPlayer().sendMessage(ChatColor.GREEN + display); + for (String s : unfinished) { + quester.getPlayer().sendMessage(ChatColor.GREEN + s); } - if (addFinished) { - quester.getPlayer().sendMessage(ChatColor.GRAY + display); + for (String s : finished) { + quester.getPlayer().sendMessage(ChatColor.GRAY + s); } } } @@ -2493,10 +2496,10 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener return; } Stage oStage = quest.getStage(Integer.valueOf(s2) - 1); - oStage.customObjectives=new LinkedList<>(); - oStage.customObjectiveCounts=new LinkedList<>(); - oStage.customObjectiveData=new LinkedList<>(); - oStage.customObjectiveDisplays=new LinkedList<>(); + oStage.customObjectives = new LinkedList<>(); + oStage.customObjectiveCounts = new LinkedList<>(); + oStage.customObjectiveData = new LinkedList<>(); + oStage.customObjectiveDisplays = new LinkedList<>(); if (config.contains("quests." + questKey + ".stages.ordered." + s2 + ".custom-objectives")) { ConfigurationSection sec = config.getConfigurationSection("quests." + questKey + ".stages.ordered." + s2 + ".custom-objectives"); for (String path : sec.getKeys(false)) { @@ -2515,11 +2518,11 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener } else { ConfigurationSection sec2 = sec.getConfigurationSection(path + ".data"); oStage.customObjectives.add(found.get()); - if (count <= 0) { - oStage.customObjectiveCounts.add(0); - } else { - oStage.customObjectiveCounts.add(count); - } + if (count <= 0) { + oStage.customObjectiveCounts.add(0); + } else { + oStage.customObjectiveCounts.add(count); + } for (Entry prompt : found.get().getData()) { Entry data = populateCustoms(sec2, prompt); oStage.customObjectiveData.add(data); diff --git a/main/src/main/java/me/blackvein/quests/prompts/CreateStagePrompt.java b/main/src/main/java/me/blackvein/quests/prompts/CreateStagePrompt.java index 15438a45a..edd5cd8a2 100644 --- a/main/src/main/java/me/blackvein/quests/prompts/CreateStagePrompt.java +++ b/main/src/main/java/me/blackvein/quests/prompts/CreateStagePrompt.java @@ -1418,22 +1418,32 @@ public class CreateStagePrompt extends FixedSetPrompt { String text = ChatColor.BOLD + "" + ChatColor.AQUA + "- "; LinkedList list = (LinkedList) context.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES); LinkedList> datamapList = (LinkedList>) context.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES_DATA); - String objName = list.getLast(); + CustomObjective found = null; + for (CustomObjective co : plugin.getCustomObjectives()) { + if (co.getName().equals(objName)) { + found = co; + break; + } + } + if (found == null) { + return "ERROR"; + } text += objName + " -\n"; int index = 1; - - for (Entry datamap : datamapList) { - text += ChatColor.BOLD + "" + ChatColor.DARK_BLUE + index + " - " + ChatColor.RESET + ChatColor.BLUE + datamap.getKey(); - if (datamap.getValue() != null) { - text += ChatColor.GREEN + " (" + datamap.getValue().toString() + ")\n"; + for (Entry datamap : found.getData()) { + for (Entry currentData : datamapList) { + if (currentData.getKey().equals(datamap.getKey())) { + text += ChatColor.BOLD + "" + ChatColor.DARK_BLUE + index + " - " + ChatColor.RESET + ChatColor.BLUE + datamap.getKey(); + if (currentData.getValue() != null) { + text += ChatColor.GREEN + " (" + currentData.getValue().toString() + ")\n"; } else { text += ChatColor.RED + " (" + Lang.get("valRequired") + ")\n"; } index++; - + } + } } - text += ChatColor.BOLD + "" + ChatColor.DARK_BLUE + index + " - " + ChatColor.AQUA + Lang.get("finish"); return text; } @@ -1441,7 +1451,23 @@ public class CreateStagePrompt extends FixedSetPrompt { @Override public Prompt acceptInput(ConversationContext context, String input) { @SuppressWarnings("unchecked") - LinkedList> datamapList = (LinkedList>) context.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES_DATA); + //LinkedList> datamapList = (LinkedList>) context.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES_DATA); + + + LinkedList list = (LinkedList) context.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES); + String objName = list.getLast(); + CustomObjective found = null; + for (CustomObjective co : plugin.getCustomObjectives()) { + if (co.getName().equals(objName)) { + found = co; + break; + } + } + if (found == null) { + plugin.getLogger().severe("ERROR"); + return new ObjectiveCustomDataListPrompt(); + } + LinkedList> datamapList = found.getData(); int numInput; try { @@ -1496,14 +1522,12 @@ public class CreateStagePrompt extends FixedSetPrompt { LinkedList> datamapList = (LinkedList>) context.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES_DATA); LinkedList> promptList = new LinkedList>(); String temp = (String) context.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES_DATA_TEMP); - for (Entry datamap : datamapList) { - if (datamap.getKey().equals(temp)) { - promptList.add(new AbstractMap.SimpleEntry(datamap.getKey(), input)); - } else { - promptList.add(new AbstractMap.SimpleEntry(datamap.getKey(), datamap.getValue())); - } - + if (datamap.getKey().equals(temp)) { + promptList.add(new AbstractMap.SimpleEntry(datamap.getKey(), input)); + } else { + promptList.add(new AbstractMap.SimpleEntry(datamap.getKey(), datamap.getValue())); + } } context.setSessionData(pref + CK.S_CUSTOM_OBJECTIVES_DATA, promptList); context.setSessionData(pref + CK.S_CUSTOM_OBJECTIVES_DATA_TEMP, null);