Skip to content

Commit

Permalink
[Experimental] Cleaned up the requirements, added FIRST_NUM_AND_ANY_NUM
Browse files Browse the repository at this point in the history
Info for developers: This commit changes the FIRST_AND_ANY_NUM to a "First 1 and any #" mode
  • Loading branch information
spaceemotion committed Aug 5, 2013
1 parent d571b6e commit 31645a9
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 102 deletions.
Expand Up @@ -3,11 +3,11 @@
import net.aufdemrand.denizen.Denizen;
import net.aufdemrand.denizen.exceptions.RequirementCheckException;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.tags.TagManager;
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.bukkit.ChatColor;

import java.util.List;
import net.aufdemrand.denizen.tags.TagManager;

/**
* This class implements requirement checking for scripts.
Expand All @@ -25,7 +25,7 @@ public RequirementChecker(Denizen denizen) {
/**
* Checks a RequirementsContext with Requirements from the RequirementsRegistry.
*
* @param context
* @param context The requirement context
* @return true if the list meets the requirements and context set
*
*/
Expand All @@ -44,16 +44,17 @@ public boolean check(RequirementsContext context) {
dB.echoDebug(ChatColor.YELLOW + "CHECK! Now checking '%s'", context.container.getName());
dB.echoDebug("Requirement mode: '%s'", context.mode.getMode().toString());

// Set up checks for requirement mode 'FIRST AND ANY #'
boolean firstReqMet = false;
boolean firstReqChecked = false;
// Index number for the current 'reqMet' index
int index = -1;

// Counter for keeping track of met requirements
int numberMet = 0;
// Counters for keeping track of checked and met requirements
int numberChecked = 0;
int[] numberMet = new int[context.mode.modeInt.length];

// Check all requirements
for (String reqEntry : context.list) {
boolean negativeRequirement = false;
boolean reqMet = false;

//
// Check if this is a Negative Requirement. Negative requirements start
Expand All @@ -73,99 +74,98 @@ public boolean check(RequirementsContext context) {
//
// Evaluate the requirement
//
if (reqString.equalsIgnoreCase("valueof")) {
if (reqString.equals("VALUEOF")) {
String arg = argumentList.get(1);

if (arg.equalsIgnoreCase("true")) {
if (!negativeRequirement) {
dB.echoApproval("Checking 'VALUEOF " + arg + "... requirement met!");
numberMet++;
} else
dB.echoApproval("Checking '-VALUEOF " + arg + "...requirement not met!");
String debug = "Checking '" + (negativeRequirement ? "-" : "") + "VALUEOF' " + arg + "... requirement ";

} else {
if (!negativeRequirement)
dB.echoApproval("Checking 'VALUEOF " + arg + "...requirement not met!");
if (arg.equalsIgnoreCase("true") != negativeRequirement) {
debug += "met!";
reqMet = true;

else {
dB.echoApproval("Checking '-VALUEOF " + arg + "...requirement met!");// Not met!
numberMet++;
}
} else {
debug += "not met!";
}
} else

dB.echoApproval(debug);

} else if (plugin.getRequirementRegistry().list().containsKey(reqString)) {
// Check requirement with RequirementRegistry
if (plugin.getRequirementRegistry().list().containsKey(reqString)) {
AbstractRequirement requirement = plugin.getRequirementRegistry().get(reqString);

// Remove command name from arguments
argumentList.remove(0);

try {
// Check if # of required args are met
int numArguments = argumentList.isEmpty() ? 0 : argumentList.size();
int neededArguments = requirement.requirementOptions.REQUIRED_ARGS;
if ((numArguments == 0 && neededArguments > 0) ||
numArguments < neededArguments) {
throw new RequirementCheckException("Not enough arguments (" + numArguments + " / " + neededArguments + ")");
}

// Check the Requirement
if (requirement.check(context, argumentList) != negativeRequirement) {
// Check first requirement for mode 'FIRST AND ANY #'
if (!firstReqChecked) {
firstReqMet = true;
firstReqChecked = true;
}
numberMet++;
dB.echoApproval("Checking Requirement '" + requirement.getName() + "'" + " ...requirement met!");
} else {
if (!firstReqChecked) {
firstReqMet = false;
firstReqChecked = true;
}
dB.echoApproval("Checking Requirement '" + requirement.getName() + "'" + " ...requirement not met!");
}

} catch (Throwable e) {
if (e instanceof RequirementCheckException) {
String msg = e.getMessage().isEmpty() || e == null ? "No Error message defined!" : e.getMessage();
dB.echoError("Woah! Invalid arguments were specified: " + msg);
dB.echoError("Usage: " + requirement.getUsageHint());
} else {
dB.echoError("Woah! An exception has been called " + (requirement != null ? "for Requirement '" + requirement.getName() + "'" : "") + "!");
if (!dB.showStackTraces) dB.echoError("Enable '/denizen stacktrace' for the nitty-gritty.");
else e.printStackTrace();
}
AbstractRequirement requirement = plugin.getRequirementRegistry().get(reqString);

// Remove command name from arguments
argumentList.remove(0);

try {
// Check if # of required args are met
int numArguments = argumentList.isEmpty() ? 0 : argumentList.size();
int neededArguments = requirement.requirementOptions.REQUIRED_ARGS;

if ((numArguments == 0 && neededArguments > 0) || numArguments < neededArguments) {
throw new RequirementCheckException("Not enough arguments (" + numArguments + " / " + neededArguments + ")");
}
}

// If the requirement is not registered with the Requirement Registry
else {
dB.echoError("Requirement '" + reqEntry.split(" ")[0] + "' not found! Check that the requirement is installed!");
// Check the Requirement
reqMet = requirement.check(context, argumentList) != negativeRequirement;

dB.echoApproval("Checking Requirement '" + requirement.getName() + "'" + " ...requirement " + (reqMet ? "" : "not") + " met!");

} catch (Throwable e) {
if (e instanceof RequirementCheckException) {
String msg = e.getMessage().isEmpty() || e == null ? "No Error message defined!" : e.getMessage();
dB.echoError("Woah! Invalid arguments were specified: " + msg);
dB.echoError("Usage: " + requirement.getUsageHint());
} else {
dB.echoError("Woah! An exception has been called " + (requirement != null ? "for Requirement '" + requirement.getName() + "'" : "") + "!");
if (!dB.showStackTraces) dB.echoError("Enable '/denizen stacktrace' for the nitty-gritty.");
else e.printStackTrace();
}
}
}
} else {
// If the requirement is not registered with the Requirement Registry
dB.echoError("Requirement '" + reqEntry.split(" ")[0] + "' not found! Check that the requirement is installed!");
}

// Check numberMet with mode-type
// Prepare the variables
if(index < 0 || context.mode.modeInt[index] == numberChecked) {
index++;

// ALL mode
if (context.mode.getMode() == RequirementsMode.Mode.ALL && numberMet == context.list.size()) return true;
numberChecked = 0;
numberMet[index] = 0;
}

// ANY # mode
else if (context.mode.getMode() == RequirementsMode.Mode.ANY_NUM) {
if (numberMet >= context.mode.modeInt) return true;
else return false;
}
// Increase the counters
numberChecked++;

// FIRST AND ANY # mode
else if (context.mode.getMode() == RequirementsMode.Mode.FIRST_AND_ANY_NUM) {
if (firstReqMet) {
if (numberMet > context.mode.modeInt) return true;
else return false;
} else return false;
// If we met the requirements, log it into the 'numberMet' array
if(reqMet) {
numberMet[index]++;
}
}

// Nothing met, return FALSE
return false;
// Check against the Mode type
switch(context.mode.getMode()) {
case ALL:
// Return true if the amount of met requirements are the same as the list size
return numberMet[0] == context.list.size();

case ANY_NUM:
// Return true if the amount of met requirements are at least as defined
return numberMet[0] == context.mode.modeInt[0];

case FIRST_NUM_AND_ANY_NUM:
// Loop through the 'modeInt' array and check against the 'numberMet' array
for(int n = 0; n < context.mode.modeInt.length; n++) {
if(context.mode.modeInt[n] != numberMet[n]) {
return false;
}
}

return true;

default:
// Nothing met, return FALSE
return false;
}
}

}
Expand Up @@ -5,12 +5,12 @@

public class RequirementsMode {

public enum Mode { ALL, NONE, ANY_NUM, FIRST_NUM_AND_ANY_NUM }

public enum Mode { ALL, NONE, ANY_NUM, FIRST_AND_ANY_NUM }
private static Pattern intsOnly = Pattern.compile("(\\d+)");
private final static Pattern intsOnly = Pattern.compile("(\\d+)");

protected Mode mode;
protected int modeInt;
protected final Mode mode;
protected int[] modeInt;

public Mode getMode() {
return mode;
Expand All @@ -23,21 +23,68 @@ public Mode getMode() {
* ALL <br/>
* NONE <br/>
* ANY # <br/>
* FIRST AND ANY # <br/>
* FIRST (#) AND ANY # (the first number is optional)<br />
*
* @param arg the value of the Requirements.Mode yaml-key from the script
*/
public RequirementsMode(String arg) {
arg = arg.toUpperCase();
if (arg.equals("NONE")) mode = Mode.NONE;
else if (arg.equals("ALL")) mode = Mode.ALL;
else if (arg.contains("FIRST")) mode = Mode.FIRST_AND_ANY_NUM;
else if (arg.contains("ANY")) mode = Mode.ANY_NUM;

Matcher findInt = intsOnly.matcher(arg);
if (findInt.matches())
modeInt = Integer.valueOf(findInt.group(1));
else modeInt = 1;
arg = arg.trim().toUpperCase();

if (arg.equals("NONE")) {
mode = Mode.NONE;

} else if (arg.equals("ALL")) {
mode = Mode.ALL;

} else {
boolean first = arg.contains("FIRST");
boolean any = arg.contains("ANY");

Matcher found = intsOnly.matcher(arg);

if(!any) {
mode = Mode.NONE;

} else if(!first) {
mode = Mode.ANY_NUM;

if(found.matches()) {
modeInt = new int[] { Integer.valueOf(found.group(1)) };

} else {
modeInt = new int[] { 1 };
}
} else {
mode = Mode.FIRST_NUM_AND_ANY_NUM;

int count = found.groupCount();
modeInt = new int[2];

if(count >= 2) {
modeInt[0] = Integer.valueOf(found.group(1));
modeInt[1] = Integer.valueOf(found.group(2));

} else if(count == 1) {
int index = 1;

// Check if the "FIRST" argument has a number
if(arg.startsWith("FIRST AND")) {
modeInt[0] = 1;

} else {
modeInt[0] = Integer.valueOf(found.group(index));
index++;
}

// Check if we end with a number or not
if(arg.endsWith("ANY")) {
modeInt[1] = 1;
} else {
modeInt[1] = Integer.valueOf(found.group(index));
}
}
}
}
}

public RequirementsMode(Mode mode) {
Expand All @@ -46,7 +93,7 @@ public RequirementsMode(Mode mode) {

public RequirementsMode(Mode mode, int num) {
this.mode = mode;
this.modeInt = num;
this.modeInt = new int[] { num };
}

}

0 comments on commit 31645a9

Please sign in to comment.