Jump to content

[1.12.2] [Solved] Changing max health based on armor set


Erfurt

Recommended Posts

Hey guys, I'm currently working on doubling the players vanilla health pool, so give the player 10 extra hearts (20 HP), if the player has a full set of my custom armor equipped. The way I have it right now works, but I feel like it's a bad way to do it, maybe someone could help me out?

I have a class that does the health boost.

public class AbilityHandler
{
	public static List<String> playersWithSet = new ArrayList<String>();
	
	public static String playerKey(EntityPlayer player)
	{
		return player.getGameProfile().getName() + ":" + player.world.isRemote;
	}
	
	@SubscribeEvent
	public void updatePlayerAbilityStatus(LivingUpdateEvent event)
	{
		if(event.getEntityLiving() instanceof EntityPlayer)
		{
			EntityPlayer player = (EntityPlayer)event.getEntityLiving();
			String key = playerKey(player);

			Boolean hasMithrilSet = ArmorMithril.isFullSet(player);
			if(playersWithSet.contains(key))
			{
				if(hasMithrilSet)
				{
					player.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(40);
				}
				else
				{
					player.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(20);
					
					playersWithSet.remove(key);
				}
			}
			else if(hasMithrilSet)
			{
				playersWithSet.add(key);
			}
		}
	}
}

 

It's the .setBaseValue(x) I feel like is the "wrong/bad" way to doubling the vanilla health pool. But I can't figure out another way. The thing is if my mod is playing with another mod, that have a lower or higher starting health pool for the player, then my mod will change it back to the vanilla values.

 

As I mentioned this works, but I strongly feel like it's a bad way to do it, so I would like to hear of a better way.

Edited by Erfurt
Solved
Link to comment
Share on other sites

I take it this was too hard to arrive at:

player.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).applyModifier(new AttributeModifier(uuid, name, 2, 2));

 

UUID is a unique UUID (you can create one at random, then save it as a hardcoded value).

name is just whatever you want to call it.

Edited by Draco18s

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

8 hours ago, Draco18s said:

I take it this was too hard to arrive at:

player.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).applyModifier(new AttributeModifier(uuid, name, 2, 2));

 

UUID is a unique UUID (you can create one at random, then save it as a hardcoded value).

name is just whatever you want to call it.

I have been working on this for like 6 hours now, and it semi works. I have made some changes from the original code. I changed the event to LivingEquipmentChangeEvent as the other one gave me a crash when I had the full set of armor equipped. The crash was Modifier is already applied on this attribute! I'm still getting that crash but as far as I can tell it's only when I have something in my main hand, while having the full set equipped. Also it doesn't seem to update the attribute if I take off one piece of armor and put in back on, it updates if I get something in my main hand. But then it crashes. All crashes is the same, and I'm not sure how to get ridt of it.

 

My new code if It's needed

public class AbilityHandler
{
	protected static final UUID MAX_HEALTH_UUID = UUID.randomUUID();
	
	public static List<String> playersWithSet = new ArrayList<String>();
	
	public static String playerKey(EntityPlayer player)
	{
		return player.getGameProfile().getName() + ":" + player.world.isRemote;
	}
	
	@SubscribeEvent
	public void updatePlayerAbilityStatus(LivingEquipmentChangeEvent event)
	{
		if(event.getEntityLiving() instanceof EntityPlayer)
		{
			EntityPlayer player = (EntityPlayer)event.getEntityLiving();
			String key = playerKey(player);

			Boolean hasMithrilSet = ArmorMithril.isFullSet(player);
			if(playersWithSet.contains(key))
			{
				if(hasMithrilSet)
				{
					player.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).applyModifier(new AttributeModifier(MAX_HEALTH_UUID, "MAX_HEALTH_UUID", 1, 2));
				}
				else
				{
					player.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).removeModifier(new AttributeModifier(MAX_HEALTH_UUID, "MAX_HEALTH_UUID", 1, 2));
					playersWithSet.remove(key);
				}
			}
			else if(hasMithrilSet)
			{
				playersWithSet.add(key);
			}
		}
	}
}

 

Link to comment
Share on other sites

I just want to look at this section for a bit:

			Boolean hasMithrilSet = ArmorMithril.isFullSet(player);
			if(playersWithSet.contains(key)) {
				//do stuff
			}
			else if(hasMithrilSet) {
				playersWithSet.add(key);
			}

Equipment changed!

Player has a full set!

We have no idea who this player is!

Store player in list!

Done!

 

Wot.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

18 hours ago, Draco18s said:

I just want to look at this section for a bit:


			Boolean hasMithrilSet = ArmorMithril.isFullSet(player);
			if(playersWithSet.contains(key)) {
				//do stuff
			}
			else if(hasMithrilSet) {
				playersWithSet.add(key);
			}

Equipment changed!

Player has a full set!

We have no idea who this player is!

Store player in list!

Done!

 

Wot.

I might be wrong, but what you are telling me to do, I believe I am already doing with this.

public static List<String> playersWithSet = new ArrayList<String>();

public static String playerKey(EntityPlayer player)
{
	return player.getGameProfile().getName() + ":" + player.world.isRemote;
}

 

 

Link to comment
Share on other sites

43 minutes ago, Erfurt said:

I might be wrong, but what you are telling me to do, I believe I am already doing with this.

That had nothing to do with what I said, at all.

 

Imagine a player equips your armor set for the full time. They will not exist in your playersWithSet collection.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

4 minutes ago, Draco18s said:

That had nothing to do with what I said, at all.

 

Imagine a player equips your armor set for the full time. They will not exist in your playersWithSet collection.

I get what you are saying, but I don't really understand why they are not in the playersWithSet collection. As far as I can tell I add them to it if they are not in it already.

Link to comment
Share on other sites

You are adding the player to the collection AFTER you check if he is in it... of course the player won't be inside of it before then
Switch the if-statements 'round to add first, THEN check if the player is in it.

Also previously known as eAndPi.

"Pi, is there a station coming up where we can board your train of thought?" -Kronnn

Published Mods: Underworld

Handy links: Vic_'s Forge events Own WIP Tutorials.

Link to comment
Share on other sites

I got it working now, might not be the correct way, doesn't seem to be exactly as you guys suggested, but for now it works without any of the crashes and other issues I had before.

Updated code for anyone who might find it useful.

Spoiler

public class AbilityHandler
{
	protected static final UUID MAX_HEALTH_UUID = UUID.randomUUID();
	
	private static final AttributeModifier HEALTH_BOOST = new AttributeModifier(MAX_HEALTH_UUID, "MAX_HEALTH_UUID", 1, 2); 
	
	public static List<String> playersWithSet = new ArrayList<String>();
	
	public static String playerKey(EntityPlayer player)
	{
		return player.getGameProfile().getName() + ":" + player.world.isRemote;
	}
	
	@SubscribeEvent
	public void updatePlayerAbilityStatus(LivingEquipmentChangeEvent event)
	{
		if(event.getEntityLiving() instanceof EntityPlayer)
		{
			EntityPlayer player = (EntityPlayer)event.getEntityLiving();
			
			String key = playerKey(player);

			Boolean hasMithrilSet = ArmorMithril.isFullSet(player);
			
			Boolean hasHealthBoost = player.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).getModifiers().contains(HEALTH_BOOST);
			
			EntityEquipmentSlot slot = event.getSlot();
			
		    if((!(slot == EntityEquipmentSlot.MAINHAND)) && (!(slot == EntityEquipmentSlot.OFFHAND)))
		    {
		    	if(playersWithSet.contains(key))
				{
		    		player.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).removeModifier(HEALTH_BOOST);
		    		
		    		if(hasMithrilSet)
		    		{
						player.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).applyModifier(HEALTH_BOOST);
					}
				}
		    	else
				{
		    		playersWithSet.add(key);
				}
		    }
		}
	}
}

 

EDIT: Scratch that, still one problem... When I close the game completely and open it again, then for some reason I get double health again. Anyway it's late and I can't think straight anymore.

Edited by Erfurt
Link to comment
Share on other sites

Again, you check to see if your data contains the player before adding the modifier (and if they're not in it, you don't add the modifier).

 

You also don't do a damn thing with hasHealthBoost

Edited by Draco18s

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

20 hours ago, Draco18s said:

Again, you check to see if your data contains the player before adding the modifier (and if they're not in it, you don't add the modifier).

 

You also don't do a damn thing with hasHealthBoost

First of, I tried doing that, and it didn't change the outcome. Secondly I had hasHealthBoost In there to do some testing, and I forgot to remove it... As I said, it was getting late and I wasn't thinking completely straight. Still working on getting this working, will update this doing the week

Link to comment
Share on other sites

Got it working now, turned out to be a a big derp from my part. The UUID I used I generated random by using UUID.randomUUID(). I didn't think about the fact that it would change everytime I opened Minecraft... Changed it to a fixed string, and now it works flawless. Thanks for your help guys.

 

For others who have problem with this, mt new working code is in the spoiler.

Spoiler

protected static final UUID MAX_HEALTH_UUID = UUID.fromString("01712f7e-776c-4d28-a28f-0fe6cb491ad9");

private static final AttributeModifier HEALTH_BOOST = new AttributeModifier(MAX_HEALTH_UUID, "MAX_HEALTH_UUID", 1, 2); 

public static List<String> playersWithSet = new ArrayList<String>();

public static String playerKey(EntityPlayer player)
{
	return player.getGameProfile().getName() + ":" + player.world.isRemote;
}

@SubscribeEvent
public void updatePlayerAbilityStatus(LivingEquipmentChangeEvent event)
{
	if(event.getEntityLiving() instanceof EntityPlayer)
	{
		EntityPlayer player = (EntityPlayer)event.getEntityLiving();

		String key = playerKey(player);

		Boolean hasMithrilSet = ArmorMithril.isFullSet(player);

		Boolean hasHealthBoost = player.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).getModifiers().contains(HEALTH_BOOST);

		EntityEquipmentSlot slot = event.getSlot();

		if((!(slot == EntityEquipmentSlot.MAINHAND)) && (!(slot == EntityEquipmentSlot.OFFHAND)))
		{
			if(!(playersWithSet.contains(key)))
			{
				playersWithSet.add(key);
			}
			else if(playersWithSet.contains(key))
			{
				if(hasHealthBoost && !hasMithrilSet)
				{
					player.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).removeModifier(HEALTH_BOOST);
				}
				else if(hasMithrilSet && !hasHealthBoost)
				{
					player.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).applyModifier(HEALTH_BOOST);
				}
			}
		}
	}
}

 

 

Link to comment
Share on other sites

On 12/9/2017 at 9:20 AM, Draco18s said:

UUID is a unique UUID (you can create one at random, then save it as a hardcoded value).

Funny. I thought I said that. 

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

4 hours ago, Draco18s said:

Funny. I thought I said that. 

As I said it was a derp from my side, why did you even have to comment on that... Most of the time your comments make you sound like a prick, sorry if you don't mean to. But maybe think about it in the future.

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.