From 9a5c84f3fc35fac6770d238dac5d2d5b7caefd11 Mon Sep 17 00:00:00 2001 From: Gabriele C Date: Sat, 5 Mar 2016 21:33:56 +0100 Subject: [PATCH] Revert "Use local shooter" --- .../authme/listener/AuthMeEntityListener.java | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/main/java/fr/xephi/authme/listener/AuthMeEntityListener.java b/src/main/java/fr/xephi/authme/listener/AuthMeEntityListener.java index ac2358d8c..c5c0b2a68 100644 --- a/src/main/java/fr/xephi/authme/listener/AuthMeEntityListener.java +++ b/src/main/java/fr/xephi/authme/listener/AuthMeEntityListener.java @@ -1,5 +1,6 @@ package fr.xephi.authme.listener; +import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.bukkit.entity.Projectile; import org.bukkit.event.EventHandler; @@ -15,11 +16,22 @@ 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 { - public AuthMeEntityListener() {} + private static Method getShooter; + private static boolean shooterIsProjectileSource; + + public AuthMeEntityListener() { + try { + Method m = Projectile.class.getDeclaredMethod("getShooter"); + shooterIsProjectileSource = m.getReturnType() != LivingEntity.class; + } catch (Exception ignored) { + } + } // TODO #360: npc status can be used to bypass security!!! @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST) @@ -84,12 +96,22 @@ public class AuthMeEntityListener implements Listener { Player player = null; Projectile projectile = event.getEntity(); - if (projectile != null && (projectile.getShooter() instanceof ProjectileSource)) { + if (shooterIsProjectileSource) { ProjectileSource shooter = projectile.getShooter(); if (shooter == null || !(shooter instanceof Player)) { return; } player = (Player) shooter; + } else { + // TODO ljacqu 20151220: Invoking getShooter() with null but method isn't static + try { + if (getShooter == null) { + getShooter = Projectile.class.getMethod("getShooter"); + } + Object obj = getShooter.invoke(null); + player = (Player) obj; + } catch (Exception ignored) { + } } if (ListenerService.shouldCancelEvent(player)) {