Skip to content

Commit

Permalink
Update Playeffect command to new argument system. Add custom Hearts a…
Browse files Browse the repository at this point in the history
…nd Explosion effects to it.
  • Loading branch information
davidcernat committed Jul 11, 2013
1 parent bd1f7ab commit a57b85b
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 62 deletions.
Expand Up @@ -36,15 +36,15 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException
scriptEntry.addObject("entities", ((dList) arg.asType(dList.class)).filter(dEntity.class));
}

if (!scriptEntry.hasObject("animation")
&& arg.matchesEnum(PlayerAnimation.values()))
// add type
scriptEntry.addObject("animation", PlayerAnimation.valueOf(arg.getValue().toUpperCase()));

if (!scriptEntry.hasObject("animation")
&& arg.matchesEnum(EntityEffect.values()))
// add type
scriptEntry.addObject("effect", EntityEffect.valueOf(arg.getValue().toUpperCase()));
if (!scriptEntry.hasObject("animation")) {
if (arg.matchesEnum(PlayerAnimation.values())) {
scriptEntry.addObject("animation", PlayerAnimation.valueOf(arg.getValue().toUpperCase()));
}
else if (arg.matchesEnum(EntityEffect.values())) {
scriptEntry.addObject("effect", EntityEffect.valueOf(arg.getValue().toUpperCase()));
}
}
}

// Check to make sure required arguments have been filled
Expand All @@ -68,7 +68,10 @@ public void execute(final ScriptEntry scriptEntry) throws CommandExecutionExcept
(EntityEffect) scriptEntry.getObject("effect") : null;

// Report to dB
dB.report(getName(), (animation != null ? animation.name() : effect.name()) +
dB.report(getName(),
(animation != null ?
aH.debugObj("animation", animation.name()) :
aH.debugObj("effect", effect.name())) +
aH.debugObj("entities", entities.toString()));

// Go through all the entities and animate them
Expand Down
Expand Up @@ -26,10 +26,11 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {

if (!scriptEntry.hasObject("action")
&& arg.matchesEnum(Action.values()))
&& arg.matchesEnum(Action.values())) {
// add Action
scriptEntry.addObject("action", Action.valueOf(arg.getValue().toUpperCase()));

}

else if (!scriptEntry.hasObject("originEntity") &&
!scriptEntry.hasObject("originLocation") &&
arg.matchesPrefix("origin, o, source, s, items, i, from, f")) {
Expand Down
@@ -1,12 +1,15 @@
package net.aufdemrand.denizen.scripts.commands.world;

import org.bukkit.Effect;
import org.bukkit.Location;
import org.bukkit.EntityEffect;
import org.bukkit.craftbukkit.v1_6_R2.entity.CraftWolf;
import org.bukkit.entity.Wolf;

import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.Element;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.aH.ArgumentType;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.debugging.dB;
Expand All @@ -30,69 +33,103 @@

public class PlayEffectCommand extends AbstractCommand {

// Implement some special effects that are not in Bukkit's Effect class
private enum SpecialEffect { HEARTS, EXPLOSION }

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

// Initialize fields
Effect effect = null;
int radius = 3;
int data = 0;
Location location = null;

// Iterate through arguments
for (String arg : scriptEntry.getArguments()){
if (aH.matchesLocation(arg))
location = aH.getLocationFrom(arg);

else if (aH.matchesValueArg("effect, e", arg, ArgumentType.Custom)) {
try {
effect = Effect.valueOf(aH.getStringFrom(arg).toUpperCase());
} catch (Exception e) {
dB.echoError("Invalid effect!");
}
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {

if (!scriptEntry.hasObject("location")
&& arg.matchesArgumentType(dLocation.class)) {
// Location arg
scriptEntry.addObject("location", arg.asType(dLocation.class));
}

else if (aH.matchesValueArg("radius, r", arg, ArgumentType.Integer))
radius = aH.getIntegerFrom(arg);

else if (aH.matchesValueArg("data, d", arg, ArgumentType.Integer))
data = aH.getIntegerFrom(arg);

else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg);
if (!scriptEntry.hasObject("effect")) {

// Add effect
if (arg.matchesEnum(Effect.values())) {
scriptEntry.addObject("effect", Effect.valueOf(arg.getValue().toUpperCase()));
}
else if (arg.matchesEnum(SpecialEffect.values())) {
scriptEntry.addObject("specialeffect", SpecialEffect.valueOf(arg.getValue().toUpperCase()));
}
}

else if (!scriptEntry.hasObject("radius")
&& arg.matchesPrimitive(aH.PrimitiveType.Integer)
&& arg.matchesPrefix("radius, r")) {
// Add value
scriptEntry.addObject("radius", arg.asElement());
}

else if (!scriptEntry.hasObject("data")
&& arg.matchesPrimitive(aH.PrimitiveType.Integer)
&& arg.matchesPrefix("data, d")) {
// Add value
scriptEntry.addObject("data", arg.asElement());
}
}

// Check required args
if (effect == null)
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "EFFECT");
if (location == null)

// Check to make sure required arguments have been filled

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

// Stash args in ScriptEntry for use in execute()
scriptEntry.addObject("location", location);
scriptEntry.addObject("effect", effect);
scriptEntry.addObject("radius", radius);
scriptEntry.addObject("data", data);
if (!scriptEntry.hasObject("effect") && !scriptEntry.hasObject("specialeffect"))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "EFFECT");

// Use a default radius and data if necessary

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

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

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {

// Extract objects from ScriptEntry
Location location = (Location) scriptEntry.getObject("location");
dLocation location = (dLocation) scriptEntry.getObject("location");
Effect effect = (Effect) scriptEntry.getObject("effect");
int radius = (Integer) scriptEntry.getObject("radius");
int data = (Integer) scriptEntry.getObject("data");

// Debugger
dB.echoApproval("Executing '" + getName() + "': "
+ "Location='" + location.getX() + "," + location.getY()
+ "," + location.getZ() + "," + location.getWorld().getName() + "', "
+ "Effect='" + effect.toString() + ", "
+ "Data='" + data + ", "
+ "Radius='" + radius + "'");

// Play the sound
location.getWorld().playEffect(location, effect, data, radius);
SpecialEffect specialEffect = (SpecialEffect) scriptEntry.getObject("specialeffect");
Element radius = (Element) scriptEntry.getObject("radius");
Element data = (Element) scriptEntry.getObject("data");

// Report to dB
dB.report(getName(),
(effect != null ?
aH.debugObj("effect", effect.name()) :
aH.debugObj("special effect", specialEffect.name())) +
aH.debugObj("location", location.toString() +
aH.debugObj("radius", radius) +
aH.debugObj("data", data)));

// Play the Bukkit effect
if (effect != null) {
location.getWorld().playEffect(location, effect, data.asInt(), radius.asInt());
}
// Play one of the special effects
else {
if (specialEffect.equals(SpecialEffect.HEARTS)) {

Wolf wolf = (Wolf) location.getWorld().spawn(location, Wolf.class);
((CraftWolf) wolf).getHandle().setInvisible(true);
wolf.playEffect(EntityEffect.WOLF_HEARTS);
wolf.remove();
}
else if (specialEffect.equals(SpecialEffect.EXPLOSION)) {

location.getWorld().createExplosion(location, 0);
}
}
}

}
Expand Up @@ -1084,7 +1084,7 @@ public void playerMoveEvent(PlayerMoveEvent event) {
String determination = doEvents(Arrays.asList
("player walks over notable",
"player walks over " + name,
"on walked over " + name),
"walked over " + name),
null, event.getPlayer(), context);

if (determination.toUpperCase().startsWith("CANCELLED") ||
Expand Down

0 comments on commit a57b85b

Please sign in to comment.