| 1 | package net.minecraft.world.entity.animal.allay;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import java.util.List;
|
| 5 | import java.util.Objects;
|
| 6 | import java.util.Optional;
|
| 7 | import java.util.UUID;
|
| 8 | import java.util.function.BiConsumer;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.core.GlobalPos;
|
| 11 | import net.minecraft.core.Holder;
|
| 12 | import net.minecraft.core.Vec3i;
|
| 13 | import net.minecraft.core.component.DataComponents;
|
| 14 | import net.minecraft.core.particles.ParticleTypes;
|
| 15 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 16 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 17 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 18 | import net.minecraft.server.level.ServerLevel;
|
| 19 | import net.minecraft.sounds.SoundEvent;
|
| 20 | import net.minecraft.sounds.SoundEvents;
|
| 21 | import net.minecraft.sounds.SoundSource;
|
| 22 | import net.minecraft.tags.GameEventTags;
|
| 23 | import net.minecraft.tags.ItemTags;
|
| 24 | import net.minecraft.tags.TagKey;
|
| 25 | import net.minecraft.util.Mth;
|
| 26 | import net.minecraft.util.profiling.Profiler;
|
| 27 | import net.minecraft.util.profiling.ProfilerFiller;
|
| 28 | import net.minecraft.world.InteractionHand;
|
| 29 | import net.minecraft.world.InteractionResult;
|
| 30 | import net.minecraft.world.SimpleContainer;
|
| 31 | import net.minecraft.world.damagesource.DamageSource;
|
| 32 | import net.minecraft.world.entity.Entity;
|
| 33 | import net.minecraft.world.entity.EntityEvent;
|
| 34 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 35 | import net.minecraft.world.entity.EntityType;
|
| 36 | import net.minecraft.world.entity.EntityTypes;
|
| 37 | import net.minecraft.world.entity.EquipmentSlot;
|
| 38 | import net.minecraft.world.entity.Mob;
|
| 39 | import net.minecraft.world.entity.PathfinderMob;
|
| 40 | import net.minecraft.world.entity.ai.Brain;
|
| 41 | import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
|
| 42 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 43 | import net.minecraft.world.entity.ai.behavior.BehaviorUtils;
|
| 44 | import net.minecraft.world.entity.ai.control.FlyingMoveControl;
|
| 45 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 46 | import net.minecraft.world.entity.ai.memory.MemoryStatus;
|
| 47 | import net.minecraft.world.entity.ai.navigation.FlyingPathNavigation;
|
| 48 | import net.minecraft.world.entity.ai.navigation.PathNavigation;
|
| 49 | import net.minecraft.world.entity.ai.sensing.SensorType;
|
| 50 | import net.minecraft.world.entity.item.ItemEntity;
|
| 51 | import net.minecraft.world.entity.npc.InventoryCarrier;
|
| 52 | import net.minecraft.world.entity.player.Player;
|
| 53 | import net.minecraft.world.item.ItemStack;
|
| 54 | import net.minecraft.world.item.alchemy.PotionContents;
|
| 55 | import net.minecraft.world.item.enchantment.EnchantmentEffectComponents;
|
| 56 | import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
| 57 | import net.minecraft.world.level.Level;
|
| 58 | import net.minecraft.world.level.block.Blocks;
|
| 59 | import net.minecraft.world.level.block.state.BlockState;
|
| 60 | import net.minecraft.world.level.gameevent.DynamicGameEventListener;
|
| 61 | import net.minecraft.world.level.gameevent.EntityPositionSource;
|
| 62 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 63 | import net.minecraft.world.level.gameevent.GameEventListener;
|
| 64 | import net.minecraft.world.level.gameevent.PositionSource;
|
| 65 | import net.minecraft.world.level.gameevent.vibrations.VibrationSystem;
|
| 66 | import net.minecraft.world.level.gamerules.GameRules;
|
| 67 | import net.minecraft.world.level.storage.ValueInput;
|
| 68 | import net.minecraft.world.level.storage.ValueOutput;
|
| 69 | import net.minecraft.world.phys.Vec3;
|
| 70 | import org.jspecify.annotations.Nullable;
|
| 71 |
|
| 72 | public class Allay extends PathfinderMob implements InventoryCarrier, VibrationSystem {
|
| 73 | private static final Vec3i ITEM_PICKUP_REACH = new Vec3i(1, 1, 1);
|
| 74 | private static final int LIFTING_ITEM_ANIMATION_DURATION = 5;
|
| 75 | private static final float DANCING_LOOP_DURATION = 55.0F;
|
| 76 | private static final float SPINNING_ANIMATION_DURATION = 15.0F;
|
| 77 | private static final int DEFAULT_DUPLICATION_COOLDOWN = 0;
|
| 78 | private static final int DUPLICATION_COOLDOWN_TICKS = 6000;
|
| 79 | private static final int NUM_OF_DUPLICATION_HEARTS = 3;
|
| 80 | public static final int MAX_NOTEBLOCK_DISTANCE = 1024;
|
| 81 | private static final EntityDataAccessor<Boolean> DATA_DANCING = SynchedEntityData.defineId(Allay.class, EntityDataSerializers.BOOLEAN);
|
| 82 | private static final EntityDataAccessor<Boolean> DATA_CAN_DUPLICATE = SynchedEntityData.defineId(Allay.class, EntityDataSerializers.BOOLEAN);
|
| 83 | private static final Brain.Provider<Allay> BRAIN_PROVIDER = Brain.provider(
|
| 84 | List.of(MemoryModuleType.LIKED_PLAYER, MemoryModuleType.LIKED_NOTEBLOCK_POSITION, MemoryModuleType.LIKED_NOTEBLOCK_COOLDOWN_TICKS),
|
| 85 | List.of(SensorType.NEAREST_LIVING_ENTITIES, SensorType.NEAREST_PLAYERS, SensorType.HURT_BY, SensorType.NEAREST_ITEMS),
|
| 86 | var0 -> AllayAi.getActivities()
|
| 87 | );
|
| 88 | public static final ImmutableList<Float> THROW_SOUND_PITCHES = ImmutableList.of(
|
| 89 | 0.5625F, 0.625F, 0.75F, 0.9375F, 1.0F, 1.0F, 1.125F, 1.25F, 1.5F, 1.875F, 2.0F, 2.25F, 2.5F, 3.0F, 3.75F, 4.0F
|
| 90 | );
|
| 91 | private final DynamicGameEventListener<VibrationSystem.Listener> dynamicVibrationListener;
|
| 92 | private VibrationSystem.Data vibrationData;
|
| 93 | private final VibrationSystem.User vibrationUser;
|
| 94 | private final DynamicGameEventListener<Allay.JukeboxListener> dynamicJukeboxListener;
|
| 95 | private final SimpleContainer inventory = new SimpleContainer(1);
|
| 96 | private @Nullable BlockPos jukeboxPos;
|
| 97 | private long duplicationCooldown = 0L;
|
| 98 | private float holdingItemAnimationTicks;
|
| 99 | private float holdingItemAnimationTicks0;
|
| 100 | private float dancingAnimationTicks;
|
| 101 | private float spinningAnimationTicks;
|
| 102 | private float spinningAnimationTicks0;
|
| 103 |
|
| 104 | public Allay(final EntityType<? extends Allay> type, final Level level) {
|
| 105 | super(type, level);
|
| 106 | this.moveControl = new FlyingMoveControl<>(this, 20, true);
|
| 107 | this.setCanPickUpLoot(this.canPickUpLoot());
|
| 108 | this.vibrationUser = new Allay.VibrationUser();
|
| 109 | this.vibrationData = new VibrationSystem.Data();
|
| 110 | this.dynamicVibrationListener = new DynamicGameEventListener<>(new VibrationSystem.Listener(this));
|
| 111 | this.dynamicJukeboxListener = new DynamicGameEventListener<>(
|
| 112 | new Allay.JukeboxListener(this.vibrationUser.getPositionSource(), GameEvent.JUKEBOX_PLAY.value().notificationRadius())
|
| 113 | );
|
| 114 | }
|
| 115 |
|
| 116 | @Override
|
| 117 | protected Brain<Allay> makeBrain(final Brain.Packed packedBrain) {
|
| 118 | return BRAIN_PROVIDER.makeBrain(this, packedBrain);
|
| 119 | }
|
| 120 |
|
| 121 | @Override
|
| 122 | public Brain<Allay> getBrain() {
|
| 123 | return super.getBrain();
|
| 124 | }
|
| 125 |
|
| 126 | public static AttributeSupplier.Builder createAttributes() {
|
| 127 | return Mob.createMobAttributes()
|
| 128 | .add(Attributes.MAX_HEALTH, 20.0)
|
| 129 | .add(Attributes.FLYING_SPEED, 0.1F)
|
| 130 | .add(Attributes.MOVEMENT_SPEED, 0.1F)
|
| 131 | .add(Attributes.ATTACK_DAMAGE, 2.0);
|
| 132 | }
|
| 133 |
|
| 134 | @Override
|
| 135 | protected PathNavigation createNavigation(final Level level) {
|
| 136 | FlyingPathNavigation flyingPathNavigation = new FlyingPathNavigation(this, level);
|
| 137 | flyingPathNavigation.setCanOpenDoors(false);
|
| 138 | flyingPathNavigation.setCanFloat(true);
|
| 139 | flyingPathNavigation.setRequiredPathLength(48.0F);
|
| 140 | return flyingPathNavigation;
|
| 141 | }
|
| 142 |
|
| 143 | @Override
|
| 144 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 145 | super.defineSynchedData(entityData);
|
| 146 | entityData.define(DATA_DANCING, false);
|
| 147 | entityData.define(DATA_CAN_DUPLICATE, true);
|
| 148 | }
|
| 149 |
|
| 150 | @Override
|
| 151 | public void travel(final Vec3 input) {
|
| 152 | this.travelFlying(input, this.getSpeed());
|
| 153 | }
|
| 154 |
|
| 155 | @Override
|
| 156 | public boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage) {
|
| 157 | return this.isLikedPlayer(source.getEntity()) ? false : super.hurtServer(level, source, damage);
|
| 158 | }
|
| 159 |
|
| 160 | @Override
|
| 161 | protected boolean considersEntityAsAlly(final Entity other) {
|
| 162 | return this.isLikedPlayer(other) || super.considersEntityAsAlly(other);
|
| 163 | }
|
| 164 |
|
| 165 | private boolean isLikedPlayer(final @Nullable Entity other) {
|
| 166 | if (!(other instanceof Player player)) {
|
| 167 | return false;
|
| 168 | } else {
|
| 169 | Optional<UUID> likedPlayer = this.getBrain().getMemory(MemoryModuleType.LIKED_PLAYER);
|
| 170 | return likedPlayer.isPresent() && player.getUUID().equals(likedPlayer.get());
|
| 171 | }
|
| 172 | }
|
| 173 |
|
| 174 | @Override
|
| 175 | protected void playStepSound(final BlockPos pos, final BlockState blockState) {
|
| 176 | }
|
| 177 |
|
| 178 | @Override
|
| 179 | protected void checkFallDamage(final double ya, final boolean onGround, final BlockState onState, final BlockPos pos) {
|
| 180 | }
|
| 181 |
|
| 182 | @Override
|
| 183 | protected SoundEvent getAmbientSound() {
|
| 184 | return this.hasItemInSlot(EquipmentSlot.MAINHAND) ? SoundEvents.ALLAY_AMBIENT_WITH_ITEM : SoundEvents.ALLAY_AMBIENT_WITHOUT_ITEM;
|
| 185 | }
|
| 186 |
|
| 187 | @Override
|
| 188 | protected SoundEvent getHurtSound(final DamageSource source) {
|
| 189 | return SoundEvents.ALLAY_HURT;
|
| 190 | }
|
| 191 |
|
| 192 | @Override
|
| 193 | protected SoundEvent getDeathSound() {
|
| 194 | return SoundEvents.ALLAY_DEATH;
|
| 195 | }
|
| 196 |
|
| 197 | @Override
|
| 198 | protected float getSoundVolume() {
|
| 199 | return 0.4F;
|
| 200 | }
|
| 201 |
|
| 202 | @Override
|
| 203 | protected void customServerAiStep(final ServerLevel level) {
|
| 204 | ProfilerFiller profiler = Profiler.get();
|
| 205 | profiler.push("allayBrain");
|
| 206 | this.getBrain().tick(level, this);
|
| 207 | profiler.pop();
|
| 208 | profiler.push("allayActivityUpdate");
|
| 209 | AllayAi.updateActivity(this);
|
| 210 | profiler.pop();
|
| 211 | super.customServerAiStep(level);
|
| 212 | }
|
| 213 |
|
| 214 | @Override
|
| 215 | public void aiStep() {
|
| 216 | super.aiStep();
|
| 217 | if (!this.level().isClientSide() && this.isAlive() && this.tickCount % 10 == 0) {
|
| 218 | this.heal(1.0F);
|
| 219 | }
|
| 220 |
|
| 221 | if (this.isDancing() && this.shouldStopDancing() && this.tickCount % 20 == 0) {
|
| 222 | this.setDancing(false);
|
| 223 | this.jukeboxPos = null;
|
| 224 | }
|
| 225 |
|
| 226 | this.updateDuplicationCooldown();
|
| 227 | }
|
| 228 |
|
| 229 | @Override
|
| 230 | public void tick() {
|
| 231 | super.tick();
|
| 232 | if (this.level().isClientSide()) {
|
| 233 | this.holdingItemAnimationTicks0 = this.holdingItemAnimationTicks;
|
| 234 | if (this.hasItemInHand()) {
|
| 235 | this.holdingItemAnimationTicks = Mth.clamp(this.holdingItemAnimationTicks + 1.0F, 0.0F, 5.0F);
|
| 236 | } else {
|
| 237 | this.holdingItemAnimationTicks = Mth.clamp(this.holdingItemAnimationTicks - 1.0F, 0.0F, 5.0F);
|
| 238 | }
|
| 239 |
|
| 240 | if (this.isDancing()) {
|
| 241 | this.dancingAnimationTicks++;
|
| 242 | this.spinningAnimationTicks0 = this.spinningAnimationTicks;
|
| 243 | if (this.isSpinning()) {
|
| 244 | this.spinningAnimationTicks++;
|
| 245 | } else {
|
| 246 | this.spinningAnimationTicks--;
|
| 247 | }
|
| 248 |
|
| 249 | this.spinningAnimationTicks = Mth.clamp(this.spinningAnimationTicks, 0.0F, 15.0F);
|
| 250 | } else {
|
| 251 | this.dancingAnimationTicks = 0.0F;
|
| 252 | this.spinningAnimationTicks = 0.0F;
|
| 253 | this.spinningAnimationTicks0 = 0.0F;
|
| 254 | }
|
| 255 | } else {
|
| 256 | VibrationSystem.Ticker.tick(this.level(), this.vibrationData, this.vibrationUser);
|
| 257 | if (this.isPanicking()) {
|
| 258 | this.setDancing(false);
|
| 259 | }
|
| 260 | }
|
| 261 | }
|
| 262 |
|
| 263 | @Override
|
| 264 | public boolean canPickUpLoot() {
|
| 265 | return !this.isOnPickupCooldown() && this.hasItemInHand();
|
| 266 | }
|
| 267 |
|
| 268 | public boolean hasItemInHand() {
|
| 269 | return !this.getItemInHand(InteractionHand.MAIN_HAND).isEmpty();
|
| 270 | }
|
| 271 |
|
| 272 | @Override
|
| 273 | protected boolean canDispenserEquipIntoSlot(final EquipmentSlot slot) {
|
| 274 | return false;
|
| 275 | }
|
| 276 |
|
| 277 | private boolean isOnPickupCooldown() {
|
| 278 | return this.getBrain().checkMemory(MemoryModuleType.ITEM_PICKUP_COOLDOWN_TICKS, MemoryStatus.VALUE_PRESENT);
|
| 279 | }
|
| 280 |
|
| 281 | @Override
|
| 282 | protected InteractionResult mobInteract(final Player player, final InteractionHand hand) {
|
| 283 | ItemStack interactionItem = player.getItemInHand(hand);
|
| 284 | ItemStack itemInHand = this.getItemInHand(InteractionHand.MAIN_HAND);
|
| 285 | if (this.isDancing() && interactionItem.is(ItemTags.DUPLICATES_ALLAYS) && this.canDuplicate()) {
|
| 286 | this.duplicateAllay();
|
| 287 | this.level().broadcastEntityEvent(this, (byte)18);
|
| 288 | this.level().playSound(player, this, SoundEvents.AMETHYST_BLOCK_CHIME, SoundSource.NEUTRAL, 2.0F, 1.0F);
|
| 289 | this.removeInteractionItem(player, interactionItem);
|
| 290 | return InteractionResult.SUCCESS;
|
| 291 | }
|
| 292 |
|
| 293 | if (itemInHand.isEmpty() && !interactionItem.isEmpty()) {
|
| 294 | ItemStack itemToGive = interactionItem.copyWithCount(1);
|
| 295 | this.setItemInHand(InteractionHand.MAIN_HAND, itemToGive);
|
| 296 | this.removeInteractionItem(player, interactionItem);
|
| 297 | this.level().playSound(player, this, SoundEvents.ALLAY_ITEM_GIVEN, SoundSource.NEUTRAL, 2.0F, 1.0F);
|
| 298 | this.getBrain().setMemory(MemoryModuleType.LIKED_PLAYER, player.getUUID());
|
| 299 | return InteractionResult.SUCCESS;
|
| 300 | }
|
| 301 |
|
| 302 | if (!itemInHand.isEmpty() && hand == InteractionHand.MAIN_HAND && interactionItem.isEmpty()) {
|
| 303 | this.setItemSlot(EquipmentSlot.MAINHAND, ItemStack.EMPTY);
|
| 304 | this.level().playSound(player, this, SoundEvents.ALLAY_ITEM_TAKEN, SoundSource.NEUTRAL, 2.0F, 1.0F);
|
| 305 | this.swing(InteractionHand.MAIN_HAND, itemInHand.getInteractAnimation());
|
| 306 |
|
| 307 | for (ItemStack itemStack : this.getInventory().removeAllItems()) {
|
| 308 | BehaviorUtils.throwItem(this, itemStack, this.position());
|
| 309 | }
|
| 310 |
|
| 311 | this.getBrain().eraseMemory(MemoryModuleType.LIKED_PLAYER);
|
| 312 | player.addItem(itemInHand);
|
| 313 | return InteractionResult.SUCCESS;
|
| 314 | } else {
|
| 315 | return super.mobInteract(player, hand);
|
| 316 | }
|
| 317 | }
|
| 318 |
|
| 319 | public void setJukeboxPlaying(final BlockPos jukebox, final boolean isPlaying) {
|
| 320 | if (isPlaying) {
|
| 321 | if (!this.isDancing()) {
|
| 322 | this.jukeboxPos = jukebox;
|
| 323 | this.setDancing(true);
|
| 324 | }
|
| 325 | } else if (jukebox.equals(this.jukeboxPos) || this.jukeboxPos == null) {
|
| 326 | this.jukeboxPos = null;
|
| 327 | this.setDancing(false);
|
| 328 | }
|
| 329 | }
|
| 330 |
|
| 331 | @Override
|
| 332 | public SimpleContainer getInventory() {
|
| 333 | return this.inventory;
|
| 334 | }
|
| 335 |
|
| 336 | @Override
|
| 337 | protected Vec3i getPickupReach() {
|
| 338 | return ITEM_PICKUP_REACH;
|
| 339 | }
|
| 340 |
|
| 341 | @Override
|
| 342 | public boolean wantsToPickUp(final ServerLevel level, final ItemStack itemStack) {
|
| 343 | ItemStack itemInHand = this.getItemInHand(InteractionHand.MAIN_HAND);
|
| 344 | return !itemInHand.isEmpty()
|
| 345 | && level.getGameRules().get(GameRules.MOB_GRIEFING)
|
| 346 | && this.inventory.canAddItem(itemStack)
|
| 347 | && this.allayConsidersItemEqual(itemInHand, itemStack);
|
| 348 | }
|
| 349 |
|
| 350 | private boolean allayConsidersItemEqual(final ItemStack item1, final ItemStack item2) {
|
| 351 | return ItemStack.isSameItem(item1, item2) && !this.hasNonMatchingPotion(item1, item2);
|
| 352 | }
|
| 353 |
|
| 354 | private boolean hasNonMatchingPotion(final ItemStack itemInHand, final ItemStack pickupItem) {
|
| 355 | PotionContents potionInHand = itemInHand.get(DataComponents.POTION_CONTENTS);
|
| 356 | PotionContents potionInPickupItem = pickupItem.get(DataComponents.POTION_CONTENTS);
|
| 357 | return !Objects.equals(potionInHand, potionInPickupItem);
|
| 358 | }
|
| 359 |
|
| 360 | @Override
|
| 361 | protected void pickUpItem(final ServerLevel level, final ItemEntity entity) {
|
| 362 | InventoryCarrier.pickUpItem(level, this, this, entity);
|
| 363 | }
|
| 364 |
|
| 365 | @Override
|
| 366 | public boolean isFlapping() {
|
| 367 | return !this.onGround();
|
| 368 | }
|
| 369 |
|
| 370 | @Override
|
| 371 | public void updateDynamicGameEventListener(final BiConsumer<DynamicGameEventListener<?>, ServerLevel> action) {
|
| 372 | if (this.level() instanceof ServerLevel serverLevel) {
|
| 373 | action.accept(this.dynamicVibrationListener, serverLevel);
|
| 374 | action.accept(this.dynamicJukeboxListener, serverLevel);
|
| 375 | }
|
| 376 | }
|
| 377 |
|
| 378 | public boolean isDancing() {
|
| 379 | return this.entityData.get(DATA_DANCING);
|
| 380 | }
|
| 381 |
|
| 382 | public void setDancing(final boolean isDancing) {
|
| 383 | if (!this.level().isClientSide() && this.isEffectiveAi() && (!isDancing || !this.isPanicking())) {
|
| 384 | this.entityData.set(DATA_DANCING, isDancing);
|
| 385 | }
|
| 386 | }
|
| 387 |
|
| 388 | private boolean shouldStopDancing() {
|
| 389 | return this.jukeboxPos == null
|
| 390 | || !this.jukeboxPos.closerToCenterThan(this.position(), GameEvent.JUKEBOX_PLAY.value().notificationRadius())
|
| 391 | || !this.level().getBlockState(this.jukeboxPos).is(Blocks.JUKEBOX);
|
| 392 | }
|
| 393 |
|
| 394 | public float getHoldingItemAnimationProgress(final float a) {
|
| 395 | return Mth.lerp(a, this.holdingItemAnimationTicks0, this.holdingItemAnimationTicks) / 5.0F;
|
| 396 | }
|
| 397 |
|
| 398 | public boolean isSpinning() {
|
| 399 | float spinningProgress = this.dancingAnimationTicks % 55.0F;
|
| 400 | return spinningProgress < 15.0F;
|
| 401 | }
|
| 402 |
|
| 403 | public float getSpinningProgress(final float a) {
|
| 404 | return Mth.lerp(a, this.spinningAnimationTicks0, this.spinningAnimationTicks) / 15.0F;
|
| 405 | }
|
| 406 |
|
| 407 | @Override
|
| 408 | public boolean equipmentHasChanged(final ItemStack previous, final ItemStack current) {
|
| 409 | return !this.allayConsidersItemEqual(previous, current);
|
| 410 | }
|
| 411 |
|
| 412 | @Override
|
| 413 | protected void dropEquipment(final ServerLevel level) {
|
| 414 | super.dropEquipment(level);
|
| 415 | this.inventory.removeAllItems().forEach(stack -> this.spawnAtLocation(level, stack));
|
| 416 | ItemStack itemStack = this.getItemBySlot(EquipmentSlot.MAINHAND);
|
| 417 | if (!itemStack.isEmpty() && !EnchantmentHelper.has(itemStack, EnchantmentEffectComponents.PREVENT_EQUIPMENT_DROP)) {
|
| 418 | this.spawnAtLocation(level, itemStack);
|
| 419 | this.setItemSlot(EquipmentSlot.MAINHAND, ItemStack.EMPTY);
|
| 420 | }
|
| 421 | }
|
| 422 |
|
| 423 | @Override
|
| 424 | public boolean removeWhenFarAway(final double distSqr) {
|
| 425 | return false;
|
| 426 | }
|
| 427 |
|
| 428 | @Override
|
| 429 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 430 | super.addAdditionalSaveData(output);
|
| 431 | this.writeInventoryToTag(output);
|
| 432 | output.store("listener", VibrationSystem.Data.CODEC, this.vibrationData);
|
| 433 | output.putLong("DuplicationCooldown", this.duplicationCooldown);
|
| 434 | }
|
| 435 |
|
| 436 | @Override
|
| 437 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 438 | super.readAdditionalSaveData(input);
|
| 439 | this.readInventoryFromTag(input);
|
| 440 | this.vibrationData = input.read("listener", VibrationSystem.Data.CODEC).orElseGet(VibrationSystem.Data::new);
|
| 441 | this.setDuplicationCooldown(input.getIntOr("DuplicationCooldown", 0));
|
| 442 | }
|
| 443 |
|
| 444 | @Override
|
| 445 | protected boolean shouldStayCloseToLeashHolder() {
|
| 446 | return false;
|
| 447 | }
|
| 448 |
|
| 449 | private void updateDuplicationCooldown() {
|
| 450 | if (!this.level().isClientSide() && this.duplicationCooldown > 0L) {
|
| 451 | this.setDuplicationCooldown(this.duplicationCooldown - 1L);
|
| 452 | }
|
| 453 | }
|
| 454 |
|
| 455 | private void setDuplicationCooldown(final long duplicationCooldown) {
|
| 456 | this.duplicationCooldown = duplicationCooldown;
|
| 457 | this.entityData.set(DATA_CAN_DUPLICATE, duplicationCooldown == 0L);
|
| 458 | }
|
| 459 |
|
| 460 | private void duplicateAllay() {
|
| 461 | Allay allay = EntityTypes.ALLAY.create(this.level(), EntitySpawnReason.BREEDING);
|
| 462 | if (allay != null) {
|
| 463 | allay.snapTo(this.position());
|
| 464 | allay.setPersistenceRequired();
|
| 465 | allay.resetDuplicationCooldown();
|
| 466 | this.resetDuplicationCooldown();
|
| 467 | this.level().addFreshEntity(allay);
|
| 468 | }
|
| 469 | }
|
| 470 |
|
| 471 | private void resetDuplicationCooldown() {
|
| 472 | this.setDuplicationCooldown(6000L);
|
| 473 | }
|
| 474 |
|
| 475 | private boolean canDuplicate() {
|
| 476 | return this.entityData.get(DATA_CAN_DUPLICATE);
|
| 477 | }
|
| 478 |
|
| 479 | private void removeInteractionItem(final Player player, final ItemStack interactionItem) {
|
| 480 | interactionItem.consume(1, player);
|
| 481 | }
|
| 482 |
|
| 483 | @Override
|
| 484 | public Vec3 getLeashOffset() {
|
| 485 | return new Vec3(0.0, this.getEyeHeight() * 0.6, this.getBbWidth() * 0.1);
|
| 486 | }
|
| 487 |
|
| 488 | @Override
|
| 489 | public void handleEntityEvent(final @EntityEvent.Value byte id) {
|
| 490 | if (id == 18) {
|
| 491 | for (int i = 0; i < 3; i++) {
|
| 492 | this.spawnHeartParticle();
|
| 493 | }
|
| 494 | } else {
|
| 495 | super.handleEntityEvent(id);
|
| 496 | }
|
| 497 | }
|
| 498 |
|
| 499 | private void spawnHeartParticle() {
|
| 500 | double xd = this.random.nextGaussian() * 0.02;
|
| 501 | double yd = this.random.nextGaussian() * 0.02;
|
| 502 | double zd = this.random.nextGaussian() * 0.02;
|
| 503 | this.level().addParticle(ParticleTypes.HEART, this.getRandomX(1.0), this.getRandomY() + 0.5, this.getRandomZ(1.0), xd, yd, zd);
|
| 504 | }
|
| 505 |
|
| 506 | @Override
|
| 507 | public VibrationSystem.Data getVibrationData() {
|
| 508 | return this.vibrationData;
|
| 509 | }
|
| 510 |
|
| 511 | @Override
|
| 512 | public VibrationSystem.User getVibrationUser() {
|
| 513 | return this.vibrationUser;
|
| 514 | }
|
| 515 |
|
| 516 | private class JukeboxListener implements GameEventListener {
|
| 517 | private final PositionSource listenerSource;
|
| 518 | private final int listenerRadius;
|
| 519 |
|
| 520 | public JukeboxListener(final PositionSource listenerSource, final int listenerRadius) {
|
| 521 | this.listenerSource = listenerSource;
|
| 522 | this.listenerRadius = listenerRadius;
|
| 523 | }
|
| 524 |
|
| 525 | @Override
|
| 526 | public PositionSource getListenerSource() {
|
| 527 | return this.listenerSource;
|
| 528 | }
|
| 529 |
|
| 530 | @Override
|
| 531 | public int getListenerRadius() {
|
| 532 | return this.listenerRadius;
|
| 533 | }
|
| 534 |
|
| 535 | @Override
|
| 536 | public boolean handleGameEvent(final ServerLevel level, final Holder<GameEvent> event, final GameEvent.Context context, final Vec3 sourcePosition) {
|
| 537 | if (event.is(GameEvent.JUKEBOX_PLAY)) {
|
| 538 | Allay.this.setJukeboxPlaying(BlockPos.containing(sourcePosition), true);
|
| 539 | return true;
|
| 540 | } else if (event.is(GameEvent.JUKEBOX_STOP_PLAY)) {
|
| 541 | Allay.this.setJukeboxPlaying(BlockPos.containing(sourcePosition), false);
|
| 542 | return true;
|
| 543 | } else {
|
| 544 | return false;
|
| 545 | }
|
| 546 | }
|
| 547 | }
|
| 548 |
|
| 549 | private class VibrationUser implements VibrationSystem.User {
|
| 550 | private static final int VIBRATION_EVENT_LISTENER_RANGE = 16;
|
| 551 | private final PositionSource positionSource = new EntityPositionSource(Allay.this, Allay.this.getEyeHeight());
|
| 552 |
|
| 553 | @Override
|
| 554 | public int getListenerRadius() {
|
| 555 | return 16;
|
| 556 | }
|
| 557 |
|
| 558 | @Override
|
| 559 | public PositionSource getPositionSource() {
|
| 560 | return this.positionSource;
|
| 561 | }
|
| 562 |
|
| 563 | @Override
|
| 564 | public boolean canReceiveVibration(final ServerLevel level, final BlockPos pos, final Holder<GameEvent> event, final GameEvent.Context context) {
|
| 565 | if (Allay.this.isNoAi()) {
|
| 566 | return false;
|
| 567 | }
|
| 568 |
|
| 569 | Optional<GlobalPos> maybeGlobalPos = Allay.this.getBrain().getMemory(MemoryModuleType.LIKED_NOTEBLOCK_POSITION);
|
| 570 | if (maybeGlobalPos.isEmpty()) {
|
| 571 | return true;
|
| 572 | }
|
| 573 |
|
| 574 | GlobalPos globalPos = maybeGlobalPos.get();
|
| 575 | return globalPos.isCloseEnough(level.dimension(), Allay.this.blockPosition(), 1024) && globalPos.pos().equals(pos);
|
| 576 | }
|
| 577 |
|
| 578 | @Override
|
| 579 | public void onReceiveVibration(
|
| 580 | final ServerLevel level,
|
| 581 | final BlockPos pos,
|
| 582 | final Holder<GameEvent> event,
|
| 583 | final @Nullable Entity sourceEntity,
|
| 584 | final @Nullable Entity projectileOwner,
|
| 585 | final float receivingDistance
|
| 586 | ) {
|
| 587 | if (event.is(GameEvent.NOTE_BLOCK_PLAY)) {
|
| 588 | AllayAi.hearNoteblock(Allay.this, pos.immutable());
|
| 589 | }
|
| 590 | }
|
| 591 |
|
| 592 | @Override
|
| 593 | public TagKey<GameEvent> getListenableEvents() {
|
| 594 | return GameEventTags.ALLAY_CAN_LISTEN;
|
| 595 | }
|
| 596 | }
|
| 597 | }
|
| 598 |
|