| 1 | package net.minecraft.world.entity.boss.enderdragon;
|
| 2 |
|
| 3 | import java.util.Optional;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 6 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 7 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 8 | import net.minecraft.server.level.ServerLevel;
|
| 9 | import net.minecraft.tags.DamageTypeTags;
|
| 10 | import net.minecraft.world.damagesource.DamageSource;
|
| 11 | import net.minecraft.world.entity.Entity;
|
| 12 | import net.minecraft.world.entity.EntityType;
|
| 13 | import net.minecraft.world.entity.EntityTypes;
|
| 14 | import net.minecraft.world.item.ItemStack;
|
| 15 | import net.minecraft.world.item.Items;
|
| 16 | import net.minecraft.world.level.Level;
|
| 17 | import net.minecraft.world.level.block.BaseFireBlock;
|
| 18 | import net.minecraft.world.level.dimension.end.EnderDragonFight;
|
| 19 | import net.minecraft.world.level.storage.ValueInput;
|
| 20 | import net.minecraft.world.level.storage.ValueOutput;
|
| 21 | import org.jspecify.annotations.Nullable;
|
| 22 |
|
| 23 | public class EndCrystal extends Entity {
|
| 24 | private static final EntityDataAccessor<Optional<BlockPos>> DATA_BEAM_TARGET = SynchedEntityData.defineId(
|
| 25 | EndCrystal.class, EntityDataSerializers.OPTIONAL_BLOCK_POS
|
| 26 | );
|
| 27 | private static final EntityDataAccessor<Boolean> DATA_SHOW_BOTTOM = SynchedEntityData.defineId(EndCrystal.class, EntityDataSerializers.BOOLEAN);
|
| 28 | private static final boolean DEFAULT_SHOW_BOTTOM = true;
|
| 29 | public int time;
|
| 30 |
|
| 31 | public EndCrystal(final EntityType<? extends EndCrystal> type, final Level level) {
|
| 32 | super(type, level);
|
| 33 | this.blocksBuilding = true;
|
| 34 | this.time = this.random.nextInt(100000);
|
| 35 | }
|
| 36 |
|
| 37 | public EndCrystal(final Level level, final double x, final double y, final double z) {
|
| 38 | this(EntityTypes.END_CRYSTAL, level);
|
| 39 | this.setPos(x, y, z);
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | protected Entity.MovementEmission getMovementEmission() {
|
| 44 | return Entity.MovementEmission.NONE;
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 49 | entityData.define(DATA_BEAM_TARGET, Optional.empty());
|
| 50 | entityData.define(DATA_SHOW_BOTTOM, true);
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public void tick() {
|
| 55 | this.time++;
|
| 56 | this.applyEffectsFromBlocks();
|
| 57 | this.handlePortal();
|
| 58 | if (this.level() instanceof ServerLevel serverLevel) {
|
| 59 | BlockPos pos = this.blockPosition();
|
| 60 | if (serverLevel.getDragonFight() != null && serverLevel.getBlockState(pos).isAir()) {
|
| 61 | serverLevel.setBlockAndUpdate(pos, BaseFireBlock.getState(serverLevel, pos));
|
| 62 | }
|
| 63 | }
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 68 | output.storeNullable("beam_target", BlockPos.CODEC, this.getBeamTarget());
|
| 69 | output.putBoolean("ShowBottom", this.showsBottom());
|
| 70 | }
|
| 71 |
|
| 72 | @Override
|
| 73 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 74 | this.setBeamTarget(input.read("beam_target", BlockPos.CODEC).orElse(null));
|
| 75 | this.setShowBottom(input.getBooleanOr("ShowBottom", true));
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public boolean isPickable() {
|
| 80 | return true;
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | public final boolean hurtClient(final DamageSource source) {
|
| 85 | return this.isInvulnerableToBase(source) ? false : !(source.getEntity() instanceof EnderDragon);
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | public final boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage) {
|
| 90 | if (this.isInvulnerableToBase(source)) {
|
| 91 | return false;
|
| 92 | }
|
| 93 |
|
| 94 | if (source.getEntity() instanceof EnderDragon) {
|
| 95 | return false;
|
| 96 | }
|
| 97 |
|
| 98 | if (!this.isRemoved()) {
|
| 99 | this.remove(Entity.RemovalReason.KILLED);
|
| 100 | if (!source.is(DamageTypeTags.IS_EXPLOSION)) {
|
| 101 | DamageSource damageSource = source.getEntity() != null ? this.damageSources().explosion(this, source.getEntity()) : null;
|
| 102 | level.explode(this, damageSource, null, this.getX(), this.getY(), this.getZ(), 6.0F, false, Level.ExplosionInteraction.BLOCK);
|
| 103 | }
|
| 104 |
|
| 105 | this.onDestroyedBy(level, source);
|
| 106 | }
|
| 107 |
|
| 108 | return true;
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | public void kill(final ServerLevel level) {
|
| 113 | this.onDestroyedBy(level, this.damageSources().generic());
|
| 114 | super.kill(level);
|
| 115 | }
|
| 116 |
|
| 117 | private void onDestroyedBy(final ServerLevel level, final DamageSource source) {
|
| 118 | EnderDragonFight fight = level.getDragonFight();
|
| 119 | if (fight != null) {
|
| 120 | fight.onCrystalDestroyed(this, source);
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | public void setBeamTarget(final @Nullable BlockPos target) {
|
| 125 | this.getEntityData().set(DATA_BEAM_TARGET, Optional.ofNullable(target));
|
| 126 | }
|
| 127 |
|
| 128 | public @Nullable BlockPos getBeamTarget() {
|
| 129 | return this.getEntityData().get(DATA_BEAM_TARGET).orElse(null);
|
| 130 | }
|
| 131 |
|
| 132 | public void setShowBottom(final boolean showBottom) {
|
| 133 | this.getEntityData().set(DATA_SHOW_BOTTOM, showBottom);
|
| 134 | }
|
| 135 |
|
| 136 | public boolean showsBottom() {
|
| 137 | return this.getEntityData().get(DATA_SHOW_BOTTOM);
|
| 138 | }
|
| 139 |
|
| 140 | @Override
|
| 141 | public boolean shouldRenderAtSqrDistance(final double distance) {
|
| 142 | return super.shouldRenderAtSqrDistance(distance) || this.getBeamTarget() != null;
|
| 143 | }
|
| 144 |
|
| 145 | @Override
|
| 146 | public ItemStack getPickResult() {
|
| 147 | return new ItemStack(Items.END_CRYSTAL);
|
| 148 | }
|
| 149 | }
|
| 150 |
|