Posted July 16, 201312 yr Hello all, just been going round in circles for a while with this problem, I'm trying to get the username of a hurt character. I currently have a filter that means only entities that are of the EntityPlayerMP class, but i cant figure out how to get the username field from there. Is there something in reflection API that I'm missing??? Or is there just a way easier way of doing this? Current Code(that doesnt work fyi): public class EILivingHurt { /** * General Living Hurt Processor */ public static void process(LivingHurtEvent event) { int xpAmount = (int)event.ammount * 10; DamageSource damageSource = event.source; EntityLivingBase entity = event.entityLiving; Class<? extends EntityLivingBase> entityClass = entity.getClass(); String className = entityClass.getName(); Type superclass = entityClass.getGenericSuperclass(); EIDebugHandler.sendDebugInfoToConsole("Entity was hurt: " + className); EIDebugHandler.sendDebugInfoToConsole(superclass + " Superclass, Type: " + superclass.toString()); switch (superclass.toString()){ case "net.minecraft.entity.player.EntityPlayer": EIDebugHandler.sendDebugInfoToConsole("Player Was Hurt"); Method method = null; try { method = entityClass.getMethod("getEntityName", EntityPlayer.class); } catch (NoSuchMethodException | SecurityException e) { e.printStackTrace(); } String username = null; try { username = (String)method.invoke(entityClass); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { e.printStackTrace(); } EIDebugHandler.sendDebugInfoToConsole("Player Damaged" + username); } } } Note that this code is just mashed together for testing purposes. The difference between stupidity and genius is that genius has its limits. - Albert Einstein
July 16, 201312 yr if(event.entityLiving instanceof EntityPlayer) { EntityPlayer player = (EntityPlayer) event.entityLiving; //do stuff here }
July 16, 201312 yr Author Sorry i should clarify... In my eventHandler class i already have @ForgeSubscribe public void onLivingHurt(LivingHurtEvent event) { EILivingHurt.process(event); } everythign works, except i cant figure out how to get the username of the "hurt" player. The difference between stupidity and genius is that genius has its limits. - Albert Einstein
July 16, 201312 yr Same thing. In your processing: if(event.entityLiving instanceof EntityPlayer) { EntityPlayer player = (EntityPlayer) event.entityLiving; String name = player.username; }
July 16, 201312 yr Author ah! Its the intanceof part im missing. Thankyou The difference between stupidity and genius is that genius has its limits. - Albert Einstein
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.