| 1 | package net.minecraft.world.effect;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
|
| 5 | import java.util.Map;
|
| 6 | import java.util.Optional;
|
| 7 | import java.util.Map.Entry;
|
| 8 | import java.util.function.BiConsumer;
|
| 9 | import java.util.function.Function;
|
| 10 | import net.fabricmc.fabric.api.entity.event.v1.effect.FabricMobEffect;
|
| 11 | import net.minecraft.core.Holder;
|
| 12 | import net.minecraft.core.particles.ColorParticleOption;
|
| 13 | import net.minecraft.core.particles.ParticleOptions;
|
| 14 | import net.minecraft.core.particles.ParticleTypes;
|
| 15 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 16 | import net.minecraft.core.registries.Registries;
|
| 17 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 18 | import net.minecraft.network.chat.Component;
|
| 19 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 20 | import net.minecraft.network.codec.StreamCodec;
|
| 21 | import net.minecraft.resources.Identifier;
|
| 22 | import net.minecraft.server.level.ServerLevel;
|
| 23 | import net.minecraft.sounds.SoundEvent;
|
| 24 | import net.minecraft.util.ARGB;
|
| 25 | import net.minecraft.util.Mth;
|
| 26 | import net.minecraft.util.Util;
|
| 27 | import net.minecraft.world.damagesource.DamageSource;
|
| 28 | import net.minecraft.world.entity.Entity;
|
| 29 | import net.minecraft.world.entity.LivingEntity;
|
| 30 | import net.minecraft.world.entity.ai.attributes.Attribute;
|
| 31 | import net.minecraft.world.entity.ai.attributes.AttributeInstance;
|
| 32 | import net.minecraft.world.entity.ai.attributes.AttributeMap;
|
| 33 | import net.minecraft.world.entity.ai.attributes.AttributeModifier;
|
| 34 | import net.minecraft.world.flag.FeatureElement;
|
| 35 | import net.minecraft.world.flag.FeatureFlag;
|
| 36 | import net.minecraft.world.flag.FeatureFlagSet;
|
| 37 | import net.minecraft.world.flag.FeatureFlags;
|
| 38 | import org.jspecify.annotations.Nullable;
|
| 39 |
|
| 40 | public class MobEffect implements FeatureElement, FabricMobEffect {
|
| 41 | public static final Codec<Holder<MobEffect>> CODEC = BuiltInRegistries.MOB_EFFECT.holderByNameCodec();
|
| 42 | public static final StreamCodec<RegistryFriendlyByteBuf, Holder<MobEffect>> STREAM_CODEC = ByteBufCodecs.holderRegistry(Registries.MOB_EFFECT);
|
| 43 | private static final int AMBIENT_ALPHA = Mth.floor(38.25F);
|
| 44 | private final Map<Holder<Attribute>, MobEffect.AttributeTemplate> attributeModifiers = new Object2ObjectOpenHashMap<>();
|
| 45 | private final MobEffectCategory category;
|
| 46 | private final int color;
|
| 47 | private final Function<MobEffectInstance, ParticleOptions> particleFactory;
|
| 48 | private @Nullable String descriptionId;
|
| 49 | private int blendInDurationTicks;
|
| 50 | private int blendOutDurationTicks;
|
| 51 | private int blendOutAdvanceTicks;
|
| 52 | private Optional<SoundEvent> soundOnAdded = Optional.empty();
|
| 53 | private FeatureFlagSet requiredFeatures = FeatureFlags.VANILLA_SET;
|
| 54 |
|
| 55 | protected MobEffect(final MobEffectCategory category, final int color) {
|
| 56 | this.category = category;
|
| 57 | this.color = color;
|
| 58 | this.particleFactory = effectInstance -> {
|
| 59 | int alpha = effectInstance.isAmbient() ? AMBIENT_ALPHA : 255;
|
| 60 | return ColorParticleOption.create(ParticleTypes.ENTITY_EFFECT, ARGB.color(alpha, color));
|
| 61 | };
|
| 62 | }
|
| 63 |
|
| 64 | protected MobEffect(final MobEffectCategory category, final int color, final ParticleOptions particleOptions) {
|
| 65 | this.category = category;
|
| 66 | this.color = color;
|
| 67 | this.particleFactory = ignored -> particleOptions;
|
| 68 | }
|
| 69 |
|
| 70 | public int getBlendInDurationTicks() {
|
| 71 | return this.blendInDurationTicks;
|
| 72 | }
|
| 73 |
|
| 74 | public int getBlendOutDurationTicks() {
|
| 75 | return this.blendOutDurationTicks;
|
| 76 | }
|
| 77 |
|
| 78 | public int getBlendOutAdvanceTicks() {
|
| 79 | return this.blendOutAdvanceTicks;
|
| 80 | }
|
| 81 |
|
| 82 | public boolean applyEffectTick(final ServerLevel serverLevel, final LivingEntity mob, final int amplification) {
|
| 83 | return true;
|
| 84 | }
|
| 85 |
|
| 86 | public void applyInstantaneousEffect(
|
| 87 | final ServerLevel level, final @Nullable Entity source, final @Nullable Entity owner, final LivingEntity mob, final int amplification, final double scale
|
| 88 | ) {
|
| 89 | this.applyEffectTick(level, mob, amplification);
|
| 90 | }
|
| 91 |
|
| 92 | public boolean shouldApplyEffectTickThisTick(final int tickCount, final int amplification) {
|
| 93 | return false;
|
| 94 | }
|
| 95 |
|
| 96 | public void onEffectStarted(final LivingEntity mob, final int amplifier) {
|
| 97 | }
|
| 98 |
|
| 99 | public void onEffectAdded(final LivingEntity mob, final int amplifier) {
|
| 100 | this.soundOnAdded.ifPresent(soundEvent -> mob.level().playSound(null, mob.getX(), mob.getY(), mob.getZ(), soundEvent, mob.getSoundSource(), 1.0F, 1.0F));
|
| 101 | }
|
| 102 |
|
| 103 | public void onMobRemoved(final ServerLevel level, final LivingEntity mob, final int amplifier, final Entity.RemovalReason reason) {
|
| 104 | }
|
| 105 |
|
| 106 | public void onMobHurt(final ServerLevel level, final LivingEntity mob, final int amplifier, final DamageSource source, final float damage) {
|
| 107 | }
|
| 108 |
|
| 109 | public boolean isInstantaneous() {
|
| 110 | return false;
|
| 111 | }
|
| 112 |
|
| 113 | protected String getOrCreateDescriptionId() {
|
| 114 | if (this.descriptionId == null) {
|
| 115 | this.descriptionId = Util.makeDescriptionId("effect", BuiltInRegistries.MOB_EFFECT.getKey(this));
|
| 116 | }
|
| 117 |
|
| 118 | return this.descriptionId;
|
| 119 | }
|
| 120 |
|
| 121 | public String getDescriptionId() {
|
| 122 | return this.getOrCreateDescriptionId();
|
| 123 | }
|
| 124 |
|
| 125 | public Component getDisplayName() {
|
| 126 | return Component.translatable(this.getDescriptionId());
|
| 127 | }
|
| 128 |
|
| 129 | public MobEffectCategory getCategory() {
|
| 130 | return this.category;
|
| 131 | }
|
| 132 |
|
| 133 | public int getColor() {
|
| 134 | return this.color;
|
| 135 | }
|
| 136 |
|
| 137 | public MobEffect addAttributeModifier(final Holder<Attribute> attribute, final Identifier id, final double amount, final AttributeModifier.Operation operation) {
|
| 138 | this.attributeModifiers.put(attribute, new MobEffect.AttributeTemplate(id, amount, operation));
|
| 139 | return this;
|
| 140 | }
|
| 141 |
|
| 142 | public MobEffect setBlendDuration(final int ticks) {
|
| 143 | return this.setBlendDuration(ticks, ticks, ticks);
|
| 144 | }
|
| 145 |
|
| 146 | public MobEffect setBlendDuration(final int inTicks, final int outTicks, final int outAdvanceTicks) {
|
| 147 | this.blendInDurationTicks = inTicks;
|
| 148 | this.blendOutDurationTicks = outTicks;
|
| 149 | this.blendOutAdvanceTicks = outAdvanceTicks;
|
| 150 | return this;
|
| 151 | }
|
| 152 |
|
| 153 | public void createModifiers(final int amplifier, final BiConsumer<Holder<Attribute>, AttributeModifier> consumer) {
|
| 154 | this.attributeModifiers.forEach((attribute, template) -> consumer.accept((Holder<Attribute>)attribute, template.create(amplifier)));
|
| 155 | }
|
| 156 |
|
| 157 | public void removeAttributeModifiers(final AttributeMap attributes) {
|
| 158 | for (Entry<Holder<Attribute>, MobEffect.AttributeTemplate> entry : this.attributeModifiers.entrySet()) {
|
| 159 | AttributeInstance attribute = attributes.getInstance(entry.getKey());
|
| 160 | if (attribute != null) {
|
| 161 | attribute.removeModifier(entry.getValue().id());
|
| 162 | }
|
| 163 | }
|
| 164 | }
|
| 165 |
|
| 166 | public void addAttributeModifiers(final AttributeMap attributes, final int amplifier) {
|
| 167 | for (Entry<Holder<Attribute>, MobEffect.AttributeTemplate> entry : this.attributeModifiers.entrySet()) {
|
| 168 | AttributeInstance attribute = attributes.getInstance(entry.getKey());
|
| 169 | if (attribute != null) {
|
| 170 | attribute.removeModifier(entry.getValue().id());
|
| 171 | attribute.addPermanentModifier(entry.getValue().create(amplifier));
|
| 172 | }
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | public boolean isBeneficial() {
|
| 177 | return this.category == MobEffectCategory.BENEFICIAL;
|
| 178 | }
|
| 179 |
|
| 180 | public ParticleOptions createParticleOptions(final MobEffectInstance mobEffectInstance) {
|
| 181 | return this.particleFactory.apply(mobEffectInstance);
|
| 182 | }
|
| 183 |
|
| 184 | public MobEffect withSoundOnAdded(final SoundEvent soundEvent) {
|
| 185 | this.soundOnAdded = Optional.of(soundEvent);
|
| 186 | return this;
|
| 187 | }
|
| 188 |
|
| 189 | public MobEffect requiredFeatures(final FeatureFlag... flags) {
|
| 190 | this.requiredFeatures = FeatureFlags.REGISTRY.subset(flags);
|
| 191 | return this;
|
| 192 | }
|
| 193 |
|
| 194 | @Override
|
| 195 | public FeatureFlagSet requiredFeatures() {
|
| 196 | return this.requiredFeatures;
|
| 197 | }
|
| 198 |
|
| 199 | private record AttributeTemplate(Identifier id, double amount, AttributeModifier.Operation operation) {
|
| 200 | public AttributeModifier create(final int amplifier) {
|
| 201 | return new AttributeModifier(this.id, this.amount * (amplifier + 1), this.operation);
|
| 202 | }
|
| 203 | }
|
| 204 | }
|
| 205 |
|