Simplify listener classes

- Remove unused fields
- Use Service methods where possible
This commit is contained in:
ljacqu 2015-12-26 21:26:19 +01:00
parent 47e97244a7
commit f37fc32717
7 changed files with 40 additions and 98 deletions

View File

@ -362,21 +362,21 @@ public class AuthMe extends JavaPlugin {
// Register event listeners
pluginManager.registerEvents(new AuthMePlayerListener(this), this);
pluginManager.registerEvents(new AuthMeBlockListener(this), this);
pluginManager.registerEvents(new AuthMeEntityListener(this), this);
pluginManager.registerEvents(new AuthMeBlockListener(), this);
pluginManager.registerEvents(new AuthMeEntityListener(), this);
pluginManager.registerEvents(new AuthMeServerListener(this), this);
// Try to register 1.6 player listeners
try {
Class.forName("org.bukkit.event.player.PlayerEditBookEvent");
pluginManager.registerEvents(new AuthMePlayerListener16(this), this);
pluginManager.registerEvents(new AuthMePlayerListener16(), this);
} catch (ClassNotFoundException ignore) {
}
// Try to register 1.8 player listeners
try {
Class.forName("org.bukkit.event.player.PlayerInteractAtEntityEvent");
pluginManager.registerEvents(new AuthMePlayerListener18(this), this);
pluginManager.registerEvents(new AuthMePlayerListener18(), this);
} catch (ClassNotFoundException ignore) {
}
}

View File

@ -1,63 +1,24 @@
package fr.xephi.authme.listener;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.util.Utils;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
/**
*/
public class AuthMeBlockListener implements Listener {
public final AuthMe instance;
/**
* Constructor for AuthMeBlockListener.
*
* @param instance AuthMe
*/
public AuthMeBlockListener(AuthMe instance) {
this.instance = instance;
}
/**
* Method onBlockPlace.
*
* @param event BlockPlaceEvent
*/
@EventHandler(ignoreCancelled = true)
public void onBlockPlace(BlockPlaceEvent event) {
if (Utils.checkAuth(event.getPlayer())) {
return;
if (ListenerService.shouldCancelEvent(event.getPlayer())) {
event.setCancelled(true);
}
// TODO: npc status can be used to bypass security!!!
if (Utils.isNPC(event.getPlayer())) {
return;
}
event.setCancelled(true);
}
/**
* Method onBlockBreak.
*
* @param event BlockBreakEvent
*/
@EventHandler(ignoreCancelled = true)
public void onBlockBreak(BlockBreakEvent event) {
if (Utils.checkAuth(event.getPlayer())) {
return;
if (ListenerService.shouldCancelEvent(event.getPlayer())) {
event.setCancelled(true);
}
// TODO: npc status can be used to bypass security!!!
if (Utils.isNPC(event.getPlayer())) {
return;
}
event.setCancelled(true);
}
}

View File

@ -1,30 +1,31 @@
package fr.xephi.authme.listener;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.util.Utils;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.*;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityInteractEvent;
import org.bukkit.event.entity.EntityRegainHealthEvent;
import org.bukkit.event.entity.EntityShootBowEvent;
import org.bukkit.event.entity.EntityTargetEvent;
import org.bukkit.event.entity.FoodLevelChangeEvent;
import org.bukkit.event.entity.ProjectileLaunchEvent;
import org.bukkit.projectiles.ProjectileSource;
import java.lang.reflect.Method;
import static fr.xephi.authme.listener.ListenerService.shouldCancelEvent;
/**
*/
public class AuthMeEntityListener implements Listener {
private static Method getShooter;
private static boolean shooterIsProjectileSource;
public final AuthMe instance;
public AuthMeEntityListener(AuthMe instance) {
this.instance = instance;
public AuthMeEntityListener() {
try {
Method m = Projectile.class.getDeclaredMethod("getShooter");
shooterIsProjectileSource = m.getReturnType() != LivingEntity.class;
@ -113,15 +114,9 @@ public class AuthMeEntityListener implements Listener {
}
}
if (Utils.checkAuth(player)) {
return;
if (ListenerService.shouldCancelEvent(player)) {
event.setCancelled(true);
}
if (Utils.isNPC(player)) {
return;
}
event.setCancelled(true);
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.NORMAL)

View File

@ -18,8 +18,8 @@ import fr.xephi.authme.util.GeoLiteAPI;
import fr.xephi.authme.util.Utils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@ -49,7 +49,6 @@ import org.bukkit.event.player.PlayerShearEntityEvent;
import java.util.concurrent.ConcurrentHashMap;
import static fr.xephi.authme.listener.ListenerService.shouldCancelEvent;
import org.bukkit.GameMode;
/**
* Listener class for player's events
@ -58,7 +57,7 @@ public class AuthMePlayerListener implements Listener {
public static final ConcurrentHashMap<String, String> joinMessage = new ConcurrentHashMap<>();
public static final ConcurrentHashMap<String, Boolean> causeByAuthMe = new ConcurrentHashMap<>();
public final AuthMe plugin;
private final AuthMe plugin;
private final Messages m;
public AuthMePlayerListener(AuthMe plugin) {
@ -401,7 +400,7 @@ public class AuthMePlayerListener implements Listener {
public void onPlayerInventoryOpen(InventoryOpenEvent event) {
final Player player = (Player) event.getPlayer();
if (Utils.checkAuth(player) || Utils.isNPC(player)) {
if (!ListenerService.shouldCancelEvent(player)) {
return;
}
event.setCancelled(true);
@ -433,17 +432,9 @@ public class AuthMePlayerListener implements Listener {
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
public void onPlayerHitPlayerEvent(EntityDamageByEntityEvent event) {
Entity damager = event.getDamager();
if (!(damager instanceof Player)) {
return;
if (ListenerService.shouldCancelEvent(event)) {
event.setCancelled(true);
}
if (Utils.checkAuth((Player) damager)) {
return;
}
if (Utils.isNPC((Player) damager)) {
return;
}
event.setCancelled(true);
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
@ -469,13 +460,10 @@ public class AuthMePlayerListener implements Listener {
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
public void onSignChange(SignChangeEvent event) {
if (Utils.checkAuth(event.getPlayer())) {
return;
Player player = event.getPlayer();
if (ListenerService.shouldCancelEvent(player)) {
event.setCancelled(true);
}
if (Utils.isNPC(event.getPlayer())) {
return;
}
event.setCancelled(true);
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)

View File

@ -1,21 +1,15 @@
package fr.xephi.authme.listener;
import fr.xephi.authme.AuthMe;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerEditBookEvent;
/**
* Listener of player events for events introduced in Minecraft 1.6.
*/
public class AuthMePlayerListener16 implements Listener {
public final AuthMe plugin;
public AuthMePlayerListener16(AuthMe plugin) {
this.plugin = plugin;
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.NORMAL)
public void onPlayerEditBook(PlayerEditBookEvent event) {
if (ListenerService.shouldCancelEvent(event)) {

View File

@ -1,21 +1,15 @@
package fr.xephi.authme.listener;
import fr.xephi.authme.AuthMe;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
/**
* Listener of player events for events introduced in Minecraft 1.8.
*/
public class AuthMePlayerListener18 implements Listener {
public final AuthMe plugin;
public AuthMePlayerListener18(AuthMe plugin) {
this.plugin = plugin;
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
public void onPlayerInteractAtEntity(PlayerInteractAtEntityEvent event) {
if (ListenerService.shouldCancelEvent(event)) {

View File

@ -27,7 +27,7 @@ final class ListenerService {
}
Player player = (Player) entity;
return !Utils.checkAuth(player) && !Utils.isNPC(player);
return shouldCancelEvent(player);
}
/**
@ -38,6 +38,16 @@ final class ListenerService {
*/
public static boolean shouldCancelEvent(PlayerEvent event) {
Player player = event.getPlayer();
return shouldCancelEvent(player);
}
/**
* Return, based on the player associated with the event, whether or not the event should be canceled.
*
* @param player The player to verify
* @return True if the associated event should be canceled, false otherwise
*/
public static boolean shouldCancelEvent(Player player) {
return player != null && !Utils.checkAuth(player) && !Utils.isNPC(player);
}