| 1 | package net.minecraft.world.entity;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import java.util.ArrayList;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Map;
|
| 7 | import net.minecraft.core.component.DataComponentGetter;
|
| 8 | import net.minecraft.core.component.DataComponentType;
|
| 9 | import net.minecraft.core.component.DataComponents;
|
| 10 | import net.minecraft.core.particles.ColorParticleOption;
|
| 11 | import net.minecraft.core.particles.ParticleOptions;
|
| 12 | import net.minecraft.core.particles.ParticleTypes;
|
| 13 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 14 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 15 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 16 | import net.minecraft.server.level.ServerLevel;
|
| 17 | import net.minecraft.util.ARGB;
|
| 18 | import net.minecraft.util.Mth;
|
| 19 | import net.minecraft.world.damagesource.DamageSource;
|
| 20 | import net.minecraft.world.effect.MobEffectInstance;
|
| 21 | import net.minecraft.world.item.alchemy.PotionContents;
|
| 22 | import net.minecraft.world.level.Level;
|
| 23 | import net.minecraft.world.level.material.PushReaction;
|
| 24 | import net.minecraft.world.level.storage.ValueInput;
|
| 25 | import net.minecraft.world.level.storage.ValueOutput;
|
| 26 | import org.jspecify.annotations.Nullable;
|
| 27 |
|
| 28 | public class AreaEffectCloud extends Entity implements TraceableEntity {
|
| 29 | private static final int TIME_BETWEEN_APPLICATIONS = 5;
|
| 30 | private static final EntityDataAccessor<Float> DATA_RADIUS = SynchedEntityData.defineId(AreaEffectCloud.class, EntityDataSerializers.FLOAT);
|
| 31 | private static final EntityDataAccessor<Boolean> DATA_WAITING = SynchedEntityData.defineId(AreaEffectCloud.class, EntityDataSerializers.BOOLEAN);
|
| 32 | private static final EntityDataAccessor<ParticleOptions> DATA_PARTICLE = SynchedEntityData.defineId(AreaEffectCloud.class, EntityDataSerializers.PARTICLE);
|
| 33 | private static final float MAX_RADIUS = 32.0F;
|
| 34 | private static final int DEFAULT_AGE = 0;
|
| 35 | private static final int DEFAULT_DURATION_ON_USE = 0;
|
| 36 | private static final float DEFAULT_RADIUS_ON_USE = 0.0F;
|
| 37 | private static final float DEFAULT_RADIUS_PER_TICK = 0.0F;
|
| 38 | private static final float DEFAULT_POTION_DURATION_SCALE = 1.0F;
|
| 39 | private static final float MINIMAL_RADIUS = 0.5F;
|
| 40 | private static final float DEFAULT_RADIUS = 3.0F;
|
| 41 | public static final float DEFAULT_WIDTH = 6.0F;
|
| 42 | public static final float HEIGHT = 0.5F;
|
| 43 | public static final int INFINITE_DURATION = -1;
|
| 44 | public static final int DEFAULT_LINGERING_DURATION = 600;
|
| 45 | private static final int DEFAULT_WAIT_TIME = 20;
|
| 46 | private static final int DEFAULT_REAPPLICATION_DELAY = 20;
|
| 47 | private static final ColorParticleOption DEFAULT_PARTICLE = ColorParticleOption.create(ParticleTypes.ENTITY_EFFECT, -1);
|
| 48 | private @Nullable ParticleOptions customParticle;
|
| 49 | private PotionContents potionContents = PotionContents.EMPTY;
|
| 50 | private float potionDurationScale = 1.0F;
|
| 51 | private final Map<Entity, Integer> victims = Maps.newHashMap();
|
| 52 | private int duration = -1;
|
| 53 | private int waitTime = 20;
|
| 54 | private int reapplicationDelay = 20;
|
| 55 | private int durationOnUse = 0;
|
| 56 | private float radiusOnUse = 0.0F;
|
| 57 | private float radiusPerTick = 0.0F;
|
| 58 | private @Nullable EntityReference<LivingEntity> owner;
|
| 59 |
|
| 60 | public AreaEffectCloud(final EntityType<? extends AreaEffectCloud> type, final Level level) {
|
| 61 | super(type, level);
|
| 62 | this.noPhysics = true;
|
| 63 | }
|
| 64 |
|
| 65 | public AreaEffectCloud(final Level level, final double x, final double y, final double z) {
|
| 66 | this(EntityTypes.AREA_EFFECT_CLOUD, level);
|
| 67 | this.setPos(x, y, z);
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 72 | entityData.define(DATA_RADIUS, 3.0F);
|
| 73 | entityData.define(DATA_WAITING, false);
|
| 74 | entityData.define(DATA_PARTICLE, DEFAULT_PARTICLE);
|
| 75 | }
|
| 76 |
|
| 77 | public void setRadius(final float radius) {
|
| 78 | if (!this.level().isClientSide()) {
|
| 79 | this.getEntityData().set(DATA_RADIUS, Mth.clamp(radius, 0.0F, 32.0F));
|
| 80 | }
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | public void refreshDimensions() {
|
| 85 | double x = this.getX();
|
| 86 | double y = this.getY();
|
| 87 | double z = this.getZ();
|
| 88 | super.refreshDimensions();
|
| 89 | this.setPos(x, y, z);
|
| 90 | }
|
| 91 |
|
| 92 | public float getRadius() {
|
| 93 | return this.getEntityData().get(DATA_RADIUS);
|
| 94 | }
|
| 95 |
|
| 96 | public void setPotionContents(final PotionContents contents) {
|
| 97 | this.potionContents = contents;
|
| 98 | this.updateParticle();
|
| 99 | }
|
| 100 |
|
| 101 | public void setCustomParticle(final @Nullable ParticleOptions customParticle) {
|
| 102 | this.customParticle = customParticle;
|
| 103 | this.updateParticle();
|
| 104 | }
|
| 105 |
|
| 106 | public void setPotionDurationScale(final float scale) {
|
| 107 | this.potionDurationScale = scale;
|
| 108 | }
|
| 109 |
|
| 110 | private void updateParticle() {
|
| 111 | if (this.customParticle != null) {
|
| 112 | this.entityData.set(DATA_PARTICLE, this.customParticle);
|
| 113 | } else {
|
| 114 | int color = ARGB.opaque(this.potionContents.getColor());
|
| 115 | this.entityData.set(DATA_PARTICLE, ColorParticleOption.create(DEFAULT_PARTICLE.getType(), color));
|
| 116 | }
|
| 117 | }
|
| 118 |
|
| 119 | public void addEffect(final MobEffectInstance effect) {
|
| 120 | this.setPotionContents(this.potionContents.withEffectAdded(effect));
|
| 121 | }
|
| 122 |
|
| 123 | public ParticleOptions getParticle() {
|
| 124 | return this.getEntityData().get(DATA_PARTICLE);
|
| 125 | }
|
| 126 |
|
| 127 | protected void setWaiting(final boolean waiting) {
|
| 128 | this.getEntityData().set(DATA_WAITING, waiting);
|
| 129 | }
|
| 130 |
|
| 131 | public boolean isWaiting() {
|
| 132 | return this.getEntityData().get(DATA_WAITING);
|
| 133 | }
|
| 134 |
|
| 135 | public int getDuration() {
|
| 136 | return this.duration;
|
| 137 | }
|
| 138 |
|
| 139 | public void setDuration(final int duration) {
|
| 140 | this.duration = duration;
|
| 141 | }
|
| 142 |
|
| 143 | @Override
|
| 144 | public void tick() {
|
| 145 | super.tick();
|
| 146 | if (this.level() instanceof ServerLevel serverLevel) {
|
| 147 | this.serverTick(serverLevel);
|
| 148 | } else {
|
| 149 | this.clientTick();
|
| 150 | }
|
| 151 | }
|
| 152 |
|
| 153 | private void clientTick() {
|
| 154 | boolean isWaiting = this.isWaiting();
|
| 155 | float radius = this.getRadius();
|
| 156 | if (!isWaiting || !this.random.nextBoolean()) {
|
| 157 | ParticleOptions particle = this.getParticle();
|
| 158 | int particleCount;
|
| 159 | float particleRadius;
|
| 160 | if (isWaiting) {
|
| 161 | particleCount = 2;
|
| 162 | particleRadius = 0.2F;
|
| 163 | } else {
|
| 164 | particleCount = Mth.ceil((float) Math.PI * radius * radius);
|
| 165 | particleRadius = radius;
|
| 166 | }
|
| 167 |
|
| 168 | for (int i = 0; i < particleCount; i++) {
|
| 169 | float angle = this.random.nextFloat() * (float) (Math.PI * 2);
|
| 170 | float distance = Mth.sqrt(this.random.nextFloat()) * particleRadius;
|
| 171 | double x = this.getX() + Mth.cos(angle) * distance;
|
| 172 | double y = this.getY();
|
| 173 | double z = this.getZ() + Mth.sin(angle) * distance;
|
| 174 | if (particle.getType() == ParticleTypes.ENTITY_EFFECT) {
|
| 175 | if (isWaiting && this.random.nextBoolean()) {
|
| 176 | this.level().addAlwaysVisibleParticle(DEFAULT_PARTICLE, x, y, z, 0.0, 0.0, 0.0);
|
| 177 | } else {
|
| 178 | this.level().addAlwaysVisibleParticle(particle, x, y, z, 0.0, 0.0, 0.0);
|
| 179 | }
|
| 180 | } else if (isWaiting) {
|
| 181 | this.level().addAlwaysVisibleParticle(particle, x, y, z, 0.0, 0.0, 0.0);
|
| 182 | } else {
|
| 183 | this.level().addAlwaysVisibleParticle(particle, x, y, z, (0.5 - this.random.nextDouble()) * 0.15, 0.01F, (0.5 - this.random.nextDouble()) * 0.15);
|
| 184 | }
|
| 185 | }
|
| 186 | }
|
| 187 | }
|
| 188 |
|
| 189 | private void serverTick(final ServerLevel serverLevel) {
|
| 190 | if (this.duration != -1 && this.tickCount - this.waitTime >= this.duration) {
|
| 191 | this.discard();
|
| 192 | } else {
|
| 193 | boolean isWaiting = this.isWaiting();
|
| 194 | boolean shouldWait = this.tickCount < this.waitTime;
|
| 195 | if (isWaiting != shouldWait) {
|
| 196 | this.setWaiting(shouldWait);
|
| 197 | }
|
| 198 |
|
| 199 | if (!shouldWait) {
|
| 200 | float radius = this.getRadius();
|
| 201 | if (this.radiusPerTick != 0.0F) {
|
| 202 | radius += this.radiusPerTick;
|
| 203 | if (radius < 0.5F) {
|
| 204 | this.discard();
|
| 205 | return;
|
| 206 | }
|
| 207 |
|
| 208 | this.setRadius(radius);
|
| 209 | }
|
| 210 |
|
| 211 | if (this.tickCount % 5 == 0) {
|
| 212 | this.victims.entrySet().removeIf(entry -> this.tickCount >= entry.getValue());
|
| 213 | if (!this.potionContents.hasEffects()) {
|
| 214 | this.victims.clear();
|
| 215 | } else {
|
| 216 | List<MobEffectInstance> allEffects = new ArrayList<>();
|
| 217 | this.potionContents.forEachEffect(allEffects::add, this.potionDurationScale);
|
| 218 | List<LivingEntity> entities = this.level().getEntitiesOfClass(LivingEntity.class, this.getBoundingBox());
|
| 219 | if (!entities.isEmpty()) {
|
| 220 | for (LivingEntity entity : entities) {
|
| 221 | if (!this.victims.containsKey(entity) && entity.isAffectedByPotions() && !allEffects.stream().noneMatch(entity::canBeAffected)) {
|
| 222 | double xd = entity.getX() - this.getX();
|
| 223 | double zd = entity.getZ() - this.getZ();
|
| 224 | double dist = xd * xd + zd * zd;
|
| 225 | if (dist <= radius * radius) {
|
| 226 | this.victims.put(entity, this.tickCount + this.reapplicationDelay);
|
| 227 |
|
| 228 | for (MobEffectInstance effect : allEffects) {
|
| 229 | if (effect.getEffect().value().isInstantaneous()) {
|
| 230 | effect.getEffect().value().applyInstantaneousEffect(serverLevel, this, this.getOwner(), entity, effect.getAmplifier(), 0.5);
|
| 231 | } else {
|
| 232 | entity.addEffect(new MobEffectInstance(effect), this);
|
| 233 | }
|
| 234 | }
|
| 235 |
|
| 236 | if (this.radiusOnUse != 0.0F) {
|
| 237 | radius += this.radiusOnUse;
|
| 238 | if (radius < 0.5F) {
|
| 239 | this.discard();
|
| 240 | return;
|
| 241 | }
|
| 242 |
|
| 243 | this.setRadius(radius);
|
| 244 | }
|
| 245 |
|
| 246 | if (this.durationOnUse != 0 && this.duration != -1) {
|
| 247 | this.duration = this.duration + this.durationOnUse;
|
| 248 | if (this.duration <= 0) {
|
| 249 | this.discard();
|
| 250 | return;
|
| 251 | }
|
| 252 | }
|
| 253 | }
|
| 254 | }
|
| 255 | }
|
| 256 | }
|
| 257 | }
|
| 258 | }
|
| 259 | }
|
| 260 | }
|
| 261 | }
|
| 262 |
|
| 263 | public float getRadiusOnUse() {
|
| 264 | return this.radiusOnUse;
|
| 265 | }
|
| 266 |
|
| 267 | public void setRadiusOnUse(final float radiusOnUse) {
|
| 268 | this.radiusOnUse = radiusOnUse;
|
| 269 | }
|
| 270 |
|
| 271 | public float getRadiusPerTick() {
|
| 272 | return this.radiusPerTick;
|
| 273 | }
|
| 274 |
|
| 275 | public void setRadiusPerTick(final float radiusPerTick) {
|
| 276 | this.radiusPerTick = radiusPerTick;
|
| 277 | }
|
| 278 |
|
| 279 | public int getDurationOnUse() {
|
| 280 | return this.durationOnUse;
|
| 281 | }
|
| 282 |
|
| 283 | public void setDurationOnUse(final int durationOnUse) {
|
| 284 | this.durationOnUse = durationOnUse;
|
| 285 | }
|
| 286 |
|
| 287 | public int getWaitTime() {
|
| 288 | return this.waitTime;
|
| 289 | }
|
| 290 |
|
| 291 | public void setWaitTime(final int waitTime) {
|
| 292 | this.waitTime = waitTime;
|
| 293 | }
|
| 294 |
|
| 295 | public void setOwner(final @Nullable LivingEntity owner) {
|
| 296 | this.owner = EntityReference.of(owner);
|
| 297 | }
|
| 298 |
|
| 299 | public @Nullable LivingEntity getOwner() {
|
| 300 | return EntityReference.getLivingEntity(this.owner, this.level());
|
| 301 | }
|
| 302 |
|
| 303 | @Override
|
| 304 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 305 | this.tickCount = input.getIntOr("Age", 0);
|
| 306 | this.duration = input.getIntOr("Duration", -1);
|
| 307 | this.waitTime = input.getIntOr("WaitTime", 20);
|
| 308 | this.reapplicationDelay = input.getIntOr("ReapplicationDelay", 20);
|
| 309 | this.durationOnUse = input.getIntOr("DurationOnUse", 0);
|
| 310 | this.radiusOnUse = input.getFloatOr("RadiusOnUse", 0.0F);
|
| 311 | this.radiusPerTick = input.getFloatOr("RadiusPerTick", 0.0F);
|
| 312 | this.setRadius(input.getFloatOr("Radius", 3.0F));
|
| 313 | this.owner = EntityReference.read(input, "Owner");
|
| 314 | this.setCustomParticle(input.read("custom_particle", ParticleTypes.CODEC).orElse(null));
|
| 315 | this.setPotionContents(input.read("potion_contents", PotionContents.CODEC).orElse(PotionContents.EMPTY));
|
| 316 | this.potionDurationScale = input.getFloatOr("potion_duration_scale", 1.0F);
|
| 317 | }
|
| 318 |
|
| 319 | @Override
|
| 320 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 321 | output.putInt("Age", this.tickCount);
|
| 322 | output.putInt("Duration", this.duration);
|
| 323 | output.putInt("WaitTime", this.waitTime);
|
| 324 | output.putInt("ReapplicationDelay", this.reapplicationDelay);
|
| 325 | output.putInt("DurationOnUse", this.durationOnUse);
|
| 326 | output.putFloat("RadiusOnUse", this.radiusOnUse);
|
| 327 | output.putFloat("RadiusPerTick", this.radiusPerTick);
|
| 328 | output.putFloat("Radius", this.getRadius());
|
| 329 | output.storeNullable("custom_particle", ParticleTypes.CODEC, this.customParticle);
|
| 330 | EntityReference.store(this.owner, output, "Owner");
|
| 331 | if (!this.potionContents.equals(PotionContents.EMPTY)) {
|
| 332 | output.store("potion_contents", PotionContents.CODEC, this.potionContents);
|
| 333 | }
|
| 334 |
|
| 335 | if (this.potionDurationScale != 1.0F) {
|
| 336 | output.putFloat("potion_duration_scale", this.potionDurationScale);
|
| 337 | }
|
| 338 | }
|
| 339 |
|
| 340 | @Override
|
| 341 | public void onSyncedDataUpdated(final EntityDataAccessor<?> accessor) {
|
| 342 | if (DATA_RADIUS.equals(accessor)) {
|
| 343 | this.refreshDimensions();
|
| 344 | }
|
| 345 |
|
| 346 | super.onSyncedDataUpdated(accessor);
|
| 347 | }
|
| 348 |
|
| 349 | @Override
|
| 350 | public PushReaction getPistonPushReaction() {
|
| 351 | return PushReaction.IGNORE_ENTITY;
|
| 352 | }
|
| 353 |
|
| 354 | @Override
|
| 355 | public EntityDimensions getDimensions(final Pose pose) {
|
| 356 | return EntityDimensions.scalable(this.getRadius() * 2.0F, 0.5F);
|
| 357 | }
|
| 358 |
|
| 359 | @Override
|
| 360 | public final boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage) {
|
| 361 | return false;
|
| 362 | }
|
| 363 |
|
| 364 | @Override
|
| 365 | public <T> @Nullable T get(final DataComponentType<? extends T> type) {
|
| 366 | if (type == DataComponents.POTION_CONTENTS) {
|
| 367 | return castComponentValue((DataComponentType<T>)type, this.potionContents);
|
| 368 | } else {
|
| 369 | return type == DataComponents.POTION_DURATION_SCALE ? castComponentValue((DataComponentType<T>)type, this.potionDurationScale) : super.get(type);
|
| 370 | }
|
| 371 | }
|
| 372 |
|
| 373 | @Override
|
| 374 | protected void applyImplicitComponents(final DataComponentGetter components) {
|
| 375 | this.applyImplicitComponentIfPresent(components, DataComponents.POTION_CONTENTS);
|
| 376 | this.applyImplicitComponentIfPresent(components, DataComponents.POTION_DURATION_SCALE);
|
| 377 | super.applyImplicitComponents(components);
|
| 378 | }
|
| 379 |
|
| 380 | @Override
|
| 381 | protected <T> boolean applyImplicitComponent(final DataComponentType<T> type, final T value) {
|
| 382 | if (type == DataComponents.POTION_CONTENTS) {
|
| 383 | this.setPotionContents(castComponentValue(DataComponents.POTION_CONTENTS, value));
|
| 384 | return true;
|
| 385 | } else if (type == DataComponents.POTION_DURATION_SCALE) {
|
| 386 | this.setPotionDurationScale(castComponentValue(DataComponents.POTION_DURATION_SCALE, value));
|
| 387 | return true;
|
| 388 | } else {
|
| 389 | return super.applyImplicitComponent(type, value);
|
| 390 | }
|
| 391 | }
|
| 392 | }
|
| 393 |
|