use of net.aufdemrand.denizencore.objects.Duration in project Denizen-For-Bukkit by DenizenScript.
the class CastCommand method parseArgs.
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
// Iterate through arguments
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("remove") && arg.matches("remove", "cancel")) {
scriptEntry.addObject("remove", new Element(true));
} else if (!scriptEntry.hasObject("ambient") && arg.matches("no_ambient")) {
scriptEntry.addObject("ambient", new Element(false));
} else if (!scriptEntry.hasObject("show_particles") && arg.matches("hide_particles")) {
scriptEntry.addObject("show_particles", new Element(false));
} else if (!scriptEntry.hasObject("duration") && arg.matchesPrefix("duration", "d") && arg.matchesArgumentType(Duration.class)) {
scriptEntry.addObject("duration", arg.asType(Duration.class));
} else if (!scriptEntry.hasObject("amplifier") && arg.matchesPrefix("power", "p", "amplifier", "a") && arg.matchesPrimitive(aH.PrimitiveType.Double)) {
scriptEntry.addObject("amplifier", arg.asElement());
} else if (!scriptEntry.hasObject("effect") && PotionEffectType.getByName(arg.asElement().asString()) != null) {
scriptEntry.addObject("effect", PotionEffectType.getByName(arg.asElement().asString()));
} else if (!scriptEntry.hasObject("entities") && arg.matchesArgumentList(dEntity.class)) {
scriptEntry.addObject("entities", arg.asType(dList.class).filter(dEntity.class));
} else {
arg.reportUnhandled();
}
}
// No targets specified, let's use defaults if available
scriptEntry.defaultObject("entities", (((BukkitScriptEntryData) scriptEntry.entryData).hasPlayer() ? Arrays.asList(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getDenizenEntity()) : null), (((BukkitScriptEntryData) scriptEntry.entryData).hasNPC() ? Arrays.asList(((BukkitScriptEntryData) scriptEntry.entryData).getNPC().getDenizenEntity()) : null));
// No potion specified? Problem!
if (!scriptEntry.hasObject("effect")) {
throw new InvalidArgumentsException("Must specify a valid PotionType!");
}
scriptEntry.defaultObject("duration", new Duration(60));
scriptEntry.defaultObject("amplifier", new Element(1));
scriptEntry.defaultObject("remove", new Element(false));
scriptEntry.defaultObject("show_particles", new Element(true));
scriptEntry.defaultObject("ambient", new Element(true));
}
use of net.aufdemrand.denizencore.objects.Duration in project Denizen-For-Bukkit by DenizenScript.
the class LookCommand method execute.
@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
final dLocation loc = (dLocation) scriptEntry.getObject("location");
final List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
final Duration duration = (Duration) scriptEntry.getObject("duration");
dB.report(scriptEntry, getName(), loc.debug() + aH.debugObj("entities", entities.toString()));
for (dEntity entity : entities) {
if (entity.isSpawned()) {
NMSHandler.getInstance().getEntityHelper().faceLocation(entity.getBukkitEntity(), loc);
}
}
if (duration != null && duration.getTicks() > 2) {
BukkitRunnable task = new BukkitRunnable() {
long bounces = 0;
public void run() {
bounces += 2;
if (bounces > duration.getTicks()) {
this.cancel();
return;
}
for (dEntity entity : entities) {
if (entity.isSpawned()) {
NMSHandler.getInstance().getEntityHelper().faceLocation(entity.getBukkitEntity(), loc);
}
}
}
};
task.runTaskTimer(DenizenAPI.getCurrentInstance(), 0, 2);
}
}
use of net.aufdemrand.denizencore.objects.Duration 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.objects.Duration in project Denizen-For-Bukkit by DenizenScript.
the class RotateCommand method parseArgs.
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("cancel") && (arg.matches("cancel") || arg.matches("stop"))) {
scriptEntry.addObject("cancel", new Element("true"));
} else if (!scriptEntry.hasObject("infinite") && arg.matches("infinite")) {
scriptEntry.addObject("infinite", new Element("true"));
} else if (!scriptEntry.hasObject("duration") && arg.matchesArgumentType(Duration.class) && arg.matchesPrefix("duration", "d")) {
scriptEntry.addObject("duration", arg.asType(Duration.class));
} else if (!scriptEntry.hasObject("frequency") && arg.matchesArgumentType(Duration.class) && arg.matchesPrefix("frequency", "f")) {
scriptEntry.addObject("frequency", arg.asType(Duration.class));
} else if (!scriptEntry.hasObject("yaw") && arg.matchesPrefix("yaw", "y", "rotation", "r") && arg.matchesPrimitive(aH.PrimitiveType.Float)) {
scriptEntry.addObject("yaw", arg.asElement());
} else if (!scriptEntry.hasObject("pitch") && arg.matchesPrefix("pitch", "p", "tilt", "t") && arg.matchesPrimitive(aH.PrimitiveType.Float)) {
scriptEntry.addObject("pitch", arg.asElement());
} else if (!scriptEntry.hasObject("entities") && arg.matchesArgumentList(dEntity.class)) {
scriptEntry.addObject("entities", arg.asType(dList.class).filter(dEntity.class));
} else {
arg.reportUnhandled();
}
}
// Use the NPC or the Player as the default entity
scriptEntry.defaultObject("entities", (((BukkitScriptEntryData) scriptEntry.entryData).hasPlayer() ? Arrays.asList(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getDenizenEntity()) : null), (((BukkitScriptEntryData) scriptEntry.entryData).hasNPC() ? Arrays.asList(((BukkitScriptEntryData) scriptEntry.entryData).getNPC().getDenizenEntity()) : null));
scriptEntry.defaultObject("yaw", new Element(10));
scriptEntry.defaultObject("pitch", new Element(0));
scriptEntry.defaultObject("duration", new Duration(20));
scriptEntry.defaultObject("frequency", Duration.valueOf("1t"));
// Check to make sure required arguments have been filled
if (!scriptEntry.hasObject("entities")) {
throw new InvalidArgumentsException("Must specify entity/entities!");
}
}
use of net.aufdemrand.denizencore.objects.Duration in project Denizen-For-Bukkit by DenizenScript.
the class WaitCommand method parseArgs.
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
// Initialize required fields
ScriptQueue queue = scriptEntry.getResidingQueue();
Duration delay = new Duration(3);
// Iterate through arguments
for (String arg : scriptEntry.getArguments()) {
// Set duration
if (aH.matchesDuration(arg)) {
delay = Duration.valueOf(arg);
}
// Specify queue
if (aH.matchesQueue(arg)) {
queue = ScriptQueue._getExistingQueue(arg);
}
}
scriptEntry.addObject("queue", queue);
scriptEntry.addObject("delay", delay);
}
Aggregations