Skip to content

Commit

Permalink
Add rotationthreshold argument to Fly command, to allow people to cus…
Browse files Browse the repository at this point in the history
…tomize the turbulence of their flights.
  • Loading branch information
davidcernat committed Sep 15, 2013
1 parent b2f82da commit abacb23
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 16 deletions.
12 changes: 11 additions & 1 deletion src/main/java/net/aufdemrand/denizen/objects/dEntity.java
Expand Up @@ -400,7 +400,7 @@ public boolean isProjectile() {
}

/**
* Get this entity's shooter
* Get this Projectile entity's shooter
*
* @return A dEntity of the shooter
*/
Expand All @@ -409,6 +409,16 @@ public dEntity getShooter() {
return new dEntity(getProjectile().getShooter());
}

/**
* Set this Projectile entity's shooter
*
*/

public void setShooter(dEntity shooter) {
if (shooter.isLivingEntity())
getProjectile().setShooter(shooter.getLivingEntity());
}

/**
* Check whether this entity has a shooter.
*
Expand Down
Expand Up @@ -812,7 +812,7 @@ public void registerCoreMembers() {
// Todo
// -->
registerCoreMember(FlyCommand.class,
"FLY", "fly (cancel) [<entity>|...] (controller:<player>) (origin:<location>) (destinations:<location>|...) (speed:<#.#>)", 1);
"FLY", "fly (cancel) [<entity>|...] (controller:<player>) (origin:<location>) (destinations:<location>|...) (speed:<#.#>) (rotationthreshold:<#.#>)", 1);

// <--[command]
// @Name Follow
Expand Down
Expand Up @@ -71,6 +71,13 @@ else if (!scriptEntry.hasObject("entities")

scriptEntry.addObject("entities", ((dList) arg.asType(dList.class)).filter(dEntity.class));
}

else if (!scriptEntry.hasObject("rotationThreshold")
&& arg.matchesPrefix("rotationthreshold, rotation, r")
&& arg.matchesPrimitive(aH.PrimitiveType.Float)) {

scriptEntry.addObject("rotationThreshold", arg.asElement());
}

else if (!scriptEntry.hasObject("speed")
&& arg.matchesPrimitive(aH.PrimitiveType.Double)) {
Expand All @@ -80,17 +87,15 @@ else if (!scriptEntry.hasObject("speed")
}

// Use the NPC or player's locations as the location if one is not specified

scriptEntry.defaultObject("origin",
scriptEntry.hasPlayer() ? scriptEntry.getPlayer().getLocation() : null,
scriptEntry.hasNPC() ? scriptEntry.getNPC().getLocation() : null);

// Use a default speed of 1.2 if one is not specified

// Use a default speed and rotation threshold if they are not specified
scriptEntry.defaultObject("speed", new Element(1.2));
scriptEntry.defaultObject("rotationThreshold", new Element(45));

// Check to make sure required arguments have been filled

if (!scriptEntry.hasObject("entities"))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "ENTITIES");
if (!scriptEntry.hasObject("origin"))
Expand Down Expand Up @@ -159,14 +164,16 @@ public void execute(final ScriptEntry scriptEntry) throws CommandExecutionExcept
}
}

final Element speed = (Element) scriptEntry.getObject("speed");
final double speed = ((Element) scriptEntry.getObject("speed")).asDouble();
final float rotationThreshold = ((Element) scriptEntry.getObject("rotationThreshold")).asFloat();
Boolean cancel = scriptEntry.hasObject("cancel");

// Report to dB
dB.report(getName(), (cancel == true ? aH.debugObj("cancel", cancel) : "") +
aH.debugObj("origin", origin) +
aH.debugObj("entities", entities.toString()) +
aH.debugObj("speed", speed) +
aH.debugObj("rotation threshold degrees", rotationThreshold) +
(freeflight ? aH.debugObj("controller", controller)
: aH.debugObj("destinations", destinations.toString())));

Expand Down Expand Up @@ -236,14 +243,14 @@ public void run() {

// To avoid excessive turbulence, only have the entity rotate
// when it really needs to
if (!Rotation.isFacingLocation(entity, location, 50)) {
if (!Rotation.isFacingLocation(entity, location, rotationThreshold)) {

Rotation.faceLocation(entity, location);
}

Vector v1 = entity.getLocation().toVector();
Vector v2 = location.toVector();
Vector v3 = v2.clone().subtract(v1).normalize().multiply(speed.asDouble());
Vector v3 = v2.clone().subtract(v1).normalize().multiply(speed);

entity.setVelocity(v3);

Expand Down
Expand Up @@ -33,19 +33,19 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException

if (!scriptEntry.hasObject("entities")
&& arg.matchesArgumentList(dEntity.class)) {
// Entity arg

scriptEntry.addObject("entities", ((dList) arg.asType(dList.class)).filter(dEntity.class));
}

else if (!scriptEntry.hasObject("region")
&& arg.matchesPrefix("region, r")) {
// Location arg

scriptEntry.addObject("region", arg.asElement());
}

else if (!scriptEntry.hasObject("world")
&& arg.matchesArgumentType(dWorld.class)) {
// add world

scriptEntry.addObject("world", arg.asType(dWorld.class));
}
}
Expand Down
Expand Up @@ -1588,10 +1588,10 @@ else if (Argument.valueOf(determination).matchesArgumentList(dEntity.class)) {
newProjectile.teleport(projectile.getLocation());
}

// Set the entity as the shooter of the projectile
if (newProjectile.getBukkitEntity() instanceof Projectile) {
((Projectile) newProjectile.getBukkitEntity())
.setShooter((LivingEntity) entity);
// Set the entity as the shooter of the projectile,
// where applicable
if (newProjectile.isProjectile()) {
newProjectile.setShooter(entity);
}
}

Expand Down

0 comments on commit abacb23

Please sign in to comment.