diff --git a/src/main/java/com/graywolf336/jail/JailMain.java b/src/main/java/com/graywolf336/jail/JailMain.java index 5559e18..eebdec0 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.command.JailHandler; import com.graywolf336.jail.enums.Settings; import com.graywolf336.jail.legacy.LegacyManager; import com.graywolf336.jail.listeners.BlockListener; +import com.graywolf336.jail.listeners.CacheListener; import com.graywolf336.jail.listeners.EntityListener; import com.graywolf336.jail.listeners.HandCuffListener; import com.graywolf336.jail.listeners.JailingListener; @@ -79,6 +80,7 @@ public class JailMain extends JavaPlugin { PluginManager plm = this.getServer().getPluginManager(); plm.registerEvents(new BlockListener(this), this); + plm.registerEvents(new CacheListener(this), this); plm.registerEvents(new EntityListener(this), this); plm.registerEvents(new HandCuffListener(this), this); plm.registerEvents(new JailingListener(this), this); diff --git a/src/main/java/com/graywolf336/jail/JailManager.java b/src/main/java/com/graywolf336/jail/JailManager.java index 27dfb59..b6bc633 100644 --- a/src/main/java/com/graywolf336/jail/JailManager.java +++ b/src/main/java/com/graywolf336/jail/JailManager.java @@ -9,6 +9,7 @@ import org.bukkit.Location; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import com.graywolf336.jail.beans.CachePrisoner; import com.graywolf336.jail.beans.Cell; import com.graywolf336.jail.beans.ConfirmPlayer; import com.graywolf336.jail.beans.CreationPlayer; @@ -42,6 +43,7 @@ public class JailManager { private HashMap jailCreators; private HashMap cellCreators; private HashMap confirms; + private HashMap cache; private JailCreationSteps jcs; private CellCreationSteps ccs; @@ -51,6 +53,7 @@ public class JailManager { this.jailCreators = new HashMap(); this.cellCreators = new HashMap(); this.confirms = new HashMap(); + this.cache = new HashMap(); this.jcs = new JailCreationSteps(); this.ccs = new CellCreationSteps(); } @@ -169,6 +172,36 @@ public class JailManager { return cells; } + /** + * Adds a prisoner to the cache. + * + * @param cache object to store + * @return The same object given + */ + public CachePrisoner addCacheObject(CachePrisoner cache) { + this.cache.put(cache.getPrisoner().getUUID(), cache); + return cache; + } + + /** + * Checks if the given uuid is in the cache. + * + * @param uuid of the player + * @return true if in cache, false if not + */ + public boolean inCache(UUID uuid) { + return this.cache.containsKey(uuid); + } + + /** + * Removes the cache object stored for this uuid. + * + * @param uuid of the prisoner to remove + */ + public void removeCacheObject(UUID uuid) { + this.cache.remove(uuid); + } + /** * Gets all the prisoners in the system, best for a system wide count of the prisoners or accessing all the prisoners at once. * @@ -191,17 +224,22 @@ public class JailManager { */ public Jail getJailPrisonerIsIn(Prisoner prisoner) { if(prisoner == null) return null; - - return getJailPlayerIsIn(prisoner.getUUID()); + else return getJailPlayerIsIn(prisoner.getUUID()); } /** * Gets the {@link Jail jail} the given player is in. * + *

+ * + * Checks the cache first. + * * @param uuid The uuid of the player who's jail we are getting. * @return The jail the player is in, CAN BE NULL. */ public Jail getJailPlayerIsIn(UUID uuid) { + if(this.cache.containsKey(uuid)) return this.cache.get(uuid).getJail(); + for(Jail j : jails.values()) if(j.isPlayerJailed(uuid)) return j; diff --git a/src/main/java/com/graywolf336/jail/beans/CachePrisoner.java b/src/main/java/com/graywolf336/jail/beans/CachePrisoner.java new file mode 100644 index 0000000..9aa8853 --- /dev/null +++ b/src/main/java/com/graywolf336/jail/beans/CachePrisoner.java @@ -0,0 +1,28 @@ +package com.graywolf336.jail.beans; + +/** + * An object for storing online cached prisoners. + * + * @author graywolf336 + * @since 3.0.0 + * @version 1.0.0 + */ +public class CachePrisoner { + private Jail jail; + private Prisoner p; + + public CachePrisoner(Jail jail, Prisoner prisoner) { + this.jail = jail; + this.p = prisoner; + } + + /** Gets the Jail this cache is in. */ + public Jail getJail() { + return this.jail; + } + + /** Gets the Prisoner in this cache. */ + public Prisoner getPrisoner() { + return this.p; + } +} diff --git a/src/main/java/com/graywolf336/jail/listeners/CacheListener.java b/src/main/java/com/graywolf336/jail/listeners/CacheListener.java new file mode 100644 index 0000000..13f7656 --- /dev/null +++ b/src/main/java/com/graywolf336/jail/listeners/CacheListener.java @@ -0,0 +1,73 @@ +package com.graywolf336.jail.listeners; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerKickEvent; +import org.bukkit.event.player.PlayerQuitEvent; + +import com.graywolf336.jail.JailMain; +import com.graywolf336.jail.beans.CachePrisoner; +import com.graywolf336.jail.beans.Jail; +import com.graywolf336.jail.beans.Prisoner; +import com.graywolf336.jail.events.PrePrisonerTransferredEvent; +import com.graywolf336.jail.events.PrisonerJailedEvent; +import com.graywolf336.jail.events.PrisonerReleasedEvent; +import com.graywolf336.jail.events.PrisonerTransferredEvent; + +public class CacheListener implements Listener { + private JailMain pl; + + public CacheListener(JailMain plugin) { + this.pl = plugin; + } + + @EventHandler(priority = EventPriority.LOWEST) + public void joinListener(PlayerJoinEvent event) { + if(pl.getJailManager().isPlayerJailed(event.getPlayer().getUniqueId())) { + Jail j = pl.getJailManager().getJailPlayerIsIn(event.getPlayer().getUniqueId()); + Prisoner p = j.getPrisoner(event.getPlayer().getUniqueId()); + + pl.getJailManager().addCacheObject(new CachePrisoner(j, p)); + } + } + + @EventHandler(priority = EventPriority.LOWEST) + public void leaveListener(PlayerQuitEvent event) { + if(pl.getJailManager().inCache(event.getPlayer().getUniqueId())) { + pl.getJailManager().removeCacheObject(event.getPlayer().getUniqueId()); + } + } + + @EventHandler(priority = EventPriority.LOWEST) + public void kickListener(PlayerKickEvent event) { + if(pl.getJailManager().inCache(event.getPlayer().getUniqueId())) { + pl.getJailManager().removeCacheObject(event.getPlayer().getUniqueId()); + } + } + + @EventHandler(priority = EventPriority.LOWEST) + public void jailListener(PrisonerJailedEvent event) { + pl.getJailManager().addCacheObject(new CachePrisoner(event.getJail(), event.getPrisoner())); + } + + @EventHandler(priority = EventPriority.LOWEST) + public void releaseListener(PrisonerReleasedEvent event) { + if(pl.getJailManager().inCache(event.getPlayer().getUniqueId())) { + pl.getJailManager().removeCacheObject(event.getPlayer().getUniqueId()); + } + } + + @EventHandler(priority = EventPriority.LOWEST) + public void beforeTransferringListener(PrePrisonerTransferredEvent event) { + if(pl.getJailManager().inCache(event.getPlayer().getUniqueId())) { + pl.getJailManager().removeCacheObject(event.getPlayer().getUniqueId()); + } + } + + @EventHandler(priority = EventPriority.LOWEST) + public void transferListener(PrisonerTransferredEvent event) { + pl.getJailManager().addCacheObject(new CachePrisoner(event.getTargetJail(), event.getPrisoner())); + } +} diff --git a/src/main/java/com/graywolf336/jail/listeners/PlayerListener.java b/src/main/java/com/graywolf336/jail/listeners/PlayerListener.java index 9aa9a97..b46cf88 100644 --- a/src/main/java/com/graywolf336/jail/listeners/PlayerListener.java +++ b/src/main/java/com/graywolf336/jail/listeners/PlayerListener.java @@ -85,7 +85,7 @@ public class PlayerListener implements Listener { } } - @EventHandler + @EventHandler(priority = EventPriority.HIGHEST) public void checkForOfflineJailStuff(PlayerJoinEvent event) { //Let's check if the player is jailed if(pl.getJailManager().isPlayerJailed(event.getPlayer().getUniqueId())) {