100 lines
3.1 KiB
Java
100 lines
3.1 KiB
Java
package net.minecraft.world.entity.vehicle;
|
|
|
|
import net.minecraft.core.component.DataComponents;
|
|
import net.minecraft.network.syncher.EntityDataAccessor;
|
|
import net.minecraft.network.syncher.EntityDataSerializers;
|
|
import net.minecraft.network.syncher.SynchedEntityData;
|
|
import net.minecraft.world.damagesource.DamageSource;
|
|
import net.minecraft.world.entity.Entity;
|
|
import net.minecraft.world.entity.EntityType;
|
|
import net.minecraft.world.entity.player.Player;
|
|
import net.minecraft.world.item.Item;
|
|
import net.minecraft.world.item.ItemStack;
|
|
import net.minecraft.world.level.GameRules;
|
|
import net.minecraft.world.level.Level;
|
|
import net.minecraft.world.level.gameevent.GameEvent;
|
|
|
|
public abstract class VehicleEntity extends Entity {
|
|
protected static final EntityDataAccessor<Integer> DATA_ID_HURT = SynchedEntityData.defineId(VehicleEntity.class, EntityDataSerializers.INT);
|
|
protected static final EntityDataAccessor<Integer> DATA_ID_HURTDIR = SynchedEntityData.defineId(VehicleEntity.class, EntityDataSerializers.INT);
|
|
protected static final EntityDataAccessor<Float> DATA_ID_DAMAGE = SynchedEntityData.defineId(VehicleEntity.class, EntityDataSerializers.FLOAT);
|
|
|
|
public VehicleEntity(EntityType<?> entityType, Level level) {
|
|
super(entityType, level);
|
|
}
|
|
|
|
@Override
|
|
public boolean hurt(DamageSource source, float amount) {
|
|
if (this.level().isClientSide || this.isRemoved()) {
|
|
return true;
|
|
} else if (this.isInvulnerableTo(source)) {
|
|
return false;
|
|
} else {
|
|
this.setHurtDir(-this.getHurtDir());
|
|
this.setHurtTime(10);
|
|
this.markHurt();
|
|
this.setDamage(this.getDamage() + amount * 10.0F);
|
|
this.gameEvent(GameEvent.ENTITY_DAMAGE, source.getEntity());
|
|
boolean bl = source.getEntity() instanceof Player && ((Player)source.getEntity()).getAbilities().instabuild;
|
|
if ((bl || !(this.getDamage() > 40.0F)) && !this.shouldSourceDestroy(source)) {
|
|
if (bl) {
|
|
this.discard();
|
|
}
|
|
} else {
|
|
this.destroy(source);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
boolean shouldSourceDestroy(DamageSource source) {
|
|
return false;
|
|
}
|
|
|
|
public void destroy(Item dropItem) {
|
|
this.kill();
|
|
if (this.level().getGameRules().getBoolean(GameRules.RULE_DOENTITYDROPS)) {
|
|
ItemStack itemStack = new ItemStack(dropItem);
|
|
itemStack.set(DataComponents.CUSTOM_NAME, this.getCustomName());
|
|
this.spawnAtLocation(itemStack);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void defineSynchedData(SynchedEntityData.Builder builder) {
|
|
builder.define(DATA_ID_HURT, 0);
|
|
builder.define(DATA_ID_HURTDIR, 1);
|
|
builder.define(DATA_ID_DAMAGE, 0.0F);
|
|
}
|
|
|
|
public void setHurtTime(int hurtTime) {
|
|
this.entityData.set(DATA_ID_HURT, hurtTime);
|
|
}
|
|
|
|
public void setHurtDir(int hurtDir) {
|
|
this.entityData.set(DATA_ID_HURTDIR, hurtDir);
|
|
}
|
|
|
|
public void setDamage(float damage) {
|
|
this.entityData.set(DATA_ID_DAMAGE, damage);
|
|
}
|
|
|
|
public float getDamage() {
|
|
return this.entityData.get(DATA_ID_DAMAGE);
|
|
}
|
|
|
|
public int getHurtTime() {
|
|
return this.entityData.get(DATA_ID_HURT);
|
|
}
|
|
|
|
public int getHurtDir() {
|
|
return this.entityData.get(DATA_ID_HURTDIR);
|
|
}
|
|
|
|
protected void destroy(DamageSource source) {
|
|
this.destroy(this.getDropItem());
|
|
}
|
|
|
|
abstract Item getDropItem();
|
|
}
|