From abacb231e15c914c2aaf87ece9dfd10554c51335 Mon Sep 17 00:00:00 2001 From: David Cernat Date: Sun, 15 Sep 2013 19:45:57 +0300 Subject: [PATCH] Add rotationthreshold argument to Fly command, to allow people to customize the turbulence of their flights. --- .../aufdemrand/denizen/objects/dEntity.java | 12 ++++++++++- .../scripts/commands/CommandRegistry.java | 2 +- .../scripts/commands/entity/FlyCommand.java | 21 ++++++++++++------- .../commands/entity/RemoveCommand.java | 6 +++--- .../containers/core/WorldScriptHelper.java | 8 +++---- 5 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/main/java/net/aufdemrand/denizen/objects/dEntity.java b/src/main/java/net/aufdemrand/denizen/objects/dEntity.java index be72531841..a23bc4decf 100644 --- a/src/main/java/net/aufdemrand/denizen/objects/dEntity.java +++ b/src/main/java/net/aufdemrand/denizen/objects/dEntity.java @@ -400,7 +400,7 @@ public boolean isProjectile() { } /** - * Get this entity's shooter + * Get this Projectile entity's shooter * * @return A dEntity of the shooter */ @@ -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. * diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/CommandRegistry.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/CommandRegistry.java index 35103f290d..435f35bad5 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/CommandRegistry.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/CommandRegistry.java @@ -812,7 +812,7 @@ public void registerCoreMembers() { // Todo // --> registerCoreMember(FlyCommand.class, - "FLY", "fly (cancel) [|...] (controller:) (origin:) (destinations:|...) (speed:<#.#>)", 1); + "FLY", "fly (cancel) [|...] (controller:) (origin:) (destinations:|...) (speed:<#.#>) (rotationthreshold:<#.#>)", 1); // <--[command] // @Name Follow diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/FlyCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/FlyCommand.java index 6a1943cc7a..29e6601852 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/FlyCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/FlyCommand.java @@ -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)) { @@ -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")) @@ -159,7 +164,8 @@ 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 @@ -167,6 +173,7 @@ public void execute(final ScriptEntry scriptEntry) throws CommandExecutionExcept 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()))); @@ -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); diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/RemoveCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/RemoveCommand.java index 1667807779..f697515396 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/RemoveCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/RemoveCommand.java @@ -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)); } } diff --git a/src/main/java/net/aufdemrand/denizen/scripts/containers/core/WorldScriptHelper.java b/src/main/java/net/aufdemrand/denizen/scripts/containers/core/WorldScriptHelper.java index 7d6808e2a4..1ec963e791 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/containers/core/WorldScriptHelper.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/containers/core/WorldScriptHelper.java @@ -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); } }