Skip to content

Commit

Permalink
Add <world.time.period> tag.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcernat committed Jul 18, 2013
1 parent 5655086 commit d6585d0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
14 changes: 14 additions & 0 deletions src/main/java/net/aufdemrand/denizen/objects/dWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,20 @@ public String getAttribute(Attribute attribute) {
// getWorld().getEntitiesByClasses())
// .getAttribute(attribute.fulfill(1));

// Return "day", "night", "dawn" or "dusk"
if (attribute.startsWith("time.period")) {

long time = getWorld().getTime();
String period;

if (time >= 23000) period = "dawn";
else if (time >= 13500) period = "night";
else if (time >= 12500) period = "dusk";
else period = "day";

return new Element(period).getAttribute(attribute.fulfill(2));
}

if (attribute.startsWith("time"))
return new Element(String.valueOf(getWorld().getTime()))
.getAttribute(attribute.fulfill(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException
scriptEntry.addObject("origin", arg.asType(dEntity.class).setPrefix("entity"));
}

else if (!scriptEntry.hasObject("projectiles")
&& arg.matchesPrefix("projectile, projectiles, p, entity, entities, e")) {
else if (!scriptEntry.hasObject("entities")
&& arg.matchesPrefix("entity, entities, e, projectile, entities, p")) {
// Entity arg
scriptEntry.addObject("projectiles", ((dList) arg.asType(dList.class)).filter(dEntity.class));
scriptEntry.addObject("entities", ((dList) arg.asType(dList.class)).filter(dEntity.class));
}

else if (!scriptEntry.hasObject("destination")
Expand Down Expand Up @@ -87,8 +87,8 @@ else if (!scriptEntry.hasObject("script")

// Check to make sure required arguments have been filled

if ((!scriptEntry.hasObject("projectiles")))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "PROJECTILES");
if ((!scriptEntry.hasObject("entities")))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "entities");

if ((!scriptEntry.hasObject("origin")))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "ORIGIN");
Expand All @@ -107,13 +107,13 @@ public void execute(final ScriptEntry scriptEntry) throws CommandExecutionExcept
shooterEntity.getEyeLocation().getDirection()
.multiply(40)));

List<dEntity> projectiles = (List<dEntity>) scriptEntry.getObject("projectiles");
List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
final Element speed = (Element) scriptEntry.getObject("speed");
final dScript script = (dScript) scriptEntry.getObject("script");

// Report to dB
dB.report(getName(), aH.debugObj("origin", shooter) +
aH.debugObj("projectiles", projectiles.toString()) +
aH.debugObj("entities", entities.toString()) +
aH.debugObj("destination", destination) +
aH.debugObj("speed", speed) +
(script != null ? aH.debugObj("script", script) : ""));
Expand All @@ -132,41 +132,41 @@ public void execute(final ScriptEntry scriptEntry) throws CommandExecutionExcept
shooterEntity.getEyeLocation().getDirection())
.subtract(0, 0.4, 0);

// Go through all the projectiles, spawning/teleporting and rotating them
for (dEntity projectile : projectiles) {
// Go through all the entities, spawning/teleporting and rotating them
for (dEntity entity : entities) {

if (projectile.isSpawned() == false) {
projectile.spawnAt(origin);
if (entity.isSpawned() == false) {
entity.spawnAt(origin);
}
else {
projectile.teleport(origin);
entity.teleport(origin);
}

Rotation.faceLocation(projectile.getBukkitEntity(), destination);
Rotation.faceLocation(entity.getBukkitEntity(), destination);

if (projectile.getBukkitEntity() instanceof Projectile) {
((Projectile) projectile.getBukkitEntity()).setShooter(shooter.getLivingEntity());
if (entity.getBukkitEntity() instanceof Projectile) {
((Projectile) entity.getBukkitEntity()).setShooter(shooter.getLivingEntity());
}
}

Position.mount(Conversion.convert(projectiles));
Position.mount(Conversion.convert(entities));

// Only use the last projectile in the task below
final Entity lastProjectile = projectiles.get(projectiles.size() - 1).getBukkitEntity();
final Entity lastEntity = entities.get(entities.size() - 1).getBukkitEntity();

BukkitRunnable task = new BukkitRunnable() {

int runs = 0;

public void run() {

if (runs < 40 && lastProjectile.isValid())
if (runs < 40 && lastEntity.isValid())
{
Vector v1 = lastProjectile.getLocation().toVector();
Vector v1 = lastEntity.getLocation().toVector();
Vector v2 = destination.toVector();
Vector v3 = v2.clone().subtract(v1).normalize().multiply(speed.asDouble());

lastProjectile.setVelocity(v3);
lastEntity.setVelocity(v3);
runs++;

// Check if the entity is close to its destination
Expand All @@ -180,7 +180,7 @@ public void run() {
// Check if the entity has collided with something
// using the most basic possible calculation

if (lastProjectile.getLocation().add(v3).getBlock().getType().toString().equals("AIR") == false) {
if (lastEntity.getLocation().add(v3).getBlock().getType().toString().equals("AIR") == false) {

runs = 40;
}
Expand All @@ -193,7 +193,7 @@ public void run() {
if (script != null)
{
Map<String, String> context = new HashMap<String, String>();
context.put("1", lastProjectile.getLocation().getX() + "," + lastProjectile.getLocation().getY() + "," + lastProjectile.getLocation().getZ() + "," + lastProjectile.getLocation().getWorld().getName());
context.put("1", lastEntity.getLocation().getX() + "," + lastEntity.getLocation().getY() + "," + lastEntity.getLocation().getZ() + "," + lastEntity.getLocation().getWorld().getName());

((TaskScriptContainer) script.getContainer()).setSpeed(new Duration(0))
.runTaskScript(scriptEntry.getPlayer(), scriptEntry.getNPC(), context);
Expand Down

0 comments on commit d6585d0

Please sign in to comment.