Skip to content

Commit

Permalink
[TEST] Attempt to fall back to reflection for health issues (one spot).
Browse files Browse the repository at this point in the history
  • Loading branch information
asofold committed Jul 6, 2013
1 parent e60006d commit f315336
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
Expand Up @@ -27,7 +27,7 @@ public static void checkMembers(String prefix, String[]... specs){
}

/**
* 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
Expand Down Expand Up @@ -75,4 +75,38 @@ else if (methodFound != null && methodFound.getParameterTypes()[0].isAssignableF
}
}

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;
}
}

}
Expand Up @@ -19,7 +19,8 @@
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
*
*/
Expand Down
Expand Up @@ -27,7 +27,9 @@
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;
Expand Down Expand Up @@ -410,6 +412,7 @@ public void onEntityRegainHealthLow(final EntityRegainHealthEvent event){
}
}

private boolean fail = false;
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onEntityRegainHealth(final EntityRegainHealthEvent event){
final Entity entity = event.getEntity();
Expand All @@ -421,7 +424,41 @@ public void onEntityRegainHealth(final EntityRegainHealthEvent event){
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
Expand Down

0 comments on commit f315336

Please sign in to comment.