Skip to content

Commit

Permalink
Add Create command to make new NPCs.
Browse files Browse the repository at this point in the history
  • Loading branch information
aufdemrand committed Nov 7, 2013
1 parent 2c6f6a8 commit 4ef8dd4
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 23 deletions.
Expand Up @@ -8,6 +8,9 @@
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.entity.EntityType;

public class ResetCommand extends AbstractCommand {

Expand All @@ -16,39 +19,29 @@ private enum Type { FINISH, FAIL, PLAYER_COOLDOWN, GLOBAL_COOLDOWN }
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

dScript script = scriptEntry.getScript();
Type type = null;

// TODO: UPDATE THIS COMMAND!

for (String arg : scriptEntry.getArguments()) {

if (aH.matchesArg("finishes, finished, finish", arg))
type = Type.FINISH;
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {

else if (aH.matchesArg("fail, fails, failed", arg))
type = Type.FAIL;
if (arg.matches("finishes, finished, finish")
&& !scriptEntry.hasObject("type"))
scriptEntry.addObject("type", Type.FINISH);

else if (aH.matchesArg("cooldown", arg))
type = Type.PLAYER_COOLDOWN;
if (arg.matches("fails, failed, fail")
&& !scriptEntry.hasObject("type"))
scriptEntry.addObject("type", Type.FAIL);

else if (aH.matchesArg("global_cooldown", arg))
type = Type.GLOBAL_COOLDOWN;
if (arg.matches("cooldown")
&& !scriptEntry.hasObject("type"))
scriptEntry.addObject("type", Type.PLAYER_COOLDOWN);

else if (aH.matchesScript(arg))
script = aH.getScriptFrom(arg);
if (arg.matches("global_cooldown")
&& !scriptEntry.hasObject("type"))
scriptEntry.addObject("type", Type.GLOBAL_COOLDOWN);

else throw new InvalidArgumentsException("Unknown argument '" + arg + "'!");
}

if (type == null)
throw new InvalidArgumentsException("Must specify a type! Valid: FAILS, FINISHES, COOLDOWN, GLOBAL_COOLDOWN");

if (scriptEntry.getPlayer() == null && type != Type.GLOBAL_COOLDOWN)
throw new InvalidArgumentsException("Must specify a player!");

scriptEntry.addObject("script", script)
.addObject("type", type);
}

@Override
Expand Down
@@ -0,0 +1,64 @@
package net.aufdemrand.denizen.scripts.commands.npc;

import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.event.DespawnReason;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.Location;

/**
* Creates a NPC.
*
*
*/

public class CreateCommand extends AbstractCommand {

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

for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {

if (!scriptEntry.hasObject("entity_type")
&& arg.matchesArgumentType(dEntity.class)) {
dEntity ent = dEntity.valueOf(arg.getValue());
if (!ent.isGeneric())
throw new InvalidArgumentsException("Entity supplied must be generic!");
scriptEntry.addObject("entity_type", ent);
}

else if (!scriptEntry.hasObject("spawn_location") &&
arg.matchesArgumentType(dLocation.class))
scriptEntry.addObject("spawn_location", arg.asType(dLocation.class));

else if (!scriptEntry.hasObject("name"))
scriptEntry.addObject("name", arg.asElement());

else arg.reportUnhandled();
}

}

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

Element name = (Element) scriptEntry.getObject("name");
dEntity type = (dEntity) scriptEntry.getObject("entity_type");
dLocation loc = (dLocation) scriptEntry.getObject("spawn_location");

dB.report(scriptEntry, getName(), name.debug() + type.debug() + (loc != null ? loc.debug() : ""));

// Add the created NPC into the script entry so it can be utilized if need be.
scriptEntry.addObject("created_npc", dNPC.mirrorCitizensNPC(CitizensAPI.getNPCRegistry()
.createNPC(type.getEntityType(), name.asString())));

if (loc != null)
((dNPC) scriptEntry.getObject("created_npc")).getCitizen().spawn(loc);
}

}

0 comments on commit 4ef8dd4

Please sign in to comment.