From 159af2ab977a0b51ccfb1709a16acc8aad8c0343 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Mon, 21 Jan 2019 21:26:21 -0500 Subject: [PATCH] Refactoring for getter/setter methods, part 10. Fixes #625 and #647 --- .../me/blackvein/quests/CustomObjective.java | 74 +++++-------------- .../blackvein/quests/CustomRequirement.java | 6 +- .../me/blackvein/quests/CustomReward.java | 4 +- .../me/blackvein/quests/QuestFactory.java | 11 +-- .../java/me/blackvein/quests/Quester.java | 20 +++-- src/main/java/me/blackvein/quests/Quests.java | 45 +++++++---- .../me/blackvein/quests/Requirements.java | 3 +- src/main/java/me/blackvein/quests/Stage.java | 5 +- .../quests/prompts/CreateStagePrompt.java | 69 +++++++++-------- 9 files changed, 118 insertions(+), 119 deletions(-) diff --git a/src/main/java/me/blackvein/quests/CustomObjective.java b/src/main/java/me/blackvein/quests/CustomObjective.java index e20b97487..0b3b0a955 100644 --- a/src/main/java/me/blackvein/quests/CustomObjective.java +++ b/src/main/java/me/blackvein/quests/CustomObjective.java @@ -12,8 +12,11 @@ package me.blackvein.quests; +import java.util.AbstractMap; import java.util.HashMap; +import java.util.LinkedList; import java.util.Map; +import java.util.Map.Entry; import org.bukkit.entity.Player; import org.bukkit.event.Listener; @@ -23,10 +26,10 @@ public abstract class CustomObjective implements Listener { private Quests plugin = Quests.getPlugin(Quests.class); private String name = null; private String author = null; - private Map data = new HashMap(); + private LinkedList> data = new LinkedList>(); private Map descriptions = new HashMap(); private String countPrompt = "Enter number"; - private String display = "%data%: %count%"; + private String display = "Progress: %count%"; private boolean showCount = true; private int count = 1; @@ -46,9 +49,9 @@ public abstract class CustomObjective implements Listener { this.author = author; } - public Map getData() { + public LinkedList> getData() { return data; - } + } /** * Add a new prompt

@@ -60,8 +63,9 @@ public abstract class CustomObjective implements Listener { * @param defaultValue Value to be used if input is not received */ public void addStringPrompt(String title, String description, Object defaultValue) { - data.put(name, defaultValue); - descriptions.put(name, description); + Entry prompt = new AbstractMap.SimpleEntry(title, defaultValue); + data.add(prompt); + descriptions.put(title, description); } /** @@ -71,7 +75,8 @@ public abstract class CustomObjective implements Listener { * @deprecated use addPrompt(name, description) */ public void addData(String name) { - data.put(name, null); + Entry prompt = new AbstractMap.SimpleEntry(name, null); + data.add(prompt); } public Map getDescriptions() { @@ -158,8 +163,9 @@ public abstract class CustomObjective implements Listener { Quester quester = plugin.getQuester(player.getUniqueId()); if (quester != null) { Stage currentStage = quester.getCurrentStage(quest); - if (currentStage == null) + if (currentStage == null) { return null; + } int index = -1; int tempIndex = 0; for (me.blackvein.quests.CustomObjective co : currentStage.customObjectives) { @@ -170,7 +176,10 @@ public abstract class CustomObjective implements Listener { tempIndex++; } if (index > -1) { - return currentStage.customObjectiveData.get(index); + Entry e = currentStage.customObjectiveData.get(index); + Map m = new HashMap(); + m.put(e.getKey(), e.getValue()); + return m; } } return null; @@ -209,51 +218,4 @@ public abstract class CustomObjective implements Listener { } } } - - @Override - public boolean equals(Object o) { - if (o instanceof CustomObjective) { - CustomObjective other = (CustomObjective) o; - if (other.name.equals(name) == false) { - return false; - } - if (other.author.equals(name) == false) { - return false; - } - for (String s : other.getData().keySet()) { - if (getData().containsKey(s) == false) { - return false; - } - } - for (Object val : other.getData().values()) { - if (getData().containsValue(val) == false) { - return false; - } - } - for (String s : other.descriptions.keySet()) { - if (descriptions.containsKey(s) == false) { - return false; - } - } - for (String s : other.descriptions.values()) { - if (descriptions.containsValue(s) == false) { - return false; - } - } - if (other.countPrompt.equals(countPrompt) == false) { - return false; - } - if (other.display.equals(display) == false) { - return false; - } - if (other.showCount != showCount) { - return false; - } - if (other.count != count) { - return false; - } - return true; - } - return false; - } } \ No newline at end of file diff --git a/src/main/java/me/blackvein/quests/CustomRequirement.java b/src/main/java/me/blackvein/quests/CustomRequirement.java index 4fa7e91bd..e5926397f 100644 --- a/src/main/java/me/blackvein/quests/CustomRequirement.java +++ b/src/main/java/me/blackvein/quests/CustomRequirement.java @@ -44,7 +44,7 @@ public abstract class CustomRequirement { public Map getData() { return data; - } + } /** * Add a new prompt

@@ -56,8 +56,8 @@ public abstract class CustomRequirement { * @param defaultValue Value to be used if input is not received */ public void addStringPrompt(String title, String description, Object defaultValue) { - data.put(name, defaultValue); - descriptions.put(name, description); + data.put(title, defaultValue); + descriptions.put(title, description); } /** diff --git a/src/main/java/me/blackvein/quests/CustomReward.java b/src/main/java/me/blackvein/quests/CustomReward.java index 4bff1b4a0..52a88fcf3 100644 --- a/src/main/java/me/blackvein/quests/CustomReward.java +++ b/src/main/java/me/blackvein/quests/CustomReward.java @@ -57,8 +57,8 @@ public abstract class CustomReward { * @param defaultValue Value to be used if input is not received */ public void addStringPrompt(String title, String description, Object defaultValue) { - data.put(name, defaultValue); - descriptions.put(name, description); + data.put(title, defaultValue); + descriptions.put(title, description); } /** diff --git a/src/main/java/me/blackvein/quests/QuestFactory.java b/src/main/java/me/blackvein/quests/QuestFactory.java index d11526ec7..418c4cefc 100644 --- a/src/main/java/me/blackvein/quests/QuestFactory.java +++ b/src/main/java/me/blackvein/quests/QuestFactory.java @@ -999,7 +999,7 @@ public class QuestFactory implements ConversationAbandonedListener { LinkedList> passPhrases; LinkedList customObjs; LinkedList customObjCounts; - LinkedList> customObjsData; + LinkedList> customObjsData; String script; String startEvent; String finishEvent; @@ -1151,7 +1151,7 @@ public class QuestFactory implements ConversationAbandonedListener { if (cc.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES) != null) { customObjs = (LinkedList) cc.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES); customObjCounts = (LinkedList) cc.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES_COUNT); - customObjsData = (LinkedList>) cc.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES_DATA); + customObjsData = (LinkedList>) cc.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES_DATA); } if (cc.getSessionData(pref + CK.S_START_EVENT) != null) { startEvent = (String) cc.getSessionData(pref + CK.S_START_EVENT); @@ -1267,8 +1267,9 @@ public class QuestFactory implements ConversationAbandonedListener { ConfigurationSection sec2 = sec.createSection("custom" + (index + 1)); sec2.set("name", customObjs.get(index)); sec2.set("count", customObjCounts.get(index)); - if (customObjsData.get(index).isEmpty() == false) { - sec2.set("data", customObjsData.get(index)); + ConfigurationSection sec3 = sec2.createSection("data"); + for (Entry e : customObjsData) { + sec3.set(e.getKey(), e.getValue()); // if anything goes wrong it's probably here } } } @@ -1620,7 +1621,7 @@ public class QuestFactory implements ConversationAbandonedListener { if (stage.customObjectives.isEmpty() == false) { LinkedList list = new LinkedList(); LinkedList countList = new LinkedList(); - LinkedList> datamapList = new LinkedList>(); + LinkedList> datamapList = new LinkedList>(); for (int i = 0; i < stage.customObjectives.size(); i++) { list.add(stage.customObjectives.get(i).getName()); countList.add(stage.customObjectiveCounts.get(i)); diff --git a/src/main/java/me/blackvein/quests/Quester.java b/src/main/java/me/blackvein/quests/Quester.java index 9018ea28c..5c0225011 100644 --- a/src/main/java/me/blackvein/quests/Quester.java +++ b/src/main/java/me/blackvein/quests/Quester.java @@ -786,10 +786,10 @@ public class Quester { for (Entry entry : getQuestData(quest).customObjectiveCounts.entrySet()) { if (co.getName().equals(entry.getKey())) { String display = co.getDisplay(); - Map datamap = getCurrentStage(quest).customObjectiveData.get(index); - for (String key : co.getData().keySet()) { + Entry datamap = getCurrentStage(quest).customObjectiveData.get(index); + for (Entry prompt : co.getData()) { try { - display = display.replace("%" + key + "%", ((String) datamap.get(key))); + display = display.replace("%" + prompt.getKey() + "%", ((String) datamap.getValue())); } catch (NullPointerException ne) { plugin.getLogger().severe("Unable to fetch display for " + co.getName() + " on " + quest.getName()); ne.printStackTrace(); @@ -885,20 +885,27 @@ public class Quester { public boolean hasCustomObjective(Quest quest, String s) { if (getQuestData(quest) == null) { + System.out.println("bing"); return false; } if (getQuestData(quest).customObjectiveCounts.containsKey(s)) { + System.out.println("bang"); int count = getQuestData(quest).customObjectiveCounts.get(s); int index = -1; for (int i = 0; i < getCurrentStage(quest).customObjectives.size(); i++) { + System.out.println("bong"); if (getCurrentStage(quest).customObjectives.get(i).getName().equals(s)) { + System.out.println("bung"); index = i; break; } } int count2 = getCurrentStage(quest).customObjectiveCounts.get(index); + System.out.println("count= " + count); + System.out.println("count2= " + count2); return count <= count2; } + System.out.println("byng"); return false; } @@ -1550,10 +1557,9 @@ public class Quester { break; } } - Map datamap = getCurrentStage(quest).customObjectiveData.get(index); - for (String key : co.getData().keySet()) { - message = message.replace("%" + ((String) key) + "%", (String) datamap.get(key)); - } + Entry datamap = getCurrentStage(quest).customObjectiveData.get(index); + message = message.replace("%" + ((String) datamap.getKey()) + "%", (String) datamap.getValue()); + if (co.canShowCount()) { message = message.replace("%count%", getCurrentStage(quest).customObjectiveCounts.get(index) + "/" + getCurrentStage(quest).customObjectiveCounts.get(index)); } diff --git a/src/main/java/me/blackvein/quests/Quests.java b/src/main/java/me/blackvein/quests/Quests.java index 2f9885958..23ccbede5 100644 --- a/src/main/java/me/blackvein/quests/Quests.java +++ b/src/main/java/me/blackvein/quests/Quests.java @@ -23,6 +23,7 @@ import java.lang.reflect.Constructor; import java.net.URISyntaxException; import java.net.URL; import java.net.URLClassLoader; +import java.util.AbstractMap; import java.util.Arrays; import java.util.ConcurrentModificationException; import java.util.Enumeration; @@ -487,7 +488,7 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener Quest quest = new Quest(); failedToLoad = false; if (config.contains("quests." + questKey + ".name")) { - quest.setName(parseString(config.getString("quests." + questKey + ".name"), quest)); + quest = getQuest(parseString(config.getString("quests." + questKey + ".name"), quest)); loadCustomSections(quest, config, questKey); } else { skipQuestProcess("Quest block \'" + questKey + "\' is missing " + ChatColor.RED + "name:"); @@ -1971,7 +1972,7 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener private void loadCustomSections(Quest quest, FileConfiguration config, String questKey) throws StageFailedException, SkipQuest { ConfigurationSection questStages = config.getConfigurationSection("quests." + questKey + ".stages.ordered"); for (String s2 : questStages.getKeys(false)) { - Stage oStage = new Stage(); + Stage oStage = quest.getStage(Integer.valueOf(s2) - 1); 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)) { @@ -1989,11 +1990,16 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener continue; } else { ConfigurationSection sec2 = sec.getConfigurationSection(path + ".data"); - Map data = populateCustoms(sec2, found.get().getData()); // Added in Github PR #554 - - oStage.customObjectives.add(found.get()); - oStage.customObjectiveCounts.add(count); - oStage.customObjectiveData.add(data); + for (Entry prompt : found.get().getData()) { + Entry data = populateCustoms(sec2, prompt); + oStage.customObjectives.add(found.get()); + if (count <= 0) { + oStage.customObjectiveCounts.add(0); + } else { + oStage.customObjectiveCounts.add(count); + } + oStage.customObjectiveData.add(data); + } } } } @@ -2049,19 +2055,32 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener } /** - * Add possibilty to use fallbacks for customs. + * Add possibilty to use fallbacks for customs maps * Avoid null objects in datamap by initialize the entry value with empty string if no fallback present. */ - - static Map populateCustoms(ConfigurationSection sec2,Map datamap) { + static Map populateCustoms(ConfigurationSection sec2, Map datamap) { Map data = new HashMap(); - if(sec2 != null) { - for(String key : datamap.keySet()) { - data.put(key, sec2.contains(key) ? sec2.get(key):datamap.get(key) != null ? datamap.get(key) : new String()); + if (sec2 != null) { + for (String key : datamap.keySet()) { + data.put(key, sec2.contains(key) ? sec2.get(key) : datamap.get(key) != null ? datamap.get(key) : new String()); } } return data; } + + /** + * Add possibilty to use fallbacks for customs entries + * Avoid null objects in datamap by initialize the entry value with empty string if no fallback present. + */ + static Entry populateCustoms(ConfigurationSection sec2, Entry datamap) { + String key = null;; + Object value = null;; + if (sec2 != null) { + key = datamap.getKey(); + value = datamap.getValue(); + } + return new AbstractMap.SimpleEntry(key, sec2.contains(key) ? sec2.get(key) : value != null ? value : new String()); + } private void stageFailed(String msg) throws StageFailedException { stageFailed(new String[] { msg }); diff --git a/src/main/java/me/blackvein/quests/Requirements.java b/src/main/java/me/blackvein/quests/Requirements.java index ca781b223..f3bb70d04 100644 --- a/src/main/java/me/blackvein/quests/Requirements.java +++ b/src/main/java/me/blackvein/quests/Requirements.java @@ -103,8 +103,7 @@ public class Requirements { public Map> getCustomRequirements() { return customRequirements; } - protected void setCustomRequirements( - Map> customRequirements) { + protected void setCustomRequirements(Map> customRequirements) { this.customRequirements = customRequirements; } public String getFailRequirements() { diff --git a/src/main/java/me/blackvein/quests/Stage.java b/src/main/java/me/blackvein/quests/Stage.java index 834c9d2bb..fb62d0056 100644 --- a/src/main/java/me/blackvein/quests/Stage.java +++ b/src/main/java/me/blackvein/quests/Stage.java @@ -16,6 +16,7 @@ import java.util.EnumMap; import java.util.HashMap; import java.util.LinkedList; import java.util.Map; +import java.util.Map.Entry; import org.bukkit.DyeColor; import org.bukkit.Location; @@ -123,7 +124,7 @@ public class Stage { protected LinkedList customObjectives = new LinkedList(); protected LinkedList customObjectiveCounts = new LinkedList(); protected LinkedList customObjectiveDisplays = new LinkedList(); - protected LinkedList> customObjectiveData = new LinkedList>(); + protected LinkedList> customObjectiveData = new LinkedList>(); public LinkedList getBlocksToBreak() { return blocksToBreak; @@ -450,7 +451,7 @@ public class Stage { return customObjectiveDisplays; } - public LinkedList> getCustomObjectiveData() { + public LinkedList> getCustomObjectiveData() { return customObjectiveData; } diff --git a/src/main/java/me/blackvein/quests/prompts/CreateStagePrompt.java b/src/main/java/me/blackvein/quests/prompts/CreateStagePrompt.java index e178cf522..726a43d64 100644 --- a/src/main/java/me/blackvein/quests/prompts/CreateStagePrompt.java +++ b/src/main/java/me/blackvein/quests/prompts/CreateStagePrompt.java @@ -12,12 +12,14 @@ package me.blackvein.quests.prompts; +import java.util.AbstractMap; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.UUID; import org.bukkit.ChatColor; @@ -3868,12 +3870,12 @@ public class CreateStagePrompt extends FixedSetPrompt { if (context.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES) != null) { // The custom objective may already have been added, so let's check that LinkedList list = (LinkedList) context.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES); - LinkedList> datamapList = (LinkedList>) context.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES_DATA); + LinkedList> datamapList = (LinkedList>) context.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES_DATA); LinkedList countList = (LinkedList) context.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES_COUNT); if (list.contains(found.getName()) == false) { // Hasn't been added yet, so let's do it list.add(found.getName()); - datamapList.add(found.getData()); + datamapList.addAll(found.getData()); countList.add(-999); context.setSessionData(pref + CK.S_CUSTOM_OBJECTIVES, list); context.setSessionData(pref + CK.S_CUSTOM_OBJECTIVES_DATA, datamapList); @@ -3884,9 +3886,9 @@ public class CreateStagePrompt extends FixedSetPrompt { } } else { // The custom objective hasn't been added yet, so let's do it - LinkedList> datamapList = new LinkedList>(); + LinkedList> datamapList = new LinkedList>(); LinkedList countList = new LinkedList(); - datamapList.add(found.getData()); + datamapList.addAll(found.getData()); countList.add(-999); LinkedList list = new LinkedList(); list.add(found.getName()); @@ -3974,25 +3976,23 @@ public class CreateStagePrompt extends FixedSetPrompt { public String getPromptText(ConversationContext context) { 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); + LinkedList> datamapList = (LinkedList>) context.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES_DATA); + String objName = list.getLast(); - Map datamap = datamapList.getLast(); text += objName + " -\n"; int index = 1; - LinkedList datamapKeys = new LinkedList(); - for (String key : datamap.keySet()) { - datamapKeys.add(key); - } - Collections.sort(datamapKeys); - for (String dataKey : datamapKeys) { - text += ChatColor.BOLD + "" + ChatColor.DARK_BLUE + index + " - " + ChatColor.RESET + ChatColor.BLUE + dataKey; - if (datamap.get(dataKey) != null) { - text += ChatColor.GREEN + " (" + (String) datamap.get(dataKey) + ")\n"; + + for (Entry datamap : datamapList) { + text += ChatColor.BOLD + "" + ChatColor.DARK_BLUE + index + " - " + ChatColor.RESET + ChatColor.BLUE + datamap.getKey(); + if (datamap.getValue() != null) { + text += ChatColor.GREEN + " (" + (String) datamap.getValue() + ")\n"; } else { text += ChatColor.RED + " (" + Lang.get("valRequired") + ")\n"; } index++; + } + text += ChatColor.BOLD + "" + ChatColor.DARK_BLUE + index + " - " + ChatColor.AQUA + Lang.get("finish"); return text; } @@ -4000,33 +4000,34 @@ 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); - Map datamap = datamapList.getLast(); + LinkedList> datamapList = (LinkedList>) context.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES_DATA); + int numInput; try { numInput = Integer.parseInt(input); } catch (NumberFormatException nfe) { return new ObjectiveCustomDataListPrompt(); } - if (numInput < 1 || numInput > datamap.size() + 1) { + if (numInput < 1 || numInput > datamapList.size() + 1) { return new ObjectiveCustomDataListPrompt(); } - if (numInput < datamap.size() + 1) { + if (numInput < datamapList.size() + 1) { LinkedList datamapKeys = new LinkedList(); - for (String key : datamap.keySet()) { - datamapKeys.add(key); + for (Entry datamap : datamapList) { + datamapKeys.add(datamap.getKey()); } Collections.sort(datamapKeys); String selectedKey = datamapKeys.get(numInput - 1); context.setSessionData(pref + CK.S_CUSTOM_OBJECTIVES_DATA_TEMP, selectedKey); return new ObjectiveCustomDataPrompt(); } else { - if (datamap.containsValue(null)) { - return new ObjectiveCustomDataListPrompt(); - } else { - context.setSessionData(pref + CK.S_CUSTOM_OBJECTIVES_DATA_DESCRIPTIONS, null); - return new CreateStagePrompt(plugin, stageNum, questFactory, citizens); + for (Entry datamap : datamapList) { + if (datamap.getValue() == null) { + return new ObjectiveCustomDataListPrompt(); + } } + context.setSessionData(pref + CK.S_CUSTOM_OBJECTIVES_DATA_DESCRIPTIONS, null); + return new CreateStagePrompt(plugin, stageNum, questFactory, citizens); } } } @@ -4051,9 +4052,19 @@ 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); - Map datamap = datamapList.getLast(); - datamap.put((String) context.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES_DATA_TEMP), input); + 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())); + } + + } + context.setSessionData(pref + CK.S_CUSTOM_OBJECTIVES_DATA, promptList); context.setSessionData(pref + CK.S_CUSTOM_OBJECTIVES_DATA_TEMP, null); return new ObjectiveCustomDataListPrompt(); }