MobEffectInstance .java

net.minecraft.world.effect.MobEffectInstance (class): members: getParticleOptions, isInfiniteDuration, withScaledDuration, copyBlendState, getBlendFactor.

net.minecraft.world.effectclass456 linescommon14.3 KB

Implements

API Surface (31)

float getBlendFactor(final LivingEntity livingEntity, final float partialTickTime)
ParticleOptions getParticleOptions()
boolean update(final MobEffectInstance takeOver)
boolean isInfiniteDuration()
boolean endsWithin(final int ticks)
MobEffectInstance withScaledDuration(final float scale)
int mapDuration(final Int2IntFunction mapper)
Holder<MobEffect> getEffect()
int getDuration()
int getAmplifier()
boolean isAmbient()
boolean isVisible()
boolean showIcon()
boolean tickServer(final ServerLevel serverLevel, final LivingEntity target, final Runnable onEffectUpdate)
void tickClient()
void onEffectStarted(final LivingEntity mob)
void onMobRemoved(final ServerLevel level, final LivingEntity mob, final Entity.RemovalReason reason)
void onMobHurt(final ServerLevel level, final LivingEntity mob, final DamageSource source, final float damage)
String getDescriptionId()
String toString()
boolean equals(final Object o)
int hashCode()
int compareTo(final MobEffectInstance o)
void onEffectAdded(final LivingEntity livingEntity)
boolean is(final Holder<MobEffect> effect)
void copyBlendState(final MobEffectInstance instance)
void skipBlending()
void setImmediate(final MobEffectInstance instance)
void copyFrom(final MobEffectInstance.BlendState other)
void tick(final MobEffectInstance instance)
float getFactor(final LivingEntity livingEntity, final float partialTickTime)

Fields & Constants (14)

LOGGER
INFINITE_DURATION
MIN_AMPLIFIER
MAX_AMPLIFIER
CODEC
STREAM_CODEC
effect
duration
amplifier
ambient
visible
showIcon
hiddenEffect
blendState

String literals (11)

", Duration: "
", Particles: false"
", Show Icon: false"
"infinite"
"MobEffectInstance.Details"
"amplifier"
"duration"
"ambient"
"show_particles"
"show_icon"
"hidden_effect"

Full Source

MobEffectInstance.java456 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.effect;
2
3
import com.google.common.collect.ComparisonChain;
4
import com.mojang.logging.LogUtils;
5
import com.mojang.serialization.Codec;
6
import com.mojang.serialization.MapCodec;
7
import com.mojang.serialization.codecs.RecordCodecBuilder;
8
import io.netty.buffer.ByteBuf;
9
import it.unimi.dsi.fastutil.ints.Int2IntFunction;
10
import java.util.Optional;
11
import net.minecraft.core.Holder;
12
import net.minecraft.core.particles.ParticleOptions;
13
import net.minecraft.network.RegistryFriendlyByteBuf;
14
import net.minecraft.network.codec.ByteBufCodecs;
15
import net.minecraft.network.codec.StreamCodec;
16
import net.minecraft.server.level.ServerLevel;
17
import net.minecraft.util.ExtraCodecs;
18
import net.minecraft.util.Mth;
19
import net.minecraft.world.damagesource.DamageSource;
20
import net.minecraft.world.entity.Entity;
21
import net.minecraft.world.entity.LivingEntity;
22
import org.jspecify.annotations.Nullable;
23
import org.slf4j.Logger;
24
25
public class MobEffectInstance implements Comparable<MobEffectInstance> {
26
	private static final Logger LOGGER = LogUtils.getLogger();
27
	public static final int INFINITE_DURATION = -1;
28
	public static final int MIN_AMPLIFIER = 0;
29
	public static final int MAX_AMPLIFIER = 255;
30
	public static final Codec<MobEffectInstance> CODEC = RecordCodecBuilder.create(
31
		i -> i.group(
32
				MobEffect.CODEC.fieldOf("id").forGetter(MobEffectInstance::getEffect), MobEffectInstance.Details.MAP_CODEC.forGetter(MobEffectInstance::asDetails)
33
			)
34
			.apply(i, MobEffectInstance::new)
35
	);
36
	public static final StreamCodec<RegistryFriendlyByteBuf, MobEffectInstance> STREAM_CODEC = StreamCodec.composite(
37
		MobEffect.STREAM_CODEC, MobEffectInstance::getEffect, MobEffectInstance.Details.STREAM_CODEC, MobEffectInstance::asDetails, MobEffectInstance::new
38
	);
39
	private final Holder<MobEffect> effect;
40
	private int duration;
41
	private int amplifier;
42
	private boolean ambient;
43
	private boolean visible;
44
	private boolean showIcon;
45
	private @Nullable MobEffectInstance hiddenEffect;
46
	private final MobEffectInstance.BlendState blendState = new MobEffectInstance.BlendState();
47
48
	public MobEffectInstance(final Holder<MobEffect> effect) {
49
		this(effect, 0, 0);
50
	}
51
52
	public MobEffectInstance(final Holder<MobEffect> effect, final int duration) {
53
		this(effect, duration, 0);
54
	}
55
56
	public MobEffectInstance(final Holder<MobEffect> effect, final int duration, final int amplifier) {
57
		this(effect, duration, amplifier, false, true);
58
	}
59
60
	public MobEffectInstance(final Holder<MobEffect> effect, final int duration, final int amplifier, final boolean ambient, final boolean visible) {
61
		this(effect, duration, amplifier, ambient, visible, visible);
62
	}
63
64
	public MobEffectInstance(
65
		final Holder<MobEffect> effect, final int duration, final int amplifier, final boolean ambient, final boolean visible, final boolean showIcon
66
	) {
67
		this(effect, duration, amplifier, ambient, visible, showIcon, null);
68
	}
69
70
	public MobEffectInstance(
71
		final Holder<MobEffect> effect,
72
		final int duration,
73
		final int amplifier,
74
		final boolean ambient,
75
		final boolean visible,
76
		final boolean showIcon,
77
		final @Nullable MobEffectInstance hiddenEffect
78
	) {
79
		this.effect = effect;
80
		this.duration = duration;
81
		this.amplifier = Mth.clamp(amplifier, 0, 255);
82
		this.ambient = ambient;
83
		this.visible = visible;
84
		this.showIcon = showIcon;
85
		this.hiddenEffect = hiddenEffect;
86
	}
87
88
	public MobEffectInstance(final MobEffectInstance copy) {
89
		this.effect = copy.effect;
90
		this.setDetailsFrom(copy);
91
	}
92
93
	private MobEffectInstance(final Holder<MobEffect> effect, final MobEffectInstance.Details details) {
94
		this(
95
			effect,
96
			details.duration(),
97
			details.amplifier(),
98
			details.ambient(),
99
			details.showParticles(),
100
			details.showIcon(),
101
			details.hiddenEffect().map(hidden -> new MobEffectInstance(effect, hidden)).orElse(null)
102
		);
103
	}
104
105
	private MobEffectInstance.Details asDetails() {
106
		return new MobEffectInstance.Details(
107
			this.getAmplifier(),
108
			this.getDuration(),
109
			this.isAmbient(),
110
			this.isVisible(),
111
			this.showIcon(),
112
			Optional.ofNullable(this.hiddenEffect).map(MobEffectInstance::asDetails)
113
		);
114
	}
115
116
	public float getBlendFactor(final LivingEntity livingEntity, final float partialTickTime) {
117
		return this.blendState.getFactor(livingEntity, partialTickTime);
118
	}
119
120
	public ParticleOptions getParticleOptions() {
121
		return this.effect.value().createParticleOptions(this);
122
	}
123
124
	private void setDetailsFrom(final MobEffectInstance copy) {
125
		this.duration = copy.duration;
126
		this.amplifier = copy.amplifier;
127
		this.ambient = copy.ambient;
128
		this.visible = copy.visible;
129
		this.showIcon = copy.showIcon;
130
	}
131
132
	public boolean update(final MobEffectInstance takeOver) {
133
		if (!this.effect.equals(takeOver.effect)) {
134
			LOGGER.warn("This method should only be called for matching effects!");
135
		}
136
137
		boolean changed = false;
138
		if (takeOver.amplifier > this.amplifier) {
139
			if (takeOver.isShorterDurationThan(this)) {
140
				MobEffectInstance prevHiddenEffect = this.hiddenEffect;
141
				this.hiddenEffect = new MobEffectInstance(this);
142
				this.hiddenEffect.hiddenEffect = prevHiddenEffect;
143
			}
144
145
			this.amplifier = takeOver.amplifier;
146
			this.duration = takeOver.duration;
147
			changed = true;
148
		} else if (this.isShorterDurationThan(takeOver)) {
149
			if (takeOver.amplifier == this.amplifier) {
150
				this.duration = takeOver.duration;
151
				changed = true;
152
			} else if (this.hiddenEffect == null) {
153
				this.hiddenEffect = new MobEffectInstance(takeOver);
154
			} else {
155
				this.hiddenEffect.update(takeOver);
156
			}
157
		}
158
159
		if (!takeOver.ambient && this.ambient || changed) {
160
			this.ambient = takeOver.ambient;
161
			changed = true;
162
		}
163
164
		if (takeOver.visible != this.visible) {
165
			this.visible = takeOver.visible;
166
			changed = true;
167
		}
168
169
		if (takeOver.showIcon != this.showIcon) {
170
			this.showIcon = takeOver.showIcon;
171
			changed = true;
172
		}
173
174
		return changed;
175
	}
176
177
	private boolean isShorterDurationThan(final MobEffectInstance other) {
178
		return !this.isInfiniteDuration() && (this.duration < other.duration || other.isInfiniteDuration());
179
	}
180
181
	public boolean isInfiniteDuration() {
182
		return this.duration == -1;
183
	}
184
185
	public boolean endsWithin(final int ticks) {
186
		return !this.isInfiniteDuration() && this.duration <= ticks;
187
	}
188
189
	public MobEffectInstance withScaledDuration(final float scale) {
190
		MobEffectInstance copy = new MobEffectInstance(this);
191
		copy.duration = copy.mapDuration(duration -> Math.max(Mth.floor(duration * scale), 1));
192
		return copy;
193
	}
194
195
	public int mapDuration(final Int2IntFunction mapper) {
196
		return !this.isInfiniteDuration() && this.duration != 0 ? mapper.applyAsInt(this.duration) : this.duration;
197
	}
198
199
	public Holder<MobEffect> getEffect() {
200
		return this.effect;
201
	}
202
203
	public int getDuration() {
204
		return this.duration;
205
	}
206
207
	public int getAmplifier() {
208
		return this.amplifier;
209
	}
210
211
	public boolean isAmbient() {
212
		return this.ambient;
213
	}
214
215
	public boolean isVisible() {
216
		return this.visible;
217
	}
218
219
	public boolean showIcon() {
220
		return this.showIcon;
221
	}
222
223
	public boolean tickServer(final ServerLevel serverLevel, final LivingEntity target, final Runnable onEffectUpdate) {
224
		if (!this.hasRemainingDuration()) {
225
			return false;
226
		}
227
228
		int tickCount = this.isInfiniteDuration() ? target.tickCount : this.duration;
229
		if (this.effect.value().shouldApplyEffectTickThisTick(tickCount, this.amplifier) && !this.effect.value().applyEffectTick(serverLevel, target, this.amplifier)
230
			)
231
		 {
232
			return false;
233
		}
234
235
		this.tickDownDuration();
236
		if (this.downgradeToHiddenEffect()) {
237
			onEffectUpdate.run();
238
		}
239
240
		return this.hasRemainingDuration();
241
	}
242
243
	public void tickClient() {
244
		if (this.hasRemainingDuration()) {
245
			this.tickDownDuration();
246
			this.downgradeToHiddenEffect();
247
		}
248
249
		this.blendState.tick(this);
250
	}
251
252
	private boolean hasRemainingDuration() {
253
		return this.isInfiniteDuration() || this.duration > 0;
254
	}
255
256
	private void tickDownDuration() {
257
		if (this.hiddenEffect != null) {
258
			this.hiddenEffect.tickDownDuration();
259
		}
260
261
		this.duration = this.mapDuration(d -> d - 1);
262
	}
263
264
	private boolean downgradeToHiddenEffect() {
265
		if (this.duration == 0 && this.hiddenEffect != null) {
266
			this.setDetailsFrom(this.hiddenEffect);
267
			this.hiddenEffect = this.hiddenEffect.hiddenEffect;
268
			return true;
269
		} else {
270
			return false;
271
		}
272
	}
273
274
	public void onEffectStarted(final LivingEntity mob) {
275
		this.effect.value().onEffectStarted(mob, this.amplifier);
276
	}
277
278
	public void onMobRemoved(final ServerLevel level, final LivingEntity mob, final Entity.RemovalReason reason) {
279
		this.effect.value().onMobRemoved(level, mob, this.amplifier, reason);
280
	}
281
282
	public void onMobHurt(final ServerLevel level, final LivingEntity mob, final DamageSource source, final float damage) {
283
		this.effect.value().onMobHurt(level, mob, this.amplifier, source, damage);
284
	}
285
286
	public String getDescriptionId() {
287
		return this.effect.value().getDescriptionId();
288
	}
289
290
	@Override
291
	public String toString() {
292
		String result;
293
		if (this.amplifier > 0) {
294
			result = this.getDescriptionId() + " x " + (this.amplifier + 1) + ", Duration: " + this.describeDuration();
295
		} else {
296
			result = this.getDescriptionId() + ", Duration: " + this.describeDuration();
297
		}
298
299
		if (!this.visible) {
300
			result = result + ", Particles: false";
301
		}
302
303
		if (!this.showIcon) {
304
			result = result + ", Show Icon: false";
305
		}
306
307
		return result;
308
	}
309
310
	private String describeDuration() {
311
		return this.isInfiniteDuration() ? "infinite" : Integer.toString(this.duration);
312
	}
313
314
	@Override
315
	public boolean equals(final Object o) {
316
		if (this == o) {
317
			return true;
318
		} else {
319
			return !(o instanceof MobEffectInstance that)
320
				? false
321
				: this.duration == that.duration
322
					&& this.amplifier == that.amplifier
323
					&& this.ambient == that.ambient
324
					&& this.visible == that.visible
325
					&& this.showIcon == that.showIcon
326
					&& this.effect.equals(that.effect);
327
		}
328
	}
329
330
	@Override
331
	public int hashCode() {
332
		int result = this.effect.hashCode();
333
		result = 31 * result + this.duration;
334
		result = 31 * result + this.amplifier;
335
		result = 31 * result + (this.ambient ? 1 : 0);
336
		result = 31 * result + (this.visible ? 1 : 0);
337
		return 31 * result + (this.showIcon ? 1 : 0);
338
	}
339
340
	public int compareTo(final MobEffectInstance o) {
341
		int updateCutOff = 32147;
342
		return (this.getDuration() <= 32147 || o.getDuration() <= 32147) && (!this.isAmbient() || !o.isAmbient())
343
			? ComparisonChain.start()
344
				.compareFalseFirst(this.isAmbient(), o.isAmbient())
345
				.compareFalseFirst(this.isInfiniteDuration(), o.isInfiniteDuration())
346
				.compare(this.getDuration(), o.getDuration())
347
				.compare(this.getEffect().value().getColor(), o.getEffect().value().getColor())
348
				.result()
349
			: ComparisonChain.start().compare(this.isAmbient(), o.isAmbient()).compare(this.getEffect().value().getColor(), o.getEffect().value().getColor()).result();
350
	}
351
352
	public void onEffectAdded(final LivingEntity livingEntity) {
353
		this.effect.value().onEffectAdded(livingEntity, this.amplifier);
354
	}
355
356
	public boolean is(final Holder<MobEffect> effect) {
357
		return this.effect.equals(effect);
358
	}
359
360
	public void copyBlendState(final MobEffectInstance instance) {
361
		this.blendState.copyFrom(instance.blendState);
362
	}
363
364
	public void skipBlending() {
365
		this.blendState.setImmediate(this);
366
	}
367
368
	private static class BlendState {
369
		private float factor;
370
		private float factorPreviousFrame;
371
372
		public void setImmediate(final MobEffectInstance instance) {
373
			this.factor = hasEffect(instance) ? 1.0F : 0.0F;
374
			this.factorPreviousFrame = this.factor;
375
		}
376
377
		public void copyFrom(final MobEffectInstance.BlendState other) {
378
			this.factor = other.factor;
379
			this.factorPreviousFrame = other.factorPreviousFrame;
380
		}
381
382
		public void tick(final MobEffectInstance instance) {
383
			this.factorPreviousFrame = this.factor;
384
			boolean hasEffect = hasEffect(instance);
385
			float target = hasEffect ? 1.0F : 0.0F;
386
			if (this.factor != target) {
387
				MobEffect effect = instance.getEffect().value();
388
				int blendDuration = hasEffect ? effect.getBlendInDurationTicks() : effect.getBlendOutDurationTicks();
389
				if (blendDuration == 0) {
390
					this.factor = target;
391
				} else {
392
					float maxDeltaPerTick = 1.0F / blendDuration;
393
					this.factor = this.factor + Mth.clamp(target - this.factor, -maxDeltaPerTick, maxDeltaPerTick);
394
				}
395
			}
396
		}
397
398
		private static boolean hasEffect(final MobEffectInstance instance) {
399
			return !instance.endsWithin(instance.getEffect().value().getBlendOutAdvanceTicks());
400
		}
401
402
		public float getFactor(final LivingEntity livingEntity, final float partialTickTime) {
403
			if (livingEntity.isRemoved()) {
404
				this.factorPreviousFrame = this.factor;
405
			}
406
407
			return Mth.lerp(partialTickTime, this.factorPreviousFrame, this.factor);
408
		}
409
	}
410
411
	private record Details(int amplifier, int duration, boolean ambient, boolean showParticles, boolean showIcon, Optional<MobEffectInstance.Details> hiddenEffect) {
412
		public static final MapCodec<MobEffectInstance.Details> MAP_CODEC = MapCodec.recursive(
413
			"MobEffectInstance.Details",
414
			codec -> RecordCodecBuilder.mapCodec(
415
				i -> i.group(
416
						ExtraCodecs.UNSIGNED_BYTE.optionalFieldOf("amplifier", 0).forGetter(MobEffectInstance.Details::amplifier),
417
						Codec.INT.optionalFieldOf("duration", 0).forGetter(MobEffectInstance.Details::duration),
418
						Codec.BOOL.optionalFieldOf("ambient", false).forGetter(MobEffectInstance.Details::ambient),
419
						Codec.BOOL.optionalFieldOf("show_particles", true).forGetter(MobEffectInstance.Details::showParticles),
420
						Codec.BOOL.optionalFieldOf("show_icon").forGetter(d -> Optional.of(d.showIcon())),
421
						codec.optionalFieldOf("hidden_effect").forGetter(MobEffectInstance.Details::hiddenEffect)
422
					)
423
					.apply(i, MobEffectInstance.Details::create)
424
			)
425
		);
426
		public static final StreamCodec<ByteBuf, MobEffectInstance.Details> STREAM_CODEC = StreamCodec.recursive(
427
			subCodec -> StreamCodec.composite(
428
				ByteBufCodecs.VAR_INT,
429
				MobEffectInstance.Details::amplifier,
430
				ByteBufCodecs.VAR_INT,
431
				MobEffectInstance.Details::duration,
432
				ByteBufCodecs.BOOL,
433
				MobEffectInstance.Details::ambient,
434
				ByteBufCodecs.BOOL,
435
				MobEffectInstance.Details::showParticles,
436
				ByteBufCodecs.BOOL,
437
				MobEffectInstance.Details::showIcon,
438
				subCodec.apply(ByteBufCodecs::optional),
439
				MobEffectInstance.Details::hiddenEffect,
440
				MobEffectInstance.Details::new
441
			)
442
		);
443
444
		private static MobEffectInstance.Details create(
445
			final int amplifier,
446
			final int duration,
447
			final boolean ambient,
448
			final boolean showParticles,
449
			final Optional<Boolean> showIcon,
450
			final Optional<MobEffectInstance.Details> hiddenEffect
451
		) {
452
			return new MobEffectInstance.Details(amplifier, duration, ambient, showParticles, showIcon.orElse(showParticles), hiddenEffect);
453
		}
454
	}
455
}
456

Decompiled Minecraft 26.3 source of MobEffectInstance (net/minecraft/world/effect/MobEffectInstance.java) with Mojang official mappings, stored verbatim on the site.