Skip to content

Commit

Permalink
allow negative durationtags
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Sep 27, 2020
1 parent 68f7b8b commit f0872eb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ public class DurationTag implements ObjectTag {
// @group Object System
// @description
// Durations are a unified and convenient way to get a 'unit of time' throughout Denizen.
// Many commands and features that require a duration can be satisfied by specifying a number
// and unit of time, especially command arguments that are prefixed 'duration:', etc.
// Many commands and features that require a duration can be satisfied by specifying a number and unit of time, especially command arguments that are prefixed 'duration:', etc.
// The unit of time can be specified by using one of the following:
// t=ticks, s=seconds, m=minutes, h=hours, d=days, w=weeks.
// Not using a unit will imply seconds. Examples: 10s, 50m, 1d, 20.
//
// Specifying a range of duration will result in a randomly selected duration that is
// in between the range specified. The smaller value should be first. Examples:
// '10s-25s', '1m-2m'.
// Specifying a range of duration will result in a randomly selected duration that is in between the range specified.
// The smaller value should be first. Examples: '10s-25s', '1m-2m'.
//
// The input of 'instant' or 'infinite' will be interpreted as 0 (for use with commands where instant/infinite logic applies).
//
Expand Down Expand Up @@ -68,8 +66,8 @@ public static DurationTag valueOf(String string, TagContext context) {
if (string.isEmpty()) {
return null;
}
// Pick a duration between a high and low number if there is a '-' present, but it's not "E-2" style scientific notation.
if (string.contains("-") && !string.contains("e-")) {
// Pick a duration between a high and low number if there is a '-' present, but it's not "E-2" style scientific notation nor a negative.
if (string.contains("-") && !string.startsWith("-") && !string.contains("e-")) {
String[] split = string.split("-", 2);
if (split.length == 2) {
DurationTag low = DurationTag.valueOf(split[0], context);
Expand Down Expand Up @@ -169,9 +167,6 @@ public static boolean matches(String string) {
*/
public DurationTag(double seconds) {
this.seconds = seconds;
if (this.seconds < 0) {
this.seconds = 0;
}
}

/**
Expand All @@ -181,9 +176,6 @@ public DurationTag(double seconds) {
*/
public DurationTag(int seconds) {
this.seconds = seconds;
if (this.seconds < 0) {
this.seconds = 0;
}
}

/**
Expand All @@ -193,9 +185,6 @@ public DurationTag(int seconds) {
*/
public DurationTag(long ticks) {
this.seconds = ticks / 20.0;
if (this.seconds < 0) {
this.seconds = 0;
}
}

/////////////////////
Expand Down Expand Up @@ -468,17 +457,19 @@ public ObjectTag getObjectAttribute(Attribute attribute) {
}

public String formatted() {
double secondsCopy = seconds;
boolean isNegative = secondsCopy < 0;
if (isNegative) {
secondsCopy *= -1;
}
// Make sure you don't change these longs into doubles
// and break the code

long seconds = (long) this.seconds;
long seconds = (long) secondsCopy;
long days = seconds / 86400;
long hours = (seconds - days * 86400) / 3600;
long minutes = (seconds - days * 86400 - hours * 3600) / 60;
seconds = seconds - days * 86400 - hours * 3600 - minutes * 60;

String timeString = "";

if (days > 0) {
timeString = days + "d ";
}
Expand All @@ -491,16 +482,14 @@ public String formatted() {
if (seconds > 0 && minutes < 10 && hours == 0 && days == 0) {
timeString = timeString + seconds + "s";
}

if (timeString.isEmpty()) {
if (this.seconds <= 0) {
if (secondsCopy == 0) {
timeString = "forever";
}
else {
timeString = ((double) ((long) (this.seconds * 100)) / 100d) + "s";
timeString = ((double) ((long) (secondsCopy * 100)) / 100d) + "s";
}
}

return timeString.trim();
return (isNegative ? "negative " : "") + timeString.trim();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public AdjustCommand() {
// You can optionally adjust a MapTag of mechanisms to values.
//
// To adjust an item in an inventory, use <@link command inventory>, as '- inventory adjust slot:<#> <mechanism>:<value>'.
// Note that that is only for items, not actual inventories.
// To adjust an actual InventoryTag mechanism, you should still use the normal 'adjust' command, not 'inventory adjust'.
//
// @Tags
// <entry[saveName].result> returns the adjusted object.
Expand Down

0 comments on commit f0872eb

Please sign in to comment.