Finished Custom Requirement API

This commit is contained in:
Blackvein 2014-01-05 21:48:38 -08:00
parent 360c0c6291
commit 4685a169b9
9 changed files with 209 additions and 59 deletions

Binary file not shown.

View File

@ -3,7 +3,7 @@
<groupId>me.blackvein.quests</groupId>
<artifactId>quests</artifactId>
<version>1.7.2-SNAPSHOT</version>
<version>1.7.3-SNAPSHOT</version>
<name>quests</name>
<url>http://dev.bukkit.org/server-mods/quests/</url>
<packaging>jar</packaging>

View File

@ -1,13 +1,17 @@
package me.blackvein.quests;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.entity.Player;
public abstract class CustomRequirement {
private String name = null;
private String author = null;
public final Map<String,Object> datamap = new HashMap<String, Object>();
public final Map<String, String> descriptions = new HashMap<String, String>();
public abstract boolean testRequirement(Player p);
public abstract boolean testRequirement(Player p, Map<String, Object> m);
public String getName() {
return name;
@ -25,4 +29,12 @@ public abstract class CustomRequirement {
this.author = author;
}
public void setData(String key, Object val) {
datamap.put(key, val);
}
public void setDescription(String key, String description){
descriptions.put(key, description);
}
}

View File

@ -8,10 +8,12 @@ import com.gmail.nossr50.util.player.UserManager;
import com.herocraftonline.heroes.characters.Hero;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import me.blackvein.quests.exceptions.InvalidStageException;
import me.blackvein.quests.util.ItemUtil;
@ -52,7 +54,7 @@ public class Quest {
List<Integer> mcMMOAmountReqs = new LinkedList<Integer>();
String heroesPrimaryClassReq = null;
String heroesSecondaryClassReq = null;
List<String> customRequirements = new LinkedList<String>();
Map<String, Map<String, Object>> customRequirements = new HashMap<String, Map<String, Object>>();
public String failRequirements = null;
//
//Rewards
@ -226,7 +228,7 @@ public class Quest {
}
for (String s : customRequirements){
for (String s : customRequirements.keySet()){
CustomRequirement found = null;
for(CustomRequirement cr : plugin.customRequirements){
@ -237,7 +239,7 @@ public class Quest {
}
if(found != null){
if(found.testRequirement(player) == false)
if(found.testRequirement(player, customRequirements.get(s)) == 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?");

View File

@ -1021,6 +1021,7 @@ public class QuestFactory implements ConversationAbandonedListener, ColorUtil {
String heroesPrimaryReq = null;
String heroesSecondaryReq = null;
LinkedList<String> customReqs = null;
LinkedList<Map<String, Object>> customReqsData = null;
String failMessage = null;
Integer moneyRew = null;
@ -1090,6 +1091,7 @@ public class QuestFactory implements ConversationAbandonedListener, ColorUtil {
if (cc.getSessionData(CK.REQ_CUSTOM) != null) {
customReqs = (LinkedList<String>) cc.getSessionData(CK.REQ_CUSTOM);
customReqsData = (LinkedList<Map<String, Object>>) cc.getSessionData(CK.REQ_CUSTOM_DATA);
}
if (cc.getSessionData(CK.Q_FAIL_MESSAGE) != null) {
@ -1188,7 +1190,14 @@ public class QuestFactory implements ConversationAbandonedListener, ColorUtil {
reqs.set("mcmmo-amounts", mcMMOAmountReqs);
reqs.set("heroes-primary-class", heroesPrimaryReq);
reqs.set("heroes-secondary-class", heroesSecondaryReq);
reqs.set("custom-requirements", customReqs);
if(customReqs != null){
ConfigurationSection customReqsSec = reqs.createSection("custom-requirements");
for(int i = 0; i < customReqs.size(); i++){
ConfigurationSection customReqSec = customReqsSec.createSection("req" + (i + 1));
customReqSec.set("name", customReqs.get(i));
customReqSec.set("data", customReqsData.get(i));
}
}
reqs.set("fail-requirement-message", failMessage);
} else {
@ -1604,6 +1613,23 @@ public class QuestFactory implements ConversationAbandonedListener, ColorUtil {
if (q.failRequirements != null) {
cc.setSessionData(CK.Q_FAIL_MESSAGE, q.failRequirements);
}
if (q.customRequirements.isEmpty() == false) {
LinkedList<String> list = new LinkedList<String>();
LinkedList<Map<String, Object>> datamapList = new LinkedList<Map<String, Object>>();
for(Entry<String, Map<String, Object>> entry : q.customRequirements.entrySet()){
list.add(entry.getKey());
datamapList.add(entry.getValue());
}
cc.setSessionData(CK.REQ_CUSTOM, list);
cc.setSessionData(CK.REQ_CUSTOM_DATA, datamapList);
}
//
//Rewards

View File

@ -455,7 +455,7 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener,
try{
JarFile jarFile = new JarFile(jar);
Enumeration<JarEntry> e = jarFile.entries();
Enumeration e = jarFile.entries();
URL[] urls = { new URL("jar:file:" + jar.getPath() + "!/") };
@ -463,39 +463,24 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener,
while (e.hasMoreElements()) {
JarEntry je = e.nextElement();
if(je.isDirectory() || !je.getName().endsWith(".class")){
continue;
}
String className = je.getName().replaceAll("\\.class$", "");
className = className.replace('/', '.');
try {
Class<?> c = Class.forName(className, true, cl);
//If CustomRequirement.class is not the super class of 'c', continue.
if (!CustomRequirement.class.isAssignableFrom(c)) {
continue;
}
Class<? extends CustomRequirement> requirementClass = c.asSubclass(CustomRequirement.class);
CustomRequirement requirement = requirementClass.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 exception) {
//If this class fails to load, go to next class instead of failing the whole module.
printSevere("[Quests] Error: Unable to load module: " + className + " from file: " + jar.getName());
continue;
}
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);
}
jarFile.close();
} catch (Exception e) {
}catch (Exception e){
printSevere("[Quests] Error: Unable to load module from file: " + jar.getName());
if(debug){
printSevere("[Quests] Error log:");
@ -504,16 +489,6 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener,
}
}
/**
* Add custom requirements to Quests,
* @param requirement, the requirement that is to be added.
* @param plugin, the plugin that adds this requirement.
*/
public void addCustomRequirement(CustomRequirement requirement, Plugin plugin) {
customRequirements.add(requirement);
printInfo("[Quests] Loaded custom requirement from: " + plugin.getName());
}
public void printHelp(Player player) {
@ -2303,24 +2278,37 @@ 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){
ConfigurationSection sec = config.getConfigurationSection("quests." + s + ".requirements.custom-requirements");
for(String path : sec.getKeys(false)){
String name = sec.getString(path + ".name");
boolean found = false;
for(CustomRequirement cr : customRequirements){
if(cr.getName().equalsIgnoreCase(req)){
if(cr.getName().equalsIgnoreCase(name)){
found = true;
break;
}
}
if(!found){
printWarning("[Quests] Custom requirement \"" + req + "\" for Quest \"" + quest.name + "\" could not be found!");
printWarning("[Quests] Custom requirement \"" + name + "\" for Quest \"" + quest.name + "\" could not be found!");
continue;
}
Map<String, Object> data = new HashMap<String, Object>();
ConfigurationSection sec2 = sec.getConfigurationSection(path + ".data");
if(sec2 != null){
for(String dataPath : sec2.getKeys(false)){
data.put(dataPath, sec2.get(dataPath));
}
}
quest.customRequirements.put(name, data);
}
}
}
@ -4854,9 +4842,7 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener,
+ "&arg5=" + quests.size()
+ "&arg6=" + (citizens != null ? "true" : "false")
+ "&arg7=" + URLEncoder.encode(stamp.toString(), "UTF-8");
System.out.println("Arguments");
System.out.println(arguments);
URL url = new URL("http://www.bcitdnd.net/php/quests.php?" + arguments);
URL url = new URL("http://www.blackvein.net/php/quests.php?" + arguments);
URLConnection yc = url.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
@ -4880,9 +4866,7 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener,
try {
String arguments = "arg1=" + getServer().getIp() + "&arg2=" + ((Integer) getServer().getPort()).toString();
URL url = new URL("http://www.bcitdnd.net/php/quests_del.php?" + arguments);
System.out.println("Delete arguments");
System.out.println(arguments);
URL url = new URL("http://www.blackvein.net/php/quests_del.php?" + arguments);
URLConnection yc = url.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(

View File

@ -6,6 +6,7 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import me.blackvein.quests.CustomRequirement;
import me.blackvein.quests.util.ColorUtil;
@ -585,18 +586,32 @@ public class RequirementsPrompt extends FixedSetPrompt implements ColorUtil {
if(context.getSessionData(CK.REQ_CUSTOM) != null){
LinkedList<String> list = (LinkedList<String>) context.getSessionData(CK.REQ_CUSTOM);
LinkedList<Map<String, Object>> datamapList = (LinkedList<Map<String, Object>>) context.getSessionData(CK.REQ_CUSTOM_DATA);
if(list.contains(found.getName()) == false){
list.add(found.getName());
datamapList.add(found.datamap);
}else{
context.getForWhom().sendRawMessage(YELLOW + "That custom requirement has already been added!");
return new CustomRequirementsPrompt();
}
}else{
LinkedList<Map<String, Object>> datamapList = new LinkedList<Map<String, Object>>();
datamapList.add(found.datamap);
LinkedList<String> list = new LinkedList<String>();
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);
return new RequirementCustomDataListPrompt();
}
//
}else{
context.getForWhom().sendRawMessage(YELLOW + "Custom requirement module not found.");
return new CustomRequirementsPrompt();
@ -604,6 +619,7 @@ public class RequirementsPrompt extends FixedSetPrompt implements ColorUtil {
} else if (input.equalsIgnoreCase("clear")) {
context.setSessionData(CK.REQ_CUSTOM, null);
context.setSessionData(CK.REQ_CUSTOM_DATA, null);
context.getForWhom().sendRawMessage(YELLOW + "Custom requirements cleared.");
}
@ -611,7 +627,114 @@ public class RequirementsPrompt extends FixedSetPrompt implements ColorUtil {
}
}
private class RequirementCustomDataListPrompt extends StringPrompt {
@Override
public String getPromptText(ConversationContext context) {
String text = BOLD + "" + AQUA + "- ";
LinkedList<String> list = (LinkedList<String>) context.getSessionData(CK.REQ_CUSTOM);
LinkedList<Map<String, Object>> datamapList = (LinkedList<Map<String, Object>>) context.getSessionData(CK.REQ_CUSTOM_DATA);
String reqName = list.getLast();
Map<String, Object> datamap = datamapList.getLast();
text += reqName + " -\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 += BOLD + "" + DARKBLUE + index + " - " + RESET + BLUE + dataKey;
if(datamap.get(dataKey) != null)
text += GREEN + " (" + (String) datamap.get(dataKey) + ")\n";
else
text += RED + " (Value required)\n";
index++;
}
text += BOLD + "" + DARKBLUE + index + " - " + AQUA + "Finish";
return text;
}
@Override
public Prompt acceptInput(ConversationContext context, String input) {
LinkedList<Map<String, Object>> datamapList = (LinkedList<Map<String, Object>>) context.getSessionData(CK.REQ_CUSTOM_DATA);
Map<String, Object> datamap = datamapList.getLast();
int numInput;
try{
numInput = Integer.parseInt(input);
}catch(NumberFormatException nfe){
return new RequirementCustomDataListPrompt();
}
if(numInput < 1 || numInput > datamap.size() + 1)
return new RequirementCustomDataListPrompt();
if(numInput < datamap.size() + 1){
LinkedList<String> datamapKeys = new LinkedList<String>();
for(String key : datamap.keySet())
datamapKeys.add(key);
Collections.sort(datamapKeys);
String selectedKey = datamapKeys.get(numInput - 1);
context.setSessionData(CK.REQ_CUSTOM_DATA_TEMP, selectedKey);
return new RequirementCustomDataPrompt();
}else{
if(datamap.containsValue(null)){
return new RequirementCustomDataListPrompt();
}else{
context.setSessionData(CK.REQ_CUSTOM_DATA_DESCRIPTIONS, null);
return new RequirementsPrompt(quests, factory);
}
}
}
}
private class RequirementCustomDataPrompt extends StringPrompt {
@Override
public String getPromptText(ConversationContext context) {
String text = "";
String temp = (String)context.getSessionData(CK.REQ_CUSTOM_DATA_TEMP);
Map<String, String> descriptions = (Map<String, String>) context.getSessionData(CK.REQ_CUSTOM_DATA_DESCRIPTIONS);
if(descriptions.get(temp) != null)
text += GOLD + descriptions.get(temp) + "\n";
text += YELLOW + "Enter value for ";
text += BOLD + temp + RESET + YELLOW + ":";
return text;
}
@Override
public Prompt acceptInput(ConversationContext context, String input) {
LinkedList<Map<String, Object>> datamapList = (LinkedList<Map<String, Object>>) context.getSessionData(CK.REQ_CUSTOM_DATA);
Map<String, Object> datamap = datamapList.getLast();
datamap.put((String)context.getSessionData(CK.REQ_CUSTOM_DATA_TEMP), input);
context.setSessionData(CK.REQ_CUSTOM_DATA_TEMP, null);
return new RequirementCustomDataListPrompt();
}
}
private class mcMMOPrompt extends FixedSetPrompt {
public mcMMOPrompt() {

View File

@ -30,6 +30,9 @@ public class CK {
public static final String REQ_QUEST = "questReqs";
public static final String REQ_QUEST_BLOCK = "questBlocks";
public static final String REQ_CUSTOM = "customReqs";
public static final String REQ_CUSTOM_DATA = "customReqData";
public static final String REQ_CUSTOM_DATA_DESCRIPTIONS = "customReqDataDesc";
public static final String REQ_CUSTOM_DATA_TEMP = "customReqDataTemp";
//Rewards
public static final String REW_MONEY = "moneyRew";

View File

@ -1,6 +1,6 @@
name: Quests
main: me.blackvein.quests.Quests
version: 1.7.2
version: 1.7.3
description: Player questing system
website: http://dev.bukkit.org/server-mods/quests/
dev-url: https://github.com/Blackvein/Quests/