Skip to content

Commit

Permalink
Clean up argumentHelper, add/fix attributes in dEntity/dWorld
Browse files Browse the repository at this point in the history
  • Loading branch information
aufdemrand committed Apr 8, 2013
1 parent d2be7cb commit ce7c479
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 46 deletions.
64 changes: 37 additions & 27 deletions src/main/java/net/aufdemrand/denizen/utilities/arguments/aH.java
Expand Up @@ -7,9 +7,12 @@
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.minecraft.server.v1_5_R2.Entity;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_5_R2.CraftWorld;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -42,17 +45,20 @@ public static String debugUniqueObj(String prefix, String id, String value) {
final static Pattern doublePtrn = Pattern.compile("(-)?(?:(?:\\d+)|)(?:(?:\\.\\d+)|)");
final static Pattern floatPtrn = Pattern.compile("^[-+]?[0-9]+[.]?[0-9]*([eE][-+]?[0-9]+)?$");
final static Pattern integerPtrn = Pattern.compile("(-)?\\d+");
final static Pattern locationPattern = Pattern.compile("location:((-)?\\d+(\\.\\d+)?,){3}\\w+", Pattern.CASE_INSENSITIVE);
final static Pattern locationPattern =
Pattern.compile("location:((-)?\\d+(\\.\\d+)?,){3}\\w+", Pattern.CASE_INSENSITIVE);
final static Pattern wordPtrn = Pattern.compile("\\w+");
final static Pattern matchesDurationPtrn = Pattern.compile("duration:(\\d+.\\d+|.\\d+|\\d+)(t|m|s|h|d|)", Pattern.CASE_INSENSITIVE);
final static Pattern matchesDurationPtrn =
Pattern.compile("duration:(\\d+.\\d+|.\\d+|\\d+)(t|m|s|h|d|)", Pattern.CASE_INSENSITIVE);
final static Pattern matchesEntityPtrn =
Pattern.compile("(?:.+?|):((ENTITY\\.|PLAYER\\.|NPC\\.).+)|(PLAYER|NPC)", Pattern.CASE_INSENSITIVE);
final static Pattern matchesQuantityPtrn = Pattern.compile("qty:(-)?\\d+", Pattern.CASE_INSENSITIVE);
final static Pattern matchesQueuePtrn = Pattern.compile("queue:(.+)", Pattern.CASE_INSENSITIVE);



/**
* <p>Used to determine if a argument string matches a non-valued custom argument.
* If a dScript valued argument (such as PLAYER:NAME) is passed, this method
* If a dScript valued argument (such as TARGET:NAME) is passed, this method
* will always return false. Also supports multiple argument names, separated by a
* comma (,) character. This method will trim() each name specified.</p>
*
Expand Down Expand Up @@ -182,7 +188,7 @@ public static boolean matchesValueArg(String names, String arg, ArgumentType typ

dB.echoError("While parsing '" + arg + "', Denizen has run into a problem. While the " +
"prefix is correct, the value is not valid. Check documentation for valid value." +
"Perhaps a replaceable Tag has failed to fill in a value?");
"Perhaps a replaceable tag has failed to fill in a value?");
return false;
}

Expand All @@ -199,8 +205,8 @@ public static boolean matchesValueArg(String names, String arg, ArgumentType typ
* <tt>'arg'</tt> will return false.
* </ol>
*
* @param arg the argument to check
* @return true or false
* @param arg the argument to check
* @return true or false
*
*/
public static boolean getBooleanFrom(String arg) {
Expand All @@ -215,10 +221,10 @@ public static boolean getBooleanFrom(String arg) {
*
* <b>Examples:</b>
* <ol>
* <tt>'LEVEL:3.5'</tt> will return '3.5D'.<br>
* <tt>'INT:1'</tt> will return '1D'.<br>
* <tt>'1950'</tt> will return '1950D'.<br>
* <tt>'-.377'</tt> will return '-0.377D'.<br>
* <tt>'LEVEL:3.5'</tt> will return '3.5'.<br>
* <tt>'INT:1'</tt> will return '1.0'.<br>
* <tt>'1950'</tt> will return '1950.0'.<br>
* <tt>'-.377'</tt> will return '-0.377'.<br>
* </ol>
*
* @param arg the argument to check
Expand All @@ -236,15 +242,12 @@ public static double getDoubleFrom(String arg) {
/**
* <p>Returns a Bukkit EntityType from a dScript argument string. Also accounts
* for the argument prefix being passed along, for convenience. Though the
* <tt>matchesEntity(...)</tt> requires an <tt>ITEM:</tt> prefix, this method
* <tt>matchesEntity(...)</tt> requires an <tt>ENTITY:</tt> prefix, this method
* does not, so it can be used in a CustomValueArg.<p>
*
* <p>Provides a line of dB output if returning null. For getting saved entities
* make with the 'NEW ENTITY Command', use {@link #getLivingEntityFrom(String)}</p>
*
* <b>Examples:</b>
* <ol>
* <tt>'zombie'</tt> will return 'EntityType.Zombie'.<br>
* <tt>'zombie'</tt> will return 'EntityType.ZOMBIE'.<br>
* <tt>'monster:skeleton'</tt> will return 'EntityType.SKELETON'.<br>
* <tt>'1983'</tt> will return 'null'.<br>
* </ol>
Expand Down Expand Up @@ -273,25 +276,32 @@ public static LivingEntity getLivingEntityFrom(String arg) {
Matcher m = matchesEntityPtrn.matcher(arg);
if (m.matches()) {
String entityGroup = m.group(1);
String entityGroupUpper =entityGroup.toUpperCase();
if (entityGroupUpper.startsWith("ENTITY.")) {
LivingEntity returnable = ((Denizen) Bukkit.getPluginManager().getPlugin("Denizen"))
.getCommandRegistry().get(NewCommand.class).getEntity(entityGroup.split("\\.")[1]);
if (returnable != null) return returnable;
else dB.echoError("Invalid entity! '" + entityGroup + "' could not be found.");
String entityGroupUpper = entityGroup.toUpperCase();
if (entityGroupUpper.startsWith("ENTITY.")
|| entityGroupUpper.startsWith("@E.")) {
int entityID = Integer.valueOf(entityGroup.split("\\.")[1]);
Entity entity = null;
for (World world : Bukkit.getWorlds()) {
entity = ((CraftWorld) world).getHandle().getEntity(entityID);
if (entity != null) break;
}
if (entity != null) return (LivingEntity) entity.getBukkitEntity();
else dB.echoError("Invalid entity! '" + entityGroup + "' could not be found. Has it been despawned or killed?");
}

else if (entityGroupUpper.startsWith("NPC.") ||
entityGroupUpper.startsWith("NPCID.")) {
else if (entityGroupUpper.startsWith("NPC.")
|| entityGroupUpper.startsWith("NPCID.")
|| entityGroupUpper.startsWith("@N.")) {
LivingEntity returnable = CitizensAPI.getNPCRegistry().getById(Integer.valueOf(entityGroup.split("\\.")[1])).getBukkitEntity();
if (returnable != null) return returnable;
else dB.echoError("Invalid NPC! '" + entityGroup + "' could not be found.");
else dB.echoError("Invalid NPC! '" + entityGroup + "' could not be found. Has it been despawned or killed?");
}

else if (entityGroupUpper.startsWith("PLAYER.")) {
else if (entityGroupUpper.startsWith("PLAYER.")
|| entityGroupUpper.startsWith("@P.")) {
LivingEntity returnable = getPlayerFrom(entityGroup.split("\\.")[1]);
if (returnable != null) return returnable;
else dB.echoError("Invalid Player! '" + entityGroup + "' could not be found.");
else dB.echoError("Invalid Player! '" + entityGroup + "' could not be found. Has the player logged off?");
}
}

Expand Down
Expand Up @@ -4,12 +4,19 @@
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.citizensnpcs.api.CitizensAPI;
import net.minecraft.server.v1_5_R2.Entity;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_5_R2.CraftWorld;
import org.bukkit.entity.LivingEntity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class dEntity implements dScriptArgument {

Expand All @@ -23,7 +30,7 @@ public class dEntity implements dScriptArgument {
*/
public static dEntity getSavedEntity(String id) {
if (entities.containsKey(id.toUpperCase()))
return entities.get(id.toUpperCase());
return entities.get(id.toUpperCase());
else return null;
}

Expand Down Expand Up @@ -60,15 +67,69 @@ public static void _saveEntities() {
}

/**
* Gets a Item Object from a string form.
* Gets a dEntity Object from a string form.</br>
* </br>
* n@13 will return NPC 13</br>
* e@5884 will return the entity with the entityid of 5884</br>
* p@aufdemrand will return the player object of aufdemrand</br>
* </br>
* Note that the NPCs, Entities, and Players must be spawned,
* one coincidentally Players must be logged in.</br>
*
*
* @param string the string or dScript argument String
* @return an Item, or null if incorrectly formatted
* @return a dEntity, or null
*
*/
@ObjectFetcher("e")
public static dEntity valueOf(String string) {

// Create entity!
// Make sure string matches what this interpreter can accept.
final Pattern matchesEntityPtrn =
Pattern.compile("(?:.+?:|)((n@|e@|p@|)(.+))",
Pattern.CASE_INSENSITIVE);

Matcher m = matchesEntityPtrn.matcher(string);

if (m.matches()) {
String entityGroup = m.group(1);
String entityGroupUpper = entityGroup.toUpperCase();

// TODO: Deprecate NPC./NPCID.
if (entityGroupUpper.startsWith("N@")) {
LivingEntity returnable = CitizensAPI.getNPCRegistry()
.getById(Integer.valueOf(m.group(4))).getBukkitEntity();

if (returnable != null) return new dEntity(returnable);
else dB.echoError("Invalid NPC! '" + entityGroup + "' could not be found. Has it been despawned or killed?");
}

// TODO: Deprecate PLAYER.
else if (entityGroupUpper.startsWith("P@")) {
LivingEntity returnable = aH.getPlayerFrom(m.group(4));

if (returnable != null) new dEntity(returnable);
else dB.echoError("Invalid Player! '" + entityGroup + "' could not be found. Has the player logged off?");
}

// Assume entity
else {

if (aH.matchesInteger(m.group(4))) {
int entityID = Integer.valueOf(m.group(4));
Entity entity = null;

for (World world : Bukkit.getWorlds()) {
entity = ((CraftWorld) world).getHandle().getEntity(entityID);
if (entity != null) break;
}

if (entity != null) return new dEntity((LivingEntity) entity.getBukkitEntity());
}
// Got this far? Invalid entity.
dB.echoError("Invalid entity! '" + entityGroup + "' could not be found. Has it been despawned or killed?");
}
}

return null;
}
Expand Down Expand Up @@ -139,16 +200,18 @@ public dScriptArgument setPrefix(String prefix) {
}

public LivingEntity getEntity(String string) {
return entity;
return entity;
}

@Override
public String getAttribute(Attribute attribute) {

if (attribute == null) return null;

// Desensitize the attribute for comparison
String id = this.id.toLowerCase();
if (entity == null) {
dB.echoDebug("dEntity has returned null.");
return "null";
}

if (attribute.startsWith("name"))
return new Element(entity.getCustomName()).getAttribute(attribute.fulfill(1));
Expand Down
Expand Up @@ -2,17 +2,16 @@

import net.aufdemrand.denizen.interfaces.dScriptArgument;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.tags.core.PlayerTags;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.depends.Depends;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class dWorld implements dScriptArgument {

Expand All @@ -22,20 +21,26 @@ public class dWorld implements dScriptArgument {
* @return a dScript dList
*
*/
@ObjectFetcher("w")
public static dWorld valueOf(String string) {
if (string == null) return null;

String prefix = null;
// Strip prefix (ie. targets:...)
if (string.split(":").length > 1) {
prefix = string.split(":", 2)[0];
string = string.split(":", 2)[1];
}
// Make sure string matches what this interpreter can accept.
final Pattern world_object_pattern =
Pattern.compile("(.+?:|)((w@|)(.+))",
Pattern.CASE_INSENSITIVE);

Matcher m = world_object_pattern.matcher(string);

for (World world : Bukkit.getWorlds())
if (world.getName().equalsIgnoreCase(string)) return new dWorld(prefix, world);
if (m.matches()) {
String prefix = m.group(1);
String world_name = m.group(2).split("@")[1];

for (World world : Bukkit.getWorlds())
if (world.getName().equalsIgnoreCase(world_name)) return new dWorld(prefix, world);
}

dB.echoError("World '" + string + "' is invalid or does not exist.");
dB.echoError("World '" + m.group(2) + "' is invalid or does not exist.");
return null;
}

Expand Down

0 comments on commit ce7c479

Please sign in to comment.