From 5231f6e86ea1205b0157754570ec82095c180252 Mon Sep 17 00:00:00 2001 From: Thijs Wiefferink Date: Sun, 3 May 2015 22:51:19 +0200 Subject: [PATCH] Change all time period settings to readable format Changed all settings that took minutes or seconds as unit before to use the format also used on signs (things like '50 seconds', '1 day', '3 months' etc). Fixed a bug with display of currency numbers being unrounded when not displayed with SI prefix. --- .../nl/evolutioncoding/areashop/AreaShop.java | 90 +++++++++++++++++-- .../nl/evolutioncoding/areashop/Utils.java | 14 +++ .../listeners/PlayerLoginListener.java | 2 +- .../areashop/regions/RentRegion.java | 48 +--------- AreaShop/src/main/resources/config.yml | 47 +++++----- AreaShop/src/main/resources/default.yml | 10 +-- 6 files changed, 131 insertions(+), 80 deletions(-) diff --git a/AreaShop/src/main/java/nl/evolutioncoding/areashop/AreaShop.java b/AreaShop/src/main/java/nl/evolutioncoding/areashop/AreaShop.java index e2667e1..a53ee3a 100644 --- a/AreaShop/src/main/java/nl/evolutioncoding/areashop/AreaShop.java +++ b/AreaShop/src/main/java/nl/evolutioncoding/areashop/AreaShop.java @@ -3,6 +3,7 @@ package nl.evolutioncoding.areashop; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.ArrayList; +import java.util.Calendar; import java.util.UUID; import java.util.logging.Logger; @@ -426,7 +427,7 @@ public final class AreaShop extends JavaPlugin implements AreaShopInterface { */ public void setupTasks() { // Rent expiration timer - int expirationCheck = this.getConfig().getInt("expiration.delay")*20; + long expirationCheck = Utils.millisToTicks(getDurationFromSecondsOrString("expiration.delay")); final AreaShop finalPlugin = this; if(expirationCheck > 0) { new BukkitRunnable() { @@ -442,7 +443,7 @@ public final class AreaShop extends JavaPlugin implements AreaShopInterface { }.runTaskTimer(this, 1, expirationCheck); } // Inactive unrenting/selling timer - int inactiveCheck = this.getConfig().getInt("inactive.delay")*60*20; + long inactiveCheck = Utils.millisToTicks(getDurationFromMinutesOrString("inactive.delay")); if(inactiveCheck > 0) { new BukkitRunnable() { @Override @@ -457,7 +458,7 @@ public final class AreaShop extends JavaPlugin implements AreaShopInterface { }.runTaskTimer(this, inactiveCheck, inactiveCheck); } // Periodic updating of signs for timeleft tags - int periodicUpdate = this.getConfig().getInt("signs.delay")*20; + long periodicUpdate = Utils.millisToTicks(getDurationFromSecondsOrString("signs.delay")); if(periodicUpdate > 0) { new BukkitRunnable() { @Override @@ -472,7 +473,7 @@ public final class AreaShop extends JavaPlugin implements AreaShopInterface { }.runTaskTimer(this, periodicUpdate, periodicUpdate); } // Saving regions and group settings - int saveFiles = this.getConfig().getInt("saving.delay")*20*60; + long saveFiles = Utils.millisToTicks(getDurationFromMinutesOrString("saving.delay")); if(saveFiles > 0) { new BukkitRunnable() { @Override @@ -487,7 +488,7 @@ public final class AreaShop extends JavaPlugin implements AreaShopInterface { }.runTaskTimer(this, saveFiles, saveFiles); } // Sending warnings about rent regions to online players - int expireWarning = this.getConfig().getInt("expireWarning.delay")*20*60; + long expireWarning = Utils.millisToTicks(getDurationFromMinutesOrString("expireWarning.delay")); if(expireWarning > 0) { new BukkitRunnable() { @Override @@ -609,7 +610,6 @@ public final class AreaShop extends JavaPlugin implements AreaShopInterface { after = "k" + after; } BigDecimal bigDecimal = new BigDecimal(amount); - bigDecimal.setScale(getConfig().getInt("fractionalNumbers"), RoundingMode.HALF_UP); if(bigDecimal.toString().contains(".")) { int frontLength = bigDecimal.toString().substring(0, bigDecimal.toString().indexOf('.')).length(); bigDecimal = bigDecimal.setScale(getConfig().getInt("fractionalNumbers") + (3-frontLength), RoundingMode.HALF_UP); @@ -617,7 +617,8 @@ public final class AreaShop extends JavaPlugin implements AreaShopInterface { result = bigDecimal.toString(); } else { BigDecimal bigDecimal = new BigDecimal(amount); - bigDecimal.setScale(getConfig().getInt("fractionalNumbers"), RoundingMode.HALF_UP); + bigDecimal = bigDecimal.setScale(getConfig().getInt("fractionalNumbers"), RoundingMode.HALF_UP); + amount = bigDecimal.doubleValue(); result = bigDecimal.toString(); if(getConfig().getBoolean("hideEmptyFractionalPart") && (amount%1.0) == 0.0 && result.contains(".")) { result = result.substring(0, result.indexOf('.')); @@ -684,6 +685,76 @@ public final class AreaShop extends JavaPlugin implements AreaShopInterface { return prefix.matches("\\d+"); } + /** + * Methode to tranlate a duration string to a millisecond value + * @param duration The duration string + * @return The duration in milliseconds translated from the durationstring, or if it is invalid then 0 + */ + public long durationStringToLong(String duration) { + if(duration == null) { + return 0; + } else if(duration.equalsIgnoreCase("disabled") || duration.equalsIgnoreCase("unlimited")) { + return -1; + } + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(0); + + ArrayList seconds = new ArrayList(this.getConfig().getStringList("seconds")); + ArrayList minutes = new ArrayList(this.getConfig().getStringList("minutes")); + ArrayList hours = new ArrayList(this.getConfig().getStringList("hours")); + ArrayList days = new ArrayList(this.getConfig().getStringList("days")); + ArrayList months = new ArrayList(this.getConfig().getStringList("months")); + ArrayList years = new ArrayList(this.getConfig().getStringList("years")); + + String durationString = duration.substring(duration.indexOf(' ')+1, duration.length()); + int durationInt = 0; + try { + durationInt = Integer.parseInt(duration.substring(0, duration.indexOf(' '))); + } catch(NumberFormatException exception) {} + + if(seconds.contains(durationString)) { + calendar.add(Calendar.SECOND, durationInt); + } else if(minutes.contains(durationString)) { + calendar.add(Calendar.MINUTE, durationInt); + } else if(hours.contains(durationString)) { + calendar.add(Calendar.HOUR, durationInt); + } else if(days.contains(durationString)) { + calendar.add(Calendar.DAY_OF_MONTH, durationInt); + } else if(months.contains(durationString)) { + calendar.add(Calendar.MONTH, durationInt); + } else if(years.contains(durationString)) { + calendar.add(Calendar.YEAR, durationInt); + } + return calendar.getTimeInMillis(); + } + + /** + * Get setting from config that could be only a number indicating seconds + * or a string indicating a duration string + * @param path Path of the setting to read + * @return milliseconds that the setting indicates + */ + public long getDurationFromSecondsOrString(String path) { + if(getConfig().isLong(path)) { + return getConfig().getLong(path)*1000; + } else { + return durationStringToLong(getConfig().getString(path)); + } + } + /** + * Get setting from config that could be only a number indicating minutes + * or a string indicating a duration string + * @param path Path of the setting to read + * @return milliseconds that the setting indicates + */ + public long getDurationFromMinutesOrString(String path) { + if(getConfig().isLong(path)) { + return getConfig().getLong(path)*60*1000; + } else { + return durationStringToLong(getConfig().getString(path)); + } + } + /** * Sends an debug message to the console * @param message The message that should be printed to the console @@ -692,7 +763,10 @@ public final class AreaShop extends JavaPlugin implements AreaShopInterface { if(AreaShop.getInstance().debug) { AreaShop.getInstance().getLogger().info("Debug: " + message); } - } + } + /** + * Non-static debug to use as implementation of the interface + */ public void debugI(String message) { AreaShop.debug(message); } diff --git a/AreaShop/src/main/java/nl/evolutioncoding/areashop/Utils.java b/AreaShop/src/main/java/nl/evolutioncoding/areashop/Utils.java index e0fdf89..18e2b54 100644 --- a/AreaShop/src/main/java/nl/evolutioncoding/areashop/Utils.java +++ b/AreaShop/src/main/java/nl/evolutioncoding/areashop/Utils.java @@ -78,6 +78,11 @@ public class Utils { return result; } + /** + * Create a comma-separated list + * @param input Collection of object which should be concatenated with comma's in between + * @return Innput object concatenated with comma's in between + */ public static String createCommaSeparatedList(Collection input) { String result = ""; boolean first = true; @@ -94,6 +99,15 @@ public class Utils { return result; } + /** + * Convert milliseconds to ticks + * @param milliseconds Milliseconds to convert + * @return milliseconds divided by 50 (20 ticks per second) + */ + public static long millisToTicks(long milliseconds) { + return milliseconds/50; + } + public static final BlockFace[] facings = { BlockFace.NORTH, BlockFace.NORTH_EAST, BlockFace.EAST, BlockFace.SOUTH_EAST, BlockFace.SOUTH, BlockFace.SOUTH_WEST, BlockFace.WEST, BlockFace.NORTH_WEST }; /** * Get the facing direction based on the yaw diff --git a/AreaShop/src/main/java/nl/evolutioncoding/areashop/listeners/PlayerLoginListener.java b/AreaShop/src/main/java/nl/evolutioncoding/areashop/listeners/PlayerLoginListener.java index 98e607c..6cadca0 100644 --- a/AreaShop/src/main/java/nl/evolutioncoding/areashop/listeners/PlayerLoginListener.java +++ b/AreaShop/src/main/java/nl/evolutioncoding/areashop/listeners/PlayerLoginListener.java @@ -63,7 +63,7 @@ public final class PlayerLoginListener implements Listener { if(warningSetting == null || warningSetting.isEmpty()) { continue; } - long warningTime = region.durationStringToLong(warningSetting); + long warningTime = plugin.durationStringToLong(warningSetting); if(region.getTimeLeft() < warningTime) { // Send the warning message later to let it appear after general MOTD messages AreaShop.getInstance().message(player, "rent-expireWarning", region); diff --git a/AreaShop/src/main/java/nl/evolutioncoding/areashop/regions/RentRegion.java b/AreaShop/src/main/java/nl/evolutioncoding/areashop/regions/RentRegion.java index 22fc2f9..aef0552 100644 --- a/AreaShop/src/main/java/nl/evolutioncoding/areashop/regions/RentRegion.java +++ b/AreaShop/src/main/java/nl/evolutioncoding/areashop/regions/RentRegion.java @@ -1,7 +1,6 @@ package nl.evolutioncoding.areashop.regions; import java.text.SimpleDateFormat; -import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.HashMap; @@ -227,46 +226,7 @@ public class RentRegion extends GeneralRegion { * @return The duration in milliseconds of 1 rent period */ public long getDuration() { - return durationStringToLong(getDurationString()); - } - - /** - * Methode to tranlate a duration string to a millisecond value - * @param duration The duration string - * @return The duration in milliseconds translated from the durationstring, or if it is invalid then 0 - */ - public long durationStringToLong(String duration) { - if(duration == null) { - return 0; - } - Calendar calendar = Calendar.getInstance(); - calendar.setTimeInMillis(0); - - ArrayList minutes = new ArrayList(plugin.getConfig().getStringList("minutes")); - ArrayList hours = new ArrayList(plugin.getConfig().getStringList("hours")); - ArrayList days = new ArrayList(plugin.getConfig().getStringList("days")); - ArrayList months = new ArrayList(plugin.getConfig().getStringList("months")); - ArrayList years = new ArrayList(plugin.getConfig().getStringList("years")); - - String durationString = duration.substring(duration.indexOf(' ')+1, duration.length()); - int durationInt = 0; - try { - durationInt = Integer.parseInt(duration.substring(0, duration.indexOf(' '))); - } catch(NumberFormatException exception) {} - - if(minutes.contains(durationString)) { - calendar.add(Calendar.MINUTE, durationInt); - } else if(hours.contains(durationString)) { - calendar.add(Calendar.HOUR, durationInt); - } else if(days.contains(durationString)) { - calendar.add(Calendar.DAY_OF_MONTH, durationInt); - } else if(months.contains(durationString)) { - calendar.add(Calendar.MONTH, durationInt); - } else if(years.contains(durationString)) { - calendar.add(Calendar.YEAR, durationInt); - } - - return calendar.getTimeInMillis(); + return plugin.durationStringToLong(getDurationString()); } /** @@ -302,7 +262,7 @@ public class RentRegion extends GeneralRegion { * @return The number of minutes until the region is unrented while player is offline */ public long getInactiveTimeUntilUnrent() { - return getLongSetting("rent.inactiveTimeUntilUnrent"); + return plugin.getDurationFromMinutesOrString("rent.inactiveTimeUntilUnrent"); } /** @@ -371,7 +331,7 @@ public class RentRegion extends GeneralRegion { * @return The maximum rent time in minutes */ public long getMaxRentTime() { - return this.getLongSetting("rent.maxRentTime"); + return plugin.getDurationFromMinutesOrString("rent.maxRentTime")/1000; } /** @@ -411,7 +371,7 @@ public class RentRegion extends GeneralRegion { return; } for(String timeBefore : section.getKeys(false)) { - long timeBeforeParsed = this.durationStringToLong(timeBefore); + long timeBeforeParsed = plugin.durationStringToLong(timeBefore); if(timeBeforeParsed <= 0) { return; } diff --git a/AreaShop/src/main/resources/config.yml b/AreaShop/src/main/resources/config.yml index 3aa918e..75b6bbc 100644 --- a/AreaShop/src/main/resources/config.yml +++ b/AreaShop/src/main/resources/config.yml @@ -71,8 +71,9 @@ timeFormatSign: 'dd-MM HH:mm' ## Timeformat used in the chat, default is like '30 june 2014 14:52', US format: 'MMMMMMMMMMMMMMMMM dd yyyy KK:mm a' ## Search for 'java SimpleDateFormat' for more options and documentation timeFormatChat: 'dd MMMMMMMMMMMMMMMMM yyyy HH:mm' -## Time indicators, used for specifing rent duration on the signs -## Be careful with deleting words, will break signs using those, adding is no problem +## Time indicators, used for specifing time periods (for example rent duration) +## Be careful with deleting words, will break settings using those, adding is no problem +seconds: [s, sec, secs, second, seconds] minutes: [m, min, mins, minute, minutes, minuten, minuut] hours: [h, hour, hours, uur, uren] days: [d, day, days, dag, dagen] @@ -295,55 +296,57 @@ expirationWarningProfiles: # ┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ # │ LIMITS AND TIMINGS: Options for limits and the frequencies for certain functions (be very carefull with these!) │ # └────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ -## Setting the 'delay' setting to 0 or lower will prevent the taks from runnning, if you don't use a certain feature you -## could switch the task off this way. - ## Maximum number of blocks to save to or restore from a .schemetic maximumBlocks: 1000000 ## Maximum number of locations the teleport function should check to find a safe spot maximumTries: 50000 + +## Setting the 'delay' setting to 0 or lower will prevent the taks from runnning, if you don't use a certain feature you +## could switch the task off this way. +## The 'delay' settings are specified using a number followed by one of the identifiers as defined above in the config +## The 'regionsPerTick' settings are to configure how fast something goes, lower value is less lag, but slower updates +## There are 20 ticks in 1 second, so if you set 'regionPerTick' to 5, then 5*20=100 regions per second will be updated ## Timings for saving files that need saving saving: ## Time between saving files that need to be saved (not all files will be saved by default) - delay: 10 + delay: '10 minutes' ## How many regions per tick will be saved regionsPerTick: 1 ## Timings for rent expiration checking expiration: - ## Time between checking if rent regions have been expired (seconds) - delay: 59 - ## Number of rent regions to check per tick (there are 20 ticks in a second) + ## Time between checking if rent regions have been expired + delay: '59 seconds' + ## Number of rent regions to check per tick regionsPerTick: 5 ## Timings for expiration warning to online players expireWarning: ## The time between checking if expiration warning need to be sent - delay: 5 - ## Number of regions to update per tick (there are 20 ticks in a second) + delay: '5 minutes' + ## Number of regions to update per tick regionsPerTick: 3 ## Timings for updating signs and region flags ('/as reload' or after '/as groupadd' or '/as groupdel') update: - ## Number of regions to update per tick (there are 20 ticks in a second) + ## Number of regions to update per tick regionsPerTick: 5 -## Time between checking if any regions need to be unrented because the player was not online for the specified time period (minutes) -## -1 means never, 1440 is one day, 43200 is one month, 525600 is one year +## Time between checking if any regions need to be unrented because the player was not online for the specified time period inactive: - ## Time between checking if a regions needs to be unrented/sold because of inactivity by the player (minutes) - delay: 15 - ## Number of rent regions to check per tick (there are 20 ticks in a second) + ## Time between checking if a regions needs to be unrented/sold because of inactivity by the player + delay: '15 minutes' + ## Number of rent regions to check per tick regionsPerTick: 5 ## Timings for the periodic updating of signs (for timeleft tags etc) signs: - ## Time between updates of all signs (seconds) - delay: 60 - ## Number of regions to update signs for per tick (there are 20 ticks in a second) + ## Time between updates of all signs + delay: '60 seconds' + ## Number of regions to update signs for per tick regionsPerTick: 3 ## Number of regions per tick to check when a player joins to see if his name changed for regions he owned (updates the regions when changed) nameupdate: - ## Number of regions to update signs for per tick (there are 20 ticks in a second) + ## Number of regions to update signs for per tick regionsPerTick: 5 ## Timings for adding regions to AreaShop ('/as stack') adding: - ## Number of regions to add per tick (there are 20 ticks in a second) + ## Number of regions to add per tick regionsPerTick: 2 diff --git a/AreaShop/src/main/resources/default.yml b/AreaShop/src/main/resources/default.yml index d6ab397..ca649c7 100644 --- a/AreaShop/src/main/resources/default.yml +++ b/AreaShop/src/main/resources/default.yml @@ -50,13 +50,13 @@ rent: moneyBack: 100 ## Maximum number of extends a player can do (-1 for infinite, 0 for no extending) maxExtends: -1 - ## The Maximum time they can have the region rented in advance (minutes, -1 for infinite) + ## The Maximum time they can have the region rented in advance (use times like '1 day' etc, or 'unlimited' for no limit) ## If you set this lower then the rent duration then players will not be able to rent the region - ## (because it always exceeds the maxRentTime in that case) - maxRentTime: -1 + ## (because it always exceeds the maxRentTime in that case) + maxRentTime: 'unlimited' ## Automatically unrent the region after the specified number of minutes between the last login time of the renter and the current time - ## -1 means never, 1440 is one day, 43200 is one month, 525600 is one year - inactiveTimeUntilUnrent: -1 + ## Use times like '1 day' etc, or 'disabled' for never + inactiveTimeUntilUnrent: 'disabled' ## If a region of a player has less then this time left when he joins the server he will get a warning ## You can find all time indicators in config.yml below the RENTING header, change to '' to disable warningOnLoginTime: '1 day'