Skip to content

Commit

Permalink
Add precision and force_along arguments to PUSH
Browse files Browse the repository at this point in the history
Yay for better control
  • Loading branch information
mcmonkey4eva committed Dec 8, 2014
1 parent 455fb32 commit fad99e3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
Expand Up @@ -1957,7 +1957,7 @@ public void registerCoreMembers() {

// <--[command]
// @Name Push
// @Syntax push [<entity>|...] (origin:<entity>/<location>) (destination:<location>) (speed:<#.#>) (duration:<duration>) (<script>)
// @Syntax push [<entity>|...] (origin:<entity>/<location>) (destination:<location>) (speed:<#.#>) (duration:<duration>) (<script>) (force_along) (precision:<#>)
// @Required 1
// @Stable stable
// @Short Pushes entities through the air in a straight line.
Expand All @@ -1977,7 +1977,7 @@ public void registerCoreMembers() {
//
// -->
registerCoreMember(PushCommand.class,
"PUSH", "push [<entity>|...] (origin:<entity>/<location>) (destination:<location>) (speed:<#.#>) (<duration>) (<script>)", 1);
"PUSH", "push [<entity>|...] (origin:<entity>/<location>) (destination:<location>) (speed:<#.#>) (<duration>) (<script>) (force_along) (precision:<#>)", 1);


// <--[command]
Expand Down
Expand Up @@ -23,6 +23,7 @@
import net.aufdemrand.denizen.utilities.entity.Position;
import net.aufdemrand.denizen.utilities.entity.Rotation;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;
Expand Down Expand Up @@ -86,11 +87,21 @@ else if (!scriptEntry.hasObject("entities")
scriptEntry.addObject("entities", arg.asType(dList.class).filter(dEntity.class));
}

else if (!scriptEntry.hasObject("force_along")
&& arg.matches("force_along")) {
scriptEntry.addObject("force_along", new Element(true));
}

else if (!scriptEntry.hasObject("no_rotate")
&& arg.matches("no_rotate")) {
scriptEntry.addObject("no_rotate", new Element(true));
}

else if (!scriptEntry.hasObject("precision")
&& arg.matchesPrefix("precision")) {
scriptEntry.addObject("precision", arg.asElement());
}

else arg.reportUnhandled();
}

Expand All @@ -105,6 +116,8 @@ else if (!scriptEntry.hasObject("no_rotate")

scriptEntry.defaultObject("speed", new Element(1.5));
scriptEntry.defaultObject("duration", new Duration(20));
scriptEntry.defaultObject("force_along", new Element(false));
scriptEntry.defaultObject("precision", new Element(2));

// Check to make sure required arguments have been filled

Expand Down Expand Up @@ -147,7 +160,11 @@ public void execute(final ScriptEntry scriptEntry) throws CommandExecutionExcept
final dScript script = (dScript) scriptEntry.getObject("script");

final double speed = scriptEntry.getElement("speed").asDouble();
final int maxTicks = ((Duration) scriptEntry.getObject("duration")).getTicksAsInt() / 2;
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) +
Expand All @@ -156,8 +173,12 @@ public void execute(final ScriptEntry scriptEntry) throws CommandExecutionExcept
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"): ""));

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();
Expand Down Expand Up @@ -193,6 +214,9 @@ public void execute(final ScriptEntry scriptEntry) throws CommandExecutionExcept
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;
Expand All @@ -203,20 +227,29 @@ public void run() {
if (runs < maxTicks && lastEntity.isValid()) {

Vector v1 = lastEntity.getLocation().toVector();
Vector v3 = v2.clone().subtract(v1).normalize().multiply(speed);
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()));
}

lastEntity.setVelocity(v3);
runs++;
runs += prec;

// Check if the entity is close to its destination
if (Math.abs(v2.getX() - v1.getX()) < 2 && Math.abs(v2.getY() - v1.getY()) < 2
&& Math.abs(v2.getZ() - v1.getZ()) < 2) {
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
if (lastEntity.getLocation().add(v3).getBlock().getType() != Material.AIR) {
if (!lastEntity.getLocation().add(v3).getBlock().getType().isSolid()) {
runs = maxTicks;
}

Expand All @@ -243,6 +276,6 @@ public void run() {
}
}
};
task.runTaskTimer(DenizenAPI.getCurrentInstance(), 0, 2);
task.runTaskTimer(DenizenAPI.getCurrentInstance(), 0, prec);
}
}

0 comments on commit fad99e3

Please sign in to comment.