Jump to content

[Solved]get username of player from LivingHurtEvent.


BigDaveNz

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.