use of net.aufdemrand.denizencore.scripts.ScriptEntry in project Denizen-For-Bukkit by DenizenScript.
the class WhileCommand method execute.
@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
Element stop = scriptEntry.getElement("stop");
Element next = scriptEntry.getElement("next");
Element callback = scriptEntry.getElement("callback");
if (stop != null && stop.asBoolean()) {
// Report to dB
dB.report(scriptEntry, getName(), stop.debug());
boolean hasnext = false;
for (int i = 0; i < scriptEntry.getResidingQueue().getQueueSize(); i++) {
ScriptEntry entry = scriptEntry.getResidingQueue().getEntry(i);
List<String> args = entry.getOriginalArguments();
if (entry.getCommandName().equalsIgnoreCase("while") && args.size() > 0 && args.get(0).equalsIgnoreCase("\0CALLBACK")) {
hasnext = true;
break;
}
}
if (hasnext) {
while (scriptEntry.getResidingQueue().getQueueSize() > 0) {
ScriptEntry entry = scriptEntry.getResidingQueue().getEntry(0);
List<String> args = entry.getOriginalArguments();
if (entry.getCommandName().equalsIgnoreCase("while") && args.size() > 0 && args.get(0).equalsIgnoreCase("\0CALLBACK")) {
scriptEntry.getResidingQueue().removeEntry(0);
break;
}
scriptEntry.getResidingQueue().removeEntry(0);
}
} else {
dB.echoError(scriptEntry.getResidingQueue(), "Cannot stop while: not in one!");
}
return;
} else if (next != null && next.asBoolean()) {
// Report to dB
dB.report(scriptEntry, getName(), next.debug());
boolean hasnext = false;
for (int i = 0; i < scriptEntry.getResidingQueue().getQueueSize(); i++) {
ScriptEntry entry = scriptEntry.getResidingQueue().getEntry(i);
List<String> args = entry.getOriginalArguments();
if (entry.getCommandName().equalsIgnoreCase("while") && args.size() > 0 && args.get(0).equalsIgnoreCase("\0CALLBACK")) {
hasnext = true;
break;
}
}
if (hasnext) {
while (scriptEntry.getResidingQueue().getQueueSize() > 0) {
ScriptEntry entry = scriptEntry.getResidingQueue().getEntry(0);
List<String> args = entry.getOriginalArguments();
if (entry.getCommandName().equalsIgnoreCase("while") && args.size() > 0 && args.get(0).equalsIgnoreCase("\0CALLBACK")) {
break;
}
scriptEntry.getResidingQueue().removeEntry(0);
}
} else {
dB.echoError(scriptEntry.getResidingQueue(), "Cannot stop while: not in one!");
}
return;
} else if (callback != null && callback.asBoolean()) {
if (scriptEntry.getOwner() != null && (scriptEntry.getOwner().getCommandName().equalsIgnoreCase("while") || scriptEntry.getOwner().getBracedSet() == null || scriptEntry.getOwner().getBracedSet().size() == 0 || scriptEntry.getBracedSet().get(0).value.get(scriptEntry.getBracedSet().get(0).value.size() - 1) != scriptEntry)) {
WhileData data = (WhileData) scriptEntry.getOwner().getData();
data.index++;
if (System.currentTimeMillis() - data.LastChecked < 50) {
data.instaTicks++;
int max = Settings.whileMaxLoops();
if (data.instaTicks > max && max != 0) {
return;
}
} else {
data.instaTicks = 0;
}
data.LastChecked = System.currentTimeMillis();
if (TagManager.tag(data.value, DenizenCore.getImplementation().getTagContextFor(scriptEntry, false)).equalsIgnoreCase("true")) {
dB.echoDebug(scriptEntry, DebugElement.Header, "While loop " + data.index);
scriptEntry.getResidingQueue().addDefinition("loop_index", String.valueOf(data.index));
List<ScriptEntry> bracedCommands = BracedCommand.getBracedCommands(scriptEntry.getOwner()).get(0).value;
ScriptEntry callbackEntry = null;
try {
callbackEntry = new ScriptEntry("WHILE", new String[] { "\0CALLBACK" }, (scriptEntry.getScript() != null ? scriptEntry.getScript().getContainer() : null));
callbackEntry.copyFrom(scriptEntry);
} catch (ScriptEntryCreationException e) {
dB.echoError(scriptEntry.getResidingQueue(), e);
}
callbackEntry.setOwner(scriptEntry.getOwner());
bracedCommands.add(callbackEntry);
for (int i = 0; i < bracedCommands.size(); i++) {
bracedCommands.get(i).setInstant(true);
bracedCommands.get(i).addObject("reqId", scriptEntry.getObject("reqId"));
}
scriptEntry.getResidingQueue().injectEntries(bracedCommands, 0);
}
} else {
dB.echoError(scriptEntry.getResidingQueue(), "While CALLBACK invalid: not a real callback!");
}
} else {
// Get objects
Element value = scriptEntry.getElement("value");
Element parsed_value = scriptEntry.getElement("parsed_value");
List<ScriptEntry> bracedCommandsList = ((List<BracedData>) scriptEntry.getObject("braces")).get(0).value;
if (bracedCommandsList == null || bracedCommandsList.isEmpty()) {
dB.echoError(scriptEntry.getResidingQueue(), "Empty braces!");
return;
}
// Report to dB
dB.report(scriptEntry, getName(), value.debug());
if (!parsed_value.asString().equalsIgnoreCase("true")) {
return;
}
WhileData datum = new WhileData();
datum.index = 1;
datum.value = value.asString();
datum.LastChecked = System.currentTimeMillis();
datum.instaTicks = 1;
scriptEntry.setData(datum);
ScriptEntry callbackEntry = null;
try {
callbackEntry = new ScriptEntry("WHILE", new String[] { "\0CALLBACK" }, (scriptEntry.getScript() != null ? scriptEntry.getScript().getContainer() : null));
callbackEntry.copyFrom(scriptEntry);
} catch (ScriptEntryCreationException e) {
dB.echoError(scriptEntry.getResidingQueue(), e);
}
callbackEntry.setOwner(scriptEntry);
bracedCommandsList.add(callbackEntry);
scriptEntry.getResidingQueue().addDefinition("loop_index", "1");
for (int i = 0; i < bracedCommandsList.size(); i++) {
bracedCommandsList.get(i).setInstant(true);
bracedCommandsList.get(i).addObject("reqId", scriptEntry.getObject("reqId"));
}
scriptEntry.getResidingQueue().injectEntries(bracedCommandsList, 0);
}
}
use of net.aufdemrand.denizencore.scripts.ScriptEntry in project Denizen-For-Bukkit by DenizenScript.
the class PushCommand method execute.
@SuppressWarnings("unchecked")
@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
dEntity originEntity = (dEntity) scriptEntry.getObject("originEntity");
dLocation originLocation = scriptEntry.hasObject("originLocation") ? (dLocation) scriptEntry.getObject("originLocation") : new dLocation(originEntity.getEyeLocation().add(originEntity.getEyeLocation().getDirection()).subtract(0, 0.4, 0));
boolean no_rotate = scriptEntry.hasObject("no_rotate") && scriptEntry.getElement("no_rotate").asBoolean();
final boolean no_damage = scriptEntry.hasObject("no_damage") && scriptEntry.getElement("no_damage").asBoolean();
// If there is no destination set, but there is a shooter, get a point
// in front of the shooter and set it as the destination
final dLocation destination = scriptEntry.hasObject("destination") ? (dLocation) scriptEntry.getObject("destination") : (originEntity != null ? new dLocation(originEntity.getEyeLocation().add(originEntity.getEyeLocation().getDirection().multiply(30))) : null);
// TODO: Should this be checked in argument parsing?
if (destination == null) {
dB.report(scriptEntry, getName(), "No destination specified!");
scriptEntry.setFinished(true);
return;
}
List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
final dScript script = (dScript) scriptEntry.getObject("script");
final dList definitions = (dList) scriptEntry.getObject("definitions");
final double speed = scriptEntry.getElement("speed").asDouble();
final int maxTicks = ((Duration) scriptEntry.getObject("duration")).getTicksAsInt();
Element force_along = scriptEntry.getElement("force_along");
Element precision = scriptEntry.getElement("precision");
// Report to dB
dB.report(scriptEntry, getName(), aH.debugObj("origin", originEntity != null ? originEntity : originLocation) + aH.debugObj("entities", entities.toString()) + aH.debugObj("destination", destination) + aH.debugObj("speed", speed) + aH.debugObj("max ticks", maxTicks) + (script != null ? script.debug() : "") + force_along.debug() + precision.debug() + (no_rotate ? aH.debugObj("no_rotate", "true") : "") + (no_damage ? aH.debugObj("no_damage", "true") : "") + (definitions != null ? definitions.debug() : ""));
final boolean forceAlong = force_along.asBoolean();
// Keep a dList of entities that can be called using <entry[name].pushed_entities>
// later in the script queue
final dList entityList = new dList();
// Go through all the entities, spawning/teleporting and rotating them
for (dEntity entity : entities) {
entity.spawnAt(originLocation);
// Only add to entityList after the entities have been
// spawned, otherwise you'll get something like "e@skeleton"
// instead of "e@57" on it
entityList.add(entity.toString());
if (!no_rotate) {
NMSHandler.getInstance().getEntityHelper().faceLocation(entity.getBukkitEntity(), destination);
}
// when applicable
if (entity.isProjectile() && originEntity != null) {
entity.setShooter(originEntity);
}
}
// Add entities to context so that the specific entities created/spawned
// can be fetched.
scriptEntry.addObject("pushed_entities", entityList);
Position.mount(Conversion.convertEntities(entities));
// Get the entity at the bottom of the entity list, because
// only its gravity should be affected and tracked considering
// that the other entities will be mounted on it
final dEntity lastEntity = entities.get(entities.size() - 1);
final Vector v2 = destination.toVector();
final Vector Origin = originLocation.toVector();
final int prec = precision.asInt();
BukkitRunnable task = new BukkitRunnable() {
int runs = 0;
dLocation lastLocation;
@Override
public void run() {
if (runs < maxTicks && lastEntity.isValid()) {
Vector v1 = lastEntity.getLocation().toVector();
Vector v3 = v2.clone().subtract(v1).normalize();
Vector newVel = v3.multiply(speed);
lastEntity.setVelocity(newVel);
if (forceAlong) {
Vector newDest = v2.clone().subtract(Origin).normalize().multiply(runs / 20).add(Origin);
lastEntity.teleport(new Location(lastEntity.getLocation().getWorld(), newDest.getX(), newDest.getY(), newDest.getZ(), lastEntity.getLocation().getYaw(), lastEntity.getLocation().getPitch()));
}
runs += prec;
// Check if the entity is close to its destination
if (Math.abs(v2.getX() - v1.getX()) < 1.5f && Math.abs(v2.getY() - v1.getY()) < 1.5f && Math.abs(v2.getZ() - v1.getZ()) < 1.5f) {
runs = maxTicks;
}
// Check if the entity has collided with something
// using the most basic possible calculation
BlockHelper blockHelper = NMSHandler.getInstance().getBlockHelper();
if (!blockHelper.isSafeBlock(lastEntity.getLocation().add(v3).getBlock().getType()) || !blockHelper.isSafeBlock(lastEntity.getLocation().add(newVel).getBlock().getType())) {
runs = maxTicks;
}
if (no_damage && lastEntity.isLivingEntity()) {
lastEntity.getLivingEntity().setFallDistance(0);
}
// Record the location in case the entity gets lost (EG, if a pushed arrow hits a mob)
lastLocation = lastEntity.getLocation();
} else {
this.cancel();
if (script != null) {
List<ScriptEntry> entries = script.getContainer().getBaseEntries(scriptEntry.entryData.clone());
ScriptQueue queue = InstantQueue.getQueue(ScriptQueue.getNextId(script.getContainer().getName())).addEntries(entries);
if (lastEntity.getLocation() != null) {
queue.addDefinition("location", lastEntity.getLocation().identify());
} else {
queue.addDefinition("location", lastLocation.identify());
}
queue.addDefinition("pushed_entities", entityList.toString());
queue.addDefinition("last_entity", lastEntity.identify());
if (definitions != null) {
int x = 1;
String[] definition_names = null;
try {
definition_names = script.getContainer().getString("definitions").split("\\|");
} catch (Exception e) {
// TODO: less lazy handling
}
for (String definition : definitions) {
String name = definition_names != null && definition_names.length >= x ? definition_names[x - 1].trim() : String.valueOf(x);
queue.addDefinition(name, definition);
dB.echoDebug(scriptEntry, "Adding definition %" + name + "% as " + definition);
x++;
}
}
queue.start();
}
scriptEntry.setFinished(true);
}
}
};
task.runTaskTimer(DenizenAPI.getCurrentInstance(), 0, prec);
}
use of net.aufdemrand.denizencore.scripts.ScriptEntry in project Denizen-For-Bukkit by DenizenScript.
the class RunCommand method execute.
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
dB.report(scriptEntry, getName(), (scriptEntry.hasObject("script") ? scriptEntry.getdObject("script").debug() : scriptEntry.getScript().debug()) + (scriptEntry.hasObject("instant") ? scriptEntry.getdObject("instant").debug() : "") + (scriptEntry.hasObject("path") ? scriptEntry.getElement("path").debug() : "") + (scriptEntry.hasObject("local") ? scriptEntry.getElement("local").debug() : "") + (scriptEntry.hasObject("delay") ? scriptEntry.getdObject("delay").debug() : "") + (scriptEntry.hasObject("id") ? scriptEntry.getdObject("id").debug() : "") + (scriptEntry.hasObject("definitions") ? scriptEntry.getdObject("definitions").debug() : "") + (scriptEntry.hasObject("speed") ? scriptEntry.getdObject("speed").debug() : ""));
// Get the script
dScript script = scriptEntry.getdObject("script");
// Get the entries
List<ScriptEntry> entries;
// If it's local
if (scriptEntry.hasObject("local")) {
entries = scriptEntry.getScript().getContainer().getEntries(scriptEntry.entryData.clone(), scriptEntry.getElement("path").asString());
script = scriptEntry.getScript();
} else // If it has a path
if (scriptEntry.hasObject("path") && scriptEntry.getObject("path") != null) {
entries = script.getContainer().getEntries(scriptEntry.entryData.clone(), scriptEntry.getElement("path").asString());
} else // Else, assume standard path
{
entries = script.getContainer().getBaseEntries(scriptEntry.entryData.clone());
}
// Get the 'id' if specified
String id = (scriptEntry.hasObject("id") ? (scriptEntry.getElement("id")).asString() : ScriptQueue.getNextId(script.getContainer().getName()));
// Build the queue
ScriptQueue queue;
if (scriptEntry.hasObject("instant")) {
queue = InstantQueue.getQueue(id).addEntries(entries);
} else {
if (scriptEntry.hasObject("speed")) {
Duration speed = scriptEntry.getdObject("speed");
queue = ((TimedQueue) TimedQueue.getQueue(id).addEntries(entries)).setSpeed(speed.getTicks());
} else {
// Check speed of the script if a TimedQueue -- if identified, use the speed from the script.
if (script != null && script.getContainer().contains("SPEED")) {
long ticks = Duration.valueOf(script.getContainer().getString("SPEED", "0")).getTicks();
if (ticks > 0) {
queue = ((TimedQueue) TimedQueue.getQueue(id).addEntries(entries)).setSpeed(ticks);
} else {
queue = InstantQueue.getQueue(id).addEntries(entries);
}
} else {
queue = TimedQueue.getQueue(id).addEntries(entries);
}
}
}
// Set any delay
if (scriptEntry.hasObject("delay")) {
queue.delayUntil(System.currentTimeMillis() + ((Duration) scriptEntry.getObject("delay")).getMillis());
}
// Set any definitions
if (scriptEntry.hasObject("definitions")) {
int x = 1;
Element raw_defintions = scriptEntry.getElement("definitions");
dList definitions = dList.valueOf(raw_defintions.asString());
String[] definition_names = null;
try {
definition_names = script.getContainer().getString("definitions").split("\\|");
} catch (Exception e) {
// TODO: less lazy handling
}
for (String definition : definitions) {
String name = definition_names != null && definition_names.length >= x ? definition_names[x - 1].trim() : String.valueOf(x);
queue.addDefinition(name, definition);
dB.echoDebug(scriptEntry, "Adding definition %" + name + "% as " + definition);
x++;
}
queue.addDefinition("raw_context", raw_defintions.asString());
}
// Setup a callback if the queue is being waited on
if (scriptEntry.shouldWaitFor()) {
// Record the ScriptEntry
final ScriptEntry se = scriptEntry;
queue.callBack(new Runnable() {
@Override
public void run() {
se.setFinished(true);
}
});
}
// Allow determinations because why not
long reqId = DetermineCommand.getNewId();
queue.setReqId(reqId);
// Also add the reqId to each of the entries for reasons
ScriptBuilder.addObjectToEntries(entries, "ReqId", reqId);
// Save the queue for script referencing
scriptEntry.addObject("created_queue", queue);
// OK, GO!
queue.start();
}
use of net.aufdemrand.denizencore.scripts.ScriptEntry in project Denizen-For-Bukkit by DenizenScript.
the class ActionHandler method doAction.
public String doAction(String actionName, dNPC npc, dPlayer player, AssignmentScriptContainer assignment, Map<String, dObject> context) {
if (context == null) {
context = new HashMap<String, dObject>();
}
String determination = "none";
if (assignment == null) {
// dB.echoDebug("Tried to do 'on " + actionName + ":' but couldn't find a matching script.");
return determination;
}
if (!assignment.contains("actions.on " + actionName)) {
return determination;
}
dB.report(assignment, "Action", aH.debugObj("Type", "On " + actionName) + aH.debugObj("NPC", npc.toString()) + assignment.getAsScriptArg().debug() + (player != null ? aH.debugObj("Player", player.getName()) : ""));
// Fetch script from Actions
List<ScriptEntry> script = assignment.getEntries(new BukkitScriptEntryData(player, npc), "actions.on " + actionName);
if (script.isEmpty()) {
return determination;
}
// Create new ID -- this is what we will look for when determining an outcome
long id = DetermineCommand.getNewId();
// Add the reqId to each of the entries for the determine command
ScriptBuilder.addObjectToEntries(script, "ReqId", id);
dB.echoDebug(assignment, DebugElement.Header, "Building action 'On " + actionName.toUpperCase() + "' for " + npc.toString());
// Add entries and context to the queue
ScriptQueue queue = InstantQueue.getQueue(ScriptQueue.getNextId(assignment.getName())).addEntries(script);
OldEventManager.OldEventContextSource oecs = new OldEventManager.OldEventContextSource();
oecs.contexts = context;
oecs.contexts.put("event_header", new Element(actionName));
queue.setContextSource(oecs);
// Start the queue!
queue.start();
// Check the determination by asking the DetermineCommand
if (DetermineCommand.hasOutcome(id)) {
determination = DetermineCommand.getOutcome(id).get(0);
}
// TODO: Multiple determination system
return determination;
}
use of net.aufdemrand.denizencore.scripts.ScriptEntry in project Denizen-For-Bukkit by DenizenScript.
the class DenizenChat method talk.
@Override
public void talk(SpeechContext speechContext) {
if (!(speechContext instanceof DenizenSpeechContext)) {
return;
}
DenizenSpeechContext context = (DenizenSpeechContext) speechContext;
Talkable talker = context.getTalker();
if (talker == null) {
return;
}
ScriptEntry entry = context.getScriptEntry();
ScriptQueue queue = entry.getResidingQueue();
String defTalker = null;
if (queue.hasDefinition("talker")) {
defTalker = queue.getDefinition("talker");
}
queue.addDefinition("talker", new dEntity(talker.getEntity()).identify());
String defMessage = null;
if (queue.hasDefinition("message")) {
defMessage = queue.getDefinition("message");
}
queue.addDefinition("message", context.getMessage());
// Chat to the world using Denizen chat settings
if (!context.hasRecipients()) {
String text = TagManager.tag(Settings.chatNoTargetFormat(), new BukkitTagContext(entry, false));
talkToBystanders(talker, text, context);
} else // Single recipient
if (context.size() <= 1) {
// Send chat to target
String text = TagManager.tag(Settings.chatToTargetFormat(), new BukkitTagContext(entry, false));
for (Talkable entity : context) {
entity.talkTo(context, text, this);
}
// Check if bystanders hear targeted chat
if (context.isBystandersEnabled()) {
String defTarget = null;
if (queue.hasDefinition("target")) {
defTarget = queue.getDefinition("target");
}
queue.addDefinition("target", new dEntity(context.iterator().next().getEntity()).identify());
String bystanderText = TagManager.tag(Settings.chatWithTargetToBystandersFormat(), new BukkitTagContext(entry, false));
talkToBystanders(talker, bystanderText, context);
if (defTarget != null) {
queue.addDefinition("target", defTarget);
}
}
} else // Multiple recipients
{
// Send chat to targets
String text = TagManager.tag(Settings.chatToTargetFormat(), new BukkitTagContext(entry, false));
for (Talkable entity : context) {
entity.talkTo(context, text, this);
}
if (context.isBystandersEnabled()) {
String[] format = Settings.chatMultipleTargetsFormat().split("%target%");
if (format.length <= 1) {
dB.echoError("Invalid 'Commands.Chat.Options.Multiple targets format' in config.yml! Must have at least 1 %target%");
}
StringBuilder parsed = new StringBuilder();
Iterator<Talkable> iter = context.iterator();
int i = 0;
while (iter.hasNext()) {
if (i == format.length) {
parsed.append(format[i]);
break;
}
parsed.append(format[i]).append(new dEntity(iter.next().getEntity()).getName());
i++;
}
String targets = TagManager.tag(parsed.toString(), new BukkitTagContext(entry, false));
String defTargets = null;
if (queue.hasDefinition("targets")) {
defTargets = queue.getDefinition("targets");
}
queue.addDefinition("targets", targets);
String bystanderText = TagManager.tag(Settings.chatWithTargetsToBystandersFormat(), new BukkitTagContext(entry, false));
talkToBystanders(talker, bystanderText, context);
if (defTargets != null) {
queue.addDefinition("targets", defTargets);
}
}
}
if (defMessage != null) {
queue.addDefinition("message", defMessage);
}
if (defTalker != null) {
queue.addDefinition("talker", defTalker);
}
}
Aggregations