Jump to content

Recommended Posts

Posted

Hey, I'm trying to extract information out of a boss bar on a server. However, there doesn't seem to be any entities in the world which could be the boss. How would I go about extracting this information without the entity?

 

Thanks in advance

  • Like 1
Posted (edited)

 

Thanks, unfortunately, this is a client-side only mod.

However, I did manage to resolve my problem using a little bit of reflection.


In case anyone else ever needs it, here's my solution:

 

	@SuppressWarnings("unchecked")
	public static List<String> getBossBarNames(Minecraft minecraft) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
		
		GuiBossOverlay bossOverlay = minecraft.ingameGUI.getBossOverlay();

		String nameAfter = null;
		
		// Find the name of the field as it will change with each obfuscation of forge
		// If you're using this, you'll want to move this lookup elsewhere and store the result
		for (Field s : GuiBossOverlay.class.getDeclaredFields()) {
			
			if (s.getType().getName().equals("java.util.Map")) {
				nameAfter = s.getName();
				break;
			}
			
		}
		
		List<String> names = new ArrayList<String>();
		
		if (nameAfter != null) {
		
			Field bossField = GuiBossOverlay.class.getDeclaredField(nameAfter);
			bossField.setAccessible(true);
			
			Map<UUID, BossInfoLerping> mapBossInfos = (Map<UUID, BossInfoLerping>) bossField.get(bossOverlay);
			
			for (BossInfoLerping bIL : mapBossInfos.values())
				names.add(bIL.getName().getFormattedText());
		
		}
		
		return names;
		
	}

 

Edited by Craftiii4
Fix possible npe
  • Like 1
Posted
Just now, diesieben07 said:

Then why did you say "on the server"? o.O

 

As for your reflection code:

  • Do not use getClass. Use a static .class reference.
  • Do not use that field-detection magic. Use the actual field name (SRG and MCP name).
  • Only look up the Field instance once and then store it in a static final field.
  • Don't cast to HashMap if the field is only of type Map.

 

By on server I meant that the boss bar is on the server, e.g. "Welcome to thisserver.com"

 

Thanks for the tips, I do only look up field once, just modified it slightly before I added the code here so it would work as a standalone function.

Posted
Just now, diesieben07 said:

Just for the future, this is very counter-productive.

Was experimenting with the boss related classes trying to get the output out I wanted, so granted it's not the best reflection ever written. I'll modify my above code to reflect the changes suggested, thanks again!

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.