From b1f4b7bb41dd5e52b8d4175d654805ed07245fa7 Mon Sep 17 00:00:00 2001 From: graywolf336 Date: Sat, 1 Feb 2014 11:52:50 -0600 Subject: [PATCH] Relocate the move protection so that we only register it if enabled. This way we save on a tiny bit of performance when it is disabled. --- .../java/com/graywolf336/jail/JailMain.java | 11 +++ .../listeners/MoveProtectionListener.java | 86 +++++++++++++++++++ .../jail/listeners/ProtectionListener.java | 68 --------------- 3 files changed, 97 insertions(+), 68 deletions(-) create mode 100644 src/main/java/com/graywolf336/jail/listeners/MoveProtectionListener.java diff --git a/src/main/java/com/graywolf336/jail/JailMain.java b/src/main/java/com/graywolf336/jail/JailMain.java index 2bf0d44..e1628ee 100644 --- a/src/main/java/com/graywolf336/jail/JailMain.java +++ b/src/main/java/com/graywolf336/jail/JailMain.java @@ -12,6 +12,7 @@ import com.graywolf336.jail.enums.Settings; import com.graywolf336.jail.listeners.BlockListener; import com.graywolf336.jail.listeners.EntityListener; import com.graywolf336.jail.listeners.HandCuffListener; +import com.graywolf336.jail.listeners.MoveProtectionListener; import com.graywolf336.jail.listeners.PlayerListener; import com.graywolf336.jail.listeners.ProtectionListener; @@ -53,6 +54,16 @@ public class JailMain extends JavaPlugin { plm.registerEvents(new PlayerListener(this), this); plm.registerEvents(new ProtectionListener(this), this); + //Only register the move protection listener if this is enabled in the + //config when we first start the plugin. The reason for this change is + //that the move event is called a ton of times per single move and so + //not registering this event listener will hopefully safe some performance. + //But doing this also forces people to restart their server if they to + //enable it after disabling it. + if(getConfig().getBoolean(Settings.MOVEPROTECTION.getPath())) { + plm.registerEvents(new MoveProtectionListener(this), this); + } + jt = new JailTimer(this); debug = getConfig().getBoolean(Settings.DEBUG.getPath()); diff --git a/src/main/java/com/graywolf336/jail/listeners/MoveProtectionListener.java b/src/main/java/com/graywolf336/jail/listeners/MoveProtectionListener.java new file mode 100644 index 0000000..71673d0 --- /dev/null +++ b/src/main/java/com/graywolf336/jail/listeners/MoveProtectionListener.java @@ -0,0 +1,86 @@ +package com.graywolf336.jail.listeners; + +import java.util.concurrent.TimeUnit; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerMoveEvent; +import org.bukkit.event.player.PlayerTeleportEvent; + +import com.graywolf336.jail.JailMain; +import com.graywolf336.jail.Util; +import com.graywolf336.jail.beans.Jail; +import com.graywolf336.jail.beans.Prisoner; +import com.graywolf336.jail.enums.LangString; +import com.graywolf336.jail.enums.Settings; + +public class MoveProtectionListener implements Listener { + private JailMain pl; + + public MoveProtectionListener(JailMain plugin) { + this.pl = plugin; + } + + @EventHandler(ignoreCancelled=true) + public void moveProtection(PlayerMoveEvent event) { + //If we have the move protection enabled, then let's do it. + //Other wise we don't need to deal with it. + if(pl.getConfig().getBoolean(Settings.MOVEPROTECTION.getPath())) { + //Let's be sure the player we're dealing with is in jail + if(pl.getJailManager().isPlayerJailed(event.getPlayer().getName())) { + Jail j = pl.getJailManager().getJailPlayerIsIn(event.getPlayer().getName()); + Prisoner p = j.getPrisoner(event.getPlayer().getName()); + + //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); + + //If the event's to location is NOT inside the jail, then let's do some action. + //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 (!j.isInside(event.getTo())) { + try { + long add = Util.getTime(pl.getConfig().getString(Settings.MOVEPENALTY.getPath())); + pl.getJailManager().getPrisoner(event.getPlayer().getName()).addTime(add); + + String msg = ""; + if(add == 0L) { + //Generate the protection message, provide the method with one argument + //which is the thing we are protecting against + msg = pl.getJailIO().getLanguageString(LangString.PROTECTIONMESSAGENOPENALTY, pl.getJailIO().getLanguageString(LangString.MOVING)); + }else { + //Generate the protection message, provide the method with two arguments + //First is the time in minutes and second is the thing we are protecting against + 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) { + 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().getName())) { + event.setTo(j.getCellPrisonerIsIn(event.getPlayer().getName()).getTeleport()); + }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()); + moveProtection(move); + } +} diff --git a/src/main/java/com/graywolf336/jail/listeners/ProtectionListener.java b/src/main/java/com/graywolf336/jail/listeners/ProtectionListener.java index 124eddf..be1e426 100644 --- a/src/main/java/com/graywolf336/jail/listeners/ProtectionListener.java +++ b/src/main/java/com/graywolf336/jail/listeners/ProtectionListener.java @@ -11,13 +11,9 @@ import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.event.player.PlayerMoveEvent; -import org.bukkit.event.player.PlayerTeleportEvent; import com.graywolf336.jail.JailMain; import com.graywolf336.jail.Util; -import com.graywolf336.jail.beans.Jail; -import com.graywolf336.jail.beans.Prisoner; import com.graywolf336.jail.enums.LangString; import com.graywolf336.jail.enums.Settings; @@ -320,68 +316,4 @@ public class ProtectionListener implements Listener { } } } - - @EventHandler(ignoreCancelled=true) - public void moveProtection(PlayerMoveEvent event) { - //If we have the move protection enabled, then let's do it. - //Other wise we don't need to deal with it. - //TODO: Should probably figure out how to not register this, as this is called so many times per step - if(pl.getConfig().getBoolean(Settings.MOVEPROTECTION.getPath())) { - //Let's be sure the player we're dealing with is in jail - if(pl.getJailManager().isPlayerJailed(event.getPlayer().getName())) { - Jail j = pl.getJailManager().getJailPlayerIsIn(event.getPlayer().getName()); - Prisoner p = j.getPrisoner(event.getPlayer().getName()); - - //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); - - //If the event's to location is NOT inside the jail, then let's do some action. - //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 (!j.isInside(event.getTo())) { - try { - long add = Util.getTime(pl.getConfig().getString(Settings.MOVEPENALTY.getPath())); - pl.getJailManager().getPrisoner(event.getPlayer().getName()).addTime(add); - - String msg = ""; - if(add == 0L) { - //Generate the protection message, provide the method with one argument - //which is the thing we are protecting against - msg = pl.getJailIO().getLanguageString(LangString.PROTECTIONMESSAGENOPENALTY, pl.getJailIO().getLanguageString(LangString.MOVING)); - }else { - //Generate the protection message, provide the method with two arguments - //First is the time in minutes and second is the thing we are protecting against - 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) { - 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().getName())) { - event.setTo(j.getCellPrisonerIsIn(event.getPlayer().getName()).getTeleport()); - }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()); - moveProtection(move); - } }