use of net.aufdemrand.denizencore.scripts.ScriptEntry in project Denizen-For-Bukkit by DenizenScript.
the class WalkCommandCitizensEvents method checkHeld.
public void checkHeld(NavigationEvent e) {
if (e.getNPC() == null) {
return;
}
// the entry to be marked 'waited for'.
for (int i = 0; i < WalkCommand.held.size(); i++) {
ScriptEntry entry = WalkCommand.held.get(i);
// Get all NPCs associated with the entry. They must all
// finish navigation before the entry can be let go
List<dNPC> tally = (List<dNPC>) entry.getObject("tally");
if (tally == null) {
continue;
}
for (int x = 0; x < tally.size(); x++) {
if (!tally.get(x).isSpawned()) {
tally.remove(x--);
}
}
// If the NPC is the NPC from the event, take it from the list.
tally.remove(dNPC.mirrorCitizensNPC(e.getNPC()));
// Check if tally is empty.
if (tally.isEmpty()) {
entry.setFinished(true);
WalkCommand.held.remove(i);
i--;
}
}
}
use of net.aufdemrand.denizencore.scripts.ScriptEntry in project Denizen-For-Bukkit by DenizenScript.
the class ShootCommand 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()));
boolean no_rotate = scriptEntry.hasObject("no_rotate") && scriptEntry.getElement("no_rotate").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().clone().add(originEntity.getEyeLocation().clone().getDirection().multiply(30))) : (originLocation != null ? new dLocation(originLocation.clone().add(originLocation.getDirection().multiply(30))) : null));
// TODO: Same as PUSH -- is this the place to do this?
if (destination == null) {
dB.report(scriptEntry, getName(), "No destination specified!");
return;
}
final List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
final dScript script = (dScript) scriptEntry.getObject("script");
final dList definitions = (dList) scriptEntry.getObject("definitions");
dEntity shooter = (dEntity) scriptEntry.getObject("shooter");
Element height = scriptEntry.getElement("height");
Element gravity = scriptEntry.getElement("gravity");
Element speed = scriptEntry.getElement("speed");
Element spread = scriptEntry.getElement("spread");
dLocation lead = (dLocation) scriptEntry.getObject("lead");
// Report to dB
dB.report(scriptEntry, getName(), aH.debugObj("origin", originEntity != null ? originEntity : originLocation) + aH.debugObj("entities", entities.toString()) + destination.debug() + height.debug() + (gravity != null ? gravity.debug() : "") + (speed != null ? speed.debug() : "") + (script != null ? script.debug() : "") + (shooter != null ? shooter.debug() : "") + (spread != null ? spread.debug() : "") + (lead != null ? lead.debug() : "") + (no_rotate ? aH.debugObj("no_rotate", "true") : "") + (definitions != null ? definitions.debug() : ""));
// Keep a dList of entities that can be called using <entry[name].shot_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) {
if (!entity.isSpawned() || !no_rotate) {
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() && (shooter != null || originEntity != null)) {
entity.setShooter(shooter != null ? shooter : originEntity);
// Also, watch for it hitting a target
arrows.put(entity.getUUID(), null);
}
}
// Add entities to context so that the specific entities created/spawned
// can be fetched.
scriptEntry.addObject("shot_entities", entityList);
if (spread == null) {
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);
if (gravity == null) {
gravity = new Element(lastEntity.getEntityType().getGravity());
}
if (speed == null) {
Vector v1 = lastEntity.getLocation().toVector();
Vector v2 = destination.toVector();
Vector v3 = Velocity.calculate(v1, v2, gravity.asDouble(), height.asDouble());
lastEntity.setVelocity(v3);
} else if (lead == null) {
Vector relative = destination.clone().subtract(originLocation).toVector();
lastEntity.setVelocity(relative.normalize().multiply(speed.asDouble()));
} else {
double g = 20;
double v = speed.asDouble();
Vector relative = destination.clone().subtract(originLocation).toVector();
double testAng = Velocity.launchAngle(originLocation, destination.toVector(), v, relative.getY(), g);
double hangTime = Velocity.hangtime(testAng, v, relative.getY(), g);
Vector to = destination.clone().add(lead.clone().multiply(hangTime)).toVector();
relative = to.clone().subtract(originLocation.toVector());
Double dist = Math.sqrt(relative.getX() * relative.getX() + relative.getZ() * relative.getZ());
if (dist == 0) {
dist = 0.1d;
}
testAng = Velocity.launchAngle(originLocation, to, v, relative.getY(), g);
relative.setY(Math.tan(testAng) * dist);
relative = relative.normalize();
v = v + (1.188 * Math.pow(hangTime, 2));
relative = relative.multiply(v / 20.0d);
lastEntity.setVelocity(relative);
}
if (spread != null) {
Vector base = lastEntity.getVelocity().clone();
float sf = spread.asFloat();
for (dEntity entity : entities) {
Vector newvel = Velocity.spread(base, (CoreUtilities.getRandom().nextDouble() > 0.5f ? 1 : -1) * Math.toRadians(CoreUtilities.getRandom().nextDouble() * sf), (CoreUtilities.getRandom().nextDouble() > 0.5f ? 1 : -1) * Math.toRadians(CoreUtilities.getRandom().nextDouble() * sf));
entity.setVelocity(newvel);
}
}
final dLocation start = new dLocation(lastEntity.getLocation());
final Vector start_vel = lastEntity.getVelocity();
// A task used to trigger a script if the entity is no longer
// being shot, when the script argument is used
BukkitRunnable task = new BukkitRunnable() {
boolean flying = true;
dLocation lastLocation = null;
Vector lastVelocity = null;
public void run() {
// If the entity is no longer spawned, stop the task
if (!lastEntity.isSpawned()) {
flying = false;
} else // the air, stop the task
if (lastVelocity != null) {
if (lastVelocity.distance(lastEntity.getBukkitEntity().getVelocity()) < 0.05) {
flying = false;
}
}
// are met
if (!flying) {
this.cancel();
if (script != null) {
if (lastLocation == null) {
lastLocation = start;
}
if (lastVelocity == null) {
lastVelocity = start_vel;
}
// Build a queue out of the targeted script
List<ScriptEntry> entries = script.getContainer().getBaseEntries(scriptEntry.entryData.clone());
ScriptQueue queue = InstantQueue.getQueue(ScriptQueue.getNextId(script.getContainer().getName())).addEntries(entries);
// Add relevant definitions
queue.addDefinition("location", lastLocation.identify());
queue.addDefinition("shot_entities", entityList.toString());
queue.addDefinition("last_entity", lastEntity.identify());
// Handle hit_entities definition
dList hitEntities = new dList();
for (dEntity entity : entities) {
if (arrows.containsKey(entity.getUUID())) {
dEntity hit = arrows.get(entity.getUUID());
arrows.remove(entity.getUUID());
if (hit != null) {
hitEntities.add(hit.identify());
}
}
}
queue.addDefinition("hit_entities", hitEntities.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++;
}
}
// Start it!
queue.start();
}
scriptEntry.setFinished(true);
} else {
// Record it's position in case the entity dies
lastLocation = lastEntity.getLocation();
lastVelocity = lastEntity.getVelocity();
}
}
};
task.runTaskTimer(DenizenAPI.getCurrentInstance(), 0, 2);
}
use of net.aufdemrand.denizencore.scripts.ScriptEntry in project Denizen-For-Bukkit by DenizenScript.
the class RandomCommand method execute.
@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
int possibilities = 0;
ScriptQueue queue = scriptEntry.getResidingQueue();
List<ScriptEntry> bracedCommands = null;
if (!scriptEntry.hasObject("braces")) {
possibilities = scriptEntry.getElement("possibilities").asInt();
} else {
bracedCommands = ((List<BracedData>) scriptEntry.getObject("braces")).get(0).value;
possibilities = bracedCommands.size();
}
int selected = CoreUtilities.getRandom().nextInt(possibilities);
// Try to not duplicate
if (selected == previous || selected == previous2 || selected == previous3) {
selected = CoreUtilities.getRandom().nextInt(possibilities);
}
if (selected == previous || selected == previous2 || selected == previous3) {
selected = CoreUtilities.getRandom().nextInt(possibilities);
}
previous3 = previous2;
previous2 = previous;
previous = selected;
scriptEntry.addObject("possibilities", new Element(possibilities));
scriptEntry.addObject("selected", new Element(selected));
dB.report(scriptEntry, getName(), aH.debugObj("possibilities", possibilities) + aH.debugObj("choice", selected + 1));
if (bracedCommands == null) {
ScriptEntry keeping = null;
for (int x = 0; x < possibilities; x++) {
if (x != selected) {
queue.removeEntry(0);
} else {
dB.echoDebug(scriptEntry, "...selected '" + queue.getEntry(0).getCommandName() + ": " + queue.getEntry(0).getArguments() + "'.");
keeping = queue.getEntry(0);
queue.removeEntry(0);
}
}
queue.injectEntry(keeping, 0);
} else {
queue.injectEntry(bracedCommands.get(selected).addObject("reqID", scriptEntry.getObject("reqID")), 0);
}
}
use of net.aufdemrand.denizencore.scripts.ScriptEntry in project Denizen-For-Bukkit by DenizenScript.
the class RepeatCommand 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");
Element quantity = scriptEntry.getElement("qty");
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("repeat") && 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("repeat") && args.size() > 0 && args.get(0).equalsIgnoreCase("\0CALLBACK")) {
scriptEntry.getResidingQueue().removeEntry(0);
break;
}
scriptEntry.getResidingQueue().removeEntry(0);
}
} else {
dB.echoError("Cannot stop repeat: 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("repeat") && 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("repeat") && args.size() > 0 && args.get(0).equalsIgnoreCase("\0CALLBACK")) {
break;
}
scriptEntry.getResidingQueue().removeEntry(0);
}
} else {
dB.echoError("Cannot stop repeat: not in one!");
}
return;
} else if (callback != null && callback.asBoolean()) {
if (scriptEntry.getOwner() != null && (scriptEntry.getOwner().getCommandName().equalsIgnoreCase("repeat") || scriptEntry.getOwner().getBracedSet() == null || scriptEntry.getOwner().getBracedSet().size() == 0 || scriptEntry.getBracedSet().get(0).value.get(scriptEntry.getBracedSet().get(0).value.size() - 1) != scriptEntry)) {
RepeatData data = (RepeatData) scriptEntry.getOwner().getData();
data.index++;
if (data.index <= data.target) {
dB.echoDebug(scriptEntry, DebugElement.Header, "Repeat loop " + data.index);
scriptEntry.getResidingQueue().addDefinition("value", String.valueOf(data.index));
List<ScriptEntry> bracedCommands = BracedCommand.getBracedCommands(scriptEntry.getOwner()).get(0).value;
ScriptEntry callbackEntry = null;
try {
callbackEntry = new ScriptEntry("REPEAT", new String[] { "\0CALLBACK" }, (scriptEntry.getScript() != null ? scriptEntry.getScript().getContainer() : null));
callbackEntry.copyFrom(scriptEntry);
} catch (ScriptEntryCreationException e) {
dB.echoError(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("Repeat CALLBACK invalid: not a real callback!");
}
} else {
// Get objects
List<ScriptEntry> bracedCommandsList = ((List<BracedData>) scriptEntry.getObject("braces")).get(0).value;
if (bracedCommandsList == null || bracedCommandsList.isEmpty()) {
dB.echoError("Empty braces!");
return;
}
// Report to dB
dB.report(scriptEntry, getName(), quantity.debug());
int target = quantity.asInt();
if (target <= 0) {
dB.echoDebug(scriptEntry, "Zero count, not looping...");
return;
}
RepeatData datum = new RepeatData();
datum.target = target;
datum.index = 1;
scriptEntry.setData(datum);
ScriptEntry callbackEntry = null;
try {
callbackEntry = new ScriptEntry("REPEAT", new String[] { "\0CALLBACK" }, (scriptEntry.getScript() != null ? scriptEntry.getScript().getContainer() : null));
callbackEntry.copyFrom(scriptEntry);
} catch (ScriptEntryCreationException e) {
dB.echoError(e);
}
callbackEntry.setOwner(scriptEntry);
bracedCommandsList.add(callbackEntry);
scriptEntry.getResidingQueue().addDefinition("value", "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 InjectCommand 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() : ""));
// 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());
} else // If it has a path
if (scriptEntry.hasObject("path")) {
entries = script.getContainer().getEntries(scriptEntry.entryData.clone(), scriptEntry.getElement("path").asString());
} else // Else, assume standard path
{
entries = script.getContainer().getBaseEntries(scriptEntry.entryData.clone());
}
// For determine
ScriptBuilder.addObjectToEntries(entries, "ReqId", scriptEntry.getObject("ReqId"));
// If 'instantly' was specified, run the commands immediately.
if (scriptEntry.hasObject("instant")) {
scriptEntry.getResidingQueue().runNow(entries, "INJECT");
} else {
// Inject the entries into the current scriptqueue
scriptEntry.getResidingQueue().injectEntries(entries, 0);
}
}
Aggregations