Added basic custom requirement API

This commit is contained in:
Blackvein 2014-01-04 02:15:52 -08:00
parent 8c31fabadc
commit 65f8f72d9f
7 changed files with 366 additions and 136 deletions

BIN
lib/CustomRequirement.jar Normal file

Binary file not shown.

View File

@ -0,0 +1,28 @@
package me.blackvein.quests;
import org.bukkit.entity.Player;
public abstract class CustomRequirement {
private String name = null;
private String author = null;
public abstract boolean testRequirement(Player p);
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}

View File

@ -52,6 +52,7 @@ public class Quest {
List<Integer> mcMMOAmountReqs = new LinkedList<Integer>(); List<Integer> mcMMOAmountReqs = new LinkedList<Integer>();
String heroesPrimaryClassReq = null; String heroesPrimaryClassReq = null;
String heroesSecondaryClassReq = null; String heroesSecondaryClassReq = null;
List<String> customRequirements = new LinkedList<String>();
public String failRequirements = null; public String failRequirements = null;
// //
//Rewards //Rewards
@ -225,6 +226,25 @@ public class Quest {
} }
for (String s : customRequirements){
CustomRequirement found = null;
for(CustomRequirement cr : plugin.customRequirements){
if(cr.getName().equalsIgnoreCase(s)){
found = cr;
break;
}
}
if(found != null){
if(found.testRequirement(player) == false)
return false;
}else{
Quests.printWarning("[Quests] Quester \"" + player.getName() + "\" attempted to take Quest \"" + name + "\", but the Custom Requirement \"" + s + "\" could not be found. Does it still exist?");
}
}
if (quester.questPoints < questPointsReq) { if (quester.questPoints < questPointsReq) {
return false; return false;
} }
@ -609,6 +629,10 @@ public class Quest {
return false; return false;
} }
if (other.customRequirements.equals(customRequirements) == false){
return false;
}
if (other.permissions.equals(permissions) == false) { if (other.permissions.equals(permissions) == false) {
return false; return false;
} }

View File

@ -1020,6 +1020,7 @@ public class QuestFactory implements ConversationAbandonedListener, ColorUtil {
LinkedList<Integer> mcMMOAmountReqs = null; LinkedList<Integer> mcMMOAmountReqs = null;
String heroesPrimaryReq = null; String heroesPrimaryReq = null;
String heroesSecondaryReq = null; String heroesSecondaryReq = null;
LinkedList<String> customReqs = null;
String failMessage = null; String failMessage = null;
Integer moneyRew = null; Integer moneyRew = null;
@ -1087,6 +1088,10 @@ public class QuestFactory implements ConversationAbandonedListener, ColorUtil {
heroesSecondaryReq = (String) cc.getSessionData(CK.REQ_HEROES_SECONDARY_CLASS); heroesSecondaryReq = (String) cc.getSessionData(CK.REQ_HEROES_SECONDARY_CLASS);
} }
if (cc.getSessionData(CK.REQ_CUSTOM) != null) {
customReqs = (LinkedList<String>) cc.getSessionData(CK.REQ_CUSTOM);
}
if (cc.getSessionData(CK.Q_FAIL_MESSAGE) != null) { if (cc.getSessionData(CK.Q_FAIL_MESSAGE) != null) {
failMessage = (String) cc.getSessionData(CK.Q_FAIL_MESSAGE); failMessage = (String) cc.getSessionData(CK.Q_FAIL_MESSAGE);
} }
@ -1160,7 +1165,7 @@ public class QuestFactory implements ConversationAbandonedListener, ColorUtil {
cs.set("initial-event", initialEvent); cs.set("initial-event", initialEvent);
cs.set("region", region); cs.set("region", region);
if (moneyReq != null || questPointsReq != null || itemReqs != null && itemReqs.isEmpty() == false || permReqs != null && permReqs.isEmpty() == false || (questReqs != null && questReqs.isEmpty() == false) || (questBlocks != null && questBlocks.isEmpty() == false) || (mcMMOSkillReqs != null && mcMMOSkillReqs.isEmpty() == false) || heroesPrimaryReq != null || heroesSecondaryReq != null) { if (moneyReq != null || questPointsReq != null || itemReqs != null && itemReqs.isEmpty() == false || permReqs != null && permReqs.isEmpty() == false || (questReqs != null && questReqs.isEmpty() == false) || (questBlocks != null && questBlocks.isEmpty() == false) || (mcMMOSkillReqs != null && mcMMOSkillReqs.isEmpty() == false) || heroesPrimaryReq != null || heroesSecondaryReq != null || customReqs != null) {
ConfigurationSection reqs = cs.createSection("requirements"); ConfigurationSection reqs = cs.createSection("requirements");
List<String> items = new LinkedList<String>(); List<String> items = new LinkedList<String>();
@ -1183,6 +1188,7 @@ public class QuestFactory implements ConversationAbandonedListener, ColorUtil {
reqs.set("mcmmo-amounts", mcMMOAmountReqs); reqs.set("mcmmo-amounts", mcMMOAmountReqs);
reqs.set("heroes-primary-class", heroesPrimaryReq); reqs.set("heroes-primary-class", heroesPrimaryReq);
reqs.set("heroes-secondary-class", heroesSecondaryReq); reqs.set("heroes-secondary-class", heroesSecondaryReq);
reqs.set("custom-requirements", customReqs);
reqs.set("fail-requirement-message", failMessage); reqs.set("fail-requirement-message", failMessage);
} else { } else {

View File

@ -82,10 +82,15 @@ import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.ProtectedRegion; import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.lang.reflect.Constructor;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLConnection; import java.net.URLConnection;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.sql.Date; import java.sql.Date;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class Quests extends JavaPlugin implements ConversationAbandonedListener, ColorUtil { public class Quests extends JavaPlugin implements ConversationAbandonedListener, ColorUtil {
@ -108,6 +113,7 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener,
public static String effect = "note"; public static String effect = "note";
public final Map<String, Quester> questers = new HashMap<String, Quester>(); public final Map<String, Quester> questers = new HashMap<String, Quester>();
public final List<String> questerBlacklist = new LinkedList<String>(); public final List<String> questerBlacklist = new LinkedList<String>();
public final List<CustomRequirement> customRequirements = new LinkedList<CustomRequirement>();
public final LinkedList<Quest> quests = new LinkedList<Quest>(); public final LinkedList<Quest> quests = new LinkedList<Quest>();
public final LinkedList<Event> events = new LinkedList<Event>(); public final LinkedList<Event> events = new LinkedList<Event>();
public final LinkedList<NPC> questNPCs = new LinkedList<NPC>(); public final LinkedList<NPC> questNPCs = new LinkedList<NPC>();
@ -223,6 +229,7 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener,
} }
loadConfig(); loadConfig();
loadModules();
lang = new Lang(this); lang = new Lang(this);
lang.initPhrases(); lang.initPhrases();
lang.save(); lang.save();
@ -423,6 +430,66 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener,
} }
public void loadModules() {
File f = new File(this.getDataFolder(), "modules");
if(f.exists() && f.isDirectory()){
File[] modules = f.listFiles();
for(File module : modules){
loadModule(module);
}
}else {
f.mkdir();
}
}
public void loadModule(File jar){
try{
JarFile jarFile = new JarFile(jar);
Enumeration e = jarFile.entries();
URL[] urls = { new URL("jar:file:" + jar.getPath() + "!/") };
ClassLoader cl = URLClassLoader.newInstance(urls, getClassLoader());
while (e.hasMoreElements()) {
JarEntry je = (JarEntry) e.nextElement();
if(je.isDirectory() || !je.getName().endsWith(".class")){
continue;
}
String className = je.getName().substring(0,je.getName().length()-6);
className = className.replace('/', '.');
Class<?> c = Class.forName(className, true, cl);
Class<? extends CustomRequirement> requirementClass = c.asSubclass(CustomRequirement.class);
Constructor<? extends CustomRequirement> cstrctr = requirementClass.getConstructor();
CustomRequirement requirement = cstrctr.newInstance();
customRequirements.add(requirement);
String name = requirement.getName() == null ? "[" + jar.getName() + "]" : requirement.getName();
String author = requirement.getAuthor() == null ? "[Unknown]" : requirement.getAuthor();
printInfo("[Quests] Loaded Module: " + name + " by " + author);
}
}catch (Exception e){
printSevere("[Quests] Error: Unable to load module from file: " + jar.getName());
if(debug){
printSevere("[Quests] Error log:");
e.printStackTrace();
}
}
}
public void printHelp(Player player) { public void printHelp(Player player) {
player.sendMessage(GOLD + "- Quests -"); player.sendMessage(GOLD + "- Quests -");
@ -2210,6 +2277,27 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener,
} }
if (config.contains("quests." + s + ".requirements.custom-requirements")) {
quest.customRequirements = config.getStringList("quests." + s + ".requirements.custom-requirements");
for(String req : quest.customRequirements){
boolean found = false;
for(CustomRequirement cr : customRequirements){
if(cr.getName().equalsIgnoreCase(req)){
found = true;
break;
}
}
if(!found){
printWarning("[Quests] Custom requirement \"" + req + "\" for Quest \"" + quest.name + "\" could not be found!");
}
}
}
} }
quest.plugin = this; quest.plugin = this;

View File

@ -6,6 +6,7 @@ import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import me.blackvein.quests.CustomRequirement;
import me.blackvein.quests.util.ColorUtil; import me.blackvein.quests.util.ColorUtil;
import me.blackvein.quests.Quest; import me.blackvein.quests.Quest;
@ -30,7 +31,7 @@ public class RequirementsPrompt extends FixedSetPrompt implements ColorUtil {
public RequirementsPrompt(Quests plugin, QuestFactory qf) { public RequirementsPrompt(Quests plugin, QuestFactory qf) {
super("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"); super("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11");
quests = plugin; quests = plugin;
factory = qf; factory = qf;
@ -135,15 +136,27 @@ public class RequirementsPrompt extends FixedSetPrompt implements ColorUtil {
text += GRAY + "8 - Set Heroes requirements (Heroes not installed)\n"; text += GRAY + "8 - Set Heroes requirements (Heroes not installed)\n";
} }
if (context.getSessionData(CK.REQ_MONEY) == null && context.getSessionData(CK.REQ_QUEST_POINTS) == null && context.getSessionData(CK.REQ_QUEST_BLOCK) == null && context.getSessionData(CK.REQ_ITEMS) == null && context.getSessionData(CK.REQ_PERMISSION) == null && context.getSessionData(CK.REQ_QUEST) == null && context.getSessionData(CK.REQ_QUEST_BLOCK) == null && context.getSessionData(CK.REQ_MCMMO_SKILLS) == null && context.getSessionData(CK.REQ_HEROES_PRIMARY_CLASS) == null && context.getSessionData(CK.REQ_HEROES_SECONDARY_CLASS) == null) { if (context.getSessionData(CK.REQ_CUSTOM) == null) {
text += GRAY + "" + BOLD + "9 - " + RESET + GRAY + "Set fail requirements message (No requirements set)\n"; text += BLUE + "" + BOLD + "9 - " + RESET + ITALIC + PURPLE + "Custom Requirements (None set)\n";
} else if (context.getSessionData(CK.Q_FAIL_MESSAGE) == null) {
text += RED + "" + BOLD + "9 - " + RESET + RED + "Set fail requirements message (Required)\n";
} else { } else {
text += BLUE + "" + BOLD + "9 - " + RESET + YELLOW + "Set fail requirements message" + GRAY + "(" + AQUA + "\"" + context.getSessionData(CK.Q_FAIL_MESSAGE) + "\"" + GRAY + ")\n"; text += BLUE + "" + BOLD + "9 - " + RESET + ITALIC + PURPLE + "Custom Requirements\n";
LinkedList<String> customReqs = (LinkedList<String>) context.getSessionData(CK.REQ_CUSTOM);
for(String s : customReqs){
text += RESET + "" + PURPLE + " - " + PINK + s + "\n";
}
} }
text += GREEN + "" + BOLD + "10" + RESET + YELLOW + " - Done"; if (context.getSessionData(CK.REQ_MONEY) == null && context.getSessionData(CK.REQ_QUEST_POINTS) == null && context.getSessionData(CK.REQ_QUEST_BLOCK) == null && context.getSessionData(CK.REQ_ITEMS) == null && context.getSessionData(CK.REQ_PERMISSION) == null && context.getSessionData(CK.REQ_QUEST) == null && context.getSessionData(CK.REQ_QUEST_BLOCK) == null && context.getSessionData(CK.REQ_MCMMO_SKILLS) == null && context.getSessionData(CK.REQ_HEROES_PRIMARY_CLASS) == null && context.getSessionData(CK.REQ_HEROES_SECONDARY_CLASS) == null && context.getSessionData(CK.REQ_CUSTOM) == null) {
text += GRAY + "" + BOLD + "10 - " + RESET + GRAY + "Set fail requirements message (No requirements set)\n";
} else if (context.getSessionData(CK.Q_FAIL_MESSAGE) == null) {
text += RED + "" + BOLD + "10 - " + RESET + RED + "Set fail requirements message (Required)\n";
} else {
text += BLUE + "" + BOLD + "10 - " + RESET + YELLOW + "Set fail requirements message" + GRAY + "(" + AQUA + "\"" + context.getSessionData(CK.Q_FAIL_MESSAGE) + "\"" + GRAY + ")\n";
}
text += GREEN + "" + BOLD + "11" + RESET + YELLOW + " - Done";
return text; return text;
@ -177,9 +190,11 @@ public class RequirementsPrompt extends FixedSetPrompt implements ColorUtil {
return new RequirementsPrompt(quests, factory); return new RequirementsPrompt(quests, factory);
} }
} else if (input.equalsIgnoreCase("9")) { } else if (input.equalsIgnoreCase("9")) {
return new FailMessagePrompt(); return new CustomRequirementsPrompt();
} else if (input.equalsIgnoreCase("10")) { } else if (input.equalsIgnoreCase("10")) {
if (context.getSessionData(CK.REQ_MONEY) != null || context.getSessionData(CK.REQ_QUEST_POINTS) != null || context.getSessionData(CK.REQ_ITEMS) != null || context.getSessionData(CK.REQ_PERMISSION) != null || context.getSessionData(CK.REQ_QUEST) != null || context.getSessionData(CK.REQ_QUEST_BLOCK) != null || context.getSessionData(CK.REQ_MCMMO_SKILLS) != null || context.getSessionData(CK.REQ_HEROES_PRIMARY_CLASS) != null || context.getSessionData(CK.REQ_HEROES_SECONDARY_CLASS) != null) { return new FailMessagePrompt();
} else if (input.equalsIgnoreCase("11")) {
if (context.getSessionData(CK.REQ_MONEY) != null || context.getSessionData(CK.REQ_QUEST_POINTS) != null || context.getSessionData(CK.REQ_ITEMS) != null || context.getSessionData(CK.REQ_PERMISSION) != null || context.getSessionData(CK.REQ_QUEST) != null || context.getSessionData(CK.REQ_QUEST_BLOCK) != null || context.getSessionData(CK.REQ_MCMMO_SKILLS) != null || context.getSessionData(CK.REQ_HEROES_PRIMARY_CLASS) != null || context.getSessionData(CK.REQ_HEROES_SECONDARY_CLASS) != null || context.getSessionData(CK.REQ_CUSTOM) != null) {
if (context.getSessionData(CK.Q_FAIL_MESSAGE) == null) { if (context.getSessionData(CK.Q_FAIL_MESSAGE) == null) {
context.getForWhom().sendRawMessage(RED + "You must set a fail requirements message!"); context.getForWhom().sendRawMessage(RED + "You must set a fail requirements message!");
@ -529,6 +544,74 @@ public class RequirementsPrompt extends FixedSetPrompt implements ColorUtil {
} }
} }
private class CustomRequirementsPrompt extends StringPrompt {
@Override
public String getPromptText(ConversationContext context) {
String text = PINK + "- Custom Requirements -\n";
if(quests.customRequirements.isEmpty()){
text += BOLD + "" + PURPLE + "(No modules loaded)";
}else {
for(CustomRequirement cr : quests.customRequirements)
text += PURPLE + " - " + cr.getName() + "\n";
}
return text + YELLOW + "Enter the name of a custom requirement to add, or enter \'clear\' to clear all custom requirements, or \'cancel\' to return.";
}
@Override
public Prompt acceptInput(ConversationContext context, String input) {
if (input.equalsIgnoreCase("cancel") == false && input.equalsIgnoreCase("clear") == false) {
CustomRequirement found = null;
for(CustomRequirement cr : quests.customRequirements){
if(cr.getName().equalsIgnoreCase(input)){
found = cr;
break;
}
}
if(found == null){
for(CustomRequirement cr : quests.customRequirements){
if(cr.getName().toLowerCase().contains(input.toLowerCase())){
found = cr;
break;
}
}
}
if(found != null){
if(context.getSessionData(CK.REQ_CUSTOM) != null){
LinkedList<String> list = (LinkedList<String>) context.getSessionData(CK.REQ_CUSTOM);
if(list.contains(found.getName()) == false){
list.add(found.getName());
}else{
context.getForWhom().sendRawMessage(YELLOW + "That custom requirement has already been added!");
return new CustomRequirementsPrompt();
}
}else{
LinkedList<String> list = new LinkedList<String>();
list.add(found.getName());
context.setSessionData(CK.REQ_CUSTOM, list);
}
}else{
context.getForWhom().sendRawMessage(YELLOW + "Custom requirement module not found.");
return new CustomRequirementsPrompt();
}
} else if (input.equalsIgnoreCase("clear")) {
context.setSessionData(CK.REQ_CUSTOM, null);
context.getForWhom().sendRawMessage(YELLOW + "Custom requirements cleared.");
}
return new RequirementsPrompt(quests, factory);
}
}
private class mcMMOPrompt extends FixedSetPrompt { private class mcMMOPrompt extends FixedSetPrompt {
public mcMMOPrompt() { public mcMMOPrompt() {

View File

@ -28,7 +28,8 @@ public class CK {
public static final String REQ_HEROES_PRIMARY_CLASS = "heroesPrimaryClassReq"; public static final String REQ_HEROES_PRIMARY_CLASS = "heroesPrimaryClassReq";
public static final String REQ_HEROES_SECONDARY_CLASS = "heroesSecondaryClassReq"; public static final String REQ_HEROES_SECONDARY_CLASS = "heroesSecondaryClassReq";
public static final String REQ_QUEST = "questReqs"; public static final String REQ_QUEST = "questReqs";
public static final String REQ_QUEST_BLOCK= "questBlocks"; public static final String REQ_QUEST_BLOCK = "questBlocks";
public static final String REQ_CUSTOM = "customReqs";
//Rewards //Rewards
public static final String REW_MONEY = "moneyRew"; public static final String REW_MONEY = "moneyRew";