Implement %prettytime% on signs, time in a pretty format. Adds #35

The variable of %prettytime% will be replaced on signs and also a nice
format on the status message will be added.
This commit is contained in:
graywolf336 2015-01-27 19:21:42 -06:00
parent 72c29bb4ea
commit 87acaf1538
5 changed files with 63 additions and 10 deletions

View File

@ -99,16 +99,17 @@ public class Util {
/** Returns a message with all the possible variables replaced. */
public static String replaceAllVariables(Prisoner p, String msg) {
msg = msg.replaceAll("%player%", p.getLastKnownName());
msg = msg.replaceAll("%uuid%", p.getUUID().toString());
msg = msg.replaceAll("%reason%", p.getReason());
msg = msg.replaceAll("%jailer", p.getJailer());
msg = msg.replaceAll("%afktime%", TimeUnit.MINUTES.convert(p.getAFKTime(), TimeUnit.MILLISECONDS) + " mins");
msg = msg.replace("%player%", p.getLastKnownName())
.replace("%uuid%", p.getUUID().toString())
.replace("%reason%", p.getReason())
.replace("%jailer", p.getJailer())
.replace("%afktime%", Util.getDurationBreakdown(p.getAFKTime()));
if(p.getRemainingTime() >= 0) {
msg = msg.replaceAll("%timeinminutes%", String.valueOf(p.getRemainingTimeInMinutes()));
msg = msg.replace("%timeinminutes%", String.valueOf(p.getRemainingTimeInMinutes()));
msg = msg.replace("%prettytime%", Util.getDurationBreakdown(p.getRemainingTime()));
}else {
msg = msg.replaceAll("%timeinminutes%", Lang.JAILEDFOREVERSIGN.get());
msg = msg.replace("%timeinminutes%", Lang.JAILEDFOREVERSIGN.get());
}
return getColorfulMessage(msg);
@ -178,6 +179,47 @@ public class Util {
return t;
}
/**
* Convert a millisecond duration to a string format
*
* @param millis A duration to convert to a string form
* @return A string of the form "XdYhZAs".
*/
public static String getDurationBreakdown(long millis) {
if(millis < 0) {
throw new IllegalArgumentException("Duration must be greater than zero!");
}
long days = TimeUnit.MILLISECONDS.toDays(millis);
millis -= TimeUnit.DAYS.toMillis(days);
long hours = TimeUnit.MILLISECONDS.toHours(millis);
millis -= TimeUnit.HOURS.toMillis(hours);
long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);
millis -= TimeUnit.MINUTES.toMillis(minutes);
long seconds = TimeUnit.MILLISECONDS.toSeconds(millis);
StringBuilder sb = new StringBuilder(64);
if(days > 0) {
sb.append(days);
sb.append("d");
}
if(days > 0 || hours > 0) {
sb.append(hours);
sb.append("h");
}
if(days > 0 || hours > 0 || minutes > 0) {
sb.append(minutes);
sb.append("m");
}
sb.append(seconds);
sb.append("s");
return(sb.toString());
}
/**
* Converts the player inventory to a String array of Base64 strings. First string is the content and second string is the armor.
*

View File

@ -4,6 +4,7 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import com.graywolf336.jail.JailManager;
import com.graywolf336.jail.Util;
import com.graywolf336.jail.beans.Prisoner;
import com.graywolf336.jail.command.Command;
import com.graywolf336.jail.command.CommandInfo;
@ -25,7 +26,7 @@ public class JailStatusCommand implements Command{
if(jm.isPlayerJailed(pl.getUniqueId())) {
Prisoner p = jm.getPrisoner(pl.getUniqueId());
//They are jailed, so let's tell them some information
sender.sendMessage(Lang.STATUS.get(new String[] { p.getReason(), p.getJailer(), String.valueOf(p.getRemainingTimeInMinutes()) }));
sender.sendMessage(Lang.STATUS.get(new String[] { p.getReason(), p.getJailer(), String.valueOf(p.getRemainingTimeInMinutes()), Util.getDurationBreakdown(p.getRemainingTime()) }));
}else {
//the sender of the command is not jailed, tell them that
sender.sendMessage(Lang.YOUARENOTJAILED.get());

View File

@ -68,7 +68,7 @@ language:
provideajail: '&cPlease provide a jail to %0% &cthem to.'
resistedarrestjailer: '&c%0% has resisted arrest thanks to having more health than you can jail at.'
resistedarrestplayer: '&cYou have resisted arrest due to having more health than %0% can jail at.'
status: "&2You have been jailed with a reason of '%0%' by %1% and have %2% minutes remaining."
status: "&2You have been jailed with a reason of '%0%' by %1% and have %2% minutes remaining (%3%)."
suggestedcell: '&cAn empty cell in the same jail, %0%, was found: %1%'
telein: "&9Teleported %0% to %1%'s teleport in location."
teleout: "&9Teleported %0% to %1%'s teleport out location."

View File

@ -18,6 +18,7 @@ import org.powermock.modules.junit4.PowerMockRunner;
import test.java.com.graywolf336.jail.util.TestInstanceCreator;
import com.graywolf336.jail.JailMain;
import com.graywolf336.jail.Util;
import com.graywolf336.jail.enums.Lang;
@RunWith(PowerMockRunner.class)
@ -116,7 +117,7 @@ public class TestJailLanguage {
assertEquals(colorize("&cPlease provide a jail to jail &cthem to."), Lang.PROVIDEAJAIL.get("jail"));
assertEquals(colorize("&cgraywolf336 has resisted arrest thanks to having more health than you can jail at."), Lang.RESISTEDARRESTJAILER.get("graywolf336"));
assertEquals(colorize("&cYou have resisted arrest due to having more health than graywolf336 can jail at."), Lang.RESISTEDARRESTPLAYER.get("graywolf336"));
assertEquals(colorize("&2You have been jailed with a reason of 'terrible coding' by graywolf336 and have 60 minutes remaining."), Lang.STATUS.get(new String[] { "terrible coding", "graywolf336", "60" }));
assertEquals(colorize("&2You have been jailed with a reason of 'terrible coding' by graywolf336 and have 60 minutes remaining (1h0m0s)."), Lang.STATUS.get(new String[] { "terrible coding", "graywolf336", "60", Util.getDurationBreakdown(3600000) }));
assertEquals(colorize("&cAn empty cell in the same jail, cloud, was found: cell_n02"), Lang.SUGGESTEDCELL.get(new String[] { "cloud", "cell_n02" }));
assertEquals(colorize("&9Teleported graywolf336 to cloud's teleport in location."), Lang.TELEIN.get(new String[] { "graywolf336", "cloud" }));
assertEquals(colorize("&9Teleported graywolf336 to cloud's teleport out location."), Lang.TELEOUT.get(new String[] { "graywolf336", "cloud" }));

View File

@ -109,4 +109,13 @@ public class TestUtilClass {
assertEquals(1L, Util.getTime("60m", TimeUnit.HOURS), 0);
assertEquals(6L, Util.getTime("6d", TimeUnit.DAYS), 0);
}
@Test
public void testDurationBreakdown() {
assertEquals("1s", Util.getDurationBreakdown(1000));
assertEquals("1m0s", Util.getDurationBreakdown(60000));
assertEquals("1h0m0s", Util.getDurationBreakdown(3600000));
assertEquals("1d0h0m0s", Util.getDurationBreakdown(86400000));
assertEquals("1d1h1m1s", Util.getDurationBreakdown(90061000));
}
}