Jump to content

How to add a Capability for Extra Health


SwimmingTuna

Recommended Posts

Hey there, as the title suggest, I'm trying to make a capability that when the player has it, there health is modified. I've already made a capability that adds a type of mana and also made an item that modifies the entity reach attribute but for some reason, I'm having trouble making a capability modifying player health. 

public class Spectator9Health {
    private int s9Health = Player.MAX_HEALTH;
    private final int MAX_S9HEALTH = 30;

    public int getS9Health() {
        return s9Health;
    }
    public AttributeSupplier.Builder createAttributes() {
        return Player.createAttributes().add(Attributes.MAX_HEALTH,10);
    }
    public void copyFrom(Spectator9Health source) {
        this.s9Health= source.s9Health;
    }

    public void saveNBTData(CompoundTag nbt) {
        nbt.putInt("s9health", s9Health);
    }

    public void loadNBTData(CompoundTag nbt) {
        s9Health = nbt.getInt("s9health");
    }
}

 

and here is the provider for it although i'm pretty sure the main issue is with the Spectator9Health class.

 

public class Spectator9HealthProvider implements ICapabilityProvider, INBTSerializable<CompoundTag> {
    public static Capability<Spectator9Health> SPECTATOR9HEALTH = CapabilityManager.get(new CapabilityToken<Spectator9Health>() {});

    private Spectator9Health s9health = null;
    private final LazyOptional<Spectator9Health> optional = LazyOptional.of(this::createSpectator9Health);

    private Spectator9Health createSpectator9Health() {
        if (this.s9health == null) {
            this.s9health = new Spectator9Health();
        }
        return this.s9health;
    }

    @Override
    public @NotNull <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
        if (cap == SPECTATOR9HEALTH) {
            return optional.cast();
        }
        return LazyOptional.empty();
    }

    @Override
    public CompoundTag serializeNBT() {
        CompoundTag nbt = new CompoundTag();
        createSpectator9Health().saveNBTData(nbt);
        return nbt;
    }

    @Override
    public void deserializeNBT(CompoundTag nbt) {
createSpectator9Health().loadNBTData(nbt);
    }
}

 

I've also tried a different method of doing the Spectator9Health class

 

public class Spectator9Health {

private int s9Health = Player.MAX_HEALTH;

private final int MAX_S9HEALTH = 30;

public int getS9Health() {

return s9Health; }

private Multimap<Attribute, AttributeModifier> createAttributeMap() {

ImmutableMultimap.Builder<Attribute, AttributeModifier> attributeBuilder = ImmutableMultimap.builder();

 attributeBuilder.put(MAX_HEALTH, new AttributeModifier(BeyonderHealthChange, "Health Modifier", 20, AttributeModifier.Operation.ADDITION));

return attributeBuilder.build(); }

 

 

and this method calls upon BeyonderHealthChange which is in a HealthChangeEvent that is coded as

 

public class HealthChangeEvent

{ public static void checkPlayerBeyonder(PlayerInteractEvent event, Entity pEntity, Player pPlayer, InteractionHand pUsedHand) {

UUID uuidForHealthChange = BeyonderHealthChange;

AttributeInstance maxHP = pPlayer.getAttribute(MAX_HEALTH);

{ AttributeModifier beyonderHealthModifier = maxHP.getModifier(BeyonderHealthChange);

if (beyonderHealthModifier != null)

{ maxHP.removeModifier(beyonderHealthModifier.getId());

double bHealth = pPlayer.getAttributeValue(MAX_HEALTH);

maxHP.addTransientModifier(beyonderHealthModifier); } } } } 

 

So yeah neither of these methods work and if anyone knows if I'm missing something or knows of a mod that accomplishes something along the lines of adding a permanent health boost without the health boost potion effect, it would be appreciated

 

 

 

 

 

Edited by SwimmingTuna
Link to comment
Share on other sites

  • SwimmingTuna changed the title to How to add a Capability for Extra Health

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.