Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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

if(event.entityLiving instanceof EntityPlayer)
{
     EntityPlayer player = (EntityPlayer) event.entityLiving;
     //do stuff here
}

  • 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

Same thing. In your processing:

 

if(event.entityLiving instanceof EntityPlayer)
{
     EntityPlayer player = (EntityPlayer) event.entityLiving;
     String name = player.username;
}

  • 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.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.