diff --git a/src/main/java/com/graywolf336/jail/JailMain.java b/src/main/java/com/graywolf336/jail/JailMain.java index 2b6957a..54f486f 100644 --- a/src/main/java/com/graywolf336/jail/JailMain.java +++ b/src/main/java/com/graywolf336/jail/JailMain.java @@ -25,6 +25,7 @@ public class JailMain extends JavaPlugin { private HandCuffManager hcm; private JailIO io; private JailManager jm; + private JailTimer jt; private PrisonerManager pm; private boolean debug = false; @@ -47,12 +48,13 @@ public class JailMain extends JavaPlugin { plm.registerEvents(new HandCuffListener(this), this); plm.registerEvents(new PlayerListener(this), this); + jt = new JailTimer(this); + debug = getConfig().getBoolean(Settings.DEBUG.getPath()); if(debug) getLogger().info("Debugging enabled."); - //For the time, we will use: - //http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/TimeUnit.html#convert(long, java.util.concurrent.TimeUnit) + getLogger().info("Successfully loaded, is our impression."); } public void onDisable() { @@ -60,6 +62,10 @@ public class JailMain extends JavaPlugin { for(Jail j : jm.getJails()) io.saveJail(j); + if(jt != null) + if(jt.getTimer() != null) + jt.getTimer().stop(); + cmdHand = null; pm = null; jm = null; diff --git a/src/main/java/com/graywolf336/jail/JailTimer.java b/src/main/java/com/graywolf336/jail/JailTimer.java new file mode 100644 index 0000000..9dcf366 --- /dev/null +++ b/src/main/java/com/graywolf336/jail/JailTimer.java @@ -0,0 +1,110 @@ +package com.graywolf336.jail; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.Timer; + +import org.bukkit.entity.Player; + +import com.graywolf336.jail.beans.Jail; +import com.graywolf336.jail.beans.Prisoner; +import com.graywolf336.jail.enums.LangString; +import com.graywolf336.jail.enums.Settings; + +/** + * Contains all the logic for counting down the time of the prisoners time. + * + * @author graywolf336 + * @since 2.x.x + * @version 3.0.0 + * + */ +public class JailTimer { + private JailMain pl; + private Timer timer; + private Long lastTime; + + public JailTimer(JailMain plugin) { + this.pl = plugin; + + this.lastTime = System.currentTimeMillis(); + if(pl.getConfig().getBoolean(Settings.USEBUKKITTIMER.getPath())) { + pl.getServer().getScheduler().scheduleSyncRepeatingTask(pl, new TimeEvent(), 20, 20); + }else { + timer = new Timer(1000, new ActionListener () { + public void actionPerformed (ActionEvent event) { + pl.getServer().getScheduler().scheduleSyncDelayedTask(pl, new TimeEvent()); + }; + }); + + timer.start(); + } + } + + /** Returns the instance of this timer. */ + public Timer getTimer() { + return this.timer; + } + + class TimeEvent implements Runnable { + public void run() { + long timePassed; + + //Let's check if more than 10 seconds has passed since the + //last time we checked + if (System.currentTimeMillis() - lastTime >= 10000) { + //set the time passed to the current time minus the last time we checked + timePassed = System.currentTimeMillis() - lastTime; + lastTime = System.currentTimeMillis(); + }else { + //Less than 10 seconds has past when we last ran this + //so let's wait till the next round before we do this + return; + } + + for(Jail j : pl.getJailManager().getJails()) { + for(Prisoner p : j.getAllPrisoners()) { + //only execute this code if the prisoner's time is more than 0 milliseconds + if(p.getRemainingTime() > 0) { + Player player = pl.getServer().getPlayerExact(p.getName()); + + //Check if the player is offline + if(player == null) { + //if they are offline AND the config has counting down the time + //while the prisoner is offline, then let's do it + if(pl.getConfig().getBoolean(Settings.COUNTDOWNTIMEOFFLINE.getPath())) { + //Set their remaining time but if it is less than zero, set it to zero + p.setRemainingTime(Math.max(0, p.getRemainingTime() - timePassed)); + if(p.getRemainingTime() == 0) pl.getPrisonerManager().releasePrisoner(player, p); + } + }else { + //The prisoner isn't offline, so let's count down + //Set their remaining time but if it is less than zero, set it to zero + p.setRemainingTime(Math.max(0, p.getRemainingTime() - timePassed)); + if(p.getRemainingTime() == 0) pl.getPrisonerManager().releasePrisoner(player, p); + + //Now, let's set and check their afk time + //add the time passed to their current afk time + try { + long afk = Util.getTime(pl.getConfig().getString(Settings.MAXAFKTIME.getPath())); + if(afk > 0) { + p.setAFKTime(p.getAFKTime() + timePassed); + if(p.getAFKTime() > afk) { + p.setAFKTime(0); + player.kickPlayer(pl.getJailIO().getLanguageString(LangString.AFKKICKMESSAGE)); + } + } + } catch (Exception e) { + pl.getLogger().severe("Error while processing the max afk time: " + e.getMessage()); + } + } + } + } + + //Save all the prisoners and jails after we're done + pl.getJailIO().saveJail(j); + } + } + } +} diff --git a/src/main/java/com/graywolf336/jail/PrisonerManager.java b/src/main/java/com/graywolf336/jail/PrisonerManager.java index ce693f6..09d02cf 100644 --- a/src/main/java/com/graywolf336/jail/PrisonerManager.java +++ b/src/main/java/com/graywolf336/jail/PrisonerManager.java @@ -267,6 +267,28 @@ public class PrisonerManager { pl.getJailIO().saveJail(jail); } + /** + * Release the given prisoner from jailing, does the checks if they are offline or not. + * + * @param player we are releasing, can be null and if so they'll be treated as offline. + * @param prisoner data to handle. + */ + public void releasePrisoner(Player player, Prisoner prisoner) { + if(player == null) { + prisoner.setOfflinePending(true); + prisoner.setRemainingTime(0); + }else { + Jail j = pl.getJailManager().getJailPlayerIsIn(player.getName()); + + try { + unJail(j, j.getCellPrisonerIsIn(player.getName()), player, prisoner); + }catch(Exception e) { + pl.getLogger().severe("Unable to unjail the prisoner " + player.getName() + " because '" + e.getMessage() + "'."); + } + + } + } + /** * Unjails a prisoner from jail, removing all their data. * diff --git a/src/main/java/com/graywolf336/jail/Util.java b/src/main/java/com/graywolf336/jail/Util.java index 2a94217..80bb065 100644 --- a/src/main/java/com/graywolf336/jail/Util.java +++ b/src/main/java/com/graywolf336/jail/Util.java @@ -93,6 +93,8 @@ public class Util { * @throws Exception if there are no matches */ public static Long getTime(String time) throws Exception { + if(time.equalsIgnoreCase("-1")) return -1L; + Long t = 10L; Matcher match = DURATION_PATTERN.matcher(time); diff --git a/src/main/java/com/graywolf336/jail/beans/Prisoner.java b/src/main/java/com/graywolf336/jail/beans/Prisoner.java index 4b748ce..70ab5ad 100644 --- a/src/main/java/com/graywolf336/jail/beans/Prisoner.java +++ b/src/main/java/com/graywolf336/jail/beans/Prisoner.java @@ -11,12 +11,12 @@ import org.bukkit.Location; * * @author graywolf336 * @since 2.x.x - * @version 2.0.1 + * @version 3.0.0 */ public class Prisoner { private String name, reason, inventory, armor; private boolean muted, offlinePending, teleporting; - private long time; + private long time, afk; private Location previousPosition; private GameMode previousGameMode; @@ -38,6 +38,7 @@ public class Prisoner { this.previousGameMode = GameMode.SURVIVAL; this.inventory = ""; this.armor = ""; + this.afk = 0; } /** Gets the name of this player. */ @@ -175,4 +176,14 @@ public class Prisoner { public void setArmor(String armor) { this.armor = armor; } + + /** Gets the time, in milliseconds, this prisoner has been afk. */ + public long getAFKTime() { + return this.afk; + } + + /** Sets the time, in milliseconds, this prisoner has been afk. */ + public void setAFKTime(long time) { + this.afk = time; + } } diff --git a/src/main/java/com/graywolf336/jail/enums/LangString.java b/src/main/java/com/graywolf336/jail/enums/LangString.java index 555d782..9447899 100644 --- a/src/main/java/com/graywolf336/jail/enums/LangString.java +++ b/src/main/java/com/graywolf336/jail/enums/LangString.java @@ -3,6 +3,8 @@ package com.graywolf336.jail.enums; public enum LangString { //Jailing section + /** The message displayed when players are kicked for being afk. */ + AFKKICKMESSAGE ("jailing"), /** The message sent when we broadcast/log the message for time below -1. */ BROADCASTMESSAGEFOREVER ("jailing"), /** The message sent when we broadcast/log the message for any time above -1. */ diff --git a/src/main/java/com/graywolf336/jail/enums/Settings.java b/src/main/java/com/graywolf336/jail/enums/Settings.java index 5b65d22..c6b2d3b 100644 --- a/src/main/java/com/graywolf336/jail/enums/Settings.java +++ b/src/main/java/com/graywolf336/jail/enums/Settings.java @@ -4,6 +4,7 @@ public enum Settings { BROADCASTJAILING("jailing.jail.broadcastJailing"), COMMANDSONJAIL("jailing.jail.commands"), COMMANDSONRELEASE("jailing.release.commands"), + COUNTDOWNTIMEOFFLINE("jailing.during.countDownTimeWhileOffline"), DEBUG("system.debug"), DEFAULTJAIL("jailing.jail.defaultJail"), DELETEINVENTORY("jailing.jail.deleteInventory"), @@ -12,12 +13,14 @@ public enum Settings { JAILEDGAMEMODE("jailing.jail.gameMode"), JAILEDSTOREINVENTORY("jailing.jail.storeInventory"), LOGJAILING("jailing.jail.logToConsole"), + MAXAFKTIME("jailing.during.maxAFKTime"), MAXFOODLEVEL("jailing.during.maxFoodLevel"), MINFOODLEVEL("jailing.during.minFoodLevel"), RELEASETOPREVIOUSPOSITION("jailing.release.backToPreviousPosition"), RESTOREPREVIOUSGAMEMODE("jailing.release.restorePreviousGameMode"), TELEPORTONRELEASE("jailing.release.teleport"), - UPDATENOTIFICATIONS("system.updateNotifications"); + UPDATENOTIFICATIONS("system.updateNotifications"), + USEBUKKITTIMER("system.useBukkitTimer"); private String path; diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 600a59f..499e7b6 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -3,6 +3,7 @@ system: debug: false language: 'en' updateNotifications: true + useBukkitTimer: true storage: type: 'flatfile' #can be flatfile, sqlite, or mysql mysql: @@ -14,6 +15,7 @@ jailing: during: countDownTimeWhileOffline: false ignoreSleeping: true + maxAFKTime: 10m #in minutes maxFoodLevel: 20 minFoodLevel: 10 openChest: true diff --git a/src/main/resources/en.yml b/src/main/resources/en.yml index ad2a353..c744ffd 100644 --- a/src/main/resources/en.yml +++ b/src/main/resources/en.yml @@ -2,6 +2,7 @@ language: general: playernotonline: '&cThat player is not online!' jailing: + afkkickmessage: '&cYou can not be afk while being jailed.' broadcastmessageforever: '&9%0% has been jailed forever.' broadcastmessageforminutes: '&9%0% has been jailed for %1% minutes.' cantbejailed: '&cThat player can not be jailed.'