| 1 | package net.minecraft.world.entity.monster.hoglin;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import java.util.List;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 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.sounds.SoundEvent;
|
| 11 | import net.minecraft.sounds.SoundEvents;
|
| 12 | import net.minecraft.sounds.SoundSource;
|
| 13 | import net.minecraft.tags.ItemTags;
|
| 14 | import net.minecraft.util.RandomSource;
|
| 15 | import net.minecraft.util.profiling.Profiler;
|
| 16 | import net.minecraft.util.profiling.ProfilerFiller;
|
| 17 | import net.minecraft.world.DifficultyInstance;
|
| 18 | import net.minecraft.world.InteractionHand;
|
| 19 | import net.minecraft.world.InteractionResult;
|
| 20 | import net.minecraft.world.attribute.EnvironmentAttributes;
|
| 21 | import net.minecraft.world.damagesource.DamageSource;
|
| 22 | import net.minecraft.world.effect.MobEffectInstance;
|
| 23 | import net.minecraft.world.effect.MobEffects;
|
| 24 | import net.minecraft.world.entity.AgeableMob;
|
| 25 | import net.minecraft.world.entity.ConversionParams;
|
| 26 | import net.minecraft.world.entity.Entity;
|
| 27 | import net.minecraft.world.entity.EntityAttachment;
|
| 28 | import net.minecraft.world.entity.EntityAttachments;
|
| 29 | import net.minecraft.world.entity.EntityDimensions;
|
| 30 | import net.minecraft.world.entity.EntityEvent;
|
| 31 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 32 | import net.minecraft.world.entity.EntityType;
|
| 33 | import net.minecraft.world.entity.EntityTypes;
|
| 34 | import net.minecraft.world.entity.LivingEntity;
|
| 35 | import net.minecraft.world.entity.Pose;
|
| 36 | import net.minecraft.world.entity.SpawnGroupData;
|
| 37 | import net.minecraft.world.entity.ai.Brain;
|
| 38 | import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
|
| 39 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 40 | import net.minecraft.world.entity.ai.sensing.SensorType;
|
| 41 | import net.minecraft.world.entity.animal.Animal;
|
| 42 | import net.minecraft.world.entity.monster.Enemy;
|
| 43 | import net.minecraft.world.entity.monster.Monster;
|
| 44 | import net.minecraft.world.entity.player.Player;
|
| 45 | import net.minecraft.world.item.ItemStack;
|
| 46 | import net.minecraft.world.level.Level;
|
| 47 | import net.minecraft.world.level.LevelAccessor;
|
| 48 | import net.minecraft.world.level.LevelReader;
|
| 49 | import net.minecraft.world.level.ServerLevelAccessor;
|
| 50 | import net.minecraft.world.level.block.Blocks;
|
| 51 | import net.minecraft.world.level.block.state.BlockState;
|
| 52 | import net.minecraft.world.level.storage.ValueInput;
|
| 53 | import net.minecraft.world.level.storage.ValueOutput;
|
| 54 | import org.jspecify.annotations.Nullable;
|
| 55 |
|
| 56 | public class Hoglin extends Animal implements Enemy, HoglinBase {
|
| 57 | private static final EntityDataAccessor<Boolean> DATA_IMMUNE_TO_ZOMBIFICATION = SynchedEntityData.defineId(Hoglin.class, EntityDataSerializers.BOOLEAN);
|
| 58 | private static final EntityDimensions BABY_DIMENSIONS = EntityDimensions.scalable(0.75F, 0.85F)
|
| 59 | .withEyeHeight(0.625F)
|
| 60 | .withAttachments(EntityAttachments.builder().attach(EntityAttachment.PASSENGER, 0.0F, 0.875F, 0.0F));
|
| 61 | private static final int MAX_HEALTH = 40;
|
| 62 | private static final float MOVEMENT_SPEED_WHEN_FIGHTING = 0.3F;
|
| 63 | private static final int ATTACK_KNOCKBACK = 1;
|
| 64 | private static final float KNOCKBACK_RESISTANCE = 0.6F;
|
| 65 | private static final int ATTACK_DAMAGE = 6;
|
| 66 | private static final float BABY_ATTACK_DAMAGE = 0.5F;
|
| 67 | private static final boolean DEFAULT_IMMUNE_TO_ZOMBIFICATION = false;
|
| 68 | private static final int DEFAULT_TIME_IN_OVERWORLD = 0;
|
| 69 | private static final boolean DEFAULT_CANNOT_BE_HUNTED = false;
|
| 70 | public static final int CONVERSION_TIME = 300;
|
| 71 | private int attackAnimationRemainingTicks;
|
| 72 | private int timeInOverworld = 0;
|
| 73 | private boolean cannotBeHunted = false;
|
| 74 | private static final Brain.Provider<Hoglin> BRAIN_PROVIDER = Brain.provider(
|
| 75 | List.of(SensorType.NEAREST_LIVING_ENTITIES, SensorType.NEAREST_PLAYERS, SensorType.NEAREST_ADULT, SensorType.HOGLIN_SPECIFIC_SENSOR),
|
| 76 | var0 -> HoglinAi.getActivities()
|
| 77 | );
|
| 78 |
|
| 79 | public Hoglin(final EntityType<? extends Hoglin> type, final Level level) {
|
| 80 | super(type, level);
|
| 81 | this.xpReward = 5;
|
| 82 | }
|
| 83 |
|
| 84 | @VisibleForTesting
|
| 85 | public void setTimeInOverworld(final int timeInOverworld) {
|
| 86 | this.timeInOverworld = timeInOverworld;
|
| 87 | }
|
| 88 |
|
| 89 | @Override
|
| 90 | public boolean canBeLeashed() {
|
| 91 | return true;
|
| 92 | }
|
| 93 |
|
| 94 | public static AttributeSupplier.Builder createAttributes() {
|
| 95 | return Monster.createMonsterAttributes()
|
| 96 | .add(Attributes.MAX_HEALTH, 40.0)
|
| 97 | .add(Attributes.MOVEMENT_SPEED, 0.3F)
|
| 98 | .add(Attributes.KNOCKBACK_RESISTANCE, 0.6F)
|
| 99 | .add(Attributes.ATTACK_KNOCKBACK, 1.0)
|
| 100 | .add(Attributes.ATTACK_DAMAGE, 6.0);
|
| 101 | }
|
| 102 |
|
| 103 | @Override
|
| 104 | public boolean doHurtTarget(final ServerLevel level, final Entity target) {
|
| 105 | if (target instanceof LivingEntity livingEntity) {
|
| 106 | this.attackAnimationRemainingTicks = 10;
|
| 107 | this.level().broadcastEntityEvent(this, (byte)4);
|
| 108 | this.makeSound(SoundEvents.HOGLIN_ATTACK);
|
| 109 | HoglinAi.onHitTarget(this, livingEntity);
|
| 110 | return HoglinBase.hurtAndThrowTarget(level, this, livingEntity);
|
| 111 | } else {
|
| 112 | return false;
|
| 113 | }
|
| 114 | }
|
| 115 |
|
| 116 | @Override
|
| 117 | protected void blockedByItem(final LivingEntity defender, final DamageSource source, final float damage, final boolean fullyBlocked) {
|
| 118 | if (this.isAdult()) {
|
| 119 | HoglinBase.throwTarget(this, defender);
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | @Override
|
| 124 | public boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage) {
|
| 125 | boolean wasHurt = super.hurtServer(level, source, damage);
|
| 126 | if (wasHurt && source.getEntity() instanceof LivingEntity sourceEntity) {
|
| 127 | HoglinAi.wasHurtBy(level, this, sourceEntity);
|
| 128 | }
|
| 129 |
|
| 130 | return wasHurt;
|
| 131 | }
|
| 132 |
|
| 133 | @Override
|
| 134 | protected Brain<Hoglin> makeBrain(final Brain.Packed packedBrain) {
|
| 135 | return BRAIN_PROVIDER.makeBrain(this, packedBrain);
|
| 136 | }
|
| 137 |
|
| 138 | @Override
|
| 139 | public Brain<Hoglin> getBrain() {
|
| 140 | return super.getBrain();
|
| 141 | }
|
| 142 |
|
| 143 | @Override
|
| 144 | protected void customServerAiStep(final ServerLevel level) {
|
| 145 | ProfilerFiller profiler = Profiler.get();
|
| 146 | profiler.push("hoglinBrain");
|
| 147 | this.getBrain().tick(level, this);
|
| 148 | profiler.pop();
|
| 149 | HoglinAi.updateActivity(this);
|
| 150 | if (this.isConverting()) {
|
| 151 | this.timeInOverworld++;
|
| 152 | if (this.timeInOverworld > 300) {
|
| 153 | this.makeSound(SoundEvents.HOGLIN_CONVERTED_TO_ZOMBIFIED);
|
| 154 | this.finishConversion();
|
| 155 | }
|
| 156 | } else {
|
| 157 | this.timeInOverworld = 0;
|
| 158 | }
|
| 159 | }
|
| 160 |
|
| 161 | @Override
|
| 162 | public void aiStep() {
|
| 163 | if (this.attackAnimationRemainingTicks > 0) {
|
| 164 | this.attackAnimationRemainingTicks--;
|
| 165 | }
|
| 166 |
|
| 167 | super.aiStep();
|
| 168 | }
|
| 169 |
|
| 170 | @Override
|
| 171 | public EntityDimensions getDefaultDimensions(final Pose pose) {
|
| 172 | return this.isBaby() ? BABY_DIMENSIONS : super.getDefaultDimensions(pose);
|
| 173 | }
|
| 174 |
|
| 175 | @Override
|
| 176 | protected void ageBoundaryReached() {
|
| 177 | if (this.isBaby()) {
|
| 178 | this.xpReward = 3;
|
| 179 | this.getAttribute(Attributes.ATTACK_DAMAGE).setBaseValue(0.5);
|
| 180 | } else {
|
| 181 | this.xpReward = 5;
|
| 182 | this.getAttribute(Attributes.ATTACK_DAMAGE).setBaseValue(6.0);
|
| 183 | }
|
| 184 | }
|
| 185 |
|
| 186 | public static boolean checkHoglinSpawnRules(
|
| 187 | final EntityType<Hoglin> type, final LevelAccessor level, final EntitySpawnReason spawnReason, final BlockPos pos, final RandomSource random
|
| 188 | ) {
|
| 189 | return !level.getBlockState(pos.below()).is(Blocks.NETHER_WART_BLOCK);
|
| 190 | }
|
| 191 |
|
| 192 | @Override
|
| 193 | public @Nullable SpawnGroupData finalizeSpawn(
|
| 194 | final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, final @Nullable SpawnGroupData groupData
|
| 195 | ) {
|
| 196 | if (level.getRandom().nextFloat() < 0.2F) {
|
| 197 | this.setBaby(true);
|
| 198 | }
|
| 199 |
|
| 200 | return super.finalizeSpawn(level, difficulty, spawnReason, groupData);
|
| 201 | }
|
| 202 |
|
| 203 | @Override
|
| 204 | public boolean removeWhenFarAway(final double distSqr) {
|
| 205 | return true;
|
| 206 | }
|
| 207 |
|
| 208 | @Override
|
| 209 | public float getWalkTargetValue(final BlockPos pos, final LevelReader level) {
|
| 210 | if (HoglinAi.isPosNearNearestRepellent(this, pos)) {
|
| 211 | return -1.0F;
|
| 212 | } else {
|
| 213 | return level.getBlockState(pos.below()).is(Blocks.CRIMSON_NYLIUM) ? 10.0F : 0.0F;
|
| 214 | }
|
| 215 | }
|
| 216 |
|
| 217 | @Override
|
| 218 | public InteractionResult mobInteract(final Player player, final InteractionHand hand) {
|
| 219 | InteractionResult interactionSucceeded = super.mobInteract(player, hand);
|
| 220 | if (interactionSucceeded.consumesAction()) {
|
| 221 | this.setPersistenceRequired();
|
| 222 | }
|
| 223 |
|
| 224 | return interactionSucceeded;
|
| 225 | }
|
| 226 |
|
| 227 | @Override
|
| 228 | public void handleEntityEvent(final @EntityEvent.Value byte id) {
|
| 229 | if (id == 4) {
|
| 230 | this.attackAnimationRemainingTicks = 10;
|
| 231 | this.makeSound(SoundEvents.HOGLIN_ATTACK);
|
| 232 | } else {
|
| 233 | super.handleEntityEvent(id);
|
| 234 | }
|
| 235 | }
|
| 236 |
|
| 237 | @Override
|
| 238 | public int getAttackAnimationRemainingTicks() {
|
| 239 | return this.attackAnimationRemainingTicks;
|
| 240 | }
|
| 241 |
|
| 242 | @Override
|
| 243 | public boolean shouldDropExperience() {
|
| 244 | return true;
|
| 245 | }
|
| 246 |
|
| 247 | @Override
|
| 248 | protected int getBaseExperienceReward(final ServerLevel level) {
|
| 249 | return this.xpReward;
|
| 250 | }
|
| 251 |
|
| 252 | private void finishConversion() {
|
| 253 | this.convertTo(EntityTypes.ZOGLIN, ConversionParams.single(this, true, false), zoglin -> zoglin.addEffect(new MobEffectInstance(MobEffects.NAUSEA, 200, 0)));
|
| 254 | }
|
| 255 |
|
| 256 | @Override
|
| 257 | public boolean isFood(final ItemStack itemStack) {
|
| 258 | return itemStack.is(ItemTags.HOGLIN_FOOD);
|
| 259 | }
|
| 260 |
|
| 261 | public boolean isAdult() {
|
| 262 | return !this.isBaby();
|
| 263 | }
|
| 264 |
|
| 265 | @Override
|
| 266 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 267 | super.defineSynchedData(entityData);
|
| 268 | entityData.define(DATA_IMMUNE_TO_ZOMBIFICATION, false);
|
| 269 | }
|
| 270 |
|
| 271 | @Override
|
| 272 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 273 | super.addAdditionalSaveData(output);
|
| 274 | output.putBoolean("IsImmuneToZombification", this.isImmuneToZombification());
|
| 275 | output.putInt("TimeInOverworld", this.timeInOverworld);
|
| 276 | output.putBoolean("CannotBeHunted", this.cannotBeHunted);
|
| 277 | }
|
| 278 |
|
| 279 | @Override
|
| 280 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 281 | super.readAdditionalSaveData(input);
|
| 282 | this.setImmuneToZombification(input.getBooleanOr("IsImmuneToZombification", false));
|
| 283 | this.timeInOverworld = input.getIntOr("TimeInOverworld", 0);
|
| 284 | this.setCannotBeHunted(input.getBooleanOr("CannotBeHunted", false));
|
| 285 | }
|
| 286 |
|
| 287 | public void setImmuneToZombification(final boolean isImmuneToZombification) {
|
| 288 | this.getEntityData().set(DATA_IMMUNE_TO_ZOMBIFICATION, isImmuneToZombification);
|
| 289 | }
|
| 290 |
|
| 291 | private boolean isImmuneToZombification() {
|
| 292 | return this.getEntityData().get(DATA_IMMUNE_TO_ZOMBIFICATION);
|
| 293 | }
|
| 294 |
|
| 295 | public boolean isConverting() {
|
| 296 | return !this.isImmuneToZombification()
|
| 297 | && !this.isNoAi()
|
| 298 | && this.level().environmentAttributes().getValue(EnvironmentAttributes.PIGLINS_ZOMBIFY, this.position());
|
| 299 | }
|
| 300 |
|
| 301 | private void setCannotBeHunted(final boolean cannotBeHunted) {
|
| 302 | this.cannotBeHunted = cannotBeHunted;
|
| 303 | }
|
| 304 |
|
| 305 | public boolean canBeHunted() {
|
| 306 | return this.isAdult() && !this.cannotBeHunted;
|
| 307 | }
|
| 308 |
|
| 309 | @Override
|
| 310 | public @Nullable AgeableMob getBreedOffspring(final ServerLevel level, final AgeableMob partner) {
|
| 311 | Hoglin offspring = EntityTypes.HOGLIN.create(level, EntitySpawnReason.BREEDING);
|
| 312 | if (offspring != null) {
|
| 313 | offspring.setPersistenceRequired();
|
| 314 | }
|
| 315 |
|
| 316 | return offspring;
|
| 317 | }
|
| 318 |
|
| 319 | @Override
|
| 320 | public boolean canFallInLove() {
|
| 321 | return !HoglinAi.isPacified(this) && super.canFallInLove();
|
| 322 | }
|
| 323 |
|
| 324 | @Override
|
| 325 | public SoundSource getSoundSource() {
|
| 326 | return SoundSource.HOSTILE;
|
| 327 | }
|
| 328 |
|
| 329 | @Override
|
| 330 | protected SoundEvent getAmbientSound() {
|
| 331 | return this.level().isClientSide() ? null : HoglinAi.getSoundForCurrentActivity(this).orElse(null);
|
| 332 | }
|
| 333 |
|
| 334 | @Override
|
| 335 | protected SoundEvent getHurtSound(final DamageSource source) {
|
| 336 | return SoundEvents.HOGLIN_HURT;
|
| 337 | }
|
| 338 |
|
| 339 | @Override
|
| 340 | protected SoundEvent getDeathSound() {
|
| 341 | return SoundEvents.HOGLIN_DEATH;
|
| 342 | }
|
| 343 |
|
| 344 | @Override
|
| 345 | protected SoundEvent getSwimSound() {
|
| 346 | return SoundEvents.HOSTILE_SWIM;
|
| 347 | }
|
| 348 |
|
| 349 | @Override
|
| 350 | protected SoundEvent getSwimSplashSound() {
|
| 351 | return SoundEvents.HOSTILE_SPLASH;
|
| 352 | }
|
| 353 |
|
| 354 | @Override
|
| 355 | protected void playStepSound(final BlockPos pos, final BlockState blockState) {
|
| 356 | this.playSound(SoundEvents.HOGLIN_STEP, 0.15F, 1.0F);
|
| 357 | }
|
| 358 |
|
| 359 | @Override
|
| 360 | public @Nullable LivingEntity getTarget() {
|
| 361 | return this.getTargetFromBrain();
|
| 362 | }
|
| 363 | }
|
| 364 |
|