Get the session during the gamemode event lazily.

This commit is contained in:
sk89q 2015-05-28 11:28:02 -07:00
parent ec5bb32510
commit a277e69e9c

View File

@ -31,6 +31,7 @@
import com.sk89q.worldguard.protection.flags.DefaultFlag; import com.sk89q.worldguard.protection.flags.DefaultFlag;
import com.sk89q.worldguard.protection.regions.ProtectedRegion; import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import com.sk89q.worldguard.session.MoveType; import com.sk89q.worldguard.session.MoveType;
import com.sk89q.worldguard.session.Session;
import com.sk89q.worldguard.session.handler.GameModeFlag; import com.sk89q.worldguard.session.handler.GameModeFlag;
import com.sk89q.worldguard.util.command.CommandFilter; import com.sk89q.worldguard.util.command.CommandFilter;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
@ -84,12 +85,15 @@ public void registerEvents() {
public void onPlayerGameModeChange(PlayerGameModeChangeEvent event) { public void onPlayerGameModeChange(PlayerGameModeChangeEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
WorldConfiguration wcfg = plugin.getGlobalStateManager().get(player.getWorld()); WorldConfiguration wcfg = plugin.getGlobalStateManager().get(player.getWorld());
GameModeFlag handler = plugin.getSessionManager().get(player).getHandler(GameModeFlag.class); Session session = plugin.getSessionManager().getIfPresent(player);
if (handler != null && wcfg.useRegions && !plugin.getGlobalRegionManager().hasBypass(player, player.getWorld())) { if (session != null) {
GameMode expected = handler.getSetGameMode(); GameModeFlag handler = session.getHandler(GameModeFlag.class);
if (handler.getOriginalGameMode() != null && expected != null && expected != event.getNewGameMode()) { if (handler != null && wcfg.useRegions && !plugin.getGlobalRegionManager().hasBypass(player, player.getWorld())) {
log.info("Game mode change on " + player.getName() + " has been blocked due to the region GAMEMODE flag"); GameMode expected = handler.getSetGameMode();
event.setCancelled(true); if (handler.getOriginalGameMode() != null && expected != null && expected != event.getNewGameMode()) {
log.info("Game mode change on " + player.getName() + " has been blocked due to the region GAMEMODE flag");
event.setCancelled(true);
}
} }
} }
} }