Skip to content

Commit

Permalink
Some work towards a functional core
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jan 11, 2015
1 parent 4ee3a56 commit e433ac7
Show file tree
Hide file tree
Showing 7 changed files with 347 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package net.aufdemrand.denizencore;

import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.ScriptEntryData;
import net.aufdemrand.denizencore.scripts.queues.ScriptQueue;
import net.aufdemrand.denizencore.utilities.YamlConfiguration;
import net.aufdemrand.denizencore.utilities.debugging.Debuggable;
import net.aufdemrand.denizencore.utilities.debugging.dB.DebugElement;

import java.io.File;
import java.util.List;
Expand Down Expand Up @@ -37,11 +41,41 @@ public interface DenizenImplementation {
*/
public abstract void debugError(String error);

/**
* Output an error to console, specific to a script queue.
*/
public abstract void debugError(ScriptQueue queue, String error);

/**
* Output a command information report.
*/
public abstract void debugReport(Debuggable caller, String name, String message);

/**
* Output an 'Okay!' message.
*/
public abstract void debugApproval(String message);

/**
* Outputs a message specific to a script entry.
*/
public abstract void debugEntry(ScriptEntry entry, String message);

/**
* Outputs a message specific to a script entry.
*/
public abstract void debugEntry(ScriptEntry entry, DebugElement element, String message);

/**
* Outputs a message specific to a script entry.
*/
public abstract void debugEntry(ScriptEntry entry, DebugElement element);

/**
* Outputs a message specific to a script queue.
*/
public abstract void debugQueue(ScriptQueue entry, String message);

/**
* Return the name of the implementation.
* EG, "Gamey Game".
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.*;

import net.aufdemrand.denizencore.BukkitScriptEntryData;
import net.aufdemrand.denizencore.exceptions.ScriptEntryCreationException;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
Expand Down Expand Up @@ -125,10 +124,10 @@ else if (arg.matches("}")) {
bracesSection.add(new ScriptEntry(cmd,
args,
scriptEntry.getScript() != null ? scriptEntry.getScript().getContainer() : null));
((BukkitScriptEntryData)bracesSection.get(bracesSection.size() - 1).entryData)
.setPlayer(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer());
((BukkitScriptEntryData)bracesSection.get(bracesSection.size() - 1).entryData)
.setNPC(((BukkitScriptEntryData) scriptEntry.entryData).getNPC());
(bracesSection.get(bracesSection.size() - 1).entryData)
.setPlayer((scriptEntry.entryData).getPlayer());
(bracesSection.get(bracesSection.size() - 1).entryData)
.setNPC((scriptEntry.entryData).getNPC());
if (hyperdebug) dB.echoDebug(scriptEntry, "Command added: " + cmd + ", with " + String.valueOf(args.length) + " arguments");
} catch (ScriptEntryCreationException e) {
if (hyperdebug) dB.echoError(scriptEntry.getResidingQueue(), e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,15 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.aufdemrand.denizencore.BukkitScriptEntryData;
import net.aufdemrand.denizencore.tags.BukkitTagContext;
import net.aufdemrand.denizencore.DenizenCore;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.objects.dNPC;
import net.aufdemrand.denizencore.objects.dPlayer;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.tags.TagManager;
import net.aufdemrand.denizencore.utilities.DenizenAPI;
import net.aufdemrand.denizencore.utilities.debugging.dB;
import net.aufdemrand.denizencore.utilities.debugging.dB.DebugElement;

import net.aufdemrand.denizencore.scripts.commands.BaseAbstractCommand;
import org.bukkit.ChatColor;

public class CommandExecuter {

Expand Down Expand Up @@ -66,7 +61,7 @@ public boolean execute(ScriptEntry scriptEntry) {
// Get the command instance ready for the execution of the scriptEntry
BaseAbstractCommand command = scriptEntry.getCommand();
if (command == null) {
command = DenizenAPI.getCurrentInstance().getCommandRegistry().get(scriptEntry.getCommandName());
command = DenizenCore.getCommandRegistry().get(scriptEntry.getCommandName());
}

if (command == null) {
Expand All @@ -76,18 +71,21 @@ public boolean execute(ScriptEntry scriptEntry) {
return false;
}

if (((BukkitScriptEntryData)scriptEntry.entryData).hasNPC() && ((BukkitScriptEntryData)scriptEntry.entryData).getNPC().getCitizen() == null)
((BukkitScriptEntryData)scriptEntry.entryData).setNPC(null);
if ((criptEntry.entryData).hasNPC() && (scriptEntry.entryData).getNPC().getCitizen() == null)
(scriptEntry.entryData).setNPC(null);

// Debugger information
if (scriptEntry.getOriginalArguments() == null ||
scriptEntry.getOriginalArguments().size() == 0 ||
!scriptEntry.getOriginalArguments().get(0).equals("\0CALLBACK")) {
if (((BukkitScriptEntryData)scriptEntry.entryData).getPlayer() != null)
dB.echoDebug(scriptEntry, DebugElement.Header, "Executing dCommand: " + scriptEntry.getCommandName() + "/p@" + ((BukkitScriptEntryData)scriptEntry.entryData).getPlayer().getName());
if ((scriptEntry.entryData).getPlayer() != null)
dB.echoDebug(scriptEntry, DebugElement.Header,
"Executing dCommand: " + scriptEntry.getCommandName() + "/p@" +
(scriptEntry.entryData).getPlayer().getName());
else
dB.echoDebug(scriptEntry, DebugElement.Header, "Executing dCommand: " +
scriptEntry.getCommandName() + (((BukkitScriptEntryData)scriptEntry.entryData).getNPC() != null ? "/n@" + ((BukkitScriptEntryData)scriptEntry.entryData).getNPC().getName() : ""));
scriptEntry.getCommandName() + ((scriptEntry.entryData).getNPC() != null ?
"/n@" + (scriptEntry.entryData).getNPC().getName() : ""));
}

// Don't execute() if problems arise in parseArgs()
Expand Down
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());
}
}
}
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();
}
}
Loading

0 comments on commit e433ac7

Please sign in to comment.