-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ee3a56
commit e433ac7
Showing
7 changed files
with
347 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/main/java/net/aufdemrand/denizencore/scripts/commands/core/DefineCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package net.aufdemrand.denizencore.scripts.commands.core; | ||
|
||
import net.aufdemrand.denizencore.exceptions.CommandExecutionException; | ||
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException; | ||
import net.aufdemrand.denizencore.objects.Element; | ||
import net.aufdemrand.denizencore.objects.aH; | ||
import net.aufdemrand.denizencore.scripts.ScriptEntry; | ||
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand; | ||
import net.aufdemrand.denizencore.utilities.debugging.dB; | ||
|
||
/** | ||
* Creates a queue/script-level variable. | ||
* | ||
*/ | ||
public class DefineCommand extends AbstractCommand { | ||
|
||
@Override | ||
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException { | ||
|
||
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) { | ||
|
||
if (!scriptEntry.hasObject("definition")) { | ||
if (arg.getValue().equals("!") && arg.hasPrefix()) { | ||
scriptEntry.addObject("remove", new Element("true")); | ||
scriptEntry.addObject("value", new Element("null")); | ||
scriptEntry.addObject("definition", arg.getPrefix().asElement()); | ||
} else | ||
scriptEntry.addObject("definition", new Element(arg.getValue().toLowerCase())); | ||
} | ||
|
||
else if (!scriptEntry.hasObject("value")) | ||
// Use the raw_value as to not exclude values with :'s in them. | ||
scriptEntry.addObject("value", new Element(arg.raw_value)); | ||
|
||
else arg.reportUnhandled(); | ||
} | ||
|
||
if (!scriptEntry.hasObject("definition") || !scriptEntry.hasObject("value")) | ||
throw new InvalidArgumentsException("Must specify a definition and value!"); | ||
|
||
} | ||
|
||
@Override | ||
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException { | ||
|
||
Element definition = scriptEntry.getElement("definition"); | ||
Element value = scriptEntry.getElement("value"); | ||
Element remove = scriptEntry.getElement("remove"); | ||
|
||
dB.report(scriptEntry, getName(), aH.debugObj("queue", scriptEntry.getResidingQueue().id) | ||
+ definition.debug() | ||
+ value.debug() | ||
+ (remove != null ? remove.debug() : "")); | ||
|
||
if (scriptEntry.hasObject("remove")) { | ||
scriptEntry.getResidingQueue().removeDefinition(definition.asString()); | ||
} | ||
else { | ||
scriptEntry.getResidingQueue().addDefinition(definition.asString(), value.asString()); | ||
} | ||
} | ||
} |
150 changes: 150 additions & 0 deletions
150
src/main/java/net/aufdemrand/denizencore/scripts/commands/core/DetermineCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package net.aufdemrand.denizencore.scripts.commands.core; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import net.aufdemrand.denizencore.exceptions.CommandExecutionException; | ||
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException; | ||
import net.aufdemrand.denizencore.objects.Element; | ||
import net.aufdemrand.denizencore.scripts.ScriptEntry; | ||
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand; | ||
import net.aufdemrand.denizencore.objects.aH; | ||
import net.aufdemrand.denizencore.utilities.debugging.dB; | ||
|
||
public class DetermineCommand extends AbstractCommand { | ||
|
||
// | ||
// Static helpers | ||
// | ||
|
||
|
||
// Default 'DETERMINE_NONE' value. | ||
public static String DETERMINE_NONE = "none"; | ||
|
||
// Map for keeping track of cache | ||
// Key: ID, Value: outcome | ||
private static Map<Long, List<String>> cache = new ConcurrentHashMap<Long, List<String>>(8, 0.9f, 1); | ||
|
||
// Start at 0 | ||
public static long uniqueId = 0; | ||
|
||
|
||
/** | ||
* Increment the counter and return it, thus returning | ||
* a unique id. Determinations are very short lived. | ||
* | ||
* @return long ID | ||
*/ | ||
public static long getNewId() { | ||
// Just in case? Start over if already max_value. | ||
if (uniqueId == Long.MAX_VALUE) | ||
uniqueId = 0; | ||
// Increment the counter | ||
return uniqueId++; | ||
} | ||
|
||
|
||
/** | ||
* Checks the cache for existence of an outcome. | ||
* | ||
* @param id the outcome id to check | ||
* @return if the cache has the outcome | ||
*/ | ||
public static boolean hasOutcome(long id) { | ||
return cache.containsKey(id) && !cache.get(id).isEmpty(); | ||
} | ||
|
||
|
||
/** | ||
* Gets the outcome, and removes it from the cache. | ||
* | ||
* @param id the outcome id to check | ||
* @return the outcome | ||
*/ | ||
public static List<String> getOutcome(long id) { | ||
List<String> outcome = cache.get(id); | ||
cache.remove(id); | ||
return outcome; | ||
} | ||
|
||
|
||
/** | ||
* Gets the current value of the outcome. | ||
* Note: The value of the outcome may change. | ||
* | ||
* @param id the outcome id to check | ||
* @return the current value of the outcome | ||
*/ | ||
public static String readOutcome(long id) { | ||
return cache.get(id).isEmpty() ? DETERMINE_NONE: cache.get(id).get(0); | ||
} | ||
|
||
|
||
// | ||
// Command Singleton | ||
// | ||
|
||
@Override | ||
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException { | ||
|
||
// | ||
// Parse the arguments | ||
// | ||
|
||
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) { | ||
|
||
if (arg.matches("passive", "passively")) | ||
scriptEntry.addObject("passively", new Element(true)); | ||
|
||
else if (!scriptEntry.hasObject("outcome")) | ||
scriptEntry.addObject("outcome", new Element(arg.raw_value)); | ||
|
||
else arg.reportUnhandled(); | ||
} | ||
|
||
// | ||
// Set defaults | ||
// | ||
|
||
scriptEntry.defaultObject("passively", new Element(false)); | ||
scriptEntry.defaultObject("outcome", new Element(DETERMINE_NONE)); | ||
} | ||
|
||
|
||
@Override | ||
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException { | ||
|
||
// Report! | ||
dB.report(scriptEntry, getName(), scriptEntry.getElement("outcome").debug() | ||
+ scriptEntry.getElement("passively").debug()); | ||
|
||
// Fetch the ScriptEntry elements | ||
String outcome = scriptEntry.getElement("outcome").asString(); | ||
Boolean passively = scriptEntry.getElement("passively").asBoolean(); | ||
|
||
Long uniqueId = (Long) scriptEntry.getObject("reqId"); | ||
|
||
// Useful for debug | ||
if (uniqueId == null) { | ||
dB.echoError(scriptEntry.getResidingQueue(), "Cannot use determine in this queue!"); | ||
return; | ||
} | ||
|
||
// Store the outcome in the cache | ||
List<String> strs; | ||
if (cache.containsKey(uniqueId)) { | ||
strs = cache.get(uniqueId); | ||
} | ||
else { | ||
strs = new ArrayList<String>(); | ||
cache.put(uniqueId, strs); | ||
} | ||
strs.add(outcome); | ||
|
||
if (!passively) | ||
// Stop the queue by clearing the remainder of it. | ||
scriptEntry.getResidingQueue().clear(); | ||
} | ||
} |
Oops, something went wrong.