Skip to content

Commit

Permalink
Add defaultObject() method to ScriptEntry and make some commands use it.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcernat committed Jul 15, 2013
1 parent ae4980a commit 35d64bd
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 122 deletions.
24 changes: 22 additions & 2 deletions src/main/java/net/aufdemrand/denizen/scripts/ScriptEntry.java
Expand Up @@ -3,8 +3,6 @@
import net.aufdemrand.denizen.exceptions.ScriptEntryCreationException;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.scripts.containers.ScriptContainer;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;

import java.util.*;

Expand Down Expand Up @@ -88,6 +86,28 @@ public ScriptEntry addObject(String key, Object object) {
objects.put(key.toUpperCase(), object);
return this;
}

/**
* If the scriptEntry lacks the object corresponding to the
* key, set it to the first non-null argument
*
* @param key The key of the object to check
* @return The scriptEntry
*
*/

public ScriptEntry defaultObject(String key, Object... objects) {

if (this.hasObject(key) == false) {
for (Object obj : objects) {
if (obj != null) {
this.addObject(key, obj);
break;
}
}
}
return this;
}

public long getRunTime() {
return runTime;
Expand Down
Expand Up @@ -46,8 +46,7 @@ else if (!scriptEntry.hasObject("duration")

// Use default duration if one is not specified

if ((!scriptEntry.hasObject("duration")))
scriptEntry.addObject("duration", Duration.valueOf("5s"));
scriptEntry.defaultObject("duration", Duration.valueOf("5s"));
}

@SuppressWarnings("unchecked")
Expand Down
Expand Up @@ -72,28 +72,21 @@ else if (!scriptEntry.hasObject("speed")
}
}

// Check to make sure required arguments have been filled

if ((!scriptEntry.hasObject("entities")))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "ENTITIES");
// Use the NPC or player's locations as the location if one is not specified

// Use the NPC or player's locations as the origin if one is not specified
scriptEntry.defaultObject("origin",
scriptEntry.getNPC().getLocation(), scriptEntry.getPlayer().getLocation());

if ((!scriptEntry.hasObject("origin"))) {

if (scriptEntry.hasNPC())
scriptEntry.addObject("origin", scriptEntry.getNPC().getLocation());
else if (scriptEntry.hasPlayer())
scriptEntry.addObject("origin", scriptEntry.getPlayer().getLocation());
else
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "ORIGIN");
}
// Use a default speed of 1.2 if one is not specified

// Use a default speed of 1.5 if one is not specified
scriptEntry.defaultObject("speed", new Element(1.2));

// Check to make sure required arguments have been filled

if ((!scriptEntry.hasObject("speed"))) {
scriptEntry.addObject("speed", new Element(1.5));
}
if ((!scriptEntry.hasObject("entities")))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "ENTITIES");
if ((!scriptEntry.hasObject("origin")))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "ORIGIN");
}

@SuppressWarnings("unchecked")
Expand Down
Expand Up @@ -50,22 +50,18 @@ else if (!scriptEntry.hasObject("entities")
}
}

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

scriptEntry.defaultObject("location",
scriptEntry.getNPC().getLocation(), scriptEntry.getPlayer().getLocation());

// Check to make sure required arguments have been filled

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

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

if ((!scriptEntry.hasObject("location"))) {

if (scriptEntry.hasNPC())
scriptEntry.addObject("location", scriptEntry.getNPC().getLocation());
else if (scriptEntry.hasPlayer())
scriptEntry.addObject("location", scriptEntry.getPlayer().getLocation());
else
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "LOCATION");
}
if ((!scriptEntry.hasObject("location")))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "LOCATION");
}

@SuppressWarnings("unchecked")
Expand All @@ -75,9 +71,7 @@ public void execute(final ScriptEntry scriptEntry) throws CommandExecutionExcept

dLocation location = (dLocation) scriptEntry.getObject("location");
List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
Boolean cancel = scriptEntry.hasObject("cancel") ?
true :
false;
Boolean cancel = scriptEntry.hasObject("cancel");

// Report to dB
dB.report(getName(), (cancel == true ? aH.debugObj("cancel", cancel) : "") +
Expand Down
Expand Up @@ -56,19 +56,11 @@ else if (!scriptEntry.hasObject("world")

// If the world has not been specified, try to use the NPC's or player's
// world, or default to "world" if necessary

if (!scriptEntry.hasObject("world")) {

if ((scriptEntry.hasNPC())) {
scriptEntry.addObject("world", new dWorld(scriptEntry.getNPC().getWorld()));
}
else if ((scriptEntry.hasPlayer())) {
scriptEntry.addObject("world", new dWorld(scriptEntry.getPlayer().getWorld()));
}
else {
scriptEntry.addObject("world", dWorld.valueOf("world"));
}
}

scriptEntry.defaultObject("world",
new dWorld(scriptEntry.getNPC().getWorld()),
new dWorld(scriptEntry.getPlayer().getWorld()),
dWorld.valueOf("world"));
}

@SuppressWarnings("unchecked")
Expand Down
Expand Up @@ -75,28 +75,23 @@ 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");

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

if ((!scriptEntry.hasObject("origin"))) {

if (scriptEntry.hasNPC())
scriptEntry.addObject("origin", scriptEntry.getNPC().getLocation());
else if (scriptEntry.hasPlayer())
scriptEntry.addObject("origin", scriptEntry.getPlayer().getLocation());
else
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "ORIGIN");
}
scriptEntry.defaultObject("origin",
scriptEntry.getNPC().getDenizenEntity(),
scriptEntry.getPlayer().getDenizenEntity());

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

if ((!scriptEntry.hasObject("speed"))) {
scriptEntry.addObject("speed", new Element(1.5));
}
scriptEntry.defaultObject("speed", new Element(1.5));

// Check to make sure required arguments have been filled

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

if ((!scriptEntry.hasObject("origin")))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "ORIGIN");
}

@SuppressWarnings("unchecked")
Expand Down
Expand Up @@ -46,23 +46,19 @@ else if (!scriptEntry.hasObject("target")
scriptEntry.addObject("target", arg.asType(dEntity.class));
}
}

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

scriptEntry.defaultObject("location",
scriptEntry.getNPC().getLocation(), scriptEntry.getPlayer().getLocation());

// Check to make sure required arguments have been filled

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

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

if ((!scriptEntry.hasObject("location"))) {

if (scriptEntry.hasNPC())
scriptEntry.addObject("location", scriptEntry.getNPC().getLocation());
else if (scriptEntry.hasPlayer())
scriptEntry.addObject("location", scriptEntry.getPlayer().getLocation());
else
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "LOCATION");
}
if ((!scriptEntry.hasObject("location")))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "LOCATION");
}

@SuppressWarnings("unchecked")
Expand Down
Expand Up @@ -89,32 +89,23 @@ else if (!scriptEntry.hasObject("offset")
}
}

// Use default values if necessary

scriptEntry.defaultObject("location",
scriptEntry.getNPC().getLocation(), scriptEntry.getPlayer().getLocation());
scriptEntry.defaultObject("data", new Element(0));
scriptEntry.defaultObject("visibility", new Element(5));
scriptEntry.defaultObject("qty", new Element(1));
scriptEntry.defaultObject("offset", new Element(0.5));

// Check to make sure required arguments have been filled

if ((!scriptEntry.hasObject("location")))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "LOCATION");

if (!scriptEntry.hasObject("effect") &&
!scriptEntry.hasObject("particleeffect"))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "EFFECT");

// Use default values if necessary

if ((!scriptEntry.hasObject("data"))) {
scriptEntry.addObject("data", new Element(0));
}

if ((!scriptEntry.hasObject("visibility"))) {
scriptEntry.addObject("visibility", new Element(5));
}

if ((!scriptEntry.hasObject("qty"))) {
scriptEntry.addObject("qty", new Element(1));
}

if ((!scriptEntry.hasObject("offset"))) {
scriptEntry.addObject("offset", new Element(0.5));
}
if (!scriptEntry.hasObject("location"))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "LOCATION");
}

@Override
Expand Down
Expand Up @@ -47,22 +47,14 @@ else if (!scriptEntry.hasObject("world")

if ((!scriptEntry.hasObject("value")))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "VALUE");

// If the world has not been specified, try to use the NPC's or player's
// world, or default to "world" if necessary

if (!scriptEntry.hasObject("world")) {

if ((scriptEntry.hasNPC())) {
scriptEntry.addObject("world", new dWorld(scriptEntry.getNPC().getWorld()));
}
else if ((scriptEntry.hasPlayer())) {
scriptEntry.addObject("world", new dWorld(scriptEntry.getPlayer().getWorld()));
}
else {
scriptEntry.addObject("world", dWorld.valueOf("world"));
}
}
scriptEntry.defaultObject("world",
new dWorld(scriptEntry.getNPC().getWorld()),
new dWorld(scriptEntry.getPlayer().getWorld()),
dWorld.valueOf("world"));
}

@Override
Expand Down
Expand Up @@ -51,18 +51,10 @@ else if (!scriptEntry.hasObject("value")
// If the world has not been specified, try to use the NPC's or player's
// world, or default to "world" if necessary

if (!scriptEntry.hasObject("world")) {

if ((scriptEntry.hasNPC())) {
scriptEntry.addObject("world", new dWorld(scriptEntry.getNPC().getWorld()));
}
else if ((scriptEntry.hasPlayer())) {
scriptEntry.addObject("world", new dWorld(scriptEntry.getPlayer().getWorld()));
}
else {
scriptEntry.addObject("world", dWorld.valueOf("world"));
}
}
scriptEntry.defaultObject("world",
new dWorld(scriptEntry.getNPC().getWorld()),
new dWorld(scriptEntry.getPlayer().getWorld()),
dWorld.valueOf("world"));
}

@Override
Expand Down

0 comments on commit 35d64bd

Please sign in to comment.