From 01bae16214fa710c0235449237f03399ddef2a2a Mon Sep 17 00:00:00 2001 From: David Cernat Date: Wed, 10 Jul 2013 16:31:39 +0300 Subject: [PATCH] Update Mount command to new argument system. --- .../scripts/commands/CommandRegistry.java | 4 +- .../scripts/commands/entity/FlyCommand.java | 28 +-- .../scripts/commands/entity/MountCommand.java | 201 +++++++----------- .../denizen/utilities/entity/Position.java | 25 ++- 4 files changed, 108 insertions(+), 150 deletions(-) diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/CommandRegistry.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/CommandRegistry.java index 47e0e891ad..7e20ce8e38 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/CommandRegistry.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/CommandRegistry.java @@ -146,7 +146,7 @@ public void registerCoreMembers() { "FLAG", "flag ({player}|npc|global) [name([#])](:action)[:value] (duration:#)", 1); registerCoreMember(FlyCommand.class, - "FLY", "fly [entities:|...] (origin:) (destination(s):|...)", 1); + "FLY", "fly (cancel) [entities:|...] (origin:) (destination(s):|...)", 1); registerCoreMember(FollowCommand.class, "FOLLOW", "follow (stop)", 0); @@ -197,7 +197,7 @@ public void registerCoreMembers() { "MIDI", "midi [file:] (listener(s):[p@|...])|(location:) (tempo:<#.#>)", 1); registerCoreMember(MountCommand.class, - "MOUNT", "mount (cancel) (location:) (target(s):[n@#]|[p@name]|[e@name])", 0); + "MOUNT", "mount (cancel) [entities:|...] (location:)", 0); registerCoreMember(ModifyBlockCommand.class, "MODIFYBLOCK", "modifyblock [location:] [(:)] (radius:<#>) (height:<#>) (depth:<#>)", 2); diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/FlyCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/FlyCommand.java index f929b82688..aebd2bbfed 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/FlyCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/FlyCommand.java @@ -98,31 +98,33 @@ public void execute(final ScriptEntry scriptEntry) throws CommandExecutionExcept false; // Report to dB - dB.report(getName(), cancel == true ? "cancel, " : "" + + dB.report(getName(), (cancel == true ? "cancel, " : "") + aH.debugObj("origin", origin) + aH.debugObj("entities", entities.toString()) + (destinations.size() > 0 ? aH.debugObj("destinations", destinations.toString()) : "")); - // Go through all the entities, spawning/teleporting them - for (dEntity entity : entities) { - - if (entity.isSpawned() == false) { - entity.spawnAt(origin); - } - else { - entity.teleport(origin); - } - } - // Mount or dismount all of the entities if (cancel == false) { + + // Go through all the entities, spawning/teleporting them + for (dEntity entity : entities) { + + if (entity.isSpawned() == false) { + entity.spawnAt(origin); + } + else { + entity.teleport(origin); + } + } + Position.mount(Conversion.convert(entities)); } else { Position.dismount(Conversion.convert(entities)); - // Go no further if we are dismounting entities + // Go no further if we are dismounting entities return; + } // Get the last entity on the list diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/MountCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/MountCommand.java index f0100f682d..5764b0a271 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/MountCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/MountCommand.java @@ -1,20 +1,19 @@ package net.aufdemrand.denizen.scripts.commands.entity; +import java.util.List; + import net.aufdemrand.denizen.exceptions.CommandExecutionException; import net.aufdemrand.denizen.exceptions.InvalidArgumentsException; import net.aufdemrand.denizen.objects.aH; -import net.aufdemrand.denizen.objects.aH.ArgumentType; import net.aufdemrand.denizen.objects.dEntity; +import net.aufdemrand.denizen.objects.dList; import net.aufdemrand.denizen.objects.dLocation; import net.aufdemrand.denizen.scripts.ScriptEntry; import net.aufdemrand.denizen.scripts.commands.AbstractCommand; +import net.aufdemrand.denizen.utilities.Conversion; import net.aufdemrand.denizen.utilities.debugging.dB; - -import org.bukkit.entity.Entity; -import org.bukkit.entity.EntityType; - -import java.util.ArrayList; -import java.util.List; +import net.aufdemrand.denizen.utilities.debugging.dB.Messages; +import net.aufdemrand.denizen.utilities.entity.Position; /** * Mounts a player on the NPC if no targets are specified. @@ -24,133 +23,85 @@ */ public class MountCommand extends AbstractCommand { - + @Override - public void parseArgs(ScriptEntry scriptEntry) - throws InvalidArgumentsException { - - // - // List of entities to be mounted. - // - List entities = new ArrayList (); + public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException { - // - // Process the arguments. - // - Boolean dismount = false; - dLocation location = null; - - for (String arg : scriptEntry.getArguments()) { - - if (aH.matchesLocation(arg)) { + // Initialize necessary fields - 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.getEntityTypeFrom(target); - 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); - } - } + for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) { + + if (!scriptEntry.hasObject("cancel") + && arg.matches("cancel")) { + + scriptEntry.addObject("cancel", ""); + } + + if (!scriptEntry.hasObject("location") + && arg.matchesArgumentType(dLocation.class)) { + // Location arg + scriptEntry.addObject("location", arg.asType(dLocation.class)); + } + + else if (!scriptEntry.hasObject("entities") + && arg.matchesPrefix("entity, entities, e")) { + // Entity arg + scriptEntry.addObject("entities", ((dList) arg.asType(dList.class)).filter(dEntity.class)); } - - else throw new InvalidArgumentsException(dB.Messages.ERROR_UNKNOWN_ARGUMENT, arg); } - - // If there are no targets, default to the player mounting this NPC + + // Check to make sure required arguments have been filled - if (entities.size() == 0) { - entities.add(scriptEntry.getPlayer().getPlayerEntity()); - entities.add(scriptEntry.getNPC().getEntity()); - } + if ((!scriptEntry.hasObject("entities"))) + throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "ENTITIES"); - // 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) { + // Use the NPC or player's locations as the origin if one is not specified + + if ((!scriptEntry.hasObject("location"))) { - entities.add(0, scriptEntry.getPlayer().getPlayerEntity()); + 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"); } - - // Store objects in ScriptEntry for use in execute() - scriptEntry.addObject("entities", entities); - scriptEntry.addObject("location", location); - scriptEntry.addObject("dismount", dismount); } - - @SuppressWarnings("unchecked") + + @SuppressWarnings("unchecked") @Override - public void execute(ScriptEntry scriptEntry) throws CommandExecutionException { - - List entities = (List) scriptEntry.getObject("entities"); - final dLocation location = (dLocation) scriptEntry.getObject("location"); - Boolean dismount = (Boolean) scriptEntry.getObject("dismount"); - - // Debug output - dB.echoApproval("Executing '" + getName() + "': " - + "Targets='" + entities.toString() + "'"); - - 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); - } + public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException { + // Get objects + + dLocation location = (dLocation) scriptEntry.getObject("location"); + List entities = (List) scriptEntry.getObject("entities"); + Boolean cancel = scriptEntry.hasObject("cancel") ? + true : + false; + + // Report to dB + dB.report(getName(), (cancel == true ? "cancel, " : "") + + aH.debugObj("location", location) + + aH.debugObj("entities", entities.toString())); + + // Mount or dismount all of the entities + if (cancel == false) { + + // Go through all the entities, spawning/teleporting them + for (dEntity entity : entities) { + + if (entity.isSpawned() == false) { + entity.spawnAt(location); + } + else { + entity.teleport(location); + } + } + + Position.mount(Conversion.convert(entities)); + } + else { + Position.dismount(Conversion.convert(entities)); + } } -} +} \ No newline at end of file diff --git a/src/main/java/net/aufdemrand/denizen/utilities/entity/Position.java b/src/main/java/net/aufdemrand/denizen/utilities/entity/Position.java index 55c6442d01..1eb0fbbf8c 100644 --- a/src/main/java/net/aufdemrand/denizen/utilities/entity/Position.java +++ b/src/main/java/net/aufdemrand/denizen/utilities/entity/Position.java @@ -18,19 +18,22 @@ public static void mount(List entities) { for (Entity entity : entities) { - if (lastEntity != null && entity != lastEntity) { + if (entity != null) { + + if (lastEntity != null && entity != lastEntity) { - // Because setPassenger() is a toggle, only use it if the new passenger - // is not already the current passenger, and also make sure we're not - // mounting the entity on itself + // Because setPassenger() is a toggle, only use it if the new passenger + // is not already the current passenger, and also make sure we're not + // mounting the entity on itself - if (entity.getPassenger() != lastEntity) { - lastEntity.teleport(entity.getLocation()); - entity.setPassenger(lastEntity); + if (entity.getPassenger() != lastEntity) { + lastEntity.teleport(entity.getLocation()); + entity.setPassenger(lastEntity); + } } - } - lastEntity = entity; + lastEntity = entity; + } } } @@ -43,7 +46,9 @@ public static void dismount(List entities) { for (Entity entity : entities) { - entity.leaveVehicle(); + if (entity != null) { + entity.leaveVehicle(); + } } } }