Refactoring for getter/setter methods, part 10. Fixes #625 and #647

This commit is contained in:
BuildTools 2019-01-21 21:26:21 -05:00
parent 570d589f3f
commit 159af2ab97
9 changed files with 118 additions and 119 deletions

View File

@ -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<String, Object> data = new HashMap<String, Object>();
private LinkedList<Entry<String, Object>> data = new LinkedList<Entry<String, Object>>();
private Map<String, String> descriptions = new HashMap<String, String>();
private String countPrompt = "Enter number";
private String display = "%data%: %count%";
private String display = "Progress: %count%";
private boolean showCount = true;
private int count = 1;
@ -46,7 +49,7 @@ public abstract class CustomObjective implements Listener {
this.author = author;
}
public Map<String, Object> getData() {
public LinkedList<Entry<String, Object>> getData() {
return data;
}
@ -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<String, Object> prompt = new AbstractMap.SimpleEntry<String, Object>(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<String, Object> prompt = new AbstractMap.SimpleEntry<String, Object>(name, null);
data.add(prompt);
}
public Map<String, String> 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<String, Object> e = currentStage.customObjectiveData.get(index);
Map<String, Object> m = new HashMap<String, Object>();
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;
}
}

View File

@ -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);
}
/**

View File

@ -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);
}
/**

View File

@ -999,7 +999,7 @@ public class QuestFactory implements ConversationAbandonedListener {
LinkedList<LinkedList<String>> passPhrases;
LinkedList<String> customObjs;
LinkedList<Integer> customObjCounts;
LinkedList<Map<String, Object>> customObjsData;
LinkedList<Entry<String, Object>> 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<String>) cc.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES);
customObjCounts = (LinkedList<Integer>) cc.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES_COUNT);
customObjsData = (LinkedList<Map<String, Object>>) cc.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES_DATA);
customObjsData = (LinkedList<Entry<String, Object>>) 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<String, Object> 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<String> list = new LinkedList<String>();
LinkedList<Integer> countList = new LinkedList<Integer>();
LinkedList<Map<String, Object>> datamapList = new LinkedList<Map<String, Object>>();
LinkedList<Entry<String, Object>> datamapList = new LinkedList<Entry<String, Object>>();
for (int i = 0; i < stage.customObjectives.size(); i++) {
list.add(stage.customObjectives.get(i).getName());
countList.add(stage.customObjectiveCounts.get(i));

View File

@ -786,10 +786,10 @@ public class Quester {
for (Entry<String, Integer> entry : getQuestData(quest).customObjectiveCounts.entrySet()) {
if (co.getName().equals(entry.getKey())) {
String display = co.getDisplay();
Map<String, Object> datamap = getCurrentStage(quest).customObjectiveData.get(index);
for (String key : co.getData().keySet()) {
Entry<String, Object> datamap = getCurrentStage(quest).customObjectiveData.get(index);
for (Entry<String,Object> 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<String, Object> datamap = getCurrentStage(quest).customObjectiveData.get(index);
for (String key : co.getData().keySet()) {
message = message.replace("%" + ((String) key) + "%", (String) datamap.get(key));
}
Entry<String, Object> 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));
}

View File

@ -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,15 +1990,20 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener
continue;
} else {
ConfigurationSection sec2 = sec.getConfigurationSection(path + ".data");
Map<String, Object> data = populateCustoms(sec2, found.get().getData()); // Added in Github PR #554
for (Entry<String,Object> prompt : found.get().getData()) {
Entry<String, Object> 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);
}
}
}
}
}
Rewards rews = quest.getRewards();
if (config.contains("quests." + questKey + ".rewards.custom-rewards")) {
ConfigurationSection sec = config.getConfigurationSection("quests." + questKey + ".rewards.custom-rewards");
@ -2049,10 +2055,9 @@ 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<String, Object> populateCustoms(ConfigurationSection sec2, Map<String, Object> datamap) {
Map<String,Object> data = new HashMap<String,Object>();
if (sec2 != null) {
@ -2063,6 +2068,20 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener
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<String, Object> populateCustoms(ConfigurationSection sec2, Entry<String, Object> datamap) {
String key = null;;
Object value = null;;
if (sec2 != null) {
key = datamap.getKey();
value = datamap.getValue();
}
return new AbstractMap.SimpleEntry<String, Object>(key, sec2.contains(key) ? sec2.get(key) : value != null ? value : new String());
}
private void stageFailed(String msg) throws StageFailedException {
stageFailed(new String[] { msg });
}

View File

@ -103,8 +103,7 @@ public class Requirements {
public Map<String, Map<String, Object>> getCustomRequirements() {
return customRequirements;
}
protected void setCustomRequirements(
Map<String, Map<String, Object>> customRequirements) {
protected void setCustomRequirements(Map<String, Map<String, Object>> customRequirements) {
this.customRequirements = customRequirements;
}
public String getFailRequirements() {

View File

@ -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<CustomObjective> customObjectives = new LinkedList<CustomObjective>();
protected LinkedList<Integer> customObjectiveCounts = new LinkedList<Integer>();
protected LinkedList<String> customObjectiveDisplays = new LinkedList<String>();
protected LinkedList<Map<String, Object>> customObjectiveData = new LinkedList<Map<String, Object>>();
protected LinkedList<Entry<String, Object>> customObjectiveData = new LinkedList<Entry<String, Object>>();
public LinkedList<ItemStack> getBlocksToBreak() {
return blocksToBreak;
@ -450,7 +451,7 @@ public class Stage {
return customObjectiveDisplays;
}
public LinkedList<Map<String, Object>> getCustomObjectiveData() {
public LinkedList<Entry<String, Object>> getCustomObjectiveData() {
return customObjectiveData;
}

View File

@ -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<String> list = (LinkedList<String>) context.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES);
LinkedList<Map<String, Object>> datamapList = (LinkedList<Map<String, Object>>) context.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES_DATA);
LinkedList<Entry<String, Object>> datamapList = (LinkedList<Entry<String, Object>>) context.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES_DATA);
LinkedList<Integer> countList = (LinkedList<Integer>) 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<Map<String, Object>> datamapList = new LinkedList<Map<String, Object>>();
LinkedList<Entry<String, Object>> datamapList = new LinkedList<Entry<String, Object>>();
LinkedList<Integer> countList = new LinkedList<Integer>();
datamapList.add(found.getData());
datamapList.addAll(found.getData());
countList.add(-999);
LinkedList<String> list = new LinkedList<String>();
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<String> list = (LinkedList<String>) context.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES);
LinkedList<Map<String, Object>> datamapList = (LinkedList<Map<String, Object>>) context.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES_DATA);
LinkedList<Entry<String, Object>> datamapList = (LinkedList<Entry<String, Object>>) context.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES_DATA);
String objName = list.getLast();
Map<String, Object> datamap = datamapList.getLast();
text += objName + " -\n";
int index = 1;
LinkedList<String> datamapKeys = new LinkedList<String>();
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<String, Object> 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,36 +4000,37 @@ public class CreateStagePrompt extends FixedSetPrompt {
@Override
public Prompt acceptInput(ConversationContext context, String input) {
@SuppressWarnings("unchecked")
LinkedList<Map<String, Object>> datamapList = (LinkedList<Map<String, Object>>) context.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES_DATA);
Map<String, Object> datamap = datamapList.getLast();
LinkedList<Entry<String, Object>> datamapList = (LinkedList<Entry<String, Object>>) 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<String> datamapKeys = new LinkedList<String>();
for (String key : datamap.keySet()) {
datamapKeys.add(key);
for (Entry<String, Object> 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)) {
for (Entry<String, Object> datamap : datamapList) {
if (datamap.getValue() == null) {
return new ObjectiveCustomDataListPrompt();
} else {
}
}
context.setSessionData(pref + CK.S_CUSTOM_OBJECTIVES_DATA_DESCRIPTIONS, null);
return new CreateStagePrompt(plugin, stageNum, questFactory, citizens);
}
}
}
}
private class ObjectiveCustomDataPrompt extends StringPrompt {
@ -4051,9 +4052,19 @@ public class CreateStagePrompt extends FixedSetPrompt {
@Override
public Prompt acceptInput(ConversationContext context, String input) {
@SuppressWarnings("unchecked")
LinkedList<Map<String, Object>> datamapList = (LinkedList<Map<String, Object>>) context.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES_DATA);
Map<String, Object> datamap = datamapList.getLast();
datamap.put((String) context.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES_DATA_TEMP), input);
LinkedList<Entry<String, Object>> datamapList = (LinkedList<Entry<String, Object>>) context.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES_DATA);
LinkedList<Entry<String, Object>> promptList = new LinkedList<Entry<String, Object>>();
String temp = (String) context.getSessionData(pref + CK.S_CUSTOM_OBJECTIVES_DATA_TEMP);
for (Entry<String, Object> datamap : datamapList) {
if (datamap.getKey().equals(temp)) {
promptList.add(new AbstractMap.SimpleEntry<String, Object>(datamap.getKey(), input));
} else {
promptList.add(new AbstractMap.SimpleEntry<String, Object>(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();
}