--- a/net/minecraft/world/entity/Mob.java +++ b/net/minecraft/world/entity/Mob.java @@ -82,6 +_,14 @@ import net.minecraft.world.phys.Vec3; import net.minecraft.world.ticks.ContainerSingleItem; +// CraftBukkit start +import org.bukkit.event.entity.CreatureSpawnEvent; +import org.bukkit.event.entity.EntityRemoveEvent; +import org.bukkit.event.entity.EntityTargetEvent; +import org.bukkit.event.entity.EntityTransformEvent; +import org.bukkit.event.entity.EntityUnleashEvent; +// CraftBukkit end + public abstract class Mob extends LivingEntity implements EquipmentUser, Leashable, Targeting { private static final EntityDataAccessor DATA_MOB_FLAGS_ID = SynchedEntityData.defineId(Mob.class, EntityDataSerializers.BYTE); private static final int MOB_FLAG_NO_AI = 1; @@ -115,6 +_,7 @@ private final BodyRotationControl bodyRotationControl; protected PathNavigation navigation; public GoalSelector goalSelector; + public @Nullable net.minecraft.world.entity.ai.goal.FloatGoal goalFloat; // Paper - Allow nerfed mobs to jump and float public GoalSelector targetSelector; @Nullable private LivingEntity target; @@ -129,6 +_,7 @@ private Leashable.LeashData leashData; private BlockPos homePosition = BlockPos.ZERO; private int homeRadius = -1; + public boolean aware = true; // CraftBukkit protected Mob(EntityType entityType, Level level) { super(entityType, level); @@ -145,6 +_,12 @@ } } + // CraftBukkit start + public void setPersistenceRequired(boolean persistenceRequired) { + this.persistenceRequired = persistenceRequired; + } + // CraftBukkit end + protected void registerGoals() { } @@ -225,7 +_,39 @@ } public void setTarget(@Nullable LivingEntity target) { + // CraftBukkit start - fire event + this.setTarget(target, EntityTargetEvent.TargetReason.UNKNOWN); + } + + public boolean setTarget(@Nullable LivingEntity target, @Nullable EntityTargetEvent.TargetReason reason) { + if (this.getTarget() == target) { + return false; + } + if (reason != null) { + if (reason == EntityTargetEvent.TargetReason.UNKNOWN && this.getTarget() != null && target == null) { + reason = this.getTarget().isAlive() ? EntityTargetEvent.TargetReason.FORGOT_TARGET : EntityTargetEvent.TargetReason.TARGET_DIED; + } + if (reason == EntityTargetEvent.TargetReason.UNKNOWN) { + this.level().getCraftServer().getLogger().log(java.util.logging.Level.WARNING, "Unknown target reason, please report on the issue tracker", new Exception()); + } + org.bukkit.craftbukkit.entity.CraftLivingEntity ctarget = null; + if (target != null) { + ctarget = (org.bukkit.craftbukkit.entity.CraftLivingEntity) target.getBukkitEntity(); + } + org.bukkit.event.entity.EntityTargetLivingEntityEvent event = new org.bukkit.event.entity.EntityTargetLivingEntityEvent(this.getBukkitEntity(), ctarget, reason); + if (!event.callEvent()) { + return false; + } + + if (event.getTarget() != null) { + target = ((org.bukkit.craftbukkit.entity.CraftLivingEntity) event.getTarget()).getHandle(); + } else { + target = null; + } + } this.target = target; + return true; + // CraftBukkit end } @Override @@ -365,13 +_,22 @@ if (this.isNoAi()) { output.putBoolean("NoAI", this.isNoAi()); } + output.putBoolean("Bukkit.Aware", this.aware); // CraftBukkit } @Override protected void readAdditionalSaveData(ValueInput input) { super.readAdditionalSaveData(input); - this.setCanPickUpLoot(input.getBooleanOr("CanPickUpLoot", false)); - this.persistenceRequired = input.getBooleanOr("PersistenceRequired", false); + // CraftBukkit start - If looting or persistence is false only use it if it was set after we started using it + boolean canPickUpLoot = input.getBooleanOr("CanPickUpLoot", false); + if (isLevelAtLeast(input, 1) || canPickUpLoot) { + this.setCanPickUpLoot(canPickUpLoot); + } + boolean persistenceRequired = input.getBooleanOr("PersistenceRequired", false); + if (isLevelAtLeast(input, 1) || persistenceRequired) { + this.persistenceRequired = persistenceRequired; + } + // CraftBukkit end this.dropChances = input.read("drop_chances", DropChances.CODEC).orElse(DropChances.DEFAULT); this.readLeashData(input); this.homeRadius = input.getIntOr("home_radius", -1); @@ -383,6 +_,7 @@ this.lootTable = input.read("DeathLootTable", LootTable.KEY_CODEC); this.lootTableSeed = input.getLongOr("DeathLootTableSeed", 0L); this.setNoAi(input.getBooleanOr("NoAI", false)); + this.aware = input.getBooleanOr("Bukkit.Aware", true); // CraftBukkit } @Override @@ -446,6 +_,11 @@ && !itemEntity.getItem().isEmpty() && !itemEntity.hasPickUpDelay() && this.wantsToPickUp(serverLevel, itemEntity.getItem())) { + // Paper start - Item#canEntityPickup + if (!itemEntity.canMobPickup) { + continue; + } + // Paper end - Item#canEntityPickup this.pickUpItem(serverLevel, itemEntity); } } @@ -460,18 +_,24 @@ protected void pickUpItem(ServerLevel level, ItemEntity entity) { ItemStack item = entity.getItem(); - ItemStack itemStack = this.equipItemIfPossible(level, item.copy()); + ItemStack itemStack = this.equipItemIfPossible(level, item.copy(), entity); // CraftBukkit - add item if (!itemStack.isEmpty()) { this.onItemPickup(entity); this.take(entity, itemStack.getCount()); item.shrink(itemStack.getCount()); if (item.isEmpty()) { - entity.discard(); + entity.discard(EntityRemoveEvent.Cause.PICKUP); // CraftBukkit - add Bukkit remove cause } } } public ItemStack equipItemIfPossible(ServerLevel level, ItemStack stack) { + // CraftBukkit start - add item + return this.equipItemIfPossible(level, stack, null); + } + + public ItemStack equipItemIfPossible(ServerLevel level, ItemStack stack, @Nullable ItemEntity entity) { + // CraftBukkit end EquipmentSlot equipmentSlotForItem = this.getEquipmentSlotForItem(stack); if (!this.isEquippableInSlot(stack, equipmentSlotForItem)) { return ItemStack.EMPTY; @@ -484,10 +_,18 @@ canReplaceCurrentItem = itemBySlot.isEmpty(); } - if (canReplaceCurrentItem && this.canHoldItem(stack)) { + // CraftBukkit start + boolean canPickup = canReplaceCurrentItem && this.canHoldItem(stack); + if (entity != null) { + canPickup = !org.bukkit.craftbukkit.event.CraftEventFactory.callEntityPickupItemEvent(this, entity, 0, !canPickup).isCancelled(); + } + if (canPickup) { + // CraftBukkit end double d = this.dropChances.byEquipment(equipmentSlotForItem); if (!itemBySlot.isEmpty() && Math.max(this.random.nextFloat() - 0.1F, 0.0F) < d) { + this.forceDrops = true; // CraftBukkit this.spawnAtLocation(level, itemBySlot); + this.forceDrops = false; // CraftBukkit } ItemStack itemStack = equipmentSlotForItem.limit(stack); @@ -608,22 +_,29 @@ @Override public void checkDespawn() { if (this.level().getDifficulty() == Difficulty.PEACEFUL && this.shouldDespawnInPeaceful()) { - this.discard(); + this.discard(EntityRemoveEvent.Cause.DESPAWN); // CraftBukkit - add Bukkit remove cause } else if (!this.isPersistenceRequired() && !this.requiresCustomPersistence()) { - Entity nearestPlayer = this.level().getNearestPlayer(this, -1.0); + Entity nearestPlayer = this.level().findNearbyPlayer(this, -1.0, EntitySelector.PLAYER_AFFECTS_SPAWNING); // Paper - Affects Spawning API if (nearestPlayer != null) { - double d = nearestPlayer.distanceToSqr(this); - int despawnDistance = this.getType().getCategory().getDespawnDistance(); - int i = despawnDistance * despawnDistance; - if (d > i && this.removeWhenFarAway(d)) { - this.discard(); - } + // Paper start - Configurable despawn distances + final io.papermc.paper.configuration.WorldConfiguration.Entities.Spawning.DespawnRangePair despawnRangePair = this.level().paperConfig().entities.spawning.despawnRanges.get(this.getType().getCategory()); + final io.papermc.paper.configuration.type.DespawnRange.Shape shape = this.level().paperConfig().entities.spawning.despawnRangeShape; + final double dy = Math.abs(nearestPlayer.getY() - this.getY()); + final double dySqr = Mth.square(dy); + final double dxSqr = Mth.square(nearestPlayer.getX() - this.getX()); + final double dzSqr = Mth.square(nearestPlayer.getZ() - this.getZ()); + final double distanceSquared = dxSqr + dzSqr + dySqr; + // Despawn if hard/soft limit is exceeded + if (despawnRangePair.hard().shouldDespawn(shape, dxSqr, dySqr, dzSqr, dy) && this.removeWhenFarAway(distanceSquared)) { + this.discard(EntityRemoveEvent.Cause.DESPAWN); // CraftBukkit - add Bukkit remove cause + } - int noDespawnDistance = this.getType().getCategory().getNoDespawnDistance(); - int i1 = noDespawnDistance * noDespawnDistance; - if (this.noActionTime > 600 && this.random.nextInt(800) == 0 && d > i1 && this.removeWhenFarAway(d)) { - this.discard(); - } else if (d < i1) { + if (despawnRangePair.soft().shouldDespawn(shape, dxSqr, dySqr, dzSqr, dy)) { + if (this.noActionTime > 600 && this.random.nextInt(800) == 0 && this.removeWhenFarAway(distanceSquared)) { + this.discard(EntityRemoveEvent.Cause.DESPAWN); // CraftBukkit - add Bukkit remove cause + } + } else { + // Paper end - Configurable despawn distances this.noActionTime = 0; } } @@ -635,6 +_,15 @@ @Override protected final void serverAiStep() { this.noActionTime++; + // Paper start - Allow nerfed mobs to jump and float + if (!this.aware) { + if (this.goalFloat != null) { + if (this.goalFloat.canUse()) this.goalFloat.tick(); + this.getJumpControl().tick(); + } + return; + } + // Paper end - Allow nerfed mobs to jump and float ProfilerFiller profilerFiller = Profiler.get(); profilerFiller.push("sensing"); this.sensing.tick(); @@ -814,14 +_,69 @@ public boolean stillValid(Player player) { return player.getVehicle() == Mob.this || player.canInteractWithEntity(Mob.this, 4.0); } + + // Paper start + private final List viewers = new java.util.ArrayList<>(); + private int maxStackSize = MAX_STACK; + + @Override + public int getMaxStackSize() { + return this.maxStackSize; + } + + @Override + public List getContents() { + return java.util.Arrays.asList(this.getTheItem()); + } + + @Override + public void onOpen(final org.bukkit.craftbukkit.entity.CraftHumanEntity player) { + this.viewers.add(player); + } + + @Override + public void onClose(final org.bukkit.craftbukkit.entity.CraftHumanEntity player) { + this.viewers.remove(player); + } + + @Override + public List getViewers() { + return this.viewers; + } + + @Override + public @Nullable org.bukkit.inventory.InventoryHolder getOwner() { + if (Mob.this.getBukkitEntity() instanceof org.bukkit.inventory.InventoryHolder inventoryHolder) { + return inventoryHolder; + } + return null; + } + + @Override + public void setMaxStackSize(final int size) { + this.maxStackSize = size; + } + + @Override + public org.bukkit.Location getLocation() { + return Mob.this.getBukkitEntity().getLocation(); + } + // Paper end }; } + // Paper start + protected boolean shouldSkipLoot(EquipmentSlot slot) { // method to avoid to fallback into the global mob loot logic (e.g. the fox) + return false; + } + // Paper end + @Override protected void dropCustomDeathLoot(ServerLevel level, DamageSource damageSource, boolean recentlyHit) { super.dropCustomDeathLoot(level, damageSource, recentlyHit); for (EquipmentSlot equipmentSlot : EquipmentSlot.VALUES) { + if (this.shouldSkipLoot(equipmentSlot)) continue; // Paper ItemStack itemBySlot = this.getItemBySlot(equipmentSlot); float f = this.dropChances.byEquipment(equipmentSlot); if (f != 0.0F) { @@ -841,7 +_,13 @@ } this.spawnAtLocation(level, itemBySlot); + if (this.clearEquipmentSlots) { // Paper this.setItemSlot(equipmentSlot, ItemStack.EMPTY); + // Paper start + } else { + this.clearedEquipmentSlots.add(equipmentSlot); + } + // Paper end } } } @@ -865,7 +_,9 @@ set.add(equipmentSlot); } else if (this.dropChances.isPreserved(equipmentSlot)) { this.setItemSlot(equipmentSlot, ItemStack.EMPTY); + this.forceDrops = true; // Paper - Add missing forceDrop toggles this.spawnAtLocation(level, itemBySlot); + this.forceDrops = false; // Paper - Add missing forceDrop toggles } } } @@ -1147,6 +_,21 @@ public T convertTo( EntityType entityType, ConversionParams conversionParams, EntitySpawnReason spawnReason, ConversionParams.AfterConversion afterConversion ) { + // Paper start - entity zap event - allow cancellation of conversion post creation + return this.convertTo(entityType, conversionParams, spawnReason, afterConversion, EntityTransformEvent.TransformReason.UNKNOWN, CreatureSpawnEvent.SpawnReason.DEFAULT); + } + + @Nullable + public T convertTo( + EntityType entityType, ConversionParams conversionParams, EntitySpawnReason spawnReason, ConversionParams.AfterConversion afterConversion, @Nullable EntityTransformEvent.TransformReason transformReason, @Nullable CreatureSpawnEvent.SpawnReason creatureSpawnReason + ) { + return this.convertTo(entityType, conversionParams, spawnReason, e -> { afterConversion.finalizeConversion(e); return true; }, transformReason, creatureSpawnReason); + } + @Nullable + public T convertTo( + EntityType entityType, ConversionParams conversionParams, EntitySpawnReason spawnReason, ConversionParams.CancellingAfterConversion afterConversion, @Nullable EntityTransformEvent.TransformReason transformReason, @Nullable CreatureSpawnEvent.SpawnReason creatureSpawnReason + ) { + // Paper end - entity zap event - allow cancellation of conversion post creation if (this.isRemoved()) { return null; } else { @@ -1155,13 +_,23 @@ return null; } else { conversionParams.type().convert(this, mob, conversionParams); - afterConversion.finalizeConversion(mob); + if (!afterConversion.finalizeConversionOrCancel(mob)) return null; // Paper - entity zap event - return null if conversion was cancelled + // CraftBukkit start + if (transformReason == null) { + // Special handling for slime split and pig lightning + return mob; + } + + if (org.bukkit.craftbukkit.event.CraftEventFactory.callEntityTransformEvent(this, mob, transformReason).isCancelled()) { + return null; + } + // CraftBukkit end if (this.level() instanceof ServerLevel serverLevel) { - serverLevel.addFreshEntity(mob); + serverLevel.addFreshEntity(mob, creatureSpawnReason); // CraftBukkit } if (conversionParams.type().shouldDiscardAfterConversion()) { - this.discard(); + this.discard(EntityRemoveEvent.Cause.TRANSFORMATION); // CraftBukkit - add Bukkit remove cause } return mob; @@ -1171,7 +_,18 @@ @Nullable public T convertTo(EntityType entityType, ConversionParams coversionParams, ConversionParams.AfterConversion afterConversion) { - return this.convertTo(entityType, coversionParams, EntitySpawnReason.CONVERSION, afterConversion); + // Paper start - entity zap event - allow cancellation of conversion post creation + return this.convertTo(entityType, coversionParams, afterConversion, EntityTransformEvent.TransformReason.UNKNOWN, CreatureSpawnEvent.SpawnReason.DEFAULT); + } + + @Nullable + public T convertTo(EntityType entityType, ConversionParams coversionParams, ConversionParams.AfterConversion afterConversion, @Nullable EntityTransformEvent.TransformReason transformReason, @Nullable CreatureSpawnEvent.SpawnReason creatureSpawnReason) { + return this.convertTo(entityType, coversionParams, e -> { afterConversion.finalizeConversion(e); return true; }, transformReason, creatureSpawnReason); + } + @Nullable + public T convertTo(EntityType entityType, ConversionParams coversionParams, ConversionParams.CancellingAfterConversion afterConversion, @Nullable EntityTransformEvent.TransformReason transformReason, @Nullable CreatureSpawnEvent.SpawnReason creatureSpawnReason) { + return this.convertTo(entityType, coversionParams, EntitySpawnReason.CONVERSION, afterConversion, transformReason, creatureSpawnReason); + // Paper end - entity zap event - allow cancellation of conversion post creation } @Nullable @@ -1213,7 +_,17 @@ public boolean startRiding(Entity entity, boolean force) { boolean flag = super.startRiding(entity, force); if (flag && this.isLeashed()) { - this.dropLeash(); + // Paper start - Expand EntityUnleashEvent + EntityUnleashEvent event = new EntityUnleashEvent(this.getBukkitEntity(), EntityUnleashEvent.UnleashReason.UNKNOWN, true); + if (!event.callEvent()) { + return flag; + } + if (event.isDropLeash()) { + this.dropLeash(); + } else { + this.removeLeash(); + } + // Paper end - Expand EntityUnleashEvent } return flag; @@ -1296,7 +_,7 @@ float knockback = this.getKnockback(source, damageSource); if (knockback > 0.0F && source instanceof LivingEntity livingEntity) { livingEntity.knockback( - knockback * 0.5F, Mth.sin(this.getYRot() * (float) (Math.PI / 180.0)), -Mth.cos(this.getYRot() * (float) (Math.PI / 180.0)) + knockback * 0.5F, Mth.sin(this.getYRot() * (float) (Math.PI / 180.0)), -Mth.cos(this.getYRot() * (float) (Math.PI / 180.0)), this, io.papermc.paper.event.entity.EntityKnockbackEvent.Cause.ENTITY_ATTACK // CraftBukkit // Paper - knockback events ); this.setDeltaMovement(this.getDeltaMovement().multiply(0.6, 1.0, 0.6)); }