| 1 | package net.minecraft.world.entity.item;
|
| 2 |
|
| 3 | import java.util.Optional;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.particles.ParticleTypes;
|
| 6 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 7 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 8 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 9 | import net.minecraft.server.level.ServerLevel;
|
| 10 | import net.minecraft.util.Mth;
|
| 11 | import net.minecraft.util.RandomSource;
|
| 12 | import net.minecraft.world.damagesource.DamageSource;
|
| 13 | import net.minecraft.world.entity.Entity;
|
| 14 | import net.minecraft.world.entity.EntityReference;
|
| 15 | import net.minecraft.world.entity.EntityType;
|
| 16 | import net.minecraft.world.entity.EntityTypes;
|
| 17 | import net.minecraft.world.entity.LivingEntity;
|
| 18 | import net.minecraft.world.entity.MoveSimulationType;
|
| 19 | import net.minecraft.world.entity.MoverType;
|
| 20 | import net.minecraft.world.entity.TraceableEntity;
|
| 21 | import net.minecraft.world.level.BlockGetter;
|
| 22 | import net.minecraft.world.level.Explosion;
|
| 23 | import net.minecraft.world.level.ExplosionDamageCalculator;
|
| 24 | import net.minecraft.world.level.Level;
|
| 25 | import net.minecraft.world.level.block.Blocks;
|
| 26 | import net.minecraft.world.level.block.state.BlockState;
|
| 27 | import net.minecraft.world.level.gamerules.GameRules;
|
| 28 | import net.minecraft.world.level.material.FluidState;
|
| 29 | import net.minecraft.world.level.portal.TeleportTransition;
|
| 30 | import net.minecraft.world.level.storage.ValueInput;
|
| 31 | import net.minecraft.world.level.storage.ValueOutput;
|
| 32 | import org.jspecify.annotations.Nullable;
|
| 33 |
|
| 34 | public class PrimedTnt extends Entity implements TraceableEntity {
|
| 35 | private static final EntityDataAccessor<Integer> DATA_FUSE_ID = SynchedEntityData.defineId(PrimedTnt.class, EntityDataSerializers.INT);
|
| 36 | private static final EntityDataAccessor<BlockState> DATA_BLOCK_STATE_ID = SynchedEntityData.defineId(PrimedTnt.class, EntityDataSerializers.BLOCK_STATE);
|
| 37 | public static final short DEFAULT_FUSE_TIME = 80;
|
| 38 | public static final int NO_FUSE = -1;
|
| 39 | private static final float DEFAULT_EXPLOSION_POWER = 4.0F;
|
| 40 | private static final BlockState DEFAULT_BLOCK_STATE = Blocks.TNT.defaultBlockState();
|
| 41 | private static final String TAG_BLOCK_STATE = "block_state";
|
| 42 | public static final String TAG_FUSE = "fuse";
|
| 43 | private static final String TAG_EXPLOSION_POWER = "explosion_power";
|
| 44 | public static final ExplosionDamageCalculator USED_PORTAL_DAMAGE_CALCULATOR = new ExplosionDamageCalculator() {
|
| 45 | @Override
|
| 46 | public boolean shouldBlockExplode(final Explosion explosion, final BlockGetter level, final BlockPos pos, final BlockState state, final float power) {
|
| 47 | return state.is(Blocks.NETHER_PORTAL) ? false : super.shouldBlockExplode(explosion, level, pos, state, power);
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | public Optional<Float> getBlockExplosionResistance(
|
| 52 | final Explosion explosion, final BlockGetter level, final BlockPos pos, final BlockState block, final FluidState fluid
|
| 53 | ) {
|
| 54 | return block.is(Blocks.NETHER_PORTAL) ? Optional.empty() : super.getBlockExplosionResistance(explosion, level, pos, block, fluid);
|
| 55 | }
|
| 56 | };
|
| 57 | private @Nullable EntityReference<LivingEntity> owner;
|
| 58 | private boolean usedPortal;
|
| 59 | private float explosionPower = 4.0F;
|
| 60 |
|
| 61 | public PrimedTnt(final EntityType<? extends PrimedTnt> type, final Level level) {
|
| 62 | super(type, level);
|
| 63 | this.blocksBuilding = true;
|
| 64 | }
|
| 65 |
|
| 66 | public PrimedTnt(final Level level, final double x, final double y, final double z, final @Nullable LivingEntity owner) {
|
| 67 | this(EntityTypes.TNT, level);
|
| 68 | this.setPos(x, y, z);
|
| 69 | double rot = level.getRandom().nextDouble() * (float) (Math.PI * 2);
|
| 70 | this.setDeltaMovement(-Math.sin(rot) * 0.02, 0.2F, -Math.cos(rot) * 0.02);
|
| 71 | this.setFuse(80);
|
| 72 | this.xo = x;
|
| 73 | this.yo = y;
|
| 74 | this.zo = z;
|
| 75 | this.owner = EntityReference.of(owner);
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 80 | entityData.define(DATA_FUSE_ID, 80);
|
| 81 | entityData.define(DATA_BLOCK_STATE_ID, DEFAULT_BLOCK_STATE);
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | protected Entity.MovementEmission getMovementEmission() {
|
| 86 | return Entity.MovementEmission.NONE;
|
| 87 | }
|
| 88 |
|
| 89 | @Override
|
| 90 | public boolean isPickable() {
|
| 91 | return !this.isRemoved();
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | protected double getDefaultGravity() {
|
| 96 | return 0.04;
|
| 97 | }
|
| 98 |
|
| 99 | @Override
|
| 100 | public void tick() {
|
| 101 | this.handlePortal();
|
| 102 | this.applyGravity();
|
| 103 | this.move(MoverType.SELF, this.getDeltaMovement());
|
| 104 | this.applyEffectsFromBlocks();
|
| 105 | this.setDeltaMovement(this.getDeltaMovement().scale(this.getAirDrag()));
|
| 106 | if (this.onGround()) {
|
| 107 | this.setDeltaMovement(this.getDeltaMovement().multiply(0.7, -0.5, 0.7));
|
| 108 | }
|
| 109 |
|
| 110 | int fuse = this.getFuse() - 1;
|
| 111 | this.setFuse(fuse);
|
| 112 | if (fuse <= 0) {
|
| 113 | this.discard();
|
| 114 | if (!this.level().isClientSide()) {
|
| 115 | this.explode();
|
| 116 | }
|
| 117 | } else {
|
| 118 | this.updateFluidInteraction();
|
| 119 | if (this.level().isClientSide()) {
|
| 120 | this.level().addParticle(ParticleTypes.SMOKE, this.getX(), this.getY() + 0.5, this.getZ(), 0.0, 0.0, 0.0);
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | this.setRequiresPrecisePosition(this.horizontalCollision || this.verticalCollision);
|
| 125 | }
|
| 126 |
|
| 127 | private void explode() {
|
| 128 | if (this.level() instanceof ServerLevel level && level.getGameRules().get(GameRules.TNT_EXPLODES)) {
|
| 129 | this.level()
|
| 130 | .explode(
|
| 131 | this,
|
| 132 | Explosion.getDefaultDamageSource(this.level(), this),
|
| 133 | this.usedPortal ? USED_PORTAL_DAMAGE_CALCULATOR : null,
|
| 134 | this.getX(),
|
| 135 | this.getY(0.0625),
|
| 136 | this.getZ(),
|
| 137 | this.explosionPower,
|
| 138 | false,
|
| 139 | Level.ExplosionInteraction.TNT
|
| 140 | );
|
| 141 | }
|
| 142 | }
|
| 143 |
|
| 144 | @Override
|
| 145 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 146 | output.putShort("fuse", (short)this.getFuse());
|
| 147 | output.store("block_state", BlockState.CODEC, this.getBlockState());
|
| 148 | if (this.explosionPower != 4.0F) {
|
| 149 | output.putFloat("explosion_power", this.explosionPower);
|
| 150 | }
|
| 151 |
|
| 152 | EntityReference.store(this.owner, output, "owner");
|
| 153 | }
|
| 154 |
|
| 155 | @Override
|
| 156 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 157 | this.setFuse(input.getShortOr("fuse", (short)80));
|
| 158 | this.setBlockState(input.read("block_state", BlockState.CODEC).orElse(DEFAULT_BLOCK_STATE));
|
| 159 | this.explosionPower = Mth.clamp(input.getFloatOr("explosion_power", 4.0F), 0.0F, 128.0F);
|
| 160 | this.owner = EntityReference.read(input, "owner");
|
| 161 | }
|
| 162 |
|
| 163 | public @Nullable LivingEntity getOwner() {
|
| 164 | return EntityReference.getLivingEntity(this.owner, this.level());
|
| 165 | }
|
| 166 |
|
| 167 | @Override
|
| 168 | public void restoreFrom(final Entity oldEntity) {
|
| 169 | super.restoreFrom(oldEntity);
|
| 170 | if (oldEntity instanceof PrimedTnt primedTnt) {
|
| 171 | this.owner = primedTnt.owner;
|
| 172 | }
|
| 173 | }
|
| 174 |
|
| 175 | public void setFuse(final int time) {
|
| 176 | this.entityData.set(DATA_FUSE_ID, time);
|
| 177 | }
|
| 178 |
|
| 179 | public int getFuse() {
|
| 180 | return this.entityData.get(DATA_FUSE_ID);
|
| 181 | }
|
| 182 |
|
| 183 | public static int getRandomShortFuse(final int fuse, final RandomSource random) {
|
| 184 | return random.nextInt(Math.max(1, fuse / 4)) + fuse / 8;
|
| 185 | }
|
| 186 |
|
| 187 | public void setBlockState(final BlockState blockState) {
|
| 188 | this.entityData.set(DATA_BLOCK_STATE_ID, blockState);
|
| 189 | }
|
| 190 |
|
| 191 | public BlockState getBlockState() {
|
| 192 | return this.entityData.get(DATA_BLOCK_STATE_ID);
|
| 193 | }
|
| 194 |
|
| 195 | private void setUsedPortal(final boolean usedPortal) {
|
| 196 | this.usedPortal = usedPortal;
|
| 197 | }
|
| 198 |
|
| 199 | @Override
|
| 200 | public @Nullable Entity teleport(final TeleportTransition transition) {
|
| 201 | Entity newEntity = super.teleport(transition);
|
| 202 | if (newEntity instanceof PrimedTnt tnt) {
|
| 203 | tnt.setUsedPortal(true);
|
| 204 | }
|
| 205 |
|
| 206 | return newEntity;
|
| 207 | }
|
| 208 |
|
| 209 | @Override
|
| 210 | public final boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage) {
|
| 211 | return false;
|
| 212 | }
|
| 213 |
|
| 214 | @Override
|
| 215 | public MoveSimulationType getMoveSimulationType() {
|
| 216 | return MoveSimulationType.SERVER_AND_CLIENT;
|
| 217 | }
|
| 218 | }
|
| 219 |
|