Skip to content

Commit

Permalink
Clean up Duration. Add .mul[x] .add[x] .sub[x] and .div[x] to Element.
Browse files Browse the repository at this point in the history
  • Loading branch information
aufdemrand committed Jul 7, 2013
1 parent ece57f9 commit d72ff17
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 18 deletions.
93 changes: 75 additions & 18 deletions src/main/java/net/aufdemrand/denizen/objects/Duration.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,54 @@

public class Duration implements dObject {

// Use regex pattern matching to easily determine if a string
// value is a valid Duration.
final static Pattern match =
Pattern.compile("(\\d+.\\d+|.\\d+|\\d+)(t|m|s|h|d)((-\\d+.\\d+|.\\d+|\\d+)(t|m|s|h|d))?",
Pattern.CASE_INSENSITIVE);


// Define a 'ZERO' Duration
final public static Duration ZERO = new Duration(0);


/**
* Gets a Duration Object from a dScript argument. Durations must be a positive
* number. Can specify the unit of time by using one of the following: T=ticks, M=minutes,
* S=seconds, H=hours, D=days. Not using a unit will imply seconds. Examples: 10s, 50m, 1d, 50.
* Can also include a prefix, though it will be ignored. Example: duration:10, time:30m.
*
* @param string the dScript argument String
* @return a Script, or null if incorrectly formatted
* @param string the Argument value.
* @return a Duration, or null if incorrectly formatted.
*/
public static Duration valueOf(String string) {
if (string == null) return null;

// Pick a duration between
// Pick a duration between a high and low number if there is a '-' present.
if (string.indexOf("-") > 0
&& Duration.matches(string.split("-", 2)[0])
&& Duration.matches(string.split("-", 2)[1])) {

String[] split = string.split("-", 2);
dB.echoDebug("Getting a Duration within range: " + split);

Duration low = Duration.valueOf(split[0]);
Duration high = Duration.valueOf(split[1]);

if (low != null && high != null) {
// Make sure 'low' and 'high' returned valid Durations,
// and that 'low' is less time than 'high'.
if (low != null && high != null
&& low.getSecondsAsInt() < high.getSecondsAsInt()) {
int seconds = Utilities.getRandom()
.nextInt((high.getSecondsAsInt() - low.getSecondsAsInt())
+ low.getSecondsAsInt());
dB.echoDebug("Getting random duration between " + low.identify() + " and " + high.identify() + "... " + seconds + "s");
// Send the result to the debugger since it's probably good to know what is being chosen.
dB.echoDebug("Getting random duration between " + low.identify()
+ " and " + high.identify() + "... " + seconds + "s");

return new Duration(seconds);
}
else return null;

} else return null;
}

// Standard Duration. Check the type and create new Duration object accordingly.
Matcher m = match.matcher(string);
if (m.matches()) {
if (m.group().toUpperCase().endsWith("T"))
Expand All @@ -76,6 +85,12 @@ else if (m.group().toUpperCase().endsWith("H"))
}


/**
* Checks to see if the string is a valid Duration.
*
* @param string the String to match.
* @return true if valid.
*/
public static boolean matches(String string) {
Matcher m = match.matcher(string);
if (m.matches()) return true;
Expand All @@ -84,61 +99,79 @@ public static boolean matches(String string) {
}


// The amount of seconds in the duration.
private double seconds;


// Duration's default dObject prefix.
private String prefix = "Duration";


/**
* Creates a duration object when given number of seconds.
*
* @param seconds number of seconds
* @param seconds the number of seconds.
*/
public Duration(double seconds) {
this.seconds = seconds;
if (this.seconds < 0) this.seconds = 0;
}


/**
* Creates a duration object when given number of seconds.
*
* @param seconds number of seconds
* @param seconds the number of seconds.
*/
public Duration(int seconds) {
this.seconds = seconds;
if (this.seconds < 0) this.seconds = 0;
}


/**
* Creates a duration object when given number of Bukkit ticks.
*
* @param ticks number of ticks
* @param ticks the number of ticks.
*/
public Duration (long ticks) {
this.seconds = ticks / 20;
if (this.seconds < 0) this.seconds = 0;
}


/**
* Gets the number of ticks of this duration.
* Gets the number of ticks of this duration. There are 20 ticks
* per second.
*
* @return number of ticks
* @return the number of ticks.
*/
public long getTicks() {
return (long) (seconds * 20);
}


/**
* Gets the number of ticks of this duration.
* Gets the number of ticks of this duration as an integer. There are
* 20 per second.
*
* @return number of ticks
* @return the number of ticks.
*/
public int getTicksAsInt() {
return Ints.checkedCast((long) (seconds * 20));
}


/**
* Gets the number of milliseconds in this duration.
*
* @return the number of milliseconds.
*/
public long getMillis() {
return (long) (seconds * 1000);
}


/**
* Gets the number of seconds of this duration.
*
Expand All @@ -148,6 +181,7 @@ public double getSeconds() {
return seconds;
}


/**
* Gets the number of seconds as an integer value of the duration.
*
Expand All @@ -160,26 +194,41 @@ public int getSecondsAsInt() {
return Ints.checkedCast(Math.round(seconds));
}


@Override
public String getPrefix() {
return prefix;
}


@Override
public String debug() {
return ChatColor.DARK_GRAY + prefix + "='" + ChatColor.YELLOW + seconds + " seconds" + ChatColor.DARK_GRAY + "' ";
return ChatColor.DARK_GRAY + prefix + "='"
+ ChatColor.YELLOW + identify()
+ ChatColor.DARK_GRAY + "' ";
}


// Durations are not unique, cannot be saved or persisted.
@Override
public boolean isUnique() {
return false;
}


@Override
public String getType() {
return "duration";
}


/**
* Return the value of this Duration. This will also return a
* valid String that can be re-interpreted with Duration.valueOf()
* thus acting as a form of 'serialization/deserialization'.
*
* @return a valid String-form Duration.
*/
@Override
public String identify() {
double seconds = getTicks() / 20;
Expand All @@ -197,17 +246,25 @@ public String identify() {
else return seconds + "s";
}


/**
* Acts just like identify().
*
* @return a valid String-form Duration.
*/
@Override
public String toString() {
return identify();
}


@Override
public dObject setPrefix(String prefix) {
this.prefix = prefix;
return this;
}


@Override
public String getAttribute(Attribute attribute) {

Expand Down
24 changes: 24 additions & 0 deletions src/main/java/net/aufdemrand/denizen/objects/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,30 @@ else if (element.toLowerCase().contains(contains.toLowerCase()))
.getAttribute(attribute.fulfill(1));
}

if (attribute.startsWith("mul")
&& attribute.hasContext(1)) {
return new Element(asDouble() * aH.getDoubleFrom(attribute.getContext(1)))
.getAttribute(attribute.fulfill(1));
}

if (attribute.startsWith("sub")
&& attribute.hasContext(1)) {
return new Element(asDouble() - aH.getDoubleFrom(attribute.getContext(1)))
.getAttribute(attribute.fulfill(1));
}

if (attribute.startsWith("add")
&& attribute.hasContext(1)) {
return new Element(asDouble() + aH.getDoubleFrom(attribute.getContext(1)))
.getAttribute(attribute.fulfill(1));
}

if (attribute.startsWith("div")
&& attribute.hasContext(1)) {
return new Element(asDouble() / aH.getDoubleFrom(attribute.getContext(1)))
.getAttribute(attribute.fulfill(1));
}

if (attribute.startsWith("length")) {
return new Element(element.length())
.getAttribute(attribute.fulfill(1));
Expand Down

0 comments on commit d72ff17

Please sign in to comment.