Skip to content

Commit

Permalink
Add <el@val[<value>]> constructor for Element.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcernat committed Sep 16, 2013
1 parent eae7eef commit b2c9ebe
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 46 deletions.
62 changes: 26 additions & 36 deletions src/main/java/net/aufdemrand/denizen/objects/Element.java
@@ -1,20 +1,26 @@
package net.aufdemrand.denizen.objects;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.aufdemrand.denizen.objects.aH.Argument;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.debugging.dB;

import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.regex.Pattern;

public class Element implements dObject {

public final static Element TRUE = new Element(Boolean.TRUE);
public final static Element FALSE = new Element(Boolean.FALSE);

final static Pattern VALUE_PATTERN =
Pattern.compile("el@val(?:ue)?\\[([^\\[\\]]+)\\].*",
Pattern.CASE_INSENSITIVE);

/**
*
* @param string the string or dScript argument String
Expand All @@ -25,14 +31,28 @@ public class Element implements dObject {
public static Element valueOf(String string) {
if (string == null) return null;

Matcher m = VALUE_PATTERN.matcher(string);

// Allow construction of elements with el@val[<value>]
if (m.matches()) {
String value = m.group(1);
Argument arg = Argument.valueOf(value);

if (arg.matchesPrimitive(aH.PrimitiveType.Integer))
return new Element(aH.getIntegerFrom(value));
else if (arg.matchesPrimitive(aH.PrimitiveType.Double))
return new Element(aH.getDoubleFrom(value));
else return new Element(value);
}

return new Element(string);
}

public static boolean matches(String string) {
return string != null;
}

private String element;
private final String element;

public Element(String string) {
this.prefix = "element";
Expand Down Expand Up @@ -343,36 +363,6 @@ public String getAttribute(Attribute attribute) {
.getAttribute(attribute.fulfill(1));


/////////////////////
// INITIALIZATION ATTRIBUTES
/////////////////

// <--[tag]
// @attribute <element.double[<#>]>
// @returns Element(Double)
// @description
// Returns a double from the value inside the brackets
// -->
if (attribute.startsWith("double")
&& attribute.hasContext(1)) {
return new Element(Double.valueOf(attribute.getContext(1)))
.getAttribute(attribute.fulfill(1));
}

// <--[tag]
// @attribute <element.int[<#>]>
// @returns Element(Integer)
// @description
// Returns an integer from the value inside the brackets, useful
// whenever <element.as_int> cannot be used.
// -->
if (attribute.startsWith("int")
&& attribute.hasContext(1)) {
return new Element(Math.round(Double.valueOf(attribute.getContext(1))))
.getAttribute(attribute.fulfill(1));
}


/////////////////////
// STRING CHECKING ATTRIBUTES
/////////////////
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/net/aufdemrand/denizen/objects/dEntity.java
Expand Up @@ -311,9 +311,17 @@ public EntityType getEntityType() {
return entity_type;
}

/**
* Get the dObject that most accurately describes this entity,
* useful for automatically saving dEntities to contexts as
* dNPCs and dPlayers
*
* @return The NPC
*/

public dObject getDenizenObject() {

if (this == null) return null;
if (entity == null) return null;

if (isNPC()) return new dNPC(getNPC());
else if (isPlayer()) return new dPlayer(getPlayer());
Expand Down
19 changes: 10 additions & 9 deletions src/main/java/net/aufdemrand/denizen/objects/dPlayer.java
@@ -1,5 +1,10 @@
package net.aufdemrand.denizen.objects;

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

import net.aufdemrand.denizen.flags.FlagManager;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.tags.core.PlayerTags;
Expand All @@ -14,11 +19,6 @@
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryType;

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

public class dPlayer implements dObject {


Expand Down Expand Up @@ -521,6 +521,7 @@ else if (attribute.startsWith("list.offline")) {
// Player is required to be online after this point...
if (!isOnline()) return new Element(identify()).getAttribute(attribute);


/////////////////////
// CITIZENS ATTRIBUTES
/////////////////
Expand Down Expand Up @@ -849,13 +850,13 @@ else if (attribute.getAttribute(2).startsWith("world"))
double maxHunger = getPlayerEntity().getMaxHealth();
if (attribute.hasContext(2))
maxHunger = attribute.getIntContext(2);
if ((float) getPlayerEntity().getFoodLevel() / maxHunger < .10)
if (getPlayerEntity().getFoodLevel() / maxHunger < .10)
return new Element("starving").getAttribute(attribute.fulfill(2));
else if ((float) getPlayerEntity().getFoodLevel() / maxHunger < .40)
else if (getPlayerEntity().getFoodLevel() / maxHunger < .40)
return new Element("famished").getAttribute(attribute.fulfill(2));
else if ((float) getPlayerEntity().getFoodLevel() / maxHunger < .75)
else if (getPlayerEntity().getFoodLevel() / maxHunger < .75)
return new Element("parched").getAttribute(attribute.fulfill(2));
else if ((float) getPlayerEntity().getFoodLevel() / maxHunger < 1)
else if (getPlayerEntity().getFoodLevel() / maxHunger < 1)
return new Element("hungry").getAttribute(attribute.fulfill(2));

else return new Element("healthy").getAttribute(attribute.fulfill(2));
Expand Down

0 comments on commit b2c9ebe

Please sign in to comment.