From 570d589f3fe9bedad147258dca8cb070e24638d9 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sat, 19 Jan 2019 13:14:10 -0500 Subject: [PATCH] Refactoring for getter/setter methods, part 9 --- README.md | 7 +- .../me/blackvein/quests/CustomObjective.java | 71 ++++++++----------- .../blackvein/quests/CustomRequirement.java | 45 ++++++++++-- .../me/blackvein/quests/CustomReward.java | 57 ++++++++++++--- src/main/java/me/blackvein/quests/Quests.java | 12 ++-- .../quests/prompts/RequirementsPrompt.java | 15 ++-- .../quests/prompts/RewardsPrompt.java | 15 ++-- 7 files changed, 145 insertions(+), 77 deletions(-) diff --git a/README.md b/README.md index c592adeda..9c3a471d0 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ -# Quests +# Quests [![Build Status](http://ci.ac3-servers.eu/buildStatus/icon?job=Quests)](http://ci.ac3-servers.eu/job/Quests/) Quests plugin for Spigot. This work depends on contributions from coders like you! -## Download [![Build Status](http://ci.ac3-servers.eu/buildStatus/icon?job=Quests)](http://ci.ac3-servers.eu/job/Quests/) +## Download Distributed exclusively through Spigot: https://www.spigotmc.org/resources/quests.3711/ @@ -12,8 +12,7 @@ Documentation can be found in the Wiki: https://github.com/FlyingPikachu/Quests/ ## Compile -Building requires Java 8 and Maven: -https://github.com/FlyingPikachu/Quests/wiki/Master-%E2%80%90-Plugin-Compilation +Requires Java 8 and Maven: https://github.com/FlyingPikachu/Quests/wiki/Master-%E2%80%90-Plugin-Compilation ## Localization [![Crowdin](https://d322cqt584bo4o.cloudfront.net/translate-quests/localized.svg)](https://crowdin.com/project/translate-quests) diff --git a/src/main/java/me/blackvein/quests/CustomObjective.java b/src/main/java/me/blackvein/quests/CustomObjective.java index 3e82bbb2c..e20b97487 100644 --- a/src/main/java/me/blackvein/quests/CustomObjective.java +++ b/src/main/java/me/blackvein/quests/CustomObjective.java @@ -25,8 +25,8 @@ public abstract class CustomObjective implements Listener { private String author = null; private Map data = new HashMap(); private Map descriptions = new HashMap(); - private String countPrompt = "null"; - private String display = "null"; + private String countPrompt = "Enter number"; + private String display = "%data%: %count%"; private boolean showCount = true; private int count = 1; @@ -48,56 +48,45 @@ public abstract class CustomObjective implements Listener { public Map getData() { return data; + } + + /** + * Add a new prompt

+ * + * Note that the "defaultValue" Object will be cast to a String internally + * + * @param title Prompt name + * @param description Description of expected input + * @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); } /** - * Add a detailed piece of datum to the data map + * Set the title of a prompt * - * @param name - * @param o - */ - public void addDatum(String name, Object o) { - if (o == null) { - data.put(name, o); - } else { - data.put(name, null); - } - } - /** - * Add a blank piece of datum to the data map - * - * @param name - */ - public void addDatum(String name) { - data.put(name, null); - } - - /** - * Add a detailed piece of datum to the data map - * - * @param name - * @deprecated use addDatum(name, o) - */ - public void addData(String name, Object o) { - addDatum(name, o); - } - - /** - * Add a blank piece of datum to the data map - * - * @param name - * @deprecated use addDatum(name) + * @param name Prompt title + * @deprecated use addPrompt(name, description) */ public void addData(String name) { - addDatum(name); + data.put(name, null); } public Map getDescriptions() { return descriptions; } - public void addDescription(String data, String description) { - descriptions.put(data, description); + /** + * Set the description for the specified prompt + * + * @param name Prompt title + * @param description Description of expected input + * @deprecated use addTaskPrompt(name, description) + */ + public void addDescription(String name, String description) { + descriptions.put(name, description); } public int getCount() { @@ -108,7 +97,7 @@ public abstract class CustomObjective implements Listener { this.count = count; } - public String getCountPrompt() { + public String getCountPrompt() { return countPrompt; } diff --git a/src/main/java/me/blackvein/quests/CustomRequirement.java b/src/main/java/me/blackvein/quests/CustomRequirement.java index b14fd3ea6..4fa7e91bd 100644 --- a/src/main/java/me/blackvein/quests/CustomRequirement.java +++ b/src/main/java/me/blackvein/quests/CustomRequirement.java @@ -21,8 +21,8 @@ public abstract class CustomRequirement { private String name = null; private String author = null; - public final Map datamap = new HashMap(); - public final Map descriptions = new HashMap(); + private Map data = new HashMap(); + private Map descriptions = new HashMap(); public abstract boolean testRequirement(Player p, Map m); @@ -42,11 +42,46 @@ public abstract class CustomRequirement { this.author = author; } + public Map getData() { + return data; + } + + /** + * Add a new prompt

+ * + * Note that the "defaultValue" Object will be cast to a String internally + * + * @param title Prompt name + * @param description Description of expected input + * @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); + } + + /** + * Set the title of a prompt + * + * @param name Prompt title + * @deprecated use addPrompt(name, description) + */ public void addData(String name) { - datamap.put(name, null); + data.put(name, null); } - public void addDescription(String data, String description) { - descriptions.put(data, description); + public Map getDescriptions() { + return descriptions; + } + + /** + * Set the description for the specified prompt + * + * @param name Prompt title + * @param description Description of expected input + * @deprecated use addTaskPrompt(name, description) + */ + public void addDescription(String name, String description) { + descriptions.put(name, description); } } diff --git a/src/main/java/me/blackvein/quests/CustomReward.java b/src/main/java/me/blackvein/quests/CustomReward.java index 3ef756bea..4bff1b4a0 100644 --- a/src/main/java/me/blackvein/quests/CustomReward.java +++ b/src/main/java/me/blackvein/quests/CustomReward.java @@ -22,8 +22,8 @@ public abstract class CustomReward { private String name = null; private String author = null; private String rewardName = null; - public final Map datamap = new HashMap(); - public final Map descriptions = new HashMap(); + private Map data = new HashMap(); + private Map descriptions = new HashMap(); public abstract void giveReward(Player p, Map m); @@ -42,20 +42,55 @@ public abstract class CustomReward { public void setAuthor(String author) { this.author = author; } - - public void addData(String name) { - datamap.put(name, null); + + public Map getData() { + return data; } - public void addDescription(String data, String description) { - descriptions.put(data, description); + /** + * Add a new prompt

+ * + * Note that the "defaultValue" Object will be cast to a String internally + * + * @param title Prompt name + * @param description Description of expected input + * @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); + } + + /** + * Set the title of a prompt + * + * @param name Prompt title + * @deprecated use addPrompt(name, description) + */ + public void addData(String name) { + data.put(name, null); + } + + public Map getDescriptions() { + return descriptions; + } + + /** + * Set the description for the specified prompt + * + * @param name Prompt title + * @param description Description of expected input + * @deprecated use addTaskPrompt(name, description) + */ + public void addDescription(String name, String description) { + descriptions.put(name, description); + } + + public String getRewardName() { + return rewardName; } public void setRewardName(String name) { rewardName = name; } - - public String getRewardName() { - return rewardName; - } } diff --git a/src/main/java/me/blackvein/quests/Quests.java b/src/main/java/me/blackvein/quests/Quests.java index 8a1c65eac..2f9885958 100644 --- a/src/main/java/me/blackvein/quests/Quests.java +++ b/src/main/java/me/blackvein/quests/Quests.java @@ -2016,7 +2016,7 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener continue; } else { ConfigurationSection sec2 = sec.getConfigurationSection(path + ".data"); - Map data=populateCustoms(sec2,found.get().datamap); + Map data = populateCustoms(sec2, found.get().getData()); temp.put(name, data); } } @@ -2040,7 +2040,7 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener skipQuestProcess((String) null); // null bc we warn, not severe for this one } else { ConfigurationSection sec2 = sec.getConfigurationSection(path + ".data"); - Map data=populateCustoms(sec2,found.get().datamap); + Map data = populateCustoms(sec2,found.get().getData()); temp.put(name, data); } } @@ -2054,10 +2054,10 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener */ static Map populateCustoms(ConfigurationSection sec2,Map datamap) { - Mapdata=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()); + 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()); } } return data; diff --git a/src/main/java/me/blackvein/quests/prompts/RequirementsPrompt.java b/src/main/java/me/blackvein/quests/prompts/RequirementsPrompt.java index f1d346f46..bb32685f0 100644 --- a/src/main/java/me/blackvein/quests/prompts/RequirementsPrompt.java +++ b/src/main/java/me/blackvein/quests/prompts/RequirementsPrompt.java @@ -490,6 +490,7 @@ public class RequirementsPrompt extends FixedSetPrompt { public Prompt acceptInput(ConversationContext context, String input) { if (input.equalsIgnoreCase(Lang.get("cmdCancel")) == false && input.equalsIgnoreCase(Lang.get("cmdClear")) == false) { CustomRequirement found = null; + // Check if we have a custom requirement with the specified name for (CustomRequirement cr : plugin.getCustomRequirements()) { if (cr.getName().equalsIgnoreCase(input)) { found = cr; @@ -497,6 +498,7 @@ public class RequirementsPrompt extends FixedSetPrompt { } } if (found == null) { + // No? Check again, but with locale sensitivity for (CustomRequirement cr : plugin.getCustomRequirements()) { if (cr.getName().toLowerCase().contains(input.toLowerCase())) { found = cr; @@ -506,31 +508,34 @@ public class RequirementsPrompt extends FixedSetPrompt { } if (found != null) { if (context.getSessionData(CK.REQ_CUSTOM) != null) { + // The custom requirement may already have been added, so let's check that LinkedList list = (LinkedList) context.getSessionData(CK.REQ_CUSTOM); LinkedList> datamapList = (LinkedList>) context.getSessionData(CK.REQ_CUSTOM_DATA); if (list.contains(found.getName()) == false) { + // Hasn't been added yet, so let's do it list.add(found.getName()); - datamapList.add(found.datamap); + datamapList.add(found.getData()); context.setSessionData(CK.REQ_CUSTOM, list); context.setSessionData(CK.REQ_CUSTOM_DATA, datamapList); } else { + // Already added, so inform user context.getForWhom().sendRawMessage(ChatColor.YELLOW + Lang.get("reqCustomAlreadyAdded")); return new CustomRequirementsPrompt(); } } else { + // The custom requirement hasn't been added yet, so let's do it LinkedList> datamapList = new LinkedList>(); - datamapList.add(found.datamap); + datamapList.add(found.getData()); LinkedList list = new LinkedList(); list.add(found.getName()); context.setSessionData(CK.REQ_CUSTOM, list); context.setSessionData(CK.REQ_CUSTOM_DATA, datamapList); } // Send user to the custom data prompt if there is any needed - if (found.datamap.isEmpty() == false) { - context.setSessionData(CK.REQ_CUSTOM_DATA_DESCRIPTIONS, found.descriptions); + if (found.getData().isEmpty() == false) { + context.setSessionData(CK.REQ_CUSTOM_DATA_DESCRIPTIONS, found.getDescriptions()); return new RequirementCustomDataListPrompt(); } - // } else { context.getForWhom().sendRawMessage(ChatColor.YELLOW + Lang.get("reqCustomNotFound")); return new CustomRequirementsPrompt(); diff --git a/src/main/java/me/blackvein/quests/prompts/RewardsPrompt.java b/src/main/java/me/blackvein/quests/prompts/RewardsPrompt.java index 4d5a2cceb..b82b8875c 100644 --- a/src/main/java/me/blackvein/quests/prompts/RewardsPrompt.java +++ b/src/main/java/me/blackvein/quests/prompts/RewardsPrompt.java @@ -769,6 +769,7 @@ public class RewardsPrompt extends FixedSetPrompt { public Prompt acceptInput(ConversationContext context, String input) { if (input.equalsIgnoreCase(Lang.get("cmdCancel")) == false && input.equalsIgnoreCase(Lang.get("cmdClear")) == false) { CustomReward found = null; + // Check if we have a custom reward with the specified name for (CustomReward cr : plugin.getCustomRewards()) { if (cr.getName().equalsIgnoreCase(input)) { found = cr; @@ -776,6 +777,7 @@ public class RewardsPrompt extends FixedSetPrompt { } } if (found == null) { + // No? Check again, but with locale sensitivity for (CustomReward cr : plugin.getCustomRewards()) { if (cr.getName().toLowerCase().contains(input.toLowerCase())) { found = cr; @@ -785,31 +787,34 @@ public class RewardsPrompt extends FixedSetPrompt { } if (found != null) { if (context.getSessionData(CK.REW_CUSTOM) != null) { + // The custom reward may already have been added, so let's check that LinkedList list = (LinkedList) context.getSessionData(CK.REW_CUSTOM); LinkedList> datamapList = (LinkedList>) context.getSessionData(CK.REW_CUSTOM_DATA); if (list.contains(found.getName()) == false) { + // Hasn't been added yet, so let's do it list.add(found.getName()); - datamapList.add(found.datamap); + datamapList.add(found.getData()); context.setSessionData(CK.REW_CUSTOM, list); context.setSessionData(CK.REW_CUSTOM_DATA, datamapList); } else { + // Already added, so inform user context.getForWhom().sendRawMessage(ChatColor.YELLOW + Lang.get("rewCustomAlreadyAdded")); return new CustomRewardsPrompt(); } } else { + // The custom reward hasn't been added yet, so let's do it LinkedList> datamapList = new LinkedList>(); - datamapList.add(found.datamap); + datamapList.add(found.getData()); LinkedList list = new LinkedList(); list.add(found.getName()); context.setSessionData(CK.REW_CUSTOM, list); context.setSessionData(CK.REW_CUSTOM_DATA, datamapList); } // Send user to the custom data prompt if there is any needed - if (found.datamap.isEmpty() == false) { - context.setSessionData(CK.REW_CUSTOM_DATA_DESCRIPTIONS, found.descriptions); + if (found.getData().isEmpty() == false) { + context.setSessionData(CK.REW_CUSTOM_DATA_DESCRIPTIONS, found.getDescriptions()); return new RewardCustomDataListPrompt(); } - // } else { context.getForWhom().sendRawMessage(ChatColor.YELLOW + Lang.get("rewCustomNotFound")); return new CustomRewardsPrompt();