Skip to content

Commit

Permalink
Make Shoot command use new argument system. Example syntax: - shoot s…
Browse files Browse the repository at this point in the history
…ource:p@davidcernat projectile:falling_block
  • Loading branch information
davidcernat committed Jun 28, 2013
1 parent db1fc9e commit aa55664
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 101 deletions.
2 changes: 1 addition & 1 deletion src/main/java/net/aufdemrand/denizen/objects/dList.java
Expand Up @@ -63,7 +63,7 @@ public static boolean matches(String arg) {

// Make sure string matches what this interpreter can accept.
final Pattern flag_by_id =
Pattern.compile("(fl\\[((?:p@|n@)(.+?))\\]@|fl@)(.+)",
Pattern.compile("(fl\\[((?:p@|n@|e@)(.+?))\\]@|fl@)(.+)",
Pattern.CASE_INSENSITIVE);

Matcher m;
Expand Down
@@ -1,23 +1,26 @@
package net.aufdemrand.denizen.scripts.commands.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.Duration;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dList;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.objects.dScript;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.scripts.containers.core.TaskScriptContainer;
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.debugging.dB.Messages;

import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Projectile;
import org.bukkit.scheduler.BukkitRunnable;
Expand All @@ -32,135 +35,104 @@

public class ShootCommand extends AbstractCommand {

private enum ShooterType { NPC, PLAYER }

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

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

// Initialize necessary fields
ShooterType shooterType = ShooterType.NPC;
LivingEntity shooter = null;
EntityType entityType = null;
//Integer qty = null;
Location location = null;
dScript newScript = null;
Boolean ride = false;
Boolean burn = false;

for (String arg : scriptEntry.getArguments()) {
if (aH.matchesArg("PLAYER", arg)) {
shooterType = ShooterType.PLAYER;
dB.echoDebug("... will be shot by the player!");
}
else if (aH.matchesEntityType(arg)) {
entityType = aH.getEntityTypeFrom(arg);
dB.echoDebug("...projectile set to '%s'.", arg);
if (!scriptEntry.hasObject("origin")
&& arg.matchesArgumentType(dEntity.class)
&& arg.matchesPrefix("origin, o, source, s")) {
// Entity arg
scriptEntry.addObject("origin", arg.asType(dEntity.class).setPrefix("entity"));
}
else if (aH.matchesLocation(arg)) {
location = aH.getLocationFrom(arg);
dB.echoDebug("...location set to '%s'.", arg);
}
else if (aH.matchesScript(arg)) {
newScript = aH.getScriptFrom(arg);
dB.echoDebug(Messages.DEBUG_SET_SCRIPT, arg);
}
else if (aH.matchesArg("ride, mount", arg)) {
ride = true;
dB.echoDebug("...will be mounted.");

else if (!scriptEntry.hasObject("projectiles")
&& arg.matchesArgumentType(dEntity.class)
&& arg.matchesPrefix("projectile, projectiles, p, entity, entities, e")) {
// Entity arg
scriptEntry.addObject("projectiles", arg.asType(dList.class));
}
else if (aH.matchesArg("burn, burning", arg)) {
burn = true;
dB.echoDebug("...will burn.");

else if (!scriptEntry.hasObject("destination")
&& arg.matchesArgumentType(dLocation.class)) {
// Location arg
scriptEntry.addObject("destination", arg.asType(dLocation.class).setPrefix("location"));
}
else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg);
}

if (shooterType.name().equals("NPC")) {

shooter = scriptEntry.getNPC().getEntity();
}
else if (shooterType.name().equals("PLAYER")) {

shooter = scriptEntry.getPlayer().getPlayerEntity();
}

if (location == null) {
else if (!scriptEntry.hasObject("duration")
&& arg.matchesArgumentType(Duration.class)) {
// add value
scriptEntry.addObject("duration", arg.asType(Duration.class));
}

location = shooter.getEyeLocation().add(
shooter.getEyeLocation().getDirection().
multiply(40));
else if (!scriptEntry.hasObject("script")
&& arg.matchesArgumentType(dScript.class)) {
// add value
scriptEntry.addObject("script", arg.asType(dScript.class));
}
}

// Check to make sure required arguments have been filled

if (entityType == null) throw new InvalidArgumentsException(Messages.ERROR_INVALID_ENTITY);

// Stash objects
scriptEntry.addObject("entityType", entityType);
scriptEntry.addObject("location", location);
scriptEntry.addObject("shooter", shooter);
scriptEntry.addObject("shooterType", shooterType);
scriptEntry.addObject("script", newScript);
scriptEntry.addObject("ride", ride);
scriptEntry.addObject("burn", burn);
if ((!scriptEntry.hasObject("projectiles")))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "PROJECTILE");
}

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

final Location location = (Location) scriptEntry.getObject("location");
LivingEntity shooter = (LivingEntity) scriptEntry.getObject("shooter");
ShooterType shooterType = (ShooterType) scriptEntry.getObject("shooterType");
EntityType entityType = (EntityType) scriptEntry.getObject("entityType");

Boolean ride = (Boolean) scriptEntry.getObject("ride");
Boolean burn = (Boolean) scriptEntry.getObject("burn");
dEntity shooter = (dEntity) scriptEntry.getObject("origin");
LivingEntity shooterEntity = shooter.getLivingEntity();
final dLocation destination = scriptEntry.hasObject("location") ?
(dLocation) scriptEntry.getObject("location") :
new dLocation(shooterEntity.getEyeLocation().add(
shooterEntity.getEyeLocation().getDirection()
.multiply(40)));

dEntity projectiles = (dEntity) scriptEntry.getObject("projectiles");
final dScript script = (dScript) scriptEntry.getObject("script");

// If the shooter is an NPC, always rotate it to face the destination
// of the projectile, but if the shooter is a player, only rotate him/her
// if he/she is not looking in the correct general direction

if (shooterType.name().equals("NPC") ||
Utilities.isFacingLocation(shooter, location, 45) == false) {

Utilities.faceLocation(shooter, location);
if (shooter.identify().startsWith("n@") ||
Utilities.isFacingLocation(shooterEntity, destination, 45) == false) {
Utilities.faceLocation(shooterEntity, destination);
}

Location spawnLocation = shooter.getEyeLocation().add(
shooter.getEyeLocation().getDirection())
.subtract(0, 0.4, 0);

final Entity entity = (entityType.name() == "FALLING_BLOCK") ?
shooter.getWorld().spawnFallingBlock(
spawnLocation, 12, (byte) 0) :
shooter.getWorld().spawnEntity(spawnLocation, entityType);

Utilities.faceLocation(entity, location);
Location origin = shooterEntity.getEyeLocation().add(
shooterEntity.getEyeLocation().getDirection())
.subtract(0, 0.4, 0);

if (ride == true) {
entity.setPassenger(scriptEntry.getPlayer().getPlayerEntity());
if (projectiles.isSpawned() == false) {
projectiles.spawnAt(origin);
}

final Entity projectile = projectiles.getBukkitEntity();

if (burn == true) {
entity.setFireTicks(500);
}
Utilities.faceLocation(projectile, destination);

if (entity instanceof Projectile) {
((Projectile) entity).setShooter(shooter);
if (projectile instanceof Projectile) {
((Projectile) projectile).setShooter(shooter.getLivingEntity());
}

BukkitRunnable task = new BukkitRunnable()
{
BukkitRunnable task = new BukkitRunnable() {

int runs = 0;

public void run() {

if (runs < 40 && entity.isValid())
if (runs < 40 && projectile.isValid())
{
Vector v1 = entity.getLocation().toVector();
Vector v2 = location.toVector();
Vector v1 = projectile.getLocation().toVector();
Vector v2 = destination.toVector();
Vector v3 = v2.clone().subtract(v1).normalize().multiply(1.5);

entity.setVelocity(v3);
projectile.setVelocity(v3);
runs++;

// Check if the entity is close to its destination
Expand All @@ -174,7 +146,7 @@ public void run() {
// Check if the entity has collided with something
// using the most basic possible calculation

if (entity.getLocation().add(v3).getBlock().getType().toString().equals("AIR") == false) {
if (projectile.getLocation().add(v3).getBlock().getType().toString().equals("AIR") == false) {

runs = 40;
}
Expand All @@ -184,13 +156,12 @@ public void run() {
this.cancel();
runs = 0;

if (scriptEntry.getObject("script") != null)
if (script != null)
{
Map<String, String> context = new HashMap<String, String>();
context.put("1", entity.getLocation().getX() + "," + entity.getLocation().getY() + "," + entity.getLocation().getZ() + "," + entity.getLocation().getWorld().getName());
context.put("1", projectile.getLocation().getX() + "," + projectile.getLocation().getY() + "," + projectile.getLocation().getZ() + "," + projectile.getLocation().getWorld().getName());

((TaskScriptContainer) ((dScript) scriptEntry.getObject("script")).
getContainer()).setSpeed(new Duration(0))
((TaskScriptContainer) script.getContainer()).setSpeed(new Duration(0))
.runTaskScript(scriptEntry.getPlayer(), scriptEntry.getNPC(), context);
}

Expand Down
@@ -0,0 +1,47 @@
package net.aufdemrand.denizen.utilities.entity;

import java.util.List;

import org.bukkit.entity.Entity;

public class Position {

/**
* Mounts a list of entities on top of each other.
*
* @param entities The list of entities
*/

public static void mount(List<Entity> entities) {

Entity lastEntity = null;

for (Entity entity : entities) {

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;
}
}

/**
* Dismounts a list of entities.
*
* @param entities The list of entities
*/
public static void dismount(List<Entity> entities) {

for (Entity entity : entities) {

entity.leaveVehicle();
}
}
}

0 comments on commit aa55664

Please sign in to comment.