Skip to content

Commit

Permalink
Add fully-featured Mount command. Example usage: - mount "targets:p@j…
Browse files Browse the repository at this point in the history
…eebiss|e@chicken|n@3|p@aufdemrand|e@cow|e@cow|e@bat"
  • Loading branch information
davidcernat committed Jun 6, 2013
1 parent 7ba26f3 commit 5596432
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 3 deletions.
Expand Up @@ -145,6 +145,9 @@ public void registerCoreMembers() {
registerCoreMember(HealCommand.class,
"HEAL", "heal (amt:#) (target:npc|{player})", 0);

registerCoreMember(HurtCommand.class,
"HURT", "hurt (amt:#) (target:npc|{player})", 0);

registerCoreMember(HealthCommand.class,
"HEALTH", "health (toggle:true|false|toggle) (set_max:#)", 1);

Expand All @@ -166,6 +169,9 @@ public void registerCoreMembers() {
registerCoreMember(LookcloseCommand.class,
"LOOKCLOSE", "lookclose [toggle:true|false]", 1);

registerCoreMember(MountCommand.class,
"MOUNT", "mount (cancel) (location:x,y,z,world) (target(s):[npc.#]|[player.name])", 0);

registerCoreMember(ModifyBlockCommand.class,
"MODIFYBLOCK", "modifyblock [location:x,y,z,world] [material:data] (radius:#) (height:#) (depth:#)", 2);

Expand Down
Expand Up @@ -48,9 +48,11 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException

} else if (aH.matchesValueArg("type", arg, ArgumentType.String)) {

String typeArg = arg.split(":", 2)[1].toUpperCase();

for (FireworkEffect.Type typeValue : FireworkEffect.Type.values()) {

if (arg.split(":", 2)[1].toUpperCase().matches(typeValue.name())) {
if (typeArg.matches(typeValue.name())) {

type = typeValue;
break;
Expand Down Expand Up @@ -107,6 +109,7 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException
scriptEntry.addObject("trail", trail);
}

@SuppressWarnings("unchecked")
@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
// Get objects
Expand Down
@@ -0,0 +1,162 @@
package net.aufdemrand.denizen.scripts.commands.core;

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.dEntity;
import net.aufdemrand.denizen.utilities.arguments.aH;
import net.aufdemrand.denizen.utilities.arguments.dLocation;
import net.aufdemrand.denizen.utilities.arguments.aH.ArgumentType;
import net.aufdemrand.denizen.utilities.debugging.dB;

import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;

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

/**
* Mounts a player on the NPC if no targets are specified.
* If targets are specified, mount them on each other in order.
*
* @author David Cernat
*/

public class MountCommand extends AbstractCommand {

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

//
// List of entities to be teleported.
//
List<Entity> entities = new ArrayList<Entity> ();

//
// Process the arguments.
//
Boolean dismount = false;
dLocation location = null;

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

if (aH.matchesLocation(arg)) {

location = aH.getLocationFrom(arg);
dB.echoDebug("...location set to '%s'.", arg);
}
else if (aH.matchesArg("cancel", arg)) {
dismount = true;
dB.echoDebug("...will dismount.");
}
else if (aH.matchesValueArg("TARGETS, TARGET", arg, ArgumentType.Custom)) {

Entity entity = null;

for (String target : aH.getListFrom(arg)) {
// Get entity
if (aH.matchesEntityType(target)) {

dLocation entityLocation = null;

// Cannot spawn an entity without a location, so go through possible locations
if (location != null)
entityLocation = location;
else if (scriptEntry.getPlayer() != null)
entityLocation = new dLocation(scriptEntry.getPlayer().getLocation());
else if (scriptEntry.getNPC() != null)
entityLocation = new dLocation(scriptEntry.getNPC().getLocation());

if (entityLocation != null) {
EntityType entityType = aH.getEntityFrom(target);

dB.echoApproval("Entity type is: " + entityType.name());
dB.echoApproval("Entity location is: " + entityLocation.toString());

entity = entityLocation.getWorld().spawnEntity(entityLocation, entityType);
}
}
else {
entity = dEntity.valueOf(target).getBukkitEntity();
}

if (entity != null) {
entities.add(entity);
}
else {
dB.echoError("Invalid TARGET '%s'!", target);
}
}
}

else throw new InvalidArgumentsException(dB.Messages.ERROR_UNKNOWN_ARGUMENT, arg);
}

// If there are no targets, default to the player mounting this NPC

if (entities.size() == 0) {
entities.add(scriptEntry.getPlayer());
entities.add(scriptEntry.getNPC().getEntity());
}

// If there is only one target entity, there will be no one to mount
// it, so make this player mount it by adding him/her to the start
// of the list

if (entities.size() == 1) {

entities.add(0, scriptEntry.getPlayer());
}

// Store objects in ScriptEntry for use in execute()
scriptEntry.addObject("entities", entities);
scriptEntry.addObject("location", location);
scriptEntry.addObject("dismount", dismount);
}

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

List<Entity> entities = (List<Entity>) scriptEntry.getObject("entities");
final dLocation location = (dLocation) scriptEntry.getObject("location");
Boolean dismount = (Boolean) scriptEntry.getObject("dismount");

// Debug output
dB.echoApproval("<G>Executing '<Y>" + getName() + "<G>': "
+ "Targets=<Y>'" + entities.toString() + "<G>'");

//Collections.reverse(entities);

Entity lastEntity = null;

for (Entity entity : entities) {

if (dismount) {

entity.leaveVehicle();
}
else {

if (lastEntity != null) {
// Because setPassenger() is a toggle, only use it if the new passenger
// is not already the current passenger

if (entity.getPassenger() != lastEntity) {
lastEntity.teleport(entity.getLocation());
entity.setPassenger(lastEntity);
}
}

lastEntity = entity;
}
}

if (location != null) {
lastEntity.teleport(location);
}
}
}
Expand Up @@ -661,7 +661,9 @@ public static ScriptQueue getQueueFrom(String arg) {
public static String getStringFrom(String arg) {
if (arg.split(":").length >= 2 &&
((arg.indexOf(':') < arg.indexOf(' ') || arg.indexOf(' ') == -1)))
return arg.split(":", 2)[1];
return arg.split(":", 2)[1];
else if (arg.split("@").length >= 2)
return arg.split("@", 2)[1];
else return arg;
}

Expand Down Expand Up @@ -792,7 +794,7 @@ public static boolean matchesColor(String arg) {
*
*/
public static boolean matchesEntityType(String arg) {
final Pattern matchesEntityPtrn = Pattern.compile("entity:(.+)", Pattern.CASE_INSENSITIVE);
final Pattern matchesEntityPtrn = Pattern.compile("(?:entity:|e@)(.+)", Pattern.CASE_INSENSITIVE);
Matcher m = matchesEntityPtrn.matcher(arg);
if (m.matches()) {
String group = m.group(1).toUpperCase();
Expand Down

0 comments on commit 5596432

Please sign in to comment.