Skip to content

Commit

Permalink
Small general fixes. Preparing to add Firework command.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcernat committed Jun 2, 2013
1 parent 71e79ee commit 53c491e
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 20 deletions.
Expand Up @@ -533,7 +533,7 @@ public String asCommaSeparatedList() {
for (String string : values)
returnList = returnList + string + ", ";

return returnList.substring(0, returnList.length() - 1);
return returnList.substring(0, returnList.length() - 2);
}

/**
Expand Down
@@ -0,0 +1,143 @@
package net.aufdemrand.denizen.scripts.commands.core;

import java.util.ArrayList;
import java.util.List;

import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.arguments.dLocation;
import net.aufdemrand.denizen.utilities.arguments.aH;
import net.aufdemrand.denizen.utilities.arguments.aH.ArgumentType;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.debugging.dB.Messages;

import org.bukkit.Color;
import org.bukkit.FireworkEffect;
import org.bukkit.FireworkEffect.Builder;
import org.bukkit.FireworkEffect.Type;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Firework;
import org.bukkit.entity.LivingEntity;
import org.bukkit.inventory.meta.FireworkMeta;

/**
* Shoots a firework into the air from a location.
* If no location is chosen, the firework is shot from the NPC's location.
*
* @author David Cernat
*/

public class FireworkCommand extends AbstractCommand {

public enum EquipType { HAND, BOOTS, LEGS, CHEST, HEAD }

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

// Initialize necessary fields
dLocation location = null;
Type type = Type.BALL;
Integer power = 1;
Boolean flicker = false;
Boolean trail = false;
List<String> effects = new ArrayList<String>();

for (String arg : scriptEntry.getArguments()) {
if (aH.matchesLocation(arg)) {
location = aH.getLocationFrom(arg);
dB.echoDebug("...location set to '%s'.", arg);

} else if (aH.matchesValueArg("type", arg, ArgumentType.String)) {
//type = aH.getTypeFrom(arg);
dB.echoDebug("...will have a power of " + power);

} else if (aH.matchesValueArg("power", arg, ArgumentType.Integer)) {
power = aH.getIntegerFrom(arg);
dB.echoDebug("...will have a power of " + power);

} else if (aH.matchesArg("flicker", arg)) {
flicker = true;
dB.echoDebug("...will flicker.");

} else if (aH.matchesArg("trail", arg)) {
trail = true;
dB.echoDebug("...will have a trail.");

} else if (aH.matchesValueArg("EFFECTS, EFFECT", arg, ArgumentType.String)) {
// May be multiple effects, so let's treat this as a potential list.
// dScript list entries are separated by pipes ('|')
for (String element : aH.getListFrom(arg)) {

String[] effect = element.split(";", 5);

dB.echoApproval("Element: " + element);
dB.echoApproval("Effect length: " + effect.length);

if (effect.length == 2) {

//if (effect[1])

//for (FireworkEffect.Type type : FireworkEffect.Type.values()) {

dB.echoApproval("Looking at: " + type.name());

if (type.name().equals(effect[1])) {
dB.echoApproval("Effect types matched!");
break;
}
//}

dB.echoApproval("Effect " + element + " seems valid!");

}
else {
dB.echoError("Invalid firework EFFECT!");
}
}
} else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg);
}

// Stash objects
scriptEntry.addObject("location", location);
scriptEntry.addObject("power", power);
scriptEntry.addObject("flicker", flicker);
scriptEntry.addObject("trail", trail);
}

@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
// Get objects

final dLocation location = scriptEntry.hasObject("location") ?
(dLocation) scriptEntry.getObject("location") :
(dLocation) scriptEntry.getNPC().getLocation();
Integer power = (Integer) scriptEntry.getObject("power");
Boolean flicker = (Boolean) scriptEntry.getObject("flicker");
Boolean trail = (Boolean) scriptEntry.getObject("trail");
Type type = (Type) scriptEntry.getObject("type");

Firework firework = location.getWorld().spawn(location, Firework.class);
FireworkMeta fireworkMeta = (FireworkMeta) firework.getFireworkMeta();
fireworkMeta.setPower(power);

Builder fireworkBuilder = FireworkEffect.builder();

fireworkBuilder.with(type);
//fireworkBuilder.withColor(arg0);
//fireworkBuilder.withFade(arg0);

if (flicker) { fireworkBuilder.withFlicker(); }
if (trail) { fireworkBuilder.withTrail(); }

//fireworkBuilder.

//fireworkMeta.addEffects(FireworkEffect.builder().withColor(Color.YELLOW).with(Type.STAR).build());
fireworkMeta.addEffects(fireworkBuilder.build());
firework.setFireworkMeta(fireworkMeta);

}

}
Expand Up @@ -45,7 +45,6 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException
Boolean ride = false;
Boolean burn = false;
double explosion = -1;
Boolean fireworks = false;

// Set some defaults
if (scriptEntry.getPlayer() != null)
Expand Down Expand Up @@ -77,10 +76,6 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException
} else if (aH.matchesValueArg("explosion", arg, ArgumentType.Double)) {
explosion = aH.getDoubleFrom(arg);
dB.echoDebug("...will have an explosion radius of " + explosion);

} else if (aH.matchesArg("fireworks", arg)) {
fireworks = true;
dB.echoDebug("...will launch fireworks.");

} else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg);
}
Expand All @@ -94,7 +89,6 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException
scriptEntry.addObject("ride", ride);
scriptEntry.addObject("burn", burn);
scriptEntry.addObject("explosion", explosion);
scriptEntry.addObject("fireworks", fireworks);
}

@Override
Expand Down Expand Up @@ -177,14 +171,7 @@ public void run() {
Duration.valueOf(Settings.ScriptQueueSpeed()).getSeconds()))
.runTaskScript(scriptEntry.getPlayer(), scriptEntry.getNPC(), null);
}
if ((Boolean) scriptEntry.getObject("fireworks"))
{
Firework firework = entity.getWorld().spawn(entity.getLocation(), Firework.class);
FireworkMeta fireworkMeta = (FireworkMeta) firework.getFireworkMeta();
fireworkMeta.addEffects(FireworkEffect.builder().withColor(Color.YELLOW).with(Type.STAR).build());
fireworkMeta.setPower(2);
firework.setFireworkMeta(fireworkMeta);
}

if ((Double) scriptEntry.getObject("explosion") > 0)
{
entity.getWorld().createExplosion(entity.getLocation(),
Expand Down
Expand Up @@ -57,7 +57,7 @@ public void execute(final ScriptEntry scriptEntry) throws CommandExecutionExcept

EntityType entityType = (EntityType) scriptEntry.getObject("entityType");

final Entity entity = scriptEntry.getNPC().getWorld().spawnEntity(
final Entity entity = location.getWorld().spawnEntity(
location, entityType);

}
Expand Down
Expand Up @@ -347,14 +347,14 @@ public static LivingEntity getLivingEntityFrom(String arg) {

else if (entityGroupUpper.startsWith("NPC.")
|| entityGroupUpper.startsWith("NPCID.")
|| entityGroupUpper.startsWith("n@")) {
|| entityGroupUpper.startsWith("N@")) {
LivingEntity returnable = CitizensAPI.getNPCRegistry().getById(Integer.valueOf(entityGroup.split("\\.")[1])).getBukkitEntity();
if (returnable != null) return returnable;
else dB.echoError("Invalid NPC! '" + entityGroup + "' could not be found. Has it been despawned or killed?");
}

else if (entityGroupUpper.startsWith("PLAYER.")
|| entityGroupUpper.startsWith("@P.")) {
|| entityGroupUpper.startsWith("P@.")) {
LivingEntity returnable = getPlayerFrom(entityGroup.split("\\.")[1]);
if (returnable != null) return returnable;
else dB.echoError("Invalid Player! '" + entityGroup + "' could not be found. Has the player logged off?");
Expand Down
Expand Up @@ -5,6 +5,7 @@
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.minecraft.server.v1_5_R3.Entity;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
Expand Down Expand Up @@ -102,8 +103,15 @@ public static dEntity valueOf(String string) {
// when an NPC is not currently spawned. Someone should talk to
// both aufdemrand and fullwall about it.

LivingEntity returnable = CitizensAPI.getNPCRegistry()
.getById(Integer.valueOf(m.group(3))).getBukkitEntity();
NPC npc = CitizensAPI.getNPCRegistry()
.getById(Integer.valueOf(m.group(3)));

if (!npc.isSpawned()) {

npc.spawn(npc.getStoredLocation());
}

LivingEntity returnable = npc.getBukkitEntity();

if (returnable != null) return new dEntity(returnable);
else dB.echoError("Invalid NPC! '" + entityGroup + "' could not be found. Has it been despawned or killed?");
Expand Down

0 comments on commit 53c491e

Please sign in to comment.