Jump to content

Attribute modifiers stacking (solved)


Alekseyev

Recommended Posts

EDIT: I just realised that I did the UUID thing wrong. Working on this...

EDIT2: It was indeed the issue. To anyone else coming across this: DO NOT use the player UUID for the modifier, use a custom one per modifier instead. For example, use UUID.fromString("<string goes here>") to generate it.

Hello, I have a problem: After reading up on other threads, I was made aware of the fact that instead of setting the base value of a SharedMonsterAttribute, it is better to add a modifier to it to keep compatibility.

This effect is applied to every player once after joining the world.

However, if I close the game and then re-launch it and join the same world again, the old effect does not seem to be deleted - in the case of the health effect (FIRE), the HP went back to 10 hearts after joining the world a third time, only to then jump to 13. Intended would be that the modifier can only be applied once at a time, for a maximum of 11 hearts (22 HP).

 

This is the code in use:

// Attribute modifiers - these serve to define modifiers (multiplicative for speed, additive for health; depends on operationIn)
AttributeModifier moveSpeedBonus = new AttributeModifier(evPlayer.getUniqueID(),"waterAirMoveBonus",0.15D,2);
AttributeModifier moveSpeedMalus = new AttributeModifier(evPlayer.getUniqueID(),"earthMoveMalus",-0.05D,2);
AttributeModifier healthBonus = new AttributeModifier(evPlayer.getUniqueID(),"fireHealthBonus",2.0D,0);
AttributeModifier healthMalus = new AttributeModifier(evPlayer.getUniqueID(),"waterHealthMalus",-2.0D,0);

// Get rid of any possible modifiers
if (evPlayer.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).hasModifier(moveSpeedBonus))
{
	evPlayer.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).removeModifier(moveSpeedBonus);
}
if (evPlayer.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).hasModifier(moveSpeedMalus))
{
	evPlayer.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).removeModifier(moveSpeedMalus);
}
if (evPlayer.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).hasModifier(healthBonus))
{
	evPlayer.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).removeModifier(healthBonus);
}
if (evPlayer.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).hasModifier(healthMalus))
{
	evPlayer.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).removeModifier(healthMalus);
}
// Apply where needed
switch(element)
{
	case FIRE:
		evPlayer.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).applyModifier(healthBonus);

		if (evPlayer.getHealth() == evPlayer.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).getBaseValue())
		{
			evPlayer.setHealth(evPlayer.getHealth() + 2); //This is to heal the player up by the 2 HP/1 heart if the element was just switched to.
		}
		break;

	case AIR:
		evPlayer.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).applyModifier(moveSpeedBonus);
		break;

	case WATER:
		evPlayer.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).applyModifier(healthMalus);
		evPlayer.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).applyModifier(moveSpeedBonus);
		break;

	case EARTH:
		evPlayer.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).applyModifier(moveSpeedMalus);
		break;

	default:
		break;
}

 

Edited by Alekseyev
solved
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.