This commit is contained in:
Sollace 2023-07-06 17:27:03 +01:00
parent 19a9132fe3
commit eb5fee8692
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB
2 changed files with 36 additions and 16 deletions

View file

@ -32,7 +32,11 @@ import com.minelittlepony.unicopia.server.world.DragonBreathStore;
import com.minelittlepony.unicopia.trinkets.TrinketsDelegate; import com.minelittlepony.unicopia.trinkets.TrinketsDelegate;
import com.minelittlepony.unicopia.util.*; import com.minelittlepony.unicopia.util.*;
import it.unimi.dsi.fastutil.floats.Float2ObjectFunction;
import net.minecraft.entity.*; import net.minecraft.entity.*;
import net.minecraft.entity.attribute.EntityAttribute;
import net.minecraft.entity.attribute.EntityAttributeInstance;
import net.minecraft.entity.attribute.EntityAttributeModifier;
import net.minecraft.entity.damage.DamageSource; import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.data.*; import net.minecraft.entity.data.*;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
@ -46,6 +50,7 @@ import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundEvents; import net.minecraft.sound.SoundEvents;
import net.minecraft.util.Hand; import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3d;
public abstract class Living<T extends LivingEntity> implements Equine<T>, Caster<T>, Transmittable { public abstract class Living<T extends LivingEntity> implements Equine<T>, Caster<T>, Transmittable {
@ -214,6 +219,31 @@ public abstract class Living<T extends LivingEntity> implements Equine<T>, Caste
updateDragonBreath(); updateDragonBreath();
} }
public void updateAttributeModifier(UUID id, EntityAttribute attribute, float desiredValue, Float2ObjectFunction<EntityAttributeModifier> modifierSupplier, boolean permanent) {
@Nullable
EntityAttributeInstance instance = asEntity().getAttributeInstance(attribute);
if (instance == null) {
return;
}
@Nullable
EntityAttributeModifier modifier = instance.getModifier(id);
if (!MathHelper.approximatelyEquals(desiredValue, modifier == null ? 0 : modifier.getValue())) {
if (modifier != null) {
instance.removeModifier(modifier);
}
if (desiredValue != 0) {
if (permanent) {
instance.addPersistentModifier(modifierSupplier.get(desiredValue));
} else {
instance.addTemporaryModifier(modifierSupplier.get(desiredValue));
}
}
}
}
public boolean canBeSeenBy(Entity entity) { public boolean canBeSeenBy(Entity entity) {
return !isInvisible() return !isInvisible()
&& getSpellSlot() && getSpellSlot()

View file

@ -17,6 +17,7 @@ import com.minelittlepony.unicopia.particle.UParticles;
import com.minelittlepony.unicopia.trinkets.TrinketsDelegate; import com.minelittlepony.unicopia.trinkets.TrinketsDelegate;
import com.minelittlepony.unicopia.util.VecHelper; import com.minelittlepony.unicopia.util.VecHelper;
import it.unimi.dsi.fastutil.floats.Float2ObjectFunction;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.item.v1.FabricItemSettings; import net.fabricmc.fabric.api.item.v1.FabricItemSettings;
@ -55,6 +56,9 @@ public class AlicornAmuletItem extends AmuletItem implements ItemTracker.Trackab
EntityAttributes.GENERIC_ARMOR_TOUGHNESS, 0.001F, EntityAttributes.GENERIC_ARMOR_TOUGHNESS, 0.001F,
EntityAttributes.GENERIC_ARMOR, 0.01F EntityAttributes.GENERIC_ARMOR, 0.01F
); );
private static final Float2ObjectFunction<EntityAttributeModifier> EFFECT_FACTORY = v -> {
return new EntityAttributeModifier(EFFECT_UUID, "Alicorn Amulet Modifier", v, EntityAttributeModifier.Operation.ADDITION);
};
public AlicornAmuletItem(FabricItemSettings settings) { public AlicornAmuletItem(FabricItemSettings settings) {
super(settings, 0, ImmutableMultimap.of()); super(settings, 0, ImmutableMultimap.of());
@ -159,12 +163,7 @@ public class AlicornAmuletItem extends AmuletItem implements ItemTracker.Trackab
} }
EFFECT_SCALES.keySet().forEach(attribute -> { EFFECT_SCALES.keySet().forEach(attribute -> {
EntityAttributeInstance instance = entity.getAttributeInstance(attribute); wearer.updateAttributeModifier(EFFECT_UUID, attribute, 0F, EFFECT_FACTORY, false);
@Nullable
EntityAttributeModifier modifier = instance.getModifier(EFFECT_UUID);
if (modifier != null) {
instance.removeModifier(modifier);
}
}); });
} }
@ -268,16 +267,7 @@ public class AlicornAmuletItem extends AmuletItem implements ItemTracker.Trackab
if (fullSecond) { if (fullSecond) {
EFFECT_SCALES.entrySet().forEach(attribute -> { EFFECT_SCALES.entrySet().forEach(attribute -> {
float seconds = (float)attachedTicks / ItemTracker.SECONDS; float seconds = (float)attachedTicks / ItemTracker.SECONDS;
EntityAttributeInstance instance = living.asEntity().getAttributeInstance(attribute.getKey()); living.updateAttributeModifier(EFFECT_UUID, attribute.getKey(), attribute.getValue() * seconds, EFFECT_FACTORY, false);
@Nullable
EntityAttributeModifier modifier = instance.getModifier(EFFECT_UUID);
float desiredValue = attribute.getValue() * seconds;
if (!MathHelper.approximatelyEquals(desiredValue, modifier == null ? 0 : modifier.getValue())) {
if (modifier != null) {
instance.removeModifier(modifier);
}
instance.addTemporaryModifier(new EntityAttributeModifier(EFFECT_UUID, "Alicorn Amulet Modifier", attribute.getValue() * seconds, EntityAttributeModifier.Operation.ADDITION));
}
}); });
} }
} }