Move the timer to be async if we're using bukkit's timer.

This should improve the performance of the plugin even more, because it
won't do any database calls while inside the loop which is what was
usually the hang up time.

This is completely untested and possibly could throw tons of errors.
This commit is contained in:
graywolf336 2014-07-21 19:11:12 -05:00
parent e4f32559b6
commit b63773c7a9
6 changed files with 54 additions and 28 deletions

View File

@ -45,7 +45,6 @@ Changes
ToDo
===
* When calculating the reducing of time, make it async and all the calls it does be scheduled
* Jail set
* Jail vote
* Jailing for swearing

View File

@ -336,7 +336,7 @@ public class JailManager {
if(j != null) {
for(Prisoner p : j.getAllPrisoners().values()) {
getPlugin().getPrisonerManager().releasePrisoner(getPlugin().getServer().getPlayer(p.getUUID()), p);
getPlugin().getPrisonerManager().schedulePrisonerRelease(p);
}
return getPlugin().getJailIO().getLanguageString(LangString.PRISONERSCLEARED, j.getName());
@ -360,7 +360,7 @@ public class JailManager {
}else {
for(Jail j : getJails()) {
for(Prisoner p : j.getAllPrisoners().values()) {
getPlugin().getPrisonerManager().releasePrisoner(getPlugin().getServer().getPlayer(p.getUUID()), p);
getPlugin().getPrisonerManager().schedulePrisonerRelease(p);
}
}

View File

@ -24,14 +24,20 @@ public class JailTimer {
private JailMain pl;
private Timer timer;
private Long lastTime;
private Long afkTime = 0L;
public JailTimer(JailMain plugin) {
this.pl = plugin;
try {
afkTime = Util.getTime(pl.getConfig().getString(Settings.MAXAFKTIME.getPath()));
} catch (Exception e) {
pl.getLogger().severe("Error while processing the max afk time: " + e.getMessage());
}
this.lastTime = System.currentTimeMillis();
if(pl.getConfig().getBoolean(Settings.USEBUKKITTIMER.getPath())) {
pl.getLogger().info("Using the Bukkit Scheduler.");
pl.getServer().getScheduler().scheduleSyncRepeatingTask(pl, new TimeEvent(), 200, 200);
pl.getServer().getScheduler().runTaskTimerAsynchronously(pl, new TimeEvent(), 200, 200);
}else {
pl.getLogger().info("Using the Java Timer.");
timer = new Timer(10000, new ActionListener () {
@ -66,7 +72,8 @@ public class JailTimer {
for(Jail j : pl.getJailManager().getJails()) {
for(Prisoner p : j.getAllPrisoners().values()) {
//only execute this code if the prisoner's time is more than 0 milliseconds
if(p.getRemainingTime() > 0) {
//and they don't have any offline pending things
if(p.getRemainingTime() > 0 && !p.isOfflinePending()) {
Player player = pl.getServer().getPlayer(p.getUUID());
//Check if the player is offline
@ -76,28 +83,21 @@ public class JailTimer {
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);
if(p.getRemainingTime() == 0) pl.getPrisonerManager().schedulePrisonerRelease(p);
}
}else {
if(afkTime > 0) {
p.setAFKTime(p.getAFKTime() + timePassed);
if(p.getAFKTime() > afkTime) {
p.setAFKTime(0);
player.kickPlayer(pl.getJailIO().getLanguageString(LangString.AFKKICKMESSAGE));
}
}
//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());
}
if(p.getRemainingTime() == 0) pl.getPrisonerManager().schedulePrisonerRelease(p);
}
}
}

View File

@ -1,5 +1,6 @@
package com.graywolf336.jail;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@ -29,9 +30,18 @@ import com.graywolf336.jail.events.PrisonerTransferredEvent;
*/
public class PrisonerManager {
private JailMain pl;
private ArrayList<Prisoner> releases;
public PrisonerManager(JailMain plugin) {
this.pl = plugin;
this.releases = new ArrayList<Prisoner>();
// Schedule the releasing of prisoners
plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
public void run() {
releaseScheduledPrisoners();
}
}, 100L, 20L);
}
/**
@ -302,13 +312,30 @@ public class PrisonerManager {
pl.getServer().getPluginManager().callEvent(event);
}
/**
* Schedules a prisoner to be released.
*
* @param prisoner to be released.
*/
public void schedulePrisonerRelease(Prisoner prisoner) {
releases.add(prisoner);
}
private void releaseScheduledPrisoners() {
ArrayList<Prisoner> lettingGo = new ArrayList<Prisoner>(releases);
for(Prisoner p : lettingGo) {
releases.remove(p);
releasePrisoner(pl.getServer().getPlayer(p.getUUID()), p);
}
}
/**
* 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) {
private void releasePrisoner(Player player, Prisoner prisoner) {
if(player == null) {
prisoner.setOfflinePending(true);
prisoner.setRemainingTime(0);

View File

@ -99,7 +99,7 @@ public class JailPayCommand implements Command {
if(amt >= bill) {
pm.pay((Player) sender, bill);
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.PAYPAIDRELEASED, String.valueOf(bill)));
jm.getPlugin().getPrisonerManager().releasePrisoner((Player) sender, p);
jm.getPlugin().getPrisonerManager().schedulePrisonerRelease(p);
}else {
long minutes = pm.getMinutesPayingFor(amt);
pm.pay((Player) sender, amt);
@ -112,7 +112,7 @@ public class JailPayCommand implements Command {
if(amt >= bill) {
pm.pay((Player) sender, bill);
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.PAYPAIDRELEASED, String.valueOf(bill)));
jm.getPlugin().getPrisonerManager().releasePrisoner((Player) sender, p);
jm.getPlugin().getPrisonerManager().schedulePrisonerRelease(p);
}else {
//You haven't provided enough money to get them out
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.PAYNOTENOUGHMONEYPROVIDED));
@ -169,7 +169,7 @@ public class JailPayCommand implements Command {
if(amt >= bill) {
pm.pay((Player) sender, bill);
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.PAYPAIDRELEASEDELSE, new String[] { String.valueOf(bill), p.getLastKnownName() }));
jm.getPlugin().getPrisonerManager().releasePrisoner(jm.getPlugin().getServer().getPlayer(p.getUUID()), p);
jm.getPlugin().getPrisonerManager().schedulePrisonerRelease(p);
}else {
long minutes = pm.getMinutesPayingFor(amt);
pm.pay((Player) sender, amt);
@ -182,7 +182,7 @@ public class JailPayCommand implements Command {
if(amt >= bill) {
pm.pay((Player) sender, bill);
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.PAYPAIDRELEASEDELSE, new String[] { String.valueOf(bill), p.getLastKnownName() }));
jm.getPlugin().getPrisonerManager().releasePrisoner(jm.getPlugin().getServer().getPlayer(p.getUUID()), p);
jm.getPlugin().getPrisonerManager().schedulePrisonerRelease(p);
}else {
//You haven't provided enough money to get them out
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.PAYNOTENOUGHMONEYPROVIDED));

View File

@ -108,7 +108,7 @@ public class PlayerListener implements Listener {
if(p.isOfflinePending()) {
if(p.getRemainingTime() == 0L) {
//If their remaining time is 0, let's unjail them
pl.getPrisonerManager().releasePrisoner(event.getPlayer(), p);
pl.getPrisonerManager().schedulePrisonerRelease(p);
}else if(p.isToBeTransferred()) {
Cell c = j.getCellPrisonerIsIn(event.getPlayer().getUniqueId());