mirror of
https://github.com/NoCheatPlus/NoCheatPlus.git
synced 2024-09-28 22:47:32 +02:00
[TEST] Attempt to fall back to reflection for health issues (one spot).
This commit is contained in:
parent
e60006d6a3
commit
f315336005
@ -27,7 +27,7 @@ public class ReflectionUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Dirty method. Does try.catch and return null for method invokation.
|
||||
* Dirty method. Does try.catch and return null for method invocation.
|
||||
* @param obj
|
||||
* @param methodName
|
||||
* @param arg
|
||||
@ -75,4 +75,38 @@ public class ReflectionUtil {
|
||||
}
|
||||
}
|
||||
|
||||
public static Object invokeMethodVoid(final Object obj, final String methodName){
|
||||
// TODO: ? provide a variation with boolean for general first or specialized first
|
||||
// TODO: copy and paste: bad!
|
||||
// TODO: Isn't there a one-line-call for this ??
|
||||
final Class<?> objClass = obj.getClass();
|
||||
// Collect methods that might work.
|
||||
Method methodFound = null;
|
||||
for (final Method method : objClass.getDeclaredMethods()){
|
||||
if (method.getName().equals(methodName)){
|
||||
final Class<?>[] parameterTypes = method.getParameterTypes();
|
||||
if (parameterTypes.length == 1 ){
|
||||
// Override the found method if none found yet and assignment is possible, or if it has a specialized argument of an already found one.
|
||||
if (methodFound != null && methodFound.getParameterTypes()[0].isAssignableFrom(parameterTypes[0])){
|
||||
methodFound = method;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (methodFound != null){
|
||||
try{
|
||||
final Object res = methodFound.invoke(obj);
|
||||
return res;
|
||||
}
|
||||
catch (Throwable t){
|
||||
// TODO: Throw something !?
|
||||
return null;
|
||||
}
|
||||
}
|
||||
else{
|
||||
// TODO: Throw something !?
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -19,7 +19,8 @@ import fr.neatmonster.nocheatplus.players.DataManager;
|
||||
import fr.neatmonster.nocheatplus.utilities.TickTask;
|
||||
|
||||
/**
|
||||
* Quick replacement for InstantEat, partly reusing InstantEat data.
|
||||
* Quick replacement for InstantEat, partly reusing InstantEat data.<br>
|
||||
* This check is added by fr.neatmonster.nocheatplus.compat.DefaultComponentFactory.
|
||||
* @author mc_dev
|
||||
*
|
||||
*/
|
||||
|
@ -27,7 +27,9 @@ import fr.neatmonster.nocheatplus.checks.moving.MovingConfig;
|
||||
import fr.neatmonster.nocheatplus.checks.moving.MovingData;
|
||||
import fr.neatmonster.nocheatplus.checks.moving.MovingListener;
|
||||
import fr.neatmonster.nocheatplus.components.JoinLeaveListener;
|
||||
import fr.neatmonster.nocheatplus.logging.LogUtil;
|
||||
import fr.neatmonster.nocheatplus.permissions.Permissions;
|
||||
import fr.neatmonster.nocheatplus.utilities.ReflectionUtil;
|
||||
import fr.neatmonster.nocheatplus.utilities.TickTask;
|
||||
import fr.neatmonster.nocheatplus.utilities.TrigUtil;
|
||||
import fr.neatmonster.nocheatplus.utilities.build.BuildParameters;
|
||||
@ -410,6 +412,7 @@ public class FightListener extends CheckListener implements JoinLeaveListener{
|
||||
}
|
||||
}
|
||||
|
||||
private boolean fail = false;
|
||||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
||||
public void onEntityRegainHealth(final EntityRegainHealthEvent event){
|
||||
final Entity entity = event.getEntity();
|
||||
@ -421,7 +424,41 @@ public class FightListener extends CheckListener implements JoinLeaveListener{
|
||||
data.regainHealthTime = System.currentTimeMillis();
|
||||
// Set god-mode health to maximum.
|
||||
// TODO: Mind that health regain might half the ndt.
|
||||
data.godModeHealth = Math.max(data.godModeHealth, player.getHealth() + event.getAmount());
|
||||
double health = 0.0;
|
||||
try{
|
||||
// Changed order.
|
||||
health = event.getAmount() + player.getHealth(); // TODO: maxHealth !
|
||||
}
|
||||
catch(AbstractMethodError e){
|
||||
if (!fail){
|
||||
fail = true;
|
||||
LogUtil.logWarning("[NoCheatPlus] Health API incompatibility detected.");
|
||||
}
|
||||
// Reflection fall-back (might fall back to int methods, at present).
|
||||
final Object o1 = ReflectionUtil.invokeMethodVoid(event, "getAmount");
|
||||
if (o1 instanceof Number){
|
||||
health += ((Number) o1).doubleValue();
|
||||
}
|
||||
final Object o2 = ReflectionUtil.invokeMethodVoid(player, "getHealth");
|
||||
if (o2 instanceof Number){
|
||||
health += ((Number) o2).doubleValue();
|
||||
}
|
||||
}
|
||||
try{
|
||||
health = Math.max(health, player.getMaxHealth());
|
||||
}
|
||||
catch(AbstractMethodError e){
|
||||
if (!fail){
|
||||
fail = true;
|
||||
LogUtil.logWarning("[NoCheatPlus] Health API incompatibility detected.");
|
||||
}
|
||||
// Reflection fall-back (might fall back to int methods, at present).
|
||||
final Object o1 = ReflectionUtil.invokeMethodVoid(player, "getMaxHealth");
|
||||
if (o1 instanceof Number){
|
||||
health = Math.min(health, ((Number) o1).doubleValue());
|
||||
}
|
||||
}
|
||||
data.godModeHealth = Math.max(data.godModeHealth, health);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user