Skip to content

Commit

Permalink
Rewrite Remove command for new argument system. Example usage: - remo…
Browse files Browse the repository at this point in the history
…ve "entities:zombie|creeper|n@1|e@59" "region:Protected Area"
  • Loading branch information
davidcernat committed Jul 12, 2013
1 parent a46b0f4 commit ea5bf53
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 86 deletions.
71 changes: 62 additions & 9 deletions src/main/java/net/aufdemrand/denizen/objects/dEntity.java
Expand Up @@ -154,7 +154,15 @@ else if (entityGroup.matches("P@")) {
Entity entity = null;

for (World world : Bukkit.getWorlds()) {
entity = ((CraftWorld) world).getHandle().getEntity(entityID).getBukkitEntity();
net.minecraft.server.v1_6_R2.Entity nmsEntity = ((CraftWorld) world).getHandle().getEntity(entityID);

if (nmsEntity != null) {
entity = nmsEntity.getBukkitEntity();
}
else {
return null;
}

if (entity != null) break;
}
if (entity != null) return new dEntity(entity);
Expand Down Expand Up @@ -281,6 +289,10 @@ public dEntity(EntityType entityType, String data1, String data2) {
private String data2 = null;
private DespawnedEntity despawned_entity = null;

public EntityType getEntityType() {
return entity_type;
}

public Entity getBukkitEntity() {
return entity;
}
Expand All @@ -294,6 +306,43 @@ public LivingEntity getLivingEntity() {
public boolean isLivingEntity() {
return (entity instanceof LivingEntity);
}

/**
* Get the NPC corresponding to this entity
*
* @return The NPC
*/

public NPC getNPC() {

return CitizensAPI.getNPCRegistry().getNPC(getBukkitEntity());
}

/**
* Whether this entity is an NPC
*
* @return true or false
*/

public boolean isNPC() {
if (CitizensAPI.getNPCRegistry().isNPC(getBukkitEntity()))
return true;
return false;
}

/**
* Whether this entity identifies as a generic
* entity type instead of a spawned entity
*
* @return true or false
*/

public boolean isGeneric() {
if (identify().matches("e@\\D+"))
return true;
return false;
}


public void spawnAt(Location location) {
// If the entity is already spawned, teleport it.
Expand Down Expand Up @@ -436,6 +485,10 @@ else if (entity != null)
public boolean isSpawned() {
return entity != null;
}

public void remove() {
entity.remove();
}

public dEntity rememberAs(String id) {
dEntity.saveAs(this, id);
Expand Down Expand Up @@ -553,14 +606,14 @@ public String identify() {

// Check if entity is a Player or NPC
if (getBukkitEntity() != null) {
if (CitizensAPI.getNPCRegistry().isNPC(getBukkitEntity()))
return "n@" + CitizensAPI.getNPCRegistry().getNPC(getBukkitEntity()).getId();
if (isNPC())
return "n@" + getNPC().getId();
else if (getBukkitEntity() instanceof Player)
return "p@" + ((Player) getBukkitEntity()).getName();
}

// Check if entity is a 'saved entity'
else if (isUnique())
if (isUnique())
return "e@" + getSaved(this);

// Check if entity is spawned, therefore having a bukkit entityId
Expand All @@ -582,7 +635,7 @@ public String toString() {
@Override
public boolean isUnique() {
if (entity instanceof Player) return true;
if (CitizensAPI.getNPCRegistry().isNPC(entity)) return true;
if (isNPC()) return true;
return isSaved(this);
}

Expand All @@ -609,8 +662,8 @@ public String getAttribute(Attribute attribute) {
}

if (attribute.startsWith("name")) {
if (CitizensAPI.getNPCRegistry().isNPC(entity))
return new Element(CitizensAPI.getNPCRegistry().getNPC(entity).getName())
if (isNPC())
return new Element(getNPC().getName())
.getAttribute(attribute.fulfill(1));
if (entity instanceof Player)
return new Element(((Player) entity).getName())
Expand Down Expand Up @@ -723,7 +776,7 @@ else if ((float) getLivingEntity().getHealth() / maxHealth < 1)
return new Element(String.valueOf(getLivingEntity().getCanPickupItems()))
.getAttribute(attribute.fulfill(1));

if (attribute.startsWith("entity_id"))
if (attribute.startsWith("id"))
return new Element(String.valueOf(entity.getEntityId()))
.getAttribute(attribute.fulfill(1));

Expand Down
9 changes: 8 additions & 1 deletion src/main/java/net/aufdemrand/denizen/objects/dList.java
Expand Up @@ -168,7 +168,14 @@ public List<dObject> filter(Class<? extends dObject> dClass) {
try {
if ((Boolean) dClass.getMethod("matches", String.class).invoke(null, element)) {

results.add((dObject) dClass.getMethod("valueOf", String.class).invoke(null, element));
dObject object = (dObject) dClass.getMethod("valueOf", String.class).invoke(null, element);

// Only add the object if it is not null, thus filtering useless
// list items

if (object != null) {
results.add(object);
}
}
} catch (Exception e) {
e.printStackTrace();
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/net/aufdemrand/denizen/objects/dNPC.java
Expand Up @@ -133,6 +133,10 @@ public InteractScriptContainer getInteractScriptQuietly(dPlayer player, Class<?
dB.debugMode = db;
return script;
}

public void destroy() {
getCitizen().destroy();
}

public dLocation getLocation() {
if (isSpawned()) return
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/net/aufdemrand/denizen/objects/dPlayer.java
Expand Up @@ -131,6 +131,16 @@ public dLocation getLocation() {
if (isOnline()) return new dLocation(getPlayerEntity().getLocation());
else return null;
}

public dLocation getEyeLocation() {
if (isOnline()) return new dLocation(getPlayerEntity().getEyeLocation());
else return null;
}

public World getWorld() {
if (isOnline()) return getPlayerEntity().getWorld();
else return null;
}

public boolean isOnline() {
if (player_name == null) return false;
Expand Down Expand Up @@ -361,7 +371,7 @@ else if (attribute.startsWith("list.offline")) {
return new Element(player_name).getAttribute(attribute.fulfill(1));

if (attribute.startsWith("eyes"))
return new dLocation(getPlayerEntity().getEyeLocation())
return new dLocation(getEyeLocation())
.getAttribute(attribute.fulfill(1));

if (attribute.startsWith("compass_target"))
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/net/aufdemrand/denizen/objects/dWorld.java
Expand Up @@ -4,7 +4,9 @@
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Chunk;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;

import java.util.ArrayList;
Expand Down Expand Up @@ -67,6 +69,10 @@ public static boolean matches(String arg) {
public World getWorld() {
return Bukkit.getWorld(world_name);
}

public List<Entity> getEntities() {
return getWorld().getEntities();
}

private String prefix;
String world_name;
Expand Down
Expand Up @@ -43,6 +43,11 @@ else if (!scriptEntry.hasObject("duration")

if ((!scriptEntry.hasObject("entities")))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "ENTITIES");

// Use default duration if one is not specified

if ((!scriptEntry.hasObject("duration")))
scriptEntry.addObject("duration", Duration.valueOf("5s"));
}

@SuppressWarnings("unchecked")
Expand Down

0 comments on commit ea5bf53

Please sign in to comment.