Performance changes to get better performance on the move protection.

We now get the cache object inside the move event and get all the other
objects from that. The reason for this is so that we don't have to then
loop through all the prisoners in a jail again to get one prisoner, just
get it from the cache object.
This commit is contained in:
graywolf336 2014-06-12 10:30:00 -05:00
parent a442887b36
commit a77e0cc472
3 changed files with 758 additions and 744 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,87 +1,89 @@
package com.graywolf336.jail.listeners; package com.graywolf336.jail.listeners;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.event.player.PlayerTeleportEvent;
import com.graywolf336.jail.JailMain; import com.graywolf336.jail.JailMain;
import com.graywolf336.jail.Util; import com.graywolf336.jail.Util;
import com.graywolf336.jail.beans.Jail; import com.graywolf336.jail.beans.CachePrisoner;
import com.graywolf336.jail.beans.Prisoner; import com.graywolf336.jail.beans.Jail;
import com.graywolf336.jail.enums.LangString; import com.graywolf336.jail.beans.Prisoner;
import com.graywolf336.jail.enums.Settings; import com.graywolf336.jail.enums.LangString;
import com.graywolf336.jail.enums.Settings;
public class MoveProtectionListener implements Listener {
private JailMain pl; public class MoveProtectionListener implements Listener {
private JailMain pl;
public MoveProtectionListener(JailMain plugin) {
this.pl = plugin; public MoveProtectionListener(JailMain plugin) {
} this.pl = plugin;
}
@EventHandler(ignoreCancelled=true)
public void moveProtection(PlayerMoveEvent event) { @EventHandler(ignoreCancelled=true)
//If we have the move protection enabled, then let's do it. public void moveProtection(PlayerMoveEvent event) {
//Other wise we don't need to deal with it. //If we have the move protection enabled, then let's do it.
if(pl.getConfig().getBoolean(Settings.MOVEPROTECTION.getPath())) { //Other wise we don't need to deal with it.
//Let's be sure the player we're dealing with is in jail if(pl.getConfig().getBoolean(Settings.MOVEPROTECTION.getPath())) {
//Let's be sure the player we're dealing with is in jail
Jail j = pl.getJailManager().getJailPlayerIsIn(event.getPlayer().getUniqueId());
if(j != null) { CachePrisoner cp = pl.getJailManager().getCacheObject(event.getPlayer().getUniqueId());
Prisoner p = j.getPrisoner(event.getPlayer().getUniqueId()); if(cp != null) {
Jail j = cp.getJail();
//If the player is being teleported, let's ignore it Prisoner p = cp.getPrisoner();
if(p.isTeleporting()) {
return; //If the player is being teleported, let's ignore it
} if(p.isTeleporting()) {
return;
//They moved, so they're no longer afk }
p.setAFKTime(0L);
//They moved, so they're no longer afk
//If the event's to location is NOT inside the jail, then let's do some action. p.setAFKTime(0L);
//For right now, we're only going to apply the time. Later we're going to do
//the guards, but first get a beta version out. //If the event's to location is NOT inside the jail, then let's do some action.
if (!j.isInside(event.getTo())) { //For right now, we're only going to apply the time. Later we're going to do
try { //the guards, but first get a beta version out.
long add = Util.getTime(pl.getConfig().getString(Settings.MOVEPENALTY.getPath())); if (!j.isInside(event.getTo())) {
p.addTime(add); try {
long add = Util.getTime(pl.getConfig().getString(Settings.MOVEPENALTY.getPath()));
String msg = ""; p.addTime(add);
if(add == 0L) {
//Generate the protection message, provide the method with one argument String msg = "";
//which is the thing we are protecting against if(add == 0L) {
msg = pl.getJailIO().getLanguageString(LangString.PROTECTIONMESSAGENOPENALTY, pl.getJailIO().getLanguageString(LangString.MOVING)); //Generate the protection message, provide the method with one argument
}else { //which is the thing we are protecting against
//Generate the protection message, provide the method with two arguments msg = pl.getJailIO().getLanguageString(LangString.PROTECTIONMESSAGENOPENALTY, pl.getJailIO().getLanguageString(LangString.MOVING));
//First is the time in minutes and second is the thing we are protecting against }else {
msg = pl.getJailIO().getLanguageString(LangString.PROTECTIONMESSAGE, //Generate the protection message, provide the method with two arguments
new String[] { String.valueOf(TimeUnit.MINUTES.convert(add, TimeUnit.MILLISECONDS)), //First is the time in minutes and second is the thing we are protecting against
pl.getJailIO().getLanguageString(LangString.MOVING) }); msg = pl.getJailIO().getLanguageString(LangString.PROTECTIONMESSAGE,
} new String[] { String.valueOf(TimeUnit.MINUTES.convert(add, TimeUnit.MILLISECONDS)),
pl.getJailIO().getLanguageString(LangString.MOVING) });
//Send the message }
event.getPlayer().sendMessage(msg);
}catch(Exception e) { //Send the message
pl.getLogger().severe("Moving (escaping) outside a jail penalty time is in the wrong format, please fix."); event.getPlayer().sendMessage(msg);
} }catch(Exception e) {
pl.getLogger().severe("Moving (escaping) outside a jail penalty time is in the wrong format, please fix.");
//If the prisoner is in a cell, then let's teleport them to the cell's in location }
if(j.isJailedInACell(event.getPlayer().getUniqueId())) {
event.setTo(j.getCellPrisonerIsIn(event.getPlayer().getUniqueId()).getTeleport()); //If the prisoner is in a cell, then let's teleport them to the cell's in location
}else { if(j.isJailedInACell(event.getPlayer().getUniqueId())) {
//Otherwise let's teleport them to the in location of the jail event.setTo(j.getCellPrisonerIsIn(event.getPlayer().getUniqueId()).getTeleport());
event.setTo(j.getTeleportIn()); }else {
} //Otherwise let's teleport them to the in location of the jail
} event.setTo(j.getTeleportIn());
} }
} }
} }
}
@EventHandler(ignoreCancelled=true) }
public void onPlayerTeleport(PlayerTeleportEvent event) {
PlayerMoveEvent move = new PlayerMoveEvent(event.getPlayer(), event.getFrom(), event.getTo()); @EventHandler(ignoreCancelled=true)
moveProtection(move); public void onPlayerTeleport(PlayerTeleportEvent event) {
} PlayerMoveEvent move = new PlayerMoveEvent(event.getPlayer(), event.getFrom(), event.getTo());
} moveProtection(move);
}
}

View File

@ -24,6 +24,7 @@ import org.powermock.modules.junit4.PowerMockRunner;
import com.carrotsearch.junitbenchmarks.AbstractBenchmark; import com.carrotsearch.junitbenchmarks.AbstractBenchmark;
import com.carrotsearch.junitbenchmarks.BenchmarkOptions; import com.carrotsearch.junitbenchmarks.BenchmarkOptions;
import com.graywolf336.jail.JailMain; import com.graywolf336.jail.JailMain;
import com.graywolf336.jail.beans.CachePrisoner;
import com.graywolf336.jail.beans.Jail; import com.graywolf336.jail.beans.Jail;
import com.graywolf336.jail.beans.Prisoner; import com.graywolf336.jail.beans.Prisoner;
@ -61,6 +62,7 @@ public class BenchmarkTest extends AbstractBenchmark {
main.getPrisonerManager().prepareJail(main.getJailManager().getJail("testingJail"), null, null, new Prisoner(i == 555 ? use.toString() : UUID.randomUUID().toString(), "mockPlayer" + i, true, 100000L, "testJailer", "Test jailing " + i)); main.getPrisonerManager().prepareJail(main.getJailManager().getJail("testingJail"), null, null, new Prisoner(i == 555 ? use.toString() : UUID.randomUUID().toString(), "mockPlayer" + i, true, 100000L, "testJailer", "Test jailing " + i));
} }
main.getJailManager().addCacheObject(new CachePrisoner(main.getJailManager().getJailPlayerIsIn(use), main.getJailManager().getPrisoner(use)));
r = new Random(); r = new Random();
} }