Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[SOLVED][1.16.4] Changing an entity's max health


Recommended Posts

I'm trying to modify an entity's max health based on a stat, but either its not working or my client isn't acknowledging the change.

 

The method for the change. Full class here.

	@Override
	public void modifyMaxHealth(LivingEntity entity) {
		if(entity == null) {
			return;
		}
		entity = getEntity();
		final float newAmount = getMaxHealth();
		final float oldAmount;
		
		final UUID MODIFIER_ID = UUID.fromString("d5d0d878-b3c2-469b-ba89-ac01c0635a9c");
		final ModifiableAttributeInstance health = entity.getAttribute(Attributes.MAX_HEALTH);
		final AttributeModifier mod = new AttributeModifier(MODIFIER_ID, "Max Health", newAmount, AttributeModifier.Operation.ADDITION);
		
		final AttributeModifier oldMod = health.getModifier(MODIFIER_ID);
		
		if(oldMod != null) {
			health.removeModifier(oldMod);
			oldAmount = (float) oldMod.getAmount();
		}else {
			oldAmount = 0;
		}
		
		health.applyPersistentModifier(mod);
		
		final float amountToHeal = newAmount - oldAmount;
		if(amountToHeal > 0) {
			entity.heal(amountToHeal);
		}
	}

 

The sync method:

	@Override
	public void sync(ServerPlayerEntity player) {
		if(entity instanceof ServerPlayerEntity) {
			PacketHandler.sendTo(new SkillsPacket(serializeNBT()), player);
			
			if(!player.world.isRemote) {
				ModifiableAttributeInstance attribute = player.getAttribute(Attributes.MAX_HEALTH);
				SEntityPropertiesPacket packet = new SEntityPropertiesPacket(player.getEntityId(), Collections.singleton(attribute));
				((ServerWorld) player.getEntityWorld()).getChunkProvider().sendToTrackingAndSelf(player, packet);
			}
		}
	}

 

My player event class:

package lk1905.gielinorcraft.events;

import lk1905.gielinorcraft.Gielinorcraft;
import lk1905.gielinorcraft.api.skill.ISkills;
import lk1905.gielinorcraft.capability.skill.SkillCapability;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.event.entity.player.PlayerEvent.PlayerChangedDimensionEvent;
import net.minecraftforge.event.entity.player.PlayerEvent.PlayerLoggedInEvent;
import net.minecraftforge.event.entity.player.PlayerEvent.PlayerRespawnEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber(modid = Gielinorcraft.MODID, bus = Mod.EventBusSubscriber.Bus.FORGE)
public class PlayerEventHandler {

	@SubscribeEvent
	public static void onPlayerClone(PlayerEvent.Clone event) {
		if(!event.isWasDeath()) {
			return;
		}
		
		ISkills oldSkills = event.getOriginal().getCapability(SkillCapability.SKILL_CAP, null).orElse(null);
		ISkills newSkills = event.getPlayer().getCapability(SkillCapability.SKILL_CAP, null).orElse(null);
		
		if(oldSkills != null) {
			if(newSkills != null) {
				for(int i = 0; i < 26; i++) {
					newSkills.setXp(i, oldSkills.getXp(i));
					newSkills.setStaticLevel(i, oldSkills.getStaticLevel(i));
					newSkills.setLevel(i, oldSkills.getLevel(i));
				}
			}
		}
	}
	
	@SubscribeEvent
	public static void onPlayerChangedDimensionEvent(PlayerChangedDimensionEvent event) {
		ServerPlayerEntity player = (ServerPlayerEntity) event.getPlayer();
		if(!player.world.isRemote) {
			player.getCapability(SkillCapability.SKILL_CAP).ifPresent(c -> c.sync(player));
			player.getCapability(SkillCapability.SKILL_CAP).ifPresent(c -> c.modifyMaxHealth(player));
		}
	}
	
	@SubscribeEvent
	public static void onRespawnEvent(PlayerRespawnEvent event) {
		ServerPlayerEntity player = (ServerPlayerEntity) event.getPlayer();
		if(!player.world.isRemote) {
			player.getCapability(SkillCapability.SKILL_CAP).ifPresent(c -> c.sync(player));
			player.getCapability(SkillCapability.SKILL_CAP).ifPresent(c -> c.modifyMaxHealth(player));
		}
	}
	
	@SubscribeEvent
	public static void onPlayerConnect(PlayerLoggedInEvent event) {
		ServerPlayerEntity player = (ServerPlayerEntity) event.getPlayer();
		if(!player.world.isRemote) {
			player.getCapability(SkillCapability.SKILL_CAP).ifPresent(c -> c.sync(player));
			player.getCapability(SkillCapability.SKILL_CAP).ifPresent(c -> c.modifyMaxHealth(player));
		}
	}
}

 

Full repo here, if you need to look at anything else.

 

 

Link to post
Share on other sites

I solved the problem.

 

The problem as that I didn't have the getMaxHealth() method saved within my serializeNBT() method, so the game still thought the value was zero. Saving that value fixed the problem.

Link to post
Share on other sites
  • LK1905 changed the title to [SOLVED][1.16.4] Changing an entity's max health

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

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



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • I'm reworking vanilla enchanting mechanics for my mod, and need to completely replace the functionality of vanilla enchanting tables and anvils. I have 2 new tile entity blocks set up, and I'm working on their functionality at the moment. When they're done, how would I replace the vanilla blocks with them? Alternatively, how would I replace the tile entity of the vanilla blocks with my own tile entities?
    • This is the crash report  Description: Initializing game java.lang.NoClassDefFoundError: slimeknights/tconstruct/library/client/MaterialRenderInfo     at lucraft.mods.lucraftcore.LCConfig$Materials.<init>(LCConfig.java:108)     at lucraft.mods.lucraftcore.LCConfig.<clinit>(LCConfig.java:36)     at lucraft.mods.lucraftcore.addonpacks.AddonPackHandler.getAddonPacksDir(AddonPackHandler.java:61)     at lucraft.mods.lucraftcore.core.LCClientHooks.insertAddonPackResourcePacks(LCClientHooks.java:37)     at net.minecraft.client.Minecraft.func_110436_a(Minecraft.java:789)     at net.minecraftforge.fml.client.FMLClientHandler.beginMinecraftLoading(FMLClientHandler.java:247)     at net.minecraft.client.Minecraft.func_71384_a(Minecraft.java:467)     at net.minecraft.client.Minecraft.func_99999_d(Minecraft.java:378)     at net.minecraft.client.main.Main.main(SourceFile:123)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     at java.lang.reflect.Method.invoke(Method.java:497)     at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)     at net.minecraft.launchwrapper.Launch.main(Launch.java:28) Caused by: java.lang.ClassNotFoundException: slimeknights.tconstruct.library.client.MaterialRenderInfo     at net.minecraft.launchwrapper.LaunchClassLoader.findClass(LaunchClassLoader.java:191)     at java.lang.ClassLoader.loadClass(ClassLoader.java:424)     at java.lang.ClassLoader.loadClass(ClassLoader.java:357)     ... 15 more Caused by: java.lang.NullPointerException A detailed walkthrough of the error, its code path and all known details is as follows: --------------------------------------------------------------------------------------- -- Head -- Thread: Client thread Stacktrace:     at lucraft.mods.lucraftcore.LCConfig$Materials.<init>(LCConfig.java:108)     at lucraft.mods.lucraftcore.LCConfig.<clinit>(LCConfig.java:36)     at lucraft.mods.lucraftcore.addonpacks.AddonPackHandler.getAddonPacksDir(AddonPackHandler.java:61)     at lucraft.mods.lucraftcore.core.LCClientHooks.insertAddonPackResourcePacks(LCClientHooks.java:37)     at net.minecraft.client.Minecraft.func_110436_a(Minecraft.java:789)     at net.minecraftforge.fml.client.FMLClientHandler.beginMinecraftLoading(FMLClientHandler.java:247)     at net.minecraft.client.Minecraft.func_71384_a(Minecraft.java:467) -- Initialization -- Details: Stacktrace:     at net.minecraft.client.Minecraft.func_99999_d(Minecraft.java:378)     at net.minecraft.client.main.Main.main(SourceFile:123)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     at java.lang.reflect.Method.invoke(Method.java:497)     at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)     at net.minecraft.launchwrapper.Launch.main(Launch.java:28)
    • So I started my mods and this error popped up  The game crashed whilst initializing game Error: java.lang.NoClassDefFoundError: slimeknights/tconstruct/library/client/MaterialRenderInfo Exit Code: -1 which is weird since I don't have tinkers construct installed or is that something else and how do I fix this.
    • I want to use an old mod for some reason, but the forge-1.12.2-14.23.4.2764 installer does not work with the following error. java.net.preferIPv4Stack=true Exception in thread "main" java.lang.reflect.InvocationTargetException     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)     at java.lang.reflect.Method.invoke(Unknown Source)     at net.minecraftforge.installer.Java6Gate.main(Java6Gate.java:48) Caused by: java.lang.ExceptionInInitializerError     at net.minecraftforge.installer.ClientInstall.getSponsorMessage(ClientInstall.java:401)     at net.minecraftforge.installer.InstallerAction.getSponsorMessage(InstallerAction.java:60)     at net.minecraftforge.installer.InstallerPanel.updateFilePath(InstallerPanel.java:334)     at net.minecraftforge.installer.InstallerPanel.<init>(InstallerPanel.java:315)     at net.minecraftforge.installer.SimpleInstaller.launchGui(SimpleInstaller.java:167)     at net.minecraftforge.installer.SimpleInstaller.main(SimpleInstaller.java:54)     ... 5 more Caused by: java.lang.ArrayIndexOutOfBoundsException: 1     at net.minecraftforge.installer.MirrorData.buildMirrorList(MirrorData.java:84)     at net.minecraftforge.installer.MirrorData.<init>(MirrorData.java:63)     at net.minecraftforge.installer.MirrorData.<clinit>(MirrorData.java:17)     ... 11 more Is there a way to install this version other than the installer? The method of copying and overwriting the version of the .minecraft folder according to the explanation site on the net did not work. I don't want such an answer [1.12 is no longer supported on this forum. Please update to a modern version of Minecraft to receive support.]
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.