Skip to content

Commit

Permalink
possibly fix mana cost crash
Browse files Browse the repository at this point in the history
  • Loading branch information
UpcraftLP committed Jan 19, 2024
1 parent fad494a commit dd5560c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<EntityAttribute> ENTITY_ATTRIBUTES = RegistryHandler.create(RegistryKeys.ENTITY_ATTRIBUTE, Arcanus.MOD_ID);
private static final RegistryHandler<EntityAttribute> ENTITY_ATTRIBUTES = RegistryHandler.create(RegistryKeys.ENTITY_ATTRIBUTE, Arcanus.MOD_ID);

public static final RegistrySupplier<EntityAttribute> 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<EntityAttribute> MANA_REGEN = ENTITY_ATTRIBUTES.register("mana_regen", () -> new ClampedEntityAttribute(Arcanus.translationKey("attribute.name.generic", "mana_regen"), 0.5d, 0d, 1024d).setTracked(true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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;
Expand All @@ -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()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit dd5560c

Please sign in to comment.