From dd5560c93f9663ef5ccb19609df8546649cfe984 Mon Sep 17 00:00:00 2001 From: Up Date: Fri, 19 Jan 2024 20:07:26 +0100 Subject: [PATCH] possibly fix mana cost crash --- .../api/entities/ArcanusEntityAttributes.java | 5 +--- .../components/entity/ManaComponent.java | 28 +++++++++++-------- .../common/items/WizardArmorItem.java | 2 +- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/main/java/dev/cammiescorner/arcanuscontinuum/api/entities/ArcanusEntityAttributes.java b/src/main/java/dev/cammiescorner/arcanuscontinuum/api/entities/ArcanusEntityAttributes.java index d18365d..2f7a301 100644 --- a/src/main/java/dev/cammiescorner/arcanuscontinuum/api/entities/ArcanusEntityAttributes.java +++ b/src/main/java/dev/cammiescorner/arcanuscontinuum/api/entities/ArcanusEntityAttributes.java @@ -7,14 +7,11 @@ import net.minecraft.entity.attribute.ClampedEntityAttribute; import net.minecraft.entity.attribute.EntityAttribute; import net.minecraft.registry.RegistryKeys; -import org.jetbrains.annotations.ApiStatus; public class ArcanusEntityAttributes { private static boolean isInitialized = false; - - @ApiStatus.Internal - public static RegistryHandler ENTITY_ATTRIBUTES = RegistryHandler.create(RegistryKeys.ENTITY_ATTRIBUTE, Arcanus.MOD_ID); + private static final RegistryHandler ENTITY_ATTRIBUTES = RegistryHandler.create(RegistryKeys.ENTITY_ATTRIBUTE, Arcanus.MOD_ID); public static final RegistrySupplier MAX_MANA = ENTITY_ATTRIBUTES.register("max_mana", () -> new ClampedEntityAttribute(Arcanus.translationKey("attribute.name.generic", "max_mana"), 10d, 0d, 1024d).setTracked(true)); public static final RegistrySupplier MANA_REGEN = ENTITY_ATTRIBUTES.register("mana_regen", () -> new ClampedEntityAttribute(Arcanus.translationKey("attribute.name.generic", "mana_regen"), 0.5d, 0d, 1024d).setTracked(true)); diff --git a/src/main/java/dev/cammiescorner/arcanuscontinuum/common/components/entity/ManaComponent.java b/src/main/java/dev/cammiescorner/arcanuscontinuum/common/components/entity/ManaComponent.java index 49ec5b4..328f0c5 100644 --- a/src/main/java/dev/cammiescorner/arcanuscontinuum/common/components/entity/ManaComponent.java +++ b/src/main/java/dev/cammiescorner/arcanuscontinuum/common/components/entity/ManaComponent.java @@ -22,7 +22,7 @@ public ManaComponent(LivingEntity entity) { public void serverTick() { EntityAttributeInstance manaRegenAttr = entity.getAttributeInstance(ArcanusEntityAttributes.MANA_REGEN.get()); - if(manaRegenAttr != null && addMana(manaRegenAttr.getValue(), true)) + if (manaRegenAttr != null && addMana(manaRegenAttr.getValue(), true)) addMana(manaRegenAttr.getValue() / (entity instanceof PlayerEntity player && player.isCreative() ? 1 : 20), false); } @@ -43,7 +43,7 @@ public double getMana() { public void setMana(double mana) { this.mana = MathHelper.clamp(mana, 0, getTrueMaxMana()); - if(entity instanceof PlayerEntity) + if (entity instanceof PlayerEntity) ArcanusComponents.MANA_COMPONENT.sync(entity); } @@ -54,7 +54,7 @@ public double getTrueMaxMana() { public double getMaxMana() { EntityAttributeInstance maxManaAttr = entity.getAttributeInstance(ArcanusEntityAttributes.MAX_MANA.get()); - if(maxManaAttr != null) + if (maxManaAttr != null) return maxManaAttr.getValue(); return 0; @@ -63,34 +63,38 @@ public double getMaxMana() { public double getManaLock() { EntityAttributeInstance manaLockAttr = entity.getAttributeInstance(ArcanusEntityAttributes.MANA_LOCK.get()); - if(manaLockAttr != null) + if (manaLockAttr != null) return manaLockAttr.getValue(); return 0; } public boolean addMana(double amount, boolean simulate) { - if(getMana() < getTrueMaxMana()) { - if(!simulate) + if (getMana() < getTrueMaxMana()) { + if (!simulate) setMana(Math.min(getTrueMaxMana(), getMana() + amount)); return true; } - if(getMana() > getTrueMaxMana()) + if (getMana() > getTrueMaxMana()) setMana(getTrueMaxMana()); return false; } public boolean drainMana(double amount, boolean simulate) { - amount *= entity.getAttributeValue(ArcanusEntityAttributes.MANA_COST.get()); - if(getMana() >= 0 && getMana() + getTrueMaxMana() >= amount) { - if(!simulate) { - if(amount > getMana()) { + EntityAttributeInstance instance = entity.getAttributeInstance(ArcanusEntityAttributes.MANA_COST.get()); + if (instance != null) { + amount *= instance.getValue(); + } + + if (getMana() >= 0 && getMana() + getTrueMaxMana() >= amount) { + if (!simulate) { + if (amount > getMana()) { ArcanusComponents.addBurnout(entity, amount - getMana(), false); - if(ArcanusComponents.getBurnout(entity) >= ArcanusComponents.getMaxMana(entity) * 0.5F) + if (ArcanusComponents.getBurnout(entity) >= ArcanusComponents.getMaxMana(entity) * 0.5F) entity.damage(entity.getDamageSources().outOfWorld(), (float) Math.min(entity.getHealth() - 1, amount - getMana())); } diff --git a/src/main/java/dev/cammiescorner/arcanuscontinuum/common/items/WizardArmorItem.java b/src/main/java/dev/cammiescorner/arcanuscontinuum/common/items/WizardArmorItem.java index a7c5e71..3dd5172 100644 --- a/src/main/java/dev/cammiescorner/arcanuscontinuum/common/items/WizardArmorItem.java +++ b/src/main/java/dev/cammiescorner/arcanuscontinuum/common/items/WizardArmorItem.java @@ -57,7 +57,7 @@ public void inventoryTick(ItemStack stack, World world, Entity entity, int slot, super.inventoryTick(stack, world, entity, slot, selected); double manaDrain = 1; - if(!world.isClient() && stack.isDamaged() && entity instanceof LivingEntity livingEntity && livingEntity.getEquippedStack(getPreferredSlot()).equals(stack) && ArcanusComponents.getMana(livingEntity) >= manaDrain && ArcanusComponents.drainMana(livingEntity, manaDrain, false)) + if(!world.isClient() && stack.isDamaged() && entity instanceof LivingEntity livingEntity && livingEntity.getEquippedStack(getPreferredSlot()) == stack && ArcanusComponents.getMana(livingEntity) >= manaDrain && ArcanusComponents.drainMana(livingEntity, manaDrain, false)) stack.setDamage(stack.getDamage() - 1); }