From 2cea7387c986211ccc5034c88e9ed2443dec15b5 Mon Sep 17 00:00:00 2001 From: DNx5 Date: Sun, 20 Sep 2015 21:25:29 +0700 Subject: [PATCH] set all listeners to use checkAuth method. --- src/main/java/fr/xephi/authme/Utils.java | 18 +++ .../authme/listener/AuthMeBlockListener.java | 43 +------ .../listener/AuthMeChestShopListener.java | 20 +-- .../authme/listener/AuthMeEntityListener.java | 115 +++--------------- .../authme/listener/AuthMePlayerListener.java | 49 +++----- 5 files changed, 52 insertions(+), 193 deletions(-) diff --git a/src/main/java/fr/xephi/authme/Utils.java b/src/main/java/fr/xephi/authme/Utils.java index 2ebb65373..9540e448e 100644 --- a/src/main/java/fr/xephi/authme/Utils.java +++ b/src/main/java/fr/xephi/authme/Utils.java @@ -1,5 +1,6 @@ package fr.xephi.authme; +import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.cache.limbo.LimboCache; import fr.xephi.authme.cache.limbo.LimboPlayer; import fr.xephi.authme.events.AuthMeTeleportEvent; @@ -107,6 +108,23 @@ public class Utils { } } + // TODO: Move to a Manager + public static boolean checkAuth(Player player) { + if (player == null || Utils.isUnrestricted(player)) { + return true; + } + String name = player.getName().toLowerCase(); + if (PlayerCache.getInstance().isAuthenticated(name)) { + return true; + } + if (!plugin.database.isAuthAvailable(name)) { + if (!Settings.isForcedRegistrationEnabled) { + return true; + } + } + return false; + } + public static boolean isUnrestricted(Player player) { return Settings.isAllowRestrictedIp && !(Settings.getUnrestrictedName == null || Settings.getUnrestrictedName.isEmpty()) && (Settings.getUnrestrictedName.contains(player.getName())); } diff --git a/src/main/java/fr/xephi/authme/listener/AuthMeBlockListener.java b/src/main/java/fr/xephi/authme/listener/AuthMeBlockListener.java index e6903ce34..ca5f68d31 100644 --- a/src/main/java/fr/xephi/authme/listener/AuthMeBlockListener.java +++ b/src/main/java/fr/xephi/authme/listener/AuthMeBlockListener.java @@ -2,9 +2,6 @@ package fr.xephi.authme.listener; import fr.xephi.authme.AuthMe; import fr.xephi.authme.Utils; -import fr.xephi.authme.cache.auth.PlayerCache; -import fr.xephi.authme.settings.Settings; -import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.BlockBreakEvent; @@ -21,51 +18,15 @@ public class AuthMeBlockListener implements Listener { @EventHandler(ignoreCancelled = true) public void onBlockPlace(BlockPlaceEvent event) { - if (event.getPlayer() == null) { + if (Utils.checkAuth(event.getPlayer())) return; - } - - Player player = event.getPlayer(); - String name = player.getName().toLowerCase(); - - if (Utils.isUnrestricted(player)) { - return; - } - - if (PlayerCache.getInstance().isAuthenticated(name)) { - return; - } - - if (!instance.database.isAuthAvailable(name)) { - if (!Settings.isForcedRegistrationEnabled) { - return; - } - } event.setCancelled(true); } @EventHandler(ignoreCancelled = true) public void onBlockBreak(BlockBreakEvent event) { - if (event.getPlayer() == null) { + if (Utils.checkAuth(event.getPlayer())) return; - } - - Player player = event.getPlayer(); - String name = player.getName().toLowerCase(); - - if (Utils.isUnrestricted(player)) { - return; - } - - if (PlayerCache.getInstance().isAuthenticated(name)) { - return; - } - - if (!instance.database.isAuthAvailable(name)) { - if (!Settings.isForcedRegistrationEnabled) { - return; - } - } event.setCancelled(true); } diff --git a/src/main/java/fr/xephi/authme/listener/AuthMeChestShopListener.java b/src/main/java/fr/xephi/authme/listener/AuthMeChestShopListener.java index 3577dc341..cf551a638 100644 --- a/src/main/java/fr/xephi/authme/listener/AuthMeChestShopListener.java +++ b/src/main/java/fr/xephi/authme/listener/AuthMeChestShopListener.java @@ -21,26 +21,8 @@ public class AuthMeChestShopListener implements Listener { @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST) public void onPreTransaction(PreTransactionEvent event) { - if (event.getClient() == null) { + if (Utils.checkAuth(event.getClient())) return; - } - - Player player = event.getClient(); - String name = player.getName().toLowerCase(); - - if (Utils.isUnrestricted(player)) { - return; - } - - if (PlayerCache.getInstance().isAuthenticated(name)) { - return; - } - - if (!plugin.database.isAuthAvailable(name)) { - if (!Settings.isForcedRegistrationEnabled) { - return; - } - } event.setCancelled(TransactionOutcome.OTHER); } } diff --git a/src/main/java/fr/xephi/authme/listener/AuthMeEntityListener.java b/src/main/java/fr/xephi/authme/listener/AuthMeEntityListener.java index f85e24971..109fe83d9 100644 --- a/src/main/java/fr/xephi/authme/listener/AuthMeEntityListener.java +++ b/src/main/java/fr/xephi/authme/listener/AuthMeEntityListener.java @@ -2,8 +2,6 @@ package fr.xephi.authme.listener; import fr.xephi.authme.AuthMe; import fr.xephi.authme.Utils; -import fr.xephi.authme.cache.auth.PlayerCache; -import fr.xephi.authme.settings.Settings; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -22,30 +20,15 @@ public class AuthMeEntityListener implements Listener { @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST) public void onEntityDamage(EntityDamageEvent event) { Entity entity = event.getEntity(); - - if (!(entity instanceof Player)) { + if (entity == null || !(entity instanceof Player)) { return; } - if (Utils.isUnrestricted((Player) entity)) { - return; - } - - if (Utils.isNPC(entity)) - return; - Player player = (Player) entity; - String name = player.getName().toLowerCase(); - - if (PlayerCache.getInstance().isAuthenticated(name)) { + if (Utils.checkAuth(player)) { return; } - if (!instance.database.isAuthAvailable(name)) { - if (!Settings.isForcedRegistrationEnabled) { - return; - } - } player.setFireTicks(0); event.setDamage(0.0); event.setCancelled(true); @@ -53,28 +36,15 @@ public class AuthMeEntityListener implements Listener { @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST) public void onEntityTarget(EntityTargetEvent event) { - if (event.getTarget() == null) - return; Entity entity = event.getTarget(); - if (!(entity instanceof Player)) { + if (entity == null || !(entity instanceof Player)) { return; } - if (Utils.isNPC(entity)) - return; - - Player player = (Player) entity; - String name = player.getName().toLowerCase(); - - if (PlayerCache.getInstance().isAuthenticated(name)) { + if (Utils.checkAuth((Player) entity)) { return; } - if (!instance.database.isAuthAvailable(name)) { - if (!Settings.isForcedRegistrationEnabled) { - return; - } - } event.setTarget(null); event.setCancelled(true); } @@ -82,75 +52,40 @@ public class AuthMeEntityListener implements Listener { @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST) public void onDmg(EntityDamageByEntityEvent event) { Entity entity = event.getDamager(); - if (entity == null || !(entity instanceof Player)) { return; } - Player player = (Player) entity; - String name = player.getName().toLowerCase(); - - if (PlayerCache.getInstance().isAuthenticated(name)) { + if (Utils.checkAuth((Player) entity)) { return; } - if (!instance.database.isAuthAvailable(name)) { - if (!Settings.isForcedRegistrationEnabled) { - return; - } - } - event.setCancelled(true); } @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST) public void onFoodLevelChange(FoodLevelChangeEvent event) { Entity entity = event.getEntity(); - if (!(entity instanceof Player)) { + if (entity == null || !(entity instanceof Player)) { return; } - if (Utils.isNPC(entity)) + if (Utils.checkAuth((Player) entity)) { return; - - Player player = (Player) entity; - String name = player.getName().toLowerCase(); - - if (PlayerCache.getInstance().isAuthenticated(name)) { - return; - } - - if (!instance.database.isAuthAvailable(name)) { - if (!Settings.isForcedRegistrationEnabled) { - return; - } } event.setCancelled(true); - } @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST) public void entityRegainHealthEvent(EntityRegainHealthEvent event) { Entity entity = event.getEntity(); - if (!(entity instanceof Player)) { + if (entity == null || !(entity instanceof Player)) { return; } - if (Utils.isNPC(entity)) + if (Utils.checkAuth((Player) entity)) { return; - - Player player = (Player) entity; - String name = player.getName().toLowerCase(); - - if (PlayerCache.getInstance().isAuthenticated(name)) { - return; - } - - if (!instance.database.isAuthAvailable(name)) { - if (!Settings.isForcedRegistrationEnabled) { - return; - } } event.setAmount(0.0); @@ -159,49 +94,29 @@ public class AuthMeEntityListener implements Listener { @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) public void onEntityInteract(EntityInteractEvent event) { - if (!(event.getEntity() instanceof Player)) { + Entity entity = event.getEntity(); + if (entity == null || !(entity instanceof Player)) { return; } - Player player = (Player) event.getEntity(); - String name = player.getName().toLowerCase(); - - if (Utils.isUnrestricted(player) || Utils.isNPC(player)) - return; - - if (PlayerCache.getInstance().isAuthenticated(player.getName())) { + if (Utils.checkAuth((Player) entity)) { return; } - if (!instance.database.isAuthAvailable(name)) { - if (!Settings.isForcedRegistrationEnabled) { - return; - } - } event.setCancelled(true); } @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST) public void onLowestEntityInteract(EntityInteractEvent event) { - if (!(event.getEntity() instanceof Player)) { + Entity entity = event.getEntity(); + if (entity == null || !(entity instanceof Player)) { return; } - Player player = (Player) event.getEntity(); - String name = player.getName().toLowerCase(); - - if (Utils.isUnrestricted(player) || Utils.isNPC(player)) - return; - - if (PlayerCache.getInstance().isAuthenticated(player.getName())) { + if (Utils.checkAuth((Player) entity)) { return; } - if (!instance.database.isAuthAvailable(name)) { - if (!Settings.isForcedRegistrationEnabled) { - return; - } - } event.setCancelled(true); } } diff --git a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java index 9d53fb7c7..390154e52 100644 --- a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java +++ b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java @@ -45,26 +45,9 @@ public class AuthMePlayerListener implements Listener { this.plugin = plugin; } - private boolean checkAuth(Player player) { - if (player == null) return true; - String name = player.getName().toLowerCase(); - if (Utils.isUnrestricted(player)) { - return true; - } - if (PlayerCache.getInstance().isAuthenticated(name)) { - return true; - } - if (!plugin.database.isAuthAvailable(name)) { - if (!Settings.isForcedRegistrationEnabled) { - return true; - } - } - return false; - } - private void handleChat(AsyncPlayerChatEvent event) { Player player = event.getPlayer(); - if (!checkAuth(player)) { + if (!Utils.checkAuth(player)) { String cmd = event.getMessage().split(" ")[0]; if (!Settings.isChatAllowed && !(Settings.allowCommands.contains(cmd))) { event.setCancelled(true); @@ -83,7 +66,7 @@ public class AuthMePlayerListener implements Listener { @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST) public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) { - if (checkAuth(event.getPlayer())) + if (Utils.checkAuth(event.getPlayer())) return; String msg = event.getMessage(); @@ -135,7 +118,7 @@ public class AuthMePlayerListener implements Listener { @EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR) public void onPlayerMove(PlayerMoveEvent event) { Player player = event.getPlayer(); - if (checkAuth(player)) + if (Utils.checkAuth(player)) return; if (!Settings.isMovementAllowed) { @@ -410,21 +393,21 @@ public class AuthMePlayerListener implements Listener { @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) public void onPlayerPickupItem(PlayerPickupItemEvent event) { - if (checkAuth(event.getPlayer())) + if (Utils.checkAuth(event.getPlayer())) return; event.setCancelled(true); } - @EventHandler(ignoreCancelled = true, priority = EventPriority.NORMAL) + @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) public void onPlayerInteract(PlayerInteractEvent event) { - if (checkAuth(event.getPlayer())) + if (Utils.checkAuth(event.getPlayer())) return; event.setCancelled(true); } @EventHandler(priority = EventPriority.NORMAL) public void onPlayerConsumeItem(PlayerItemConsumeEvent event) { - if (checkAuth(event.getPlayer())) + if (Utils.checkAuth(event.getPlayer())) return; event.setCancelled(true); } @@ -432,7 +415,7 @@ public class AuthMePlayerListener implements Listener { @EventHandler(priority = EventPriority.HIGHEST) public void onPlayerInventoryOpen(InventoryOpenEvent event) { final Player player = (Player) event.getPlayer(); - if (checkAuth(player)) + if (Utils.checkAuth(player)) return; event.setCancelled(true); @@ -456,7 +439,7 @@ public class AuthMePlayerListener implements Listener { return; if (!(event.getWhoClicked() instanceof Player)) return; - if (checkAuth((Player) event.getWhoClicked())) + if (Utils.checkAuth((Player) event.getWhoClicked())) return; event.setCancelled(true); } @@ -467,7 +450,7 @@ public class AuthMePlayerListener implements Listener { if (!(damager instanceof Player)) { return; } - if (checkAuth((Player) damager)) + if (Utils.checkAuth((Player) damager)) return; event.setCancelled(true); @@ -475,28 +458,28 @@ public class AuthMePlayerListener implements Listener { @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST) public void onPlayerInteractEntity(PlayerInteractEntityEvent event) { - if (checkAuth(event.getPlayer())) + if (Utils.checkAuth(event.getPlayer())) return; event.setCancelled(true); } @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST) public void onPlayerDropItem(PlayerDropItemEvent event) { - if (checkAuth(event.getPlayer())) + if (Utils.checkAuth(event.getPlayer())) return; event.setCancelled(true); } @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST) public void onPlayerBedEnter(PlayerBedEnterEvent event) { - if (checkAuth(event.getPlayer())) + if (Utils.checkAuth(event.getPlayer())) return; event.setCancelled(true); } @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST) public void onSignChange(SignChangeEvent event) { - if (checkAuth(event.getPlayer())) + if (Utils.checkAuth(event.getPlayer())) return; event.setCancelled(true); } @@ -504,7 +487,7 @@ public class AuthMePlayerListener implements Listener { @EventHandler(priority = EventPriority.HIGHEST) public void onPlayerRespawn(PlayerRespawnEvent event) { Player player = event.getPlayer(); - if (checkAuth(player)) + if (Utils.checkAuth(player)) return; String name = player.getName().toLowerCase(); Location spawn = plugin.getSpawnLocation(player); @@ -524,7 +507,7 @@ public class AuthMePlayerListener implements Listener { return; if (plugin.authmePermissible(player, "authme.bypassforcesurvival")) return; - if (checkAuth(player)) + if (Utils.checkAuth(player)) return; String name = player.getName().toLowerCase();