Source MobInfo - Spiderman31807/Mob_Selector_Mod GitHub Wiki

package playasmob;

import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.Targeting;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.core.BlockPos;

public abstract class MobInfo extends BaseInfo implements Targeting {
 	public static final Synched aggressive = Synched.Aggressive;
	public ItemStack bodyArmorItem = ItemStack.EMPTY;
	public int ambientSoundTime;
	public int targetStamp;
	public int attackStamp;
	public LivingEntity target;
	public LivingEntity attacker;

	public MobInfo(EntityType type, Class typeClass, Player player, CompoundTag compound) {
		super(type, typeClass, player, compound);
	}

	// Custom Code
	@Override
	public void reset() {
		super.reset();
		this.reset(aggressive);
	}
	
	@Override
	public boolean attack(Object object, Entity entity) {
		if (entity instanceof LivingEntity attacker)
			this.setTarget(attacker);
		return super.attack(null, entity);
	}

	@Override
	public boolean actuallyHurt(Object object, ServerLevel server, DamageSource source, float damage) {
		if (source.getEntity() instanceof LivingEntity attacker)
			this.setAttacker(attacker);
		return super.actuallyHurt(null, server, source, damage);
	}

	public void setAttacker(LivingEntity attacker) {
		this.attackStamp = this.player.tickCount + 100;
		this.attacker = attacker;
		this.updateAggressive();
	}

	public void updateAggressive() {
		this.setAggressive(this.getTarget() != null);
	}

	public LivingEntity findTarget() {
		if(Utils.getHitEntity(this.player) instanceof LivingEntity target)
			this.setTarget(target);
		return this.getTarget();
	}

	// Mob.class Code
	@Override
	public LivingEntity getTarget() {
		if (this.target instanceof LivingEntity)
			return this.target;
		if (this.attacker instanceof LivingEntity)
			return this.attacker;
		return null;
	}

	public void setTarget(LivingEntity target) {
		this.targetStamp = this.player.tickCount + 100;
		this.target = target;
		this.updateAggressive();
	}

	public int getAmbientSoundInterval() {
		return 80;
	}

	public void playAmbientSound() {
		this.makeSound(this.getAmbientSound());
	}

	@Override
	public boolean baseTick(Object object) {
		if (!super.baseTick(object))
			return false;
		if (this.isAlive() && this.random.nextInt(1000) < this.ambientSoundTime++) {
			this.resetAmbientSoundTime();
			this.playAmbientSound();
		}
		if (this.attacker != null && (this.attacker.isRemoved() || this.attackStamp < this.player.tickCount))
			this.setAttacker(null);
		if (this.target != null && (this.target.isRemoved() || this.targetStamp < this.player.tickCount))
			this.setTarget(null);
		return true;
	}

	@Override
	public boolean playHurtSound(Object object, DamageSource source) {
		this.resetAmbientSoundTime();
		return super.playHurtSound(object, source);
	}

	public void resetAmbientSoundTime() {
		this.ambientSoundTime = -this.getAmbientSoundInterval();
	}

	public void spawnAnim() {
		if (this.level().isClientSide) {
			this.makePoofParticles();
		} else {
			this.level().broadcastEntityEvent(this.player, (byte) 20);
		}
	}

	@Override
	public boolean handleEntityEvent(Object object, byte event) {
		if (!super.handleEntityEvent(null, event))
			return false;
		if (event == 20)
			this.spawnAnim();
		return true;
	}

	public SoundEvent getAmbientSound() {
		return null;
	}

	public ItemStack getBodyArmorItem() {
		return this.bodyArmorItem;
	}

	@Override
	public boolean canUseSlot(boolean original, EquipmentSlot slot) {
		return super.canUseSlot(original, slot) && slot != EquipmentSlot.BODY;
	}

	public boolean isWearingBodyArmor() {
		return !this.getItemBySlot(EquipmentSlot.BODY).isEmpty();
	}

	public void setBodyArmorItem(ItemStack stack) {
		this.setItemSlot(EquipmentSlot.BODY, stack);
	}

	public void setAggressive(boolean aggressive) {
		this.set(this.aggressive, aggressive);
	}

	public boolean isAggressive() {
		return this.get(aggressive);
	}

	public boolean isSunBurnTick() {
		if (this.level().isDay() && !this.level().isClientSide) {
			float f = this.getLightLevelDependentMagicValue();
			BlockPos blockpos = BlockPos.containing(this.getX(), this.getEyeY(), this.getZ());
			boolean flag = this.isInWaterRainOrBubble() || this.player.isInPowderSnow || this.player.wasInPowderSnow;
			if (f > 0.5F && this.random.nextFloat() * 30.0F < (f - 0.4F) * 2.0F && !flag && this.level().canSeeSky(blockpos)) {
				return true;
			}
		}
		return false;
	}
}