| 1 | package net.minecraft.world.entity.monster;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.mojang.datafixers.util.Pair;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Optional;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 9 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 10 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 11 | import net.minecraft.server.level.ServerLevel;
|
| 12 | import net.minecraft.sounds.SoundEvent;
|
| 13 | import net.minecraft.sounds.SoundEvents;
|
| 14 | import net.minecraft.util.profiling.Profiler;
|
| 15 | import net.minecraft.util.profiling.ProfilerFiller;
|
| 16 | import net.minecraft.util.valueproviders.UniformInt;
|
| 17 | import net.minecraft.world.DifficultyInstance;
|
| 18 | import net.minecraft.world.damagesource.DamageSource;
|
| 19 | import net.minecraft.world.entity.Entity;
|
| 20 | import net.minecraft.world.entity.EntityAttachment;
|
| 21 | import net.minecraft.world.entity.EntityAttachments;
|
| 22 | import net.minecraft.world.entity.EntityDimensions;
|
| 23 | import net.minecraft.world.entity.EntityEvent;
|
| 24 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 25 | import net.minecraft.world.entity.EntityType;
|
| 26 | import net.minecraft.world.entity.EntityTypes;
|
| 27 | import net.minecraft.world.entity.LivingEntity;
|
| 28 | import net.minecraft.world.entity.Mob;
|
| 29 | import net.minecraft.world.entity.Pose;
|
| 30 | import net.minecraft.world.entity.SpawnGroupData;
|
| 31 | import net.minecraft.world.entity.ai.ActivityData;
|
| 32 | import net.minecraft.world.entity.ai.Brain;
|
| 33 | import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
|
| 34 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 35 | import net.minecraft.world.entity.ai.behavior.BehaviorUtils;
|
| 36 | import net.minecraft.world.entity.ai.behavior.DoNothing;
|
| 37 | import net.minecraft.world.entity.ai.behavior.LookAtTargetSink;
|
| 38 | import net.minecraft.world.entity.ai.behavior.MeleeAttack;
|
| 39 | import net.minecraft.world.entity.ai.behavior.MoveToTargetSink;
|
| 40 | import net.minecraft.world.entity.ai.behavior.RandomStroll;
|
| 41 | import net.minecraft.world.entity.ai.behavior.RunOne;
|
| 42 | import net.minecraft.world.entity.ai.behavior.SetEntityLookTargetSometimes;
|
| 43 | import net.minecraft.world.entity.ai.behavior.SetWalkTargetFromAttackTargetIfTargetOutOfReach;
|
| 44 | import net.minecraft.world.entity.ai.behavior.SetWalkTargetFromLookTarget;
|
| 45 | import net.minecraft.world.entity.ai.behavior.StartAttacking;
|
| 46 | import net.minecraft.world.entity.ai.behavior.StopAttackingIfTargetInvalid;
|
| 47 | import net.minecraft.world.entity.ai.behavior.declarative.BehaviorBuilder;
|
| 48 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 49 | import net.minecraft.world.entity.ai.memory.NearestVisibleLivingEntities;
|
| 50 | import net.minecraft.world.entity.ai.sensing.Sensor;
|
| 51 | import net.minecraft.world.entity.ai.sensing.SensorType;
|
| 52 | import net.minecraft.world.entity.monster.hoglin.HoglinBase;
|
| 53 | import net.minecraft.world.entity.schedule.Activity;
|
| 54 | import net.minecraft.world.level.Level;
|
| 55 | import net.minecraft.world.level.ServerLevelAccessor;
|
| 56 | import net.minecraft.world.level.block.state.BlockState;
|
| 57 | import net.minecraft.world.level.storage.ValueInput;
|
| 58 | import net.minecraft.world.level.storage.ValueOutput;
|
| 59 | import org.jspecify.annotations.Nullable;
|
| 60 |
|
| 61 | public class Zoglin extends Monster implements HoglinBase {
|
| 62 | private static final EntityDataAccessor<Boolean> DATA_BABY_ID = SynchedEntityData.defineId(Zoglin.class, EntityDataSerializers.BOOLEAN);
|
| 63 | private static final EntityDimensions BABY_DIMENSIONS = EntityDimensions.scalable(0.75F, 0.85F)
|
| 64 | .withEyeHeight(0.625F)
|
| 65 | .withAttachments(EntityAttachments.builder().attach(EntityAttachment.PASSENGER, 0.0F, 0.875F, 0.0F));
|
| 66 | private static final int MAX_HEALTH = 40;
|
| 67 | private static final int ATTACK_KNOCKBACK = 1;
|
| 68 | private static final float KNOCKBACK_RESISTANCE = 0.6F;
|
| 69 | private static final int ATTACK_DAMAGE = 6;
|
| 70 | private static final float BABY_ATTACK_DAMAGE = 0.5F;
|
| 71 | private static final int ATTACK_INTERVAL = 40;
|
| 72 | private static final int BABY_ATTACK_INTERVAL = 15;
|
| 73 | private static final int ATTACK_DURATION = 200;
|
| 74 | private static final float MOVEMENT_SPEED_WHEN_FIGHTING = 0.3F;
|
| 75 | private static final float SPEED_MULTIPLIER_WHEN_IDLING = 0.4F;
|
| 76 | private static final boolean DEFAULT_BABY = false;
|
| 77 | private int attackAnimationRemainingTicks;
|
| 78 | private static final Brain.Provider<Zoglin> BRAIN_PROVIDER = Brain.provider(
|
| 79 | List.of(SensorType.NEAREST_LIVING_ENTITIES, SensorType.NEAREST_PLAYERS), var0 -> getActivities()
|
| 80 | );
|
| 81 |
|
| 82 | public Zoglin(final EntityType<? extends Zoglin> type, final Level level) {
|
| 83 | super(type, level);
|
| 84 | this.xpReward = 5;
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | protected Brain<Zoglin> makeBrain(final Brain.Packed packedBrain) {
|
| 89 | return BRAIN_PROVIDER.makeBrain(this, packedBrain);
|
| 90 | }
|
| 91 |
|
| 92 | protected static List<ActivityData<Zoglin>> getActivities() {
|
| 93 | return List.of(initCoreActivity(), initIdleActivity(), initFightActivity());
|
| 94 | }
|
| 95 |
|
| 96 | private static ActivityData<Zoglin> initCoreActivity() {
|
| 97 | return ActivityData.create(Activity.CORE, 0, ImmutableList.of(new LookAtTargetSink(45, 90), new MoveToTargetSink()));
|
| 98 | }
|
| 99 |
|
| 100 | private static ActivityData<Zoglin> initIdleActivity() {
|
| 101 | return ActivityData.create(
|
| 102 | Activity.IDLE,
|
| 103 | 10,
|
| 104 | ImmutableList.of(
|
| 105 | StartAttacking.create(Zoglin::findNearestValidAttackTarget),
|
| 106 | SetEntityLookTargetSometimes.create(8.0F, UniformInt.of(30, 60)),
|
| 107 | new RunOne<>(
|
| 108 | ImmutableList.of(Pair.of(RandomStroll.stroll(0.4F), 2), Pair.of(SetWalkTargetFromLookTarget.create(0.4F, 3), 2), Pair.of(new DoNothing(30, 60), 1))
|
| 109 | )
|
| 110 | )
|
| 111 | );
|
| 112 | }
|
| 113 |
|
| 114 | @Override
|
| 115 | public EntityDimensions getDefaultDimensions(final Pose pose) {
|
| 116 | return this.isBaby() ? BABY_DIMENSIONS : super.getDefaultDimensions(pose);
|
| 117 | }
|
| 118 |
|
| 119 | private static ActivityData<Zoglin> initFightActivity() {
|
| 120 | return ActivityData.create(
|
| 121 | Activity.FIGHT,
|
| 122 | 10,
|
| 123 | ImmutableList.of(
|
| 124 | SetWalkTargetFromAttackTargetIfTargetOutOfReach.create(1.0F),
|
| 125 | BehaviorBuilder.triggerIf(Zoglin::isAdult, MeleeAttack.create(40)),
|
| 126 | BehaviorBuilder.triggerIf(Zoglin::isBaby, MeleeAttack.create(15)),
|
| 127 | StopAttackingIfTargetInvalid.create()
|
| 128 | ),
|
| 129 | MemoryModuleType.ATTACK_TARGET
|
| 130 | );
|
| 131 | }
|
| 132 |
|
| 133 | private static Optional<? extends LivingEntity> findNearestValidAttackTarget(final ServerLevel level, final Mob mob) {
|
| 134 | return mob.getBrain()
|
| 135 | .getMemory(MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES)
|
| 136 | .orElse(NearestVisibleLivingEntities.empty())
|
| 137 | .findClosest(target -> !target.is(EntityTypes.ZOGLIN) && !target.is(EntityTypes.CREEPER) && Sensor.isEntityAttackable(level, mob, target));
|
| 138 | }
|
| 139 |
|
| 140 | @Override
|
| 141 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 142 | super.defineSynchedData(entityData);
|
| 143 | entityData.define(DATA_BABY_ID, false);
|
| 144 | }
|
| 145 |
|
| 146 | @Override
|
| 147 | public void onSyncedDataUpdated(final EntityDataAccessor<?> accessor) {
|
| 148 | super.onSyncedDataUpdated(accessor);
|
| 149 | if (DATA_BABY_ID.equals(accessor)) {
|
| 150 | this.refreshDimensions();
|
| 151 | }
|
| 152 | }
|
| 153 |
|
| 154 | @Override
|
| 155 | public @Nullable SpawnGroupData finalizeSpawn(
|
| 156 | final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, final @Nullable SpawnGroupData groupData
|
| 157 | ) {
|
| 158 | if (level.getRandom().nextFloat() < 0.2F) {
|
| 159 | this.setBaby(true);
|
| 160 | }
|
| 161 |
|
| 162 | return super.finalizeSpawn(level, difficulty, spawnReason, groupData);
|
| 163 | }
|
| 164 |
|
| 165 | public static AttributeSupplier.Builder createAttributes() {
|
| 166 | return Monster.createMonsterAttributes()
|
| 167 | .add(Attributes.MAX_HEALTH, 40.0)
|
| 168 | .add(Attributes.MOVEMENT_SPEED, 0.3F)
|
| 169 | .add(Attributes.KNOCKBACK_RESISTANCE, 0.6F)
|
| 170 | .add(Attributes.ATTACK_KNOCKBACK, 1.0)
|
| 171 | .add(Attributes.ATTACK_DAMAGE, 6.0);
|
| 172 | }
|
| 173 |
|
| 174 | public boolean isAdult() {
|
| 175 | return !this.isBaby();
|
| 176 | }
|
| 177 |
|
| 178 | @Override
|
| 179 | public boolean doHurtTarget(final ServerLevel level, final Entity target) {
|
| 180 | if (target instanceof LivingEntity entity) {
|
| 181 | this.attackAnimationRemainingTicks = 10;
|
| 182 | level.broadcastEntityEvent(this, (byte)4);
|
| 183 | this.makeSound(SoundEvents.ZOGLIN_ATTACK);
|
| 184 | return HoglinBase.hurtAndThrowTarget(level, this, entity);
|
| 185 | } else {
|
| 186 | return false;
|
| 187 | }
|
| 188 | }
|
| 189 |
|
| 190 | @Override
|
| 191 | public boolean canBeLeashed() {
|
| 192 | return true;
|
| 193 | }
|
| 194 |
|
| 195 | @Override
|
| 196 | protected void blockedByItem(final LivingEntity defender, final DamageSource source, final float damage, final boolean fullyBlocked) {
|
| 197 | if (!this.isBaby()) {
|
| 198 | HoglinBase.throwTarget(this, defender);
|
| 199 | }
|
| 200 | }
|
| 201 |
|
| 202 | @Override
|
| 203 | public boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage) {
|
| 204 | boolean wasHurt = super.hurtServer(level, source, damage);
|
| 205 | if (wasHurt && source.getEntity() instanceof LivingEntity attacker) {
|
| 206 | if (this.canAttack(attacker) && !BehaviorUtils.isOtherTargetMuchFurtherAwayThanCurrentAttackTarget(this, attacker, 4.0)) {
|
| 207 | this.setAttackTarget(attacker);
|
| 208 | }
|
| 209 |
|
| 210 | return true;
|
| 211 | } else {
|
| 212 | return wasHurt;
|
| 213 | }
|
| 214 | }
|
| 215 |
|
| 216 | private void setAttackTarget(final LivingEntity target) {
|
| 217 | this.brain.eraseMemory(MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE);
|
| 218 | this.brain.setMemoryWithExpiry(MemoryModuleType.ATTACK_TARGET, target, 200L);
|
| 219 | }
|
| 220 |
|
| 221 | @Override
|
| 222 | public Brain<Zoglin> getBrain() {
|
| 223 | return super.getBrain();
|
| 224 | }
|
| 225 |
|
| 226 | protected void updateActivity() {
|
| 227 | Activity oldActivity = this.brain.getActiveNonCoreActivity().orElse(null);
|
| 228 | this.brain.setActiveActivityToFirstValid(ImmutableList.of(Activity.FIGHT, Activity.IDLE));
|
| 229 | Activity newActivity = this.brain.getActiveNonCoreActivity().orElse(null);
|
| 230 | if (newActivity == Activity.FIGHT && oldActivity != Activity.FIGHT) {
|
| 231 | this.playAngrySound();
|
| 232 | }
|
| 233 |
|
| 234 | this.setAggressive(this.brain.hasMemoryValue(MemoryModuleType.ATTACK_TARGET));
|
| 235 | }
|
| 236 |
|
| 237 | @Override
|
| 238 | protected void customServerAiStep(final ServerLevel level) {
|
| 239 | ProfilerFiller profiler = Profiler.get();
|
| 240 | profiler.push("zoglinBrain");
|
| 241 | this.getBrain().tick(level, this);
|
| 242 | profiler.pop();
|
| 243 | this.updateActivity();
|
| 244 | }
|
| 245 |
|
| 246 | @Override
|
| 247 | public void setBaby(final boolean baby) {
|
| 248 | this.getEntityData().set(DATA_BABY_ID, baby);
|
| 249 | if (!this.level().isClientSide() && baby) {
|
| 250 | this.getAttribute(Attributes.ATTACK_DAMAGE).setBaseValue(0.5);
|
| 251 | }
|
| 252 | }
|
| 253 |
|
| 254 | @Override
|
| 255 | public boolean isBaby() {
|
| 256 | return this.getEntityData().get(DATA_BABY_ID);
|
| 257 | }
|
| 258 |
|
| 259 | @Override
|
| 260 | public void aiStep() {
|
| 261 | if (this.attackAnimationRemainingTicks > 0) {
|
| 262 | this.attackAnimationRemainingTicks--;
|
| 263 | }
|
| 264 |
|
| 265 | super.aiStep();
|
| 266 | }
|
| 267 |
|
| 268 | @Override
|
| 269 | public void handleEntityEvent(final @EntityEvent.Value byte id) {
|
| 270 | if (id == 4) {
|
| 271 | this.attackAnimationRemainingTicks = 10;
|
| 272 | this.makeSound(SoundEvents.ZOGLIN_ATTACK);
|
| 273 | } else {
|
| 274 | super.handleEntityEvent(id);
|
| 275 | }
|
| 276 | }
|
| 277 |
|
| 278 | @Override
|
| 279 | public int getAttackAnimationRemainingTicks() {
|
| 280 | return this.attackAnimationRemainingTicks;
|
| 281 | }
|
| 282 |
|
| 283 | @Override
|
| 284 | protected SoundEvent getAmbientSound() {
|
| 285 | if (this.level().isClientSide()) {
|
| 286 | return null;
|
| 287 | } else {
|
| 288 | return this.brain.hasMemoryValue(MemoryModuleType.ATTACK_TARGET) ? SoundEvents.ZOGLIN_ANGRY : SoundEvents.ZOGLIN_AMBIENT;
|
| 289 | }
|
| 290 | }
|
| 291 |
|
| 292 | @Override
|
| 293 | protected SoundEvent getHurtSound(final DamageSource source) {
|
| 294 | return SoundEvents.ZOGLIN_HURT;
|
| 295 | }
|
| 296 |
|
| 297 | @Override
|
| 298 | protected SoundEvent getDeathSound() {
|
| 299 | return SoundEvents.ZOGLIN_DEATH;
|
| 300 | }
|
| 301 |
|
| 302 | @Override
|
| 303 | protected void playStepSound(final BlockPos pos, final BlockState blockState) {
|
| 304 | this.playSound(SoundEvents.ZOGLIN_STEP, 0.15F, 1.0F);
|
| 305 | }
|
| 306 |
|
| 307 | protected void playAngrySound() {
|
| 308 | this.makeSound(SoundEvents.ZOGLIN_ANGRY);
|
| 309 | }
|
| 310 |
|
| 311 | @Override
|
| 312 | public @Nullable LivingEntity getTarget() {
|
| 313 | return this.getTargetFromBrain();
|
| 314 | }
|
| 315 |
|
| 316 | @Override
|
| 317 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 318 | super.addAdditionalSaveData(output);
|
| 319 | output.putBoolean("IsBaby", this.isBaby());
|
| 320 | }
|
| 321 |
|
| 322 | @Override
|
| 323 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 324 | super.readAdditionalSaveData(input);
|
| 325 | this.setBaby(input.getBooleanOr("IsBaby", false));
|
| 326 | }
|
| 327 | }
|
| 328 |
|