| 1 | package net.minecraft.world.entity;
|
| 2 |
|
| 3 | import java.util.function.BiConsumer;
|
| 4 | import java.util.function.BooleanSupplier;
|
| 5 | import java.util.function.Supplier;
|
| 6 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.world.level.block.LevelEvent;
|
| 9 | import net.minecraft.world.level.storage.ValueInput;
|
| 10 | import net.minecraft.world.level.storage.ValueOutput;
|
| 11 | import org.jspecify.annotations.Nullable;
|
| 12 |
|
| 13 | public class ConversionTracker<M extends Mob> {
|
| 14 | private static final int NOT_CONVERTING = -1;
|
| 15 | private static final int DEFAULT_AFFLICTION_TIME = 0;
|
| 16 | private final M mob;
|
| 17 | private final EntityDataAccessor<Boolean> conversionId;
|
| 18 | private final Supplier<EntityType<? extends M>> convertsTo;
|
| 19 | private final Supplier<@LevelEvent.Value Integer> conversionSound;
|
| 20 | private final BooleanSupplier isAfflicted;
|
| 21 | private final String afflictionTimeTag;
|
| 22 | private final int totalAfflictionTime;
|
| 23 | private final String conversionTimeTag;
|
| 24 | private final int totalConversionTime;
|
| 25 | private final @Nullable BiConsumer<M, ServerLevel> postConversionStep;
|
| 26 | private int afflictionTime = 0;
|
| 27 | private int conversionTime;
|
| 28 |
|
| 29 | public ConversionTracker(
|
| 30 | final M mob,
|
| 31 | final EntityDataAccessor<Boolean> conversionId,
|
| 32 | final Supplier<EntityType<? extends M>> convertsTo,
|
| 33 | final Supplier<@LevelEvent.Value Integer> conversionSound,
|
| 34 | final BooleanSupplier isAfflicted,
|
| 35 | final String afflictionTimeTag,
|
| 36 | final int totalAfflictionTime,
|
| 37 | final String conversionTimeTag,
|
| 38 | final int totalConversionTime,
|
| 39 | final @Nullable BiConsumer<M, ServerLevel> postConversionStep
|
| 40 | ) {
|
| 41 | this.mob = mob;
|
| 42 | this.conversionId = conversionId;
|
| 43 | this.convertsTo = convertsTo;
|
| 44 | this.conversionSound = conversionSound;
|
| 45 | this.isAfflicted = isAfflicted;
|
| 46 | this.afflictionTimeTag = afflictionTimeTag;
|
| 47 | this.totalAfflictionTime = totalAfflictionTime;
|
| 48 | this.conversionTimeTag = conversionTimeTag;
|
| 49 | this.totalConversionTime = totalConversionTime;
|
| 50 | this.postConversionStep = postConversionStep;
|
| 51 | }
|
| 52 |
|
| 53 | public void tick() {
|
| 54 | if (this.mob.level() instanceof ServerLevel serverLevel && this.mob.isAlive() && !this.mob.isNoAi()) {
|
| 55 | if (this.isAfflicted.getAsBoolean()) {
|
| 56 | if (this.isConverting()) {
|
| 57 | this.conversionTime--;
|
| 58 | if (this.conversionTime < 0) {
|
| 59 | this.doConversion(serverLevel);
|
| 60 | }
|
| 61 | } else {
|
| 62 | this.afflictionTime++;
|
| 63 | if (this.afflictionTime >= this.totalAfflictionTime) {
|
| 64 | this.startConversion(this.totalConversionTime);
|
| 65 | }
|
| 66 | }
|
| 67 | } else {
|
| 68 | this.afflictionTime = -1;
|
| 69 | this.setConverting(false);
|
| 70 | }
|
| 71 | }
|
| 72 | }
|
| 73 |
|
| 74 | public boolean isConverting() {
|
| 75 | return this.mob.getEntityData().get(this.conversionId);
|
| 76 | }
|
| 77 |
|
| 78 | private void setConverting(final boolean isConverting) {
|
| 79 | this.mob.getEntityData().set(this.conversionId, isConverting);
|
| 80 | }
|
| 81 |
|
| 82 | public void addAdditionalSaveData(final ValueOutput output) {
|
| 83 | output.putInt(this.conversionTimeTag, this.isConverting() ? this.conversionTime : -1);
|
| 84 | output.putInt(this.afflictionTimeTag, this.isAfflicted.getAsBoolean() ? this.afflictionTime : -1);
|
| 85 | }
|
| 86 |
|
| 87 | public void readAdditionalSaveData(final ValueInput input) {
|
| 88 | this.afflictionTime = input.getIntOr(this.afflictionTimeTag, 0);
|
| 89 | int conversionTime = input.getIntOr(this.conversionTimeTag, -1);
|
| 90 | if (conversionTime != -1) {
|
| 91 | this.startConversion(conversionTime);
|
| 92 | } else {
|
| 93 | this.setConverting(false);
|
| 94 | }
|
| 95 | }
|
| 96 |
|
| 97 | private void doConversion(final ServerLevel serverLevel) {
|
| 98 | this.mob.convertTo(this.convertsTo.get(), ConversionParams.single(this.mob, true, true), converted -> {
|
| 99 | if (this.postConversionStep != null) {
|
| 100 | this.postConversionStep.accept((M)converted, serverLevel);
|
| 101 | }
|
| 102 |
|
| 103 | if (!converted.isSilent()) {
|
| 104 | serverLevel.levelEvent(null, this.conversionSound.get(), converted.blockPosition(), 0);
|
| 105 | }
|
| 106 | });
|
| 107 | }
|
| 108 |
|
| 109 | public void startConversion(final int time) {
|
| 110 | this.conversionTime = time;
|
| 111 | this.setConverting(true);
|
| 112 | }
|
| 113 |
|
| 114 | public void setAfflictionTime(final int afflictionTime) {
|
| 115 | this.afflictionTime = afflictionTime;
|
| 116 | }
|
| 117 |
|
| 118 | public void setConversionTime(final int conversionTime) {
|
| 119 | this.conversionTime = conversionTime;
|
| 120 | }
|
| 121 | }
|
| 122 |
|