| 1 | package net.minecraft.world.entity.monster.illager;
|
| 2 |
|
| 3 | import java.util.EnumSet;
|
| 4 | import java.util.function.IntFunction;
|
| 5 | import net.minecraft.core.particles.ColorParticleOption;
|
| 6 | import net.minecraft.core.particles.ParticleTypes;
|
| 7 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 8 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 9 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 10 | import net.minecraft.server.level.ServerLevel;
|
| 11 | import net.minecraft.sounds.SoundEvent;
|
| 12 | import net.minecraft.util.ByIdMap;
|
| 13 | import net.minecraft.util.Mth;
|
| 14 | import net.minecraft.world.entity.EntityType;
|
| 15 | import net.minecraft.world.entity.LivingEntity;
|
| 16 | import net.minecraft.world.entity.ai.goal.Goal;
|
| 17 | import net.minecraft.world.level.Level;
|
| 18 | import net.minecraft.world.level.storage.ValueInput;
|
| 19 | import net.minecraft.world.level.storage.ValueOutput;
|
| 20 | import org.jspecify.annotations.Nullable;
|
| 21 |
|
| 22 | public abstract class SpellcasterIllager extends AbstractIllager {
|
| 23 | private static final EntityDataAccessor<Byte> DATA_SPELL_CASTING_ID = SynchedEntityData.defineId(SpellcasterIllager.class, EntityDataSerializers.BYTE);
|
| 24 | private static final int DEFAULT_SPELLCASTING_TICKS = 0;
|
| 25 | protected int spellCastingTickCount = 0;
|
| 26 | private SpellcasterIllager.IllagerSpell currentSpell = SpellcasterIllager.IllagerSpell.NONE;
|
| 27 |
|
| 28 | protected SpellcasterIllager(final EntityType<? extends SpellcasterIllager> type, final Level level) {
|
| 29 | super(type, level);
|
| 30 | }
|
| 31 |
|
| 32 | @Override
|
| 33 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 34 | super.defineSynchedData(entityData);
|
| 35 | entityData.define(DATA_SPELL_CASTING_ID, (byte)0);
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 40 | super.readAdditionalSaveData(input);
|
| 41 | this.spellCastingTickCount = input.getIntOr("SpellTicks", 0);
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 46 | super.addAdditionalSaveData(output);
|
| 47 | output.putInt("SpellTicks", this.spellCastingTickCount);
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | public AbstractIllager.IllagerArmPose getArmPose() {
|
| 52 | if (this.isCastingSpell()) {
|
| 53 | return AbstractIllager.IllagerArmPose.SPELLCASTING;
|
| 54 | } else {
|
| 55 | return this.isCelebrating() ? AbstractIllager.IllagerArmPose.CELEBRATING : AbstractIllager.IllagerArmPose.CROSSED;
|
| 56 | }
|
| 57 | }
|
| 58 |
|
| 59 | public boolean isCastingSpell() {
|
| 60 | return this.level().isClientSide() ? this.entityData.get(DATA_SPELL_CASTING_ID) > 0 : this.spellCastingTickCount > 0;
|
| 61 | }
|
| 62 |
|
| 63 | public void setIsCastingSpell(final SpellcasterIllager.IllagerSpell spell) {
|
| 64 | this.currentSpell = spell;
|
| 65 | this.entityData.set(DATA_SPELL_CASTING_ID, (byte)spell.id);
|
| 66 | }
|
| 67 |
|
| 68 | protected SpellcasterIllager.IllagerSpell getCurrentSpell() {
|
| 69 | return !this.level().isClientSide() ? this.currentSpell : SpellcasterIllager.IllagerSpell.byId(this.entityData.get(DATA_SPELL_CASTING_ID));
|
| 70 | }
|
| 71 |
|
| 72 | @Override
|
| 73 | protected void customServerAiStep(final ServerLevel level) {
|
| 74 | super.customServerAiStep(level);
|
| 75 | if (this.spellCastingTickCount > 0) {
|
| 76 | this.spellCastingTickCount--;
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | @Override
|
| 81 | public void tick() {
|
| 82 | super.tick();
|
| 83 | if (this.level().isClientSide() && this.isCastingSpell()) {
|
| 84 | SpellcasterIllager.IllagerSpell spell = this.getCurrentSpell();
|
| 85 | float red = (float)spell.spellColor[0];
|
| 86 | float green = (float)spell.spellColor[1];
|
| 87 | float blue = (float)spell.spellColor[2];
|
| 88 | float bodyAngle = this.yBodyRot * (float) (Math.PI / 180.0) + Mth.cos(this.tickCount * 0.6662F) * 0.25F;
|
| 89 | float cos = Mth.cos(bodyAngle);
|
| 90 | float sin = Mth.sin(bodyAngle);
|
| 91 | double handDistance = 0.6 * this.getScale();
|
| 92 | double handHeight = 1.8 * this.getScale();
|
| 93 | this.level()
|
| 94 | .addParticle(
|
| 95 | ColorParticleOption.create(ParticleTypes.ENTITY_EFFECT, red, green, blue),
|
| 96 | this.getX() + cos * handDistance,
|
| 97 | this.getY() + handHeight,
|
| 98 | this.getZ() + sin * handDistance,
|
| 99 | 0.0,
|
| 100 | 0.0,
|
| 101 | 0.0
|
| 102 | );
|
| 103 | this.level()
|
| 104 | .addParticle(
|
| 105 | ColorParticleOption.create(ParticleTypes.ENTITY_EFFECT, red, green, blue),
|
| 106 | this.getX() - cos * handDistance,
|
| 107 | this.getY() + handHeight,
|
| 108 | this.getZ() - sin * handDistance,
|
| 109 | 0.0,
|
| 110 | 0.0,
|
| 111 | 0.0
|
| 112 | );
|
| 113 | }
|
| 114 | }
|
| 115 |
|
| 116 | protected int getSpellCastingTime() {
|
| 117 | return this.spellCastingTickCount;
|
| 118 | }
|
| 119 |
|
| 120 | protected abstract SoundEvent getCastingSoundEvent();
|
| 121 |
|
| 122 | protected enum IllagerSpell {
|
| 123 | NONE(0, 0.0, 0.0, 0.0),
|
| 124 | SUMMON_VEX(1, 0.7, 0.7, 0.8),
|
| 125 | FANGS(2, 0.4, 0.3, 0.35),
|
| 126 | WOLOLO(3, 0.7, 0.5, 0.2),
|
| 127 | DISAPPEAR(4, 0.3, 0.3, 0.8),
|
| 128 | BLINDNESS(5, 0.1, 0.1, 0.2);
|
| 129 |
|
| 130 | private static final IntFunction<SpellcasterIllager.IllagerSpell> BY_ID = ByIdMap.continuous(e -> e.id, values(), ByIdMap.OutOfBoundsStrategy.ZERO);
|
| 131 | private final int id;
|
| 132 | private final double[] spellColor;
|
| 133 |
|
| 134 | IllagerSpell(final int id, final double red, final double green, final double blue) {
|
| 135 | this.id = id;
|
| 136 | this.spellColor = new double[]{red, green, blue};
|
| 137 | }
|
| 138 |
|
| 139 | public static SpellcasterIllager.IllagerSpell byId(final int id) {
|
| 140 | return BY_ID.apply(id);
|
| 141 | }
|
| 142 | }
|
| 143 |
|
| 144 | protected class SpellcasterCastingSpellGoal extends Goal {
|
| 145 | public SpellcasterCastingSpellGoal() {
|
| 146 | this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.LOOK));
|
| 147 | }
|
| 148 |
|
| 149 | @Override
|
| 150 | public boolean canUse() {
|
| 151 | return SpellcasterIllager.this.getSpellCastingTime() > 0;
|
| 152 | }
|
| 153 |
|
| 154 | @Override
|
| 155 | public void start() {
|
| 156 | super.start();
|
| 157 | SpellcasterIllager.this.navigation.stop();
|
| 158 | }
|
| 159 |
|
| 160 | @Override
|
| 161 | public void stop() {
|
| 162 | super.stop();
|
| 163 | SpellcasterIllager.this.setIsCastingSpell(SpellcasterIllager.IllagerSpell.NONE);
|
| 164 | }
|
| 165 |
|
| 166 | @Override
|
| 167 | public void tick() {
|
| 168 | if (SpellcasterIllager.this.getTarget() != null) {
|
| 169 | SpellcasterIllager.this.getLookControl()
|
| 170 | .setLookAt(SpellcasterIllager.this.getTarget(), SpellcasterIllager.this.getMaxHeadYRot(), SpellcasterIllager.this.getMaxHeadXRot());
|
| 171 | }
|
| 172 | }
|
| 173 | }
|
| 174 |
|
| 175 | protected abstract class SpellcasterUseSpellGoal extends Goal {
|
| 176 | protected int attackWarmupDelay;
|
| 177 | protected int nextAttackTickCount;
|
| 178 |
|
| 179 | @Override
|
| 180 | public boolean canUse() {
|
| 181 | LivingEntity target = SpellcasterIllager.this.getTarget();
|
| 182 | if (target == null || !target.isAlive()) {
|
| 183 | return false;
|
| 184 | } else {
|
| 185 | return SpellcasterIllager.this.isCastingSpell() ? false : SpellcasterIllager.this.tickCount >= this.nextAttackTickCount;
|
| 186 | }
|
| 187 | }
|
| 188 |
|
| 189 | @Override
|
| 190 | public boolean canContinueToUse() {
|
| 191 | LivingEntity target = SpellcasterIllager.this.getTarget();
|
| 192 | return target != null && target.isAlive() && this.attackWarmupDelay > 0;
|
| 193 | }
|
| 194 |
|
| 195 | @Override
|
| 196 | public void start() {
|
| 197 | this.attackWarmupDelay = this.adjustedTickDelay(this.getCastWarmupTime());
|
| 198 | SpellcasterIllager.this.spellCastingTickCount = this.getCastingTime();
|
| 199 | this.nextAttackTickCount = SpellcasterIllager.this.tickCount + this.getCastingInterval();
|
| 200 | SoundEvent spellPrepareSound = this.getSpellPrepareSound();
|
| 201 | if (spellPrepareSound != null) {
|
| 202 | SpellcasterIllager.this.playSound(spellPrepareSound, 1.0F, 1.0F);
|
| 203 | }
|
| 204 |
|
| 205 | SpellcasterIllager.this.setIsCastingSpell(this.getSpell());
|
| 206 | }
|
| 207 |
|
| 208 | @Override
|
| 209 | public void tick() {
|
| 210 | this.attackWarmupDelay--;
|
| 211 | if (this.attackWarmupDelay == 0) {
|
| 212 | this.performSpellCasting();
|
| 213 | SpellcasterIllager.this.playSound(SpellcasterIllager.this.getCastingSoundEvent(), 1.0F, 1.0F);
|
| 214 | }
|
| 215 | }
|
| 216 |
|
| 217 | protected abstract void performSpellCasting();
|
| 218 |
|
| 219 | protected int getCastWarmupTime() {
|
| 220 | return 20;
|
| 221 | }
|
| 222 |
|
| 223 | protected abstract int getCastingTime();
|
| 224 |
|
| 225 | protected abstract int getCastingInterval();
|
| 226 |
|
| 227 | protected abstract @Nullable SoundEvent getSpellPrepareSound();
|
| 228 |
|
| 229 | protected abstract SpellcasterIllager.IllagerSpell getSpell();
|
| 230 | }
|
| 231 | }
|
| 232 |
|