From c2eaf673d12f846c2fa8b7bfde474ab1206c579f Mon Sep 17 00:00:00 2001 From: McHorse Date: Sat, 28 Dec 2019 18:28:49 +0000 Subject: [PATCH 1/4] Bump version --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 22ad307a..6ad711a0 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ # Metamorph gradle properties mclib=1.0.4 -version=1.1.10 +version=1.2 mc_version=1.10.2 forge_version=12.18.3.2511 From 55bc7b2c8ad74b4d3b35eb35b3e480643c8d305c Mon Sep 17 00:00:00 2001 From: McHorse Date: Sat, 28 Dec 2019 20:49:45 +0000 Subject: [PATCH 2/4] Move last health to capability, instead of morph --- .gitignore | 1 + .../metamorph/api/morphs/AbstractMorph.java | 101 +++--------------- .../metamorph/api/morphs/EntityMorph.java | 3 +- .../capabilities/morphing/IMorphing.java | 10 ++ .../capabilities/morphing/Morphing.java | 93 +++++++++++++++- .../morphing/MorphingStorage.java | 26 ++--- .../vanilla_pack/morphs/BlockMorph.java | 8 +- .../vanilla_pack/morphs/PlayerMorph.java | 4 +- .../vanilla_pack/morphs/UndeadMorph.java | 3 +- 9 files changed, 130 insertions(+), 119 deletions(-) diff --git a/.gitignore b/.gitignore index fdc4c03e..2599faa0 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ out *.iws *.iml .idea +classes/ # gradle build diff --git a/src/main/java/mchorse/metamorph/api/morphs/AbstractMorph.java b/src/main/java/mchorse/metamorph/api/morphs/AbstractMorph.java index f46b707a..4260367d 100644 --- a/src/main/java/mchorse/metamorph/api/morphs/AbstractMorph.java +++ b/src/main/java/mchorse/metamorph/api/morphs/AbstractMorph.java @@ -30,13 +30,6 @@ */ public abstract class AbstractMorph { - /* Abilities */ - - /** - * Morph settings - */ - public MorphSettings settings = MorphSettings.DEFAULT; - /* Meta information */ /** @@ -49,10 +42,12 @@ public abstract class AbstractMorph */ public boolean favorite = false; + /* Abilities */ + /** - * Health when the player morphed into this morph + * Morph settings */ - protected float lastHealth; + public MorphSettings settings = MorphSettings.DEFAULT; /* Rendering */ @@ -63,6 +58,15 @@ public abstract class AbstractMorph @SideOnly(Side.CLIENT) public Render renderer; + /* Clone code */ + + public static void copyBase(AbstractMorph from, AbstractMorph to) + { + to.name = from.name; + to.favorite = from.favorite; + to.settings = from.settings; + } + /* Render methods */ /** @@ -94,11 +98,6 @@ public boolean renderHand(EntityPlayer player, EnumHand hand) */ public void update(EntityLivingBase target, IMorphing cap) { - if (!Metamorph.proxy.config.disable_health) - { - this.setMaxHealth(target, this.settings.health); - } - if (this.settings.speed != 0.1F) { target.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(this.settings.speed); @@ -120,9 +119,6 @@ public void update(EntityLivingBase target, IMorphing cap) */ public void morph(EntityLivingBase target) { - this.lastHealth = (float)target.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).getBaseValue(); - this.setHealth(target, this.settings.health); - for (IAbility ability : this.settings.abilities) { ability.onMorph(target); @@ -137,9 +133,6 @@ public void morph(EntityLivingBase target) */ public void demorph(EntityLivingBase target) { - /* 20 is default player's health */ - this.setHealth(target, this.lastHealth <= 0.0F ? 20.0F : this.lastHealth); - for (IAbility ability : this.settings.abilities) { ability.onDemorph(target); @@ -190,68 +183,6 @@ public static void updateSizeDefault(EntityLivingBase target, float width, float } } - /* Adjusting health */ - - /** - * Set player's health proportional to the current health with given max - * health. - * - * @author asanetargoss - */ - protected void setHealth(EntityLivingBase target, float health) - { - if (Metamorph.proxy.config.disable_health) - { - return; - } - - float maxHealth = target.getMaxHealth(); - float currentHealth = target.getHealth(); - float ratio = currentHealth / maxHealth; - - // A sanity check to prevent "healing" health when morphing to and from - // a mob - // with essentially zero health - if (target instanceof EntityPlayer) - { - IMorphing capability = Morphing.get((EntityPlayer) target); - if (capability != null) - { - // Check if a health ratio makes sense for the old health value - if (maxHealth > IMorphing.REASONABLE_HEALTH_VALUE) - { - // If it makes sense, store that ratio in the capability - capability.setLastHealthRatio(ratio); - } - else if (health > IMorphing.REASONABLE_HEALTH_VALUE) - { - // If it doesn't make sense, BUT the new max health makes - // sense, retrieve the - // ratio from the capability and use that instead - ratio = capability.getLastHealthRatio(); - } - } - } - - this.setMaxHealth(target, health); - // We need to retrieve the max health of the target after modifiers are - // applied - // to get a sensible value - float proportionalHealth = target.getMaxHealth() * ratio; - target.setHealth(proportionalHealth <= 0.0F ? Float.MIN_VALUE : proportionalHealth); - } - - /** - * Set player's max health - */ - protected void setMaxHealth(EntityLivingBase target, float health) - { - if (target.getMaxHealth() != health) - { - target.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(health); - } - } - /* Safe shortcuts for activating action and attack */ /** @@ -281,9 +212,7 @@ public void attack(Entity target, EntityLivingBase source) * *

* IMPORTANT: when you subclass other morphs, don't forget to override - * their method with your own, because otherwise its going to create - * another {@link CustomMorph} instance, for example, instead of - * MyCustomMorph instance. + * their method with your own. *

*/ public abstract AbstractMorph clone(boolean isRemote); @@ -404,7 +333,6 @@ public void reset() public void toNBT(NBTTagCompound tag) { tag.setString("Name", this.name); - tag.setFloat("LastHealth", this.lastHealth); if (this.favorite) tag.setBoolean("Favorite", this.favorite); } @@ -417,7 +345,6 @@ public void fromNBT(NBTTagCompound tag) this.reset(); this.name = tag.getString("Name"); - this.lastHealth = tag.getFloat("LastHealth"); if (tag.hasKey("Favorite")) this.favorite = tag.getBoolean("Favorite"); } diff --git a/src/main/java/mchorse/metamorph/api/morphs/EntityMorph.java b/src/main/java/mchorse/metamorph/api/morphs/EntityMorph.java index 38f511e0..64c8fab3 100644 --- a/src/main/java/mchorse/metamorph/api/morphs/EntityMorph.java +++ b/src/main/java/mchorse/metamorph/api/morphs/EntityMorph.java @@ -750,8 +750,7 @@ public AbstractMorph clone(boolean isRemote) { EntityMorph morph = new EntityMorph(); - morph.name = this.name; - morph.settings = this.settings; + AbstractMorph.copyBase(this, morph); morph.entityData = this.entityData.copy(); return morph; diff --git a/src/main/java/mchorse/metamorph/capabilities/morphing/IMorphing.java b/src/main/java/mchorse/metamorph/capabilities/morphing/IMorphing.java index 0666208f..a378a95d 100644 --- a/src/main/java/mchorse/metamorph/capabilities/morphing/IMorphing.java +++ b/src/main/java/mchorse/metamorph/capabilities/morphing/IMorphing.java @@ -142,6 +142,16 @@ public interface IMorphing */ public void setSquidAir(int squidAir); + /** + * Get last health + */ + public float getLastHealth(); + + /** + * Set last health + */ + public void setLastHealth(float lastHealth); + /** * Update the player */ diff --git a/src/main/java/mchorse/metamorph/capabilities/morphing/Morphing.java b/src/main/java/mchorse/metamorph/capabilities/morphing/Morphing.java index 0827157d..a481a32d 100644 --- a/src/main/java/mchorse/metamorph/capabilities/morphing/Morphing.java +++ b/src/main/java/mchorse/metamorph/capabilities/morphing/Morphing.java @@ -8,6 +8,8 @@ import mchorse.metamorph.api.morphs.AbstractMorph; import mchorse.metamorph.client.gui.elements.GuiSurvivalMorphs; import net.minecraft.client.renderer.GlStateManager; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.entity.SharedMonsterAttributes; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.SoundEvents; import net.minecraft.nbt.NBTTagCompound; @@ -68,6 +70,11 @@ public class Morphing implements IMorphing */ private int squidAir = 300; + /** + * Last health that player had before morphing, should fix issue that people complain about + */ + private float lastHealth; + /** * GUI menu which is responsible for choosing morphs */ @@ -280,6 +287,9 @@ public boolean setCurrentMorph(AbstractMorph morph, EntityPlayer player, boolean if (player != null) { + this.lastHealth = (float) player.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).getBaseValue(); + this.setHealth(player, this.morph.settings.health); + this.morph.morph(player); } @@ -297,6 +307,9 @@ public void demorph(EntityPlayer player) this.morph.demorph(player); } + /* 20 is default player's health */ + this.setHealth(player, this.lastHealth <= 0.0F ? 20.0F : this.lastHealth); + this.setMorph(null, player == null ? false : player.worldObj.isRemote); } @@ -411,6 +424,18 @@ public void setSquidAir(int squidAir) this.squidAir = squidAir; } + @Override + public float getLastHealth() + { + return this.lastHealth; + } + + @Override + public void setLastHealth(float lastHealth) + { + this.lastHealth = lastHealth; + } + @Override public void update(EntityPlayer player) { @@ -429,19 +454,85 @@ public void update(EntityPlayer player) if (this.morph != null) { + if (!Metamorph.proxy.config.disable_health) + { + this.setMaxHealth(player, this.morph.settings.health); + } + this.morph.update(player, this); } } + /* Adjusting health */ + + /** + * Set player's health proportional to the current health with given max + * health. + * + * @author asanetargoss + */ + protected void setHealth(EntityLivingBase target, float health) + { + if (Metamorph.proxy.config.disable_health) + { + return; + } + + float maxHealth = target.getMaxHealth(); + float currentHealth = target.getHealth(); + float ratio = currentHealth / maxHealth; + + // A sanity check to prevent "healing" health when morphing to and from + // a mob with essentially zero health + if (target instanceof EntityPlayer) + { + IMorphing capability = Morphing.get((EntityPlayer) target); + if (capability != null) + { + // Check if a health ratio makes sense for the old health value + if (maxHealth > IMorphing.REASONABLE_HEALTH_VALUE) + { + // If it makes sense, store that ratio in the capability + capability.setLastHealthRatio(ratio); + } + else if (health > IMorphing.REASONABLE_HEALTH_VALUE) + { + // If it doesn't make sense, BUT the new max health makes + // sense, retrieve the ratio from the capability and use that instead + ratio = capability.getLastHealthRatio(); + } + } + } + + this.setMaxHealth(target, health); + // We need to retrieve the max health of the target after modifiers are + // applied to get a sensible value + float proportionalHealth = target.getMaxHealth() * ratio; + target.setHealth(proportionalHealth <= 0.0F ? Float.MIN_VALUE : proportionalHealth); + } + + /** + * Set player's max health + */ + protected void setMaxHealth(EntityLivingBase target, float health) + { + if (target.getMaxHealth() != health) + { + target.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(health); + } + } + @Override @SideOnly(Side.CLIENT) - public GuiSurvivalMorphs getOverlay() { + public GuiSurvivalMorphs getOverlay() + { if (this.overlay == null) { this.overlay = new GuiSurvivalMorphs(); this.overlay.setupMorphs(this); this.hasOverlay = true; } + return this.overlay; } } \ No newline at end of file diff --git a/src/main/java/mchorse/metamorph/capabilities/morphing/MorphingStorage.java b/src/main/java/mchorse/metamorph/capabilities/morphing/MorphingStorage.java index 26ecd739..09c64bb2 100644 --- a/src/main/java/mchorse/metamorph/capabilities/morphing/MorphingStorage.java +++ b/src/main/java/mchorse/metamorph/capabilities/morphing/MorphingStorage.java @@ -31,9 +31,11 @@ public NBTBase writeNBT(Capability capability, IMorphing instance, En { NBTTagCompound tag = new NBTTagCompound(); NBTTagList acquired = new NBTTagList(); - tag.setTag("lastHealthRatio", new NBTTagFloat(instance.getLastHealthRatio())); - tag.setTag("HasSquidAir", new NBTTagByte(instance.getHasSquidAir() ? (byte)1 : (byte)0)); - tag.setTag("SquidAir", new NBTTagInt(instance.getSquidAir())); + + tag.setFloat("lastHealthRatio", instance.getLastHealthRatio()); + tag.setBoolean("HasSquidAir", instance.getHasSquidAir()); + tag.setInteger("SquidAir", instance.getSquidAir()); + tag.setFloat("lastHealth", instance.getLastHealth()); if (instance.getCurrentMorph() != null) { @@ -63,11 +65,12 @@ public void readNBT(Capability capability, IMorphing instance, EnumFa { NBTTagCompound tag = (NBTTagCompound) nbt; NBTTagList acquired = tag.getTagList("Morphs", 10); - NBTTagList favorites = tag.getTagList("Favorites", 3); NBTTagCompound morphTag = tag.getCompoundTag("Morph"); + instance.setLastHealthRatio(tag.getFloat("LastHealthRatio")); - instance.setHasSquidAir(tag.getByte("HasSquidAir") == 1); + instance.setHasSquidAir(tag.getBoolean("HasSquidAir")); instance.setSquidAir(tag.getInteger("SquidAir")); + instance.setLastHealth(tag.getFloat("lastHealth")); if (!tag.hasNoTags()) { @@ -91,19 +94,6 @@ public void readNBT(Capability capability, IMorphing instance, EnumFa instance.setAcquiredMorphs(acquiredMorphs); } - - if (!favorites.hasNoTags()) - { - for (int i = 0; i < favorites.tagCount(); i++) - { - int index = favorites.getIntAt(i); - - if (index >= 0 && index < acquiredMorphs.size()) - { - acquiredMorphs.get(index).favorite = true; - } - } - } } } } diff --git a/src/main/java/mchorse/vanilla_pack/morphs/BlockMorph.java b/src/main/java/mchorse/vanilla_pack/morphs/BlockMorph.java index a7f957e3..27b92d3a 100644 --- a/src/main/java/mchorse/vanilla_pack/morphs/BlockMorph.java +++ b/src/main/java/mchorse/vanilla_pack/morphs/BlockMorph.java @@ -123,13 +123,7 @@ public AbstractMorph clone(boolean isRemote) { BlockMorph morph = new BlockMorph(); - morph.name = this.name; - morph.settings = this.settings; - - if (isRemote) - { - morph.renderer = this.renderer; - } + AbstractMorph.copyBase(this, morph); /* Data relevant only to this morph */ morph.block = this.block; diff --git a/src/main/java/mchorse/vanilla_pack/morphs/PlayerMorph.java b/src/main/java/mchorse/vanilla_pack/morphs/PlayerMorph.java index d419b104..56cfc9a0 100644 --- a/src/main/java/mchorse/vanilla_pack/morphs/PlayerMorph.java +++ b/src/main/java/mchorse/vanilla_pack/morphs/PlayerMorph.java @@ -163,8 +163,8 @@ public AbstractMorph clone(boolean isRemote) { PlayerMorph morph = new PlayerMorph(); - morph.name = this.name; - morph.settings = this.settings; + AbstractMorph.copyBase(this, morph); + morph.entityData = this.entityData.copy(); morph.profile = this.profile; diff --git a/src/main/java/mchorse/vanilla_pack/morphs/UndeadMorph.java b/src/main/java/mchorse/vanilla_pack/morphs/UndeadMorph.java index 72050712..cb0ba8b3 100644 --- a/src/main/java/mchorse/vanilla_pack/morphs/UndeadMorph.java +++ b/src/main/java/mchorse/vanilla_pack/morphs/UndeadMorph.java @@ -51,8 +51,7 @@ public AbstractMorph clone(boolean isRemote) { UndeadMorph morph = new UndeadMorph(); - morph.name = this.name; - morph.settings = this.settings; + AbstractMorph.copyBase(this, morph); morph.entityData = this.entityData.copy(); return morph; From defc693da7b78a2c9052f9727626c64d2cba0301 Mon Sep 17 00:00:00 2001 From: McHorse Date: Sat, 28 Dec 2019 23:46:40 +0000 Subject: [PATCH 3/4] Fix NPE and lastHealth value saving for morphed state as well --- .../metamorph/capabilities/morphing/Morphing.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/java/mchorse/metamorph/capabilities/morphing/Morphing.java b/src/main/java/mchorse/metamorph/capabilities/morphing/Morphing.java index a481a32d..0b2fb29a 100644 --- a/src/main/java/mchorse/metamorph/capabilities/morphing/Morphing.java +++ b/src/main/java/mchorse/metamorph/capabilities/morphing/Morphing.java @@ -278,6 +278,11 @@ public boolean setCurrentMorph(AbstractMorph morph, EntityPlayer player, boolean if (force || creative || this.acquiredMorph(morph)) { + if (this.morph == null) + { + this.lastHealth = (float) player.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).getBaseValue(); + } + if (player != null && this.morph != null) { this.morph.demorph(player); @@ -287,7 +292,6 @@ public boolean setCurrentMorph(AbstractMorph morph, EntityPlayer player, boolean if (player != null) { - this.lastHealth = (float) player.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).getBaseValue(); this.setHealth(player, this.morph.settings.health); this.morph.morph(player); @@ -307,8 +311,11 @@ public void demorph(EntityPlayer player) this.morph.demorph(player); } - /* 20 is default player's health */ - this.setHealth(player, this.lastHealth <= 0.0F ? 20.0F : this.lastHealth); + if (player != null) + { + /* 20 is default player's health */ + this.setHealth(player, this.lastHealth <= 0.0F ? 20.0F : this.lastHealth); + } this.setMorph(null, player == null ? false : player.worldObj.isRemote); } From 0fd5f9d01b40e24b63f93d868e02a9cc75c03c0d Mon Sep 17 00:00:00 2001 From: McHorse Date: Sun, 5 Jan 2020 08:59:07 +0000 Subject: [PATCH 4/4] :facepalm: --- src/main/java/mchorse/metamorph/bodypart/MorphBodyPart.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/mchorse/metamorph/bodypart/MorphBodyPart.java b/src/main/java/mchorse/metamorph/bodypart/MorphBodyPart.java index b75cc260..c42bb211 100644 --- a/src/main/java/mchorse/metamorph/bodypart/MorphBodyPart.java +++ b/src/main/java/mchorse/metamorph/bodypart/MorphBodyPart.java @@ -97,7 +97,6 @@ public void render(EntityLivingBase entity, float partialTicks) } @Override - @SideOnly(Side.CLIENT) public void update(EntityLivingBase entity, IMorphing cap) { entity = this.useTarget ? entity : this.entity;