From a6f5a2eb5eaf21f4b6fc3718913e18b7f74d2abc Mon Sep 17 00:00:00 2001 From: Thijs Wiefferink Date: Sat, 15 Nov 2014 21:57:26 +0100 Subject: [PATCH] Expire warning on login for players that rent regions + bugfixes An option has been added to show a warning to players when their rent almost runs out on login (so when they login they get a message for each region that almost runs out, that means lower timeleft than the 'warningOnLoginTime' setting). Also a bug with schematic restoring has been fixed and some other minor changes done. --- default.yml | 5 +- lang/EN.yml | 1 + src/nl/evolutioncoding/areashop/AreaShop.java | 6 +- .../evolutioncoding/areashop/FileManager.java | 16 +++- .../areashop/PlayerLoginListener.java | 93 +++++++++++++++++++ .../areashop/SignBreakListener.java | 3 + .../areashop/SignChangeListener.java | 5 +- .../areashop/regions/RentRegion.java | 21 ++++- 8 files changed, 142 insertions(+), 8 deletions(-) create mode 100644 src/nl/evolutioncoding/areashop/PlayerLoginListener.java diff --git a/default.yml b/default.yml index d3eae07..61a489c 100644 --- a/default.yml +++ b/default.yml @@ -36,7 +36,7 @@ general: rent: ## The default price of a renting region price: 1000 - ## The default duration of a renting region + ## The default duration of a renting region, you can find all time indicators in config.yml below the RENTING header duration: '1 day' ## The percentage of the renting price you get back if you unrent the region (price of time left will be multiplied by this/100) moneyBack: 100 @@ -48,6 +48,9 @@ rent: ## 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 + ## 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' # ┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ # │ BUY: Options for buy regions │ diff --git a/lang/EN.yml b/lang/EN.yml index 5656864..73fe37f 100644 --- a/lang/EN.yml +++ b/lang/EN.yml @@ -67,6 +67,7 @@ rent-maxExtends: "You cannot extend this rent anymore (the maximum is %0% times) rent-maxRentTime: "You cannot rent this region more time in advance, the maximum time is %0% minutes and you have currently rented it for %1% minutes" rent-restrictedToWorld: "You need to be in the '%0%' world to rent this region (you are in '%1%')" rent-restrictedToRegion: "You need to be inside '%0%' to rent it" +rent-loginExpireWarning: "Your region %region% has only %timeleft% left, be sure to extend it if you want to keep it" buy-help: "/as buy [regionname], the region you stand in will be used if not specified" buy-noPermission: "You don't have permission to buy a region" diff --git a/src/nl/evolutioncoding/areashop/AreaShop.java b/src/nl/evolutioncoding/areashop/AreaShop.java index 6957722..41be31e 100644 --- a/src/nl/evolutioncoding/areashop/AreaShop.java +++ b/src/nl/evolutioncoding/areashop/AreaShop.java @@ -128,6 +128,7 @@ public final class AreaShop extends JavaPlugin { this.getServer().getPluginManager().registerEvents(new SignChangeListener(this), this); this.getServer().getPluginManager().registerEvents(new SignBreakListener(this), this); this.getServer().getPluginManager().registerEvents(new SignClickListener(this), this); + this.getServer().getPluginManager().registerEvents(new PlayerLoginListener(this), this); setupTasks(); @@ -148,14 +149,17 @@ public final class AreaShop extends JavaPlugin { * Called on shutdown or reload of the server */ public void onDisable() { - fileManager.saveRequiredFiles(); + fileManager.saveRequiredFilesAtOnce(); Bukkit.getServer().getScheduler().cancelTasks(this); /* set variables to null to prevent memory leaks */ worldGuard = null; + worldEdit = null; economy = null; fileManager = null; languageManager = null; + commandManager = null; + chatprefix = null; debug = false; } diff --git a/src/nl/evolutioncoding/areashop/FileManager.java b/src/nl/evolutioncoding/areashop/FileManager.java index 15ae576..8f3920b 100644 --- a/src/nl/evolutioncoding/areashop/FileManager.java +++ b/src/nl/evolutioncoding/areashop/FileManager.java @@ -465,7 +465,7 @@ public class FileManager { /** - * Save all region related files + * Save all region related files spread over time (low load) */ public void saveRequiredFiles() { if(isSaveGroupsRequired()) { @@ -493,6 +493,20 @@ public class FileManager { }.runTaskTimer(plugin, 1, 1); } + /** + * Save all region related files directly (only for cases like onDisable()) + */ + public void saveRequiredFilesAtOnce() { + if(isSaveGroupsRequired()) { + saveGroupsNow(); + } + for(GeneralRegion region : getRegions()) { + if(region.isSaveRequired()) { + region.saveNow(); + } + } + } + public String getRegionFolder() { return regionsPath; } diff --git a/src/nl/evolutioncoding/areashop/PlayerLoginListener.java b/src/nl/evolutioncoding/areashop/PlayerLoginListener.java new file mode 100644 index 0000000..8e58b97 --- /dev/null +++ b/src/nl/evolutioncoding/areashop/PlayerLoginListener.java @@ -0,0 +1,93 @@ +package nl.evolutioncoding.areashop; + +import nl.evolutioncoding.areashop.regions.RentRegion; + +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerLoginEvent; +import org.bukkit.event.player.PlayerLoginEvent.Result; +import org.bukkit.scheduler.BukkitRunnable; + +/** + * Checks for placement of signs for this plugin + * @author NLThijs48 + */ +public final class PlayerLoginListener implements Listener { + AreaShop plugin; + + /** + * Constructor + * @param plugin The AreaShop plugin + */ + public PlayerLoginListener(AreaShop plugin) { + this.plugin = plugin; + } + + /** + * Called when a sign is changed + * @param event The event + */ + @EventHandler(priority = EventPriority.MONITOR) + public void onPlayerLogin(PlayerLoginEvent event) { + if(event.getResult() != Result.ALLOWED) { + return; + } + Player player = event.getPlayer(); + + for(RentRegion region : plugin.getFileManager().getRents()) { + if(region.isRenter(player)) { + String warningSetting = region.getStringSetting("rent.warningOnLoginTime"); + if(warningSetting == null || warningSetting.isEmpty()) { + continue; + } + long warningTime = region.durationStringToLong(warningSetting); + if(region.getTimeLeft() < warningTime) { + // Send the warning message later to let it appear after general MOTD messages + final Player finalPlayer = player; + final AreaShop finalPlugin = plugin; + final RentRegion finalRegion = region; + new BukkitRunnable() { + @Override + public void run() { + finalPlugin.message(finalPlayer, "rent-loginExpireWarning", finalRegion); + } + }.runTaskLater(plugin, 2); + } + } + } + } +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/nl/evolutioncoding/areashop/SignBreakListener.java b/src/nl/evolutioncoding/areashop/SignBreakListener.java index 75ce180..d73e4bd 100644 --- a/src/nl/evolutioncoding/areashop/SignBreakListener.java +++ b/src/nl/evolutioncoding/areashop/SignBreakListener.java @@ -35,6 +35,9 @@ public final class SignBreakListener implements Listener { */ @EventHandler(priority = EventPriority.HIGH) public void onSignBreak(BlockBreakEvent event) { + if(event.isCancelled()) { + return; + } Block block = event.getBlock(); /* Check if it is a sign */ if(block.getType() == Material.WALL_SIGN || block.getType() == Material.SIGN_POST) { diff --git a/src/nl/evolutioncoding/areashop/SignChangeListener.java b/src/nl/evolutioncoding/areashop/SignChangeListener.java index 6645c60..be8f7e4 100644 --- a/src/nl/evolutioncoding/areashop/SignChangeListener.java +++ b/src/nl/evolutioncoding/areashop/SignChangeListener.java @@ -39,8 +39,11 @@ public final class SignChangeListener implements Listener { * Called when a sign is changed * @param event The event */ - @EventHandler(priority = EventPriority.HIGH) + @EventHandler(priority = EventPriority.MONITOR) public void onSignChange(SignChangeEvent event) { + if(event.isCancelled()) { + return; + } Player player = event.getPlayer(); // Check if the sign is meant for this plugin diff --git a/src/nl/evolutioncoding/areashop/regions/RentRegion.java b/src/nl/evolutioncoding/areashop/regions/RentRegion.java index 3a2547d..d4bf346 100644 --- a/src/nl/evolutioncoding/areashop/regions/RentRegion.java +++ b/src/nl/evolutioncoding/areashop/regions/RentRegion.java @@ -203,9 +203,19 @@ public class RentRegion extends GeneralRegion { * Get the duration of 1 rent period * @return The duration in milliseconds of 1 rent period */ - public long getDuration() { - /* Get the time until the region will be rented */ - String duration = getDurationString(); + 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); @@ -216,7 +226,10 @@ public class RentRegion extends GeneralRegion { ArrayList years = new ArrayList(plugin.getConfig().getStringList("years")); String durationString = duration.substring(duration.indexOf(' ')+1, duration.length()); - int durationInt = Integer.parseInt(duration.substring(0, duration.indexOf(' '))); + int durationInt = 0; + try { + durationInt = Integer.parseInt(duration.substring(0, duration.indexOf(' '))); + } catch(NumberFormatException exception) {} if(minutes.contains(durationString)) { calendar.add(Calendar.MINUTE, durationInt);