| 1 | package net.minecraft.world.entity.vehicle;
|
| 2 |
|
| 3 | import net.minecraft.core.component.DataComponents;
|
| 4 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 5 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 6 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.world.damagesource.DamageSource;
|
| 9 | import net.minecraft.world.entity.Entity;
|
| 10 | import net.minecraft.world.entity.EntityType;
|
| 11 | import net.minecraft.world.entity.Mob;
|
| 12 | import net.minecraft.world.entity.player.Player;
|
| 13 | import net.minecraft.world.item.Item;
|
| 14 | import net.minecraft.world.item.ItemStack;
|
| 15 | import net.minecraft.world.level.Explosion;
|
| 16 | import net.minecraft.world.level.Level;
|
| 17 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 18 | import net.minecraft.world.level.gamerules.GameRules;
|
| 19 |
|
| 20 | public abstract class VehicleEntity extends Entity {
|
| 21 | protected static final EntityDataAccessor<Integer> DATA_ID_HURT = SynchedEntityData.defineId(VehicleEntity.class, EntityDataSerializers.INT);
|
| 22 | protected static final EntityDataAccessor<Integer> DATA_ID_HURTDIR = SynchedEntityData.defineId(VehicleEntity.class, EntityDataSerializers.INT);
|
| 23 | protected static final EntityDataAccessor<Float> DATA_ID_DAMAGE = SynchedEntityData.defineId(VehicleEntity.class, EntityDataSerializers.FLOAT);
|
| 24 |
|
| 25 | public VehicleEntity(final EntityType<?> type, final Level level) {
|
| 26 | super(type, level);
|
| 27 | }
|
| 28 |
|
| 29 | @Override
|
| 30 | public boolean hurtClient(final DamageSource source) {
|
| 31 | return true;
|
| 32 | }
|
| 33 |
|
| 34 | @Override
|
| 35 | public boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage) {
|
| 36 | if (this.isRemoved()) {
|
| 37 | return true;
|
| 38 | }
|
| 39 |
|
| 40 | if (this.isInvulnerableToBase(source)) {
|
| 41 | return false;
|
| 42 | }
|
| 43 |
|
| 44 | this.setHurtDir(-this.getHurtDir());
|
| 45 | this.setHurtTime(10);
|
| 46 | this.markHurt();
|
| 47 | this.setDamage(this.getDamage() + damage * 10.0F);
|
| 48 | this.gameEvent(GameEvent.ENTITY_DAMAGE, source.getEntity());
|
| 49 | boolean creativePlayer = source.getEntity() instanceof Player player && player.getAbilities().instabuild;
|
| 50 | if ((creativePlayer || !(this.getDamage() > 40.0F)) && !this.shouldSourceDestroy(source)) {
|
| 51 | if (creativePlayer) {
|
| 52 | this.discard();
|
| 53 | }
|
| 54 | } else {
|
| 55 | this.destroy(level, source);
|
| 56 | }
|
| 57 |
|
| 58 | return true;
|
| 59 | }
|
| 60 |
|
| 61 | protected boolean shouldSourceDestroy(final DamageSource source) {
|
| 62 | return false;
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | public boolean ignoreExplosion(final Explosion explosion) {
|
| 67 | return explosion.getIndirectSourceEntity() instanceof Mob && !explosion.level().getGameRules().get(GameRules.MOB_GRIEFING);
|
| 68 | }
|
| 69 |
|
| 70 | public void destroy(final ServerLevel level, final Item dropItem) {
|
| 71 | this.kill(level);
|
| 72 | if (level.getGameRules().get(GameRules.ENTITY_DROPS)) {
|
| 73 | ItemStack itemStack = new ItemStack(dropItem);
|
| 74 | itemStack.set(DataComponents.CUSTOM_NAME, this.getCustomName());
|
| 75 | this.spawnAtLocation(level, itemStack);
|
| 76 | }
|
| 77 | }
|
| 78 |
|
| 79 | @Override
|
| 80 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 81 | entityData.define(DATA_ID_HURT, 0);
|
| 82 | entityData.define(DATA_ID_HURTDIR, 1);
|
| 83 | entityData.define(DATA_ID_DAMAGE, 0.0F);
|
| 84 | }
|
| 85 |
|
| 86 | public void setHurtTime(final int hurtTime) {
|
| 87 | this.entityData.set(DATA_ID_HURT, hurtTime);
|
| 88 | }
|
| 89 |
|
| 90 | public void setHurtDir(final int hurtDir) {
|
| 91 | this.entityData.set(DATA_ID_HURTDIR, hurtDir);
|
| 92 | }
|
| 93 |
|
| 94 | public void setDamage(final float damage) {
|
| 95 | this.entityData.set(DATA_ID_DAMAGE, damage);
|
| 96 | }
|
| 97 |
|
| 98 | public float getDamage() {
|
| 99 | return this.entityData.get(DATA_ID_DAMAGE);
|
| 100 | }
|
| 101 |
|
| 102 | public int getHurtTime() {
|
| 103 | return this.entityData.get(DATA_ID_HURT);
|
| 104 | }
|
| 105 |
|
| 106 | public int getHurtDir() {
|
| 107 | return this.entityData.get(DATA_ID_HURTDIR);
|
| 108 | }
|
| 109 |
|
| 110 | protected void destroy(final ServerLevel level, final DamageSource source) {
|
| 111 | this.destroy(level, this.getDropItem());
|
| 112 | }
|
| 113 |
|
| 114 | @Override
|
| 115 | public int getDimensionChangingDelay() {
|
| 116 | return 10;
|
| 117 | }
|
| 118 |
|
| 119 | protected abstract Item getDropItem();
|
| 120 | }
|
| 121 |
|