--- a/net/minecraft/server/EntityHuman.java +++ b/net/minecraft/server/EntityHuman.java @@ -16,10 +16,24 @@ import java.util.function.Predicate; import javax.annotation.Nullable; +// CraftBukkit start +import org.bukkit.craftbukkit.entity.CraftHumanEntity; +import org.bukkit.craftbukkit.event.CraftEventFactory; +import org.bukkit.craftbukkit.util.CraftVector; +import org.bukkit.entity.Item; +import org.bukkit.entity.Player; +import org.bukkit.event.entity.CreatureSpawnEvent; +import org.bukkit.event.entity.EntityCombustByEntityEvent; +import org.bukkit.event.entity.EntityExhaustionEvent; +import org.bukkit.event.player.PlayerDropItemEvent; +import org.bukkit.event.player.PlayerVelocityEvent; +// CraftBukkit end + public abstract class EntityHuman extends EntityLiving { public static final EntitySize bh = EntitySize.b(0.6F, 1.8F); - private static final Map b = ImmutableMap.builder().put(EntityPose.STANDING, EntityHuman.bh).put(EntityPose.SLEEPING, EntityHuman.ah).put(EntityPose.FALL_FLYING, EntitySize.b(0.6F, 0.6F)).put(EntityPose.SWIMMING, EntitySize.b(0.6F, 0.6F)).put(EntityPose.SPIN_ATTACK, EntitySize.b(0.6F, 0.6F)).put(EntityPose.CROUCHING, EntitySize.b(0.6F, 1.5F)).put(EntityPose.DYING, EntitySize.c(0.2F, 0.2F)).build(); + // CraftBukkit - decompile error + private static final Map b = ImmutableMap.builder().put(EntityPose.STANDING, EntityHuman.bh).put(EntityPose.SLEEPING, EntityHuman.ah).put(EntityPose.FALL_FLYING, EntitySize.b(0.6F, 0.6F)).put(EntityPose.SWIMMING, EntitySize.b(0.6F, 0.6F)).put(EntityPose.SPIN_ATTACK, EntitySize.b(0.6F, 0.6F)).put(EntityPose.CROUCHING, EntitySize.b(0.6F, 1.5F)).put(EntityPose.DYING, EntitySize.c(0.2F, 0.2F)).build(); private static final DataWatcherObject c = DataWatcher.a(EntityHuman.class, DataWatcherRegistry.c); private static final DataWatcherObject d = DataWatcher.a(EntityHuman.class, DataWatcherRegistry.b); protected static final DataWatcherObject bi = DataWatcher.a(EntityHuman.class, DataWatcherRegistry.a); @@ -28,10 +42,10 @@ protected static final DataWatcherObject bl = DataWatcher.a(EntityHuman.class, DataWatcherRegistry.p); private long e; public final PlayerInventory inventory = new PlayerInventory(this); - protected InventoryEnderChest enderChest = new InventoryEnderChest(); + protected InventoryEnderChest enderChest = new InventoryEnderChest(this); // CraftBukkit - add "this" to constructor public final ContainerPlayer defaultContainer; public Container activeContainer; - protected FoodMetaData foodData = new FoodMetaData(); + protected FoodMetaData foodData = new FoodMetaData(this); // CraftBukkit - add "this" to constructor protected int br; public float bs; public float bt; @@ -57,6 +71,16 @@ @Nullable public EntityFishingHook hookedFish; + // CraftBukkit start + public boolean fauxSleeping; + public int oldLevel = -1; + + @Override + public CraftHumanEntity getBukkitEntity() { + return (CraftHumanEntity) super.getBukkitEntity(); + } + // CraftBukkit end + public EntityHuman(World world, BlockPosition blockposition, float f, GameProfile gameprofile) { super(EntityTypes.PLAYER, world); this.bL = ItemStack.b; @@ -194,7 +218,7 @@ ItemStack itemstack = this.getEquipment(EnumItemSlot.HEAD); if (itemstack.getItem() == Items.TURTLE_HELMET && !this.a((Tag) TagsFluid.WATER)) { - this.addEffect(new MobEffect(MobEffects.WATER_BREATHING, 200, 0, false, false, true)); + this.addEffect(new MobEffect(MobEffects.WATER_BREATHING, 200, 0, false, false, true), org.bukkit.event.entity.EntityPotionEffectEvent.Cause.TURTLE_HELMET); // CraftBukkit } } @@ -359,7 +383,8 @@ if (this.world.getDifficulty() == EnumDifficulty.PEACEFUL && this.world.getGameRules().getBoolean(GameRules.NATURAL_REGENERATION)) { if (this.getHealth() < this.getMaxHealth() && this.ticksLived % 20 == 0) { - this.heal(1.0F); + // CraftBukkit - added regain reason of "REGEN" for filtering purposes. + this.heal(1.0F, org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason.REGEN); } if (this.foodData.c() && this.ticksLived % 10 == 0) { @@ -544,6 +569,30 @@ entityitem.setMot((double) (-f3 * f2 * 0.3F) + Math.cos((double) f5) * (double) f6, (double) (-f1 * 0.3F + 0.1F + (this.random.nextFloat() - this.random.nextFloat()) * 0.1F), (double) (f4 * f2 * 0.3F) + Math.sin((double) f5) * (double) f6); } + // CraftBukkit start - fire PlayerDropItemEvent + Player player = (Player) this.getBukkitEntity(); + Item drop = (Item) entityitem.getBukkitEntity(); + + PlayerDropItemEvent event = new PlayerDropItemEvent(player, drop); + this.world.getServer().getPluginManager().callEvent(event); + + if (event.isCancelled()) { + org.bukkit.inventory.ItemStack cur = player.getInventory().getItemInHand(); + if (flag1 && (cur == null || cur.getAmount() == 0)) { + // The complete stack was dropped + player.getInventory().setItemInHand(drop.getItemStack()); + } else if (flag1 && cur.isSimilar(drop.getItemStack()) && cur.getAmount() < cur.getMaxStackSize() && drop.getItemStack().getAmount() == 1) { + // Only one item is dropped + cur.setAmount(cur.getAmount() + 1); + player.getInventory().setItemInHand(cur); + } else { + // Fallback + player.getInventory().addItem(drop.getItemStack()); + } + return null; + } + // CraftBukkit end + return entityitem; } } @@ -670,16 +719,17 @@ if (this.isInvulnerable(damagesource)) { return false; } else if (this.abilities.isInvulnerable && !damagesource.ignoresInvulnerability()) { + this.forceExplosionKnockback = true; // SPIGOT-5258 - Make invulnerable players get knockback from explosions return false; } else { this.ticksFarFromPlayer = 0; if (this.dl()) { return false; } else { - this.releaseShoulderEntities(); + // this.releaseShoulderEntities(); // CraftBukkit - moved down if (damagesource.s()) { if (this.world.getDifficulty() == EnumDifficulty.PEACEFUL) { - f = 0.0F; + return false; // CraftBukkit - f = 0.0f -> return false } if (this.world.getDifficulty() == EnumDifficulty.EASY) { @@ -691,7 +741,13 @@ } } - return f == 0.0F ? false : super.damageEntity(damagesource, f); + // CraftBukkit start - Don't filter out 0 damage + boolean damaged = super.damageEntity(damagesource, f); + if (damaged) { + this.releaseShoulderEntities(); + } + return damaged; + // CraftBukkit end } } } @@ -706,10 +762,29 @@ } public boolean a(EntityHuman entityhuman) { - ScoreboardTeamBase scoreboardteambase = this.getScoreboardTeam(); - ScoreboardTeamBase scoreboardteambase1 = entityhuman.getScoreboardTeam(); + // CraftBukkit start - Change to check OTHER player's scoreboard team according to API + // To summarize this method's logic, it's "Can parameter hurt this" + org.bukkit.scoreboard.Team team; + if (entityhuman instanceof EntityPlayer) { + EntityPlayer thatPlayer = (EntityPlayer) entityhuman; + team = thatPlayer.getBukkitEntity().getScoreboard().getPlayerTeam(thatPlayer.getBukkitEntity()); + if (team == null || team.allowFriendlyFire()) { + return true; + } + } else { + // This should never be called, but is implemented anyway + org.bukkit.OfflinePlayer thisPlayer = entityhuman.world.getServer().getOfflinePlayer(entityhuman.getName()); + team = entityhuman.world.getServer().getScoreboardManager().getMainScoreboard().getPlayerTeam(thisPlayer); + if (team == null || team.allowFriendlyFire()) { + return true; + } + } - return scoreboardteambase == null ? true : (!scoreboardteambase.isAlly(scoreboardteambase1) ? true : scoreboardteambase.allowFriendlyFire()); + if (this instanceof EntityPlayer) { + return !team.hasPlayer(((EntityPlayer) this).getBukkitEntity()); + } + return !team.hasPlayer(this.world.getServer().getOfflinePlayer(this.getName())); + // CraftBukkit end } @Override @@ -746,8 +821,13 @@ } } + // CraftBukkit start @Override - protected void damageEntity0(DamageSource damagesource, float f) { + protected boolean damageEntity0(DamageSource damagesource, float f) { // void -> boolean + if (true) { + return super.damageEntity0(damagesource, f); + } + // CraftBukkit end if (!this.isInvulnerable(damagesource)) { f = this.applyArmorModifier(damagesource, f); f = this.applyMagicModifier(damagesource, f); @@ -762,7 +842,7 @@ } if (f != 0.0F) { - this.applyExhaustion(damagesource.getExhaustionCost()); + this.applyExhaustion(damagesource.getExhaustionCost(), EntityExhaustionEvent.ExhaustionReason.DAMAGED); // CraftBukkit - EntityExhaustionEvent float f3 = this.getHealth(); this.setHealth(this.getHealth() - f); @@ -773,6 +853,7 @@ } } + return false; // CraftBukkit } @Override @@ -932,7 +1013,7 @@ f *= 0.2F + f2 * f2 * 0.8F; f1 *= f2; - this.resetAttackCooldown(); + // this.resetAttackCooldown(); // CraftBukkit - Moved to EntityLiving to reset the cooldown after the damage is dealt if (f > 0.0F || f1 > 0.0F) { boolean flag = f2 > 0.9F; boolean flag1 = false; @@ -971,8 +1052,15 @@ if (entity instanceof EntityLiving) { f3 = ((EntityLiving) entity).getHealth(); if (j > 0 && !entity.isBurning()) { - flag4 = true; - entity.setOnFire(1); + // CraftBukkit start - Call a combust event when somebody hits with a fire enchanted item + EntityCombustByEntityEvent combustEvent = new EntityCombustByEntityEvent(this.getBukkitEntity(), entity.getBukkitEntity(), 1); + org.bukkit.Bukkit.getPluginManager().callEvent(combustEvent); + + if (!combustEvent.isCancelled()) { + flag4 = true; + entity.setOnFire(combustEvent.getDuration(), false); + } + // CraftBukkit end } } @@ -1000,8 +1088,11 @@ EntityLiving entityliving = (EntityLiving) iterator.next(); if (entityliving != this && entityliving != entity && !this.r(entityliving) && (!(entityliving instanceof EntityArmorStand) || !((EntityArmorStand) entityliving).isMarker()) && this.h((Entity) entityliving) < 9.0D) { + // CraftBukkit start - Only apply knockback if the damage hits + if (entityliving.damageEntity(DamageSource.playerAttack(this).sweep(), f4)) { entityliving.a(0.4F, (double) MathHelper.sin(this.yaw * 0.017453292F), (double) (-MathHelper.cos(this.yaw * 0.017453292F))); - entityliving.damageEntity(DamageSource.playerAttack(this), f4); + } + // CraftBukkit end } } @@ -1010,9 +1101,26 @@ } if (entity instanceof EntityPlayer && entity.velocityChanged) { + // CraftBukkit start - Add Velocity Event + boolean cancelled = false; + Player player = (Player) entity.getBukkitEntity(); + org.bukkit.util.Vector velocity = CraftVector.toBukkit(vec3d); + + PlayerVelocityEvent event = new PlayerVelocityEvent(player, velocity.clone()); + world.getServer().getPluginManager().callEvent(event); + + if (event.isCancelled()) { + cancelled = true; + } else if (!velocity.equals(event.getVelocity())) { + player.setVelocity(event.getVelocity()); + } + + if (!cancelled) { ((EntityPlayer) entity).playerConnection.sendPacket(new PacketPlayOutEntityVelocity(entity)); entity.velocityChanged = false; entity.setMot(vec3d); + } + // CraftBukkit end } if (flag2) { @@ -1057,7 +1165,14 @@ this.a(StatisticList.DAMAGE_DEALT, Math.round(f5 * 10.0F)); if (j > 0) { - entity.setOnFire(j * 4); + // CraftBukkit start - Call a combust event when somebody hits with a fire enchanted item + EntityCombustByEntityEvent combustEvent = new EntityCombustByEntityEvent(this.getBukkitEntity(), entity.getBukkitEntity(), j * 4); + org.bukkit.Bukkit.getPluginManager().callEvent(combustEvent); + + if (!combustEvent.isCancelled()) { + entity.setOnFire(combustEvent.getDuration(), false); + } + // CraftBukkit end } if (this.world instanceof WorldServer && f5 > 2.0F) { @@ -1067,12 +1182,17 @@ } } - this.applyExhaustion(0.1F); + this.applyExhaustion(0.1F, EntityExhaustionEvent.ExhaustionReason.ATTACK); // CraftBukkit - EntityExhaustionEvent } else { this.world.playSound((EntityHuman) null, this.locX(), this.locY(), this.locZ(), SoundEffects.ENTITY_PLAYER_ATTACK_NODAMAGE, this.getSoundCategory(), 1.0F, 1.0F); if (flag4) { entity.extinguish(); } + // CraftBukkit start - resync on cancelled event + if (this instanceof EntityPlayer) { + ((EntityPlayer) this).getBukkitEntity().updateInventory(); + } + // CraftBukkit end } } @@ -1133,6 +1253,12 @@ } public Either sleep(BlockPosition blockposition) { + // CraftBukkit start + return this.sleep(blockposition, false); + } + + public Either sleep(BlockPosition blockposition, boolean force) { + // CraftBukkit end this.entitySleep(blockposition); this.sleepTicks = 0; return Either.right(Unit.INSTANCE); @@ -1217,9 +1343,9 @@ super.jump(); this.a(StatisticList.JUMP); if (this.isSprinting()) { - this.applyExhaustion(0.2F); + this.applyExhaustion(0.2F, EntityExhaustionEvent.ExhaustionReason.JUMP_SPRINT); // CraftBukkit - EntityExhaustionEvent } else { - this.applyExhaustion(0.05F); + this.applyExhaustion(0.05F, EntityExhaustionEvent.ExhaustionReason.JUMP); // CraftBukkit - EntityExhaustionEvent } } @@ -1253,7 +1379,11 @@ this.setMot(vec3d2.x, d3 * 0.6D, vec3d2.z); this.aE = f; this.fallDistance = 0.0F; - this.setFlag(7, false); + // CraftBukkit start + if (getFlag(7) && !org.bukkit.craftbukkit.event.CraftEventFactory.callToggleGlideEvent(this, false).isCancelled()) { + this.setFlag(7, false); + } + // CraftBukkit end } else { super.g(vec3d); } @@ -1288,19 +1418,19 @@ i = Math.round(MathHelper.sqrt(d0 * d0 + d1 * d1 + d2 * d2) * 100.0F); if (i > 0) { this.a(StatisticList.SWIM_ONE_CM, i); - this.applyExhaustion(0.01F * (float) i * 0.01F); + this.applyExhaustion(0.01F * (float) i * 0.01F, EntityExhaustionEvent.ExhaustionReason.SWIM); // CraftBukkit - EntityExhaustionEvent } } else if (this.a((Tag) TagsFluid.WATER)) { i = Math.round(MathHelper.sqrt(d0 * d0 + d1 * d1 + d2 * d2) * 100.0F); if (i > 0) { this.a(StatisticList.WALK_UNDER_WATER_ONE_CM, i); - this.applyExhaustion(0.01F * (float) i * 0.01F); + this.applyExhaustion(0.01F * (float) i * 0.01F, EntityExhaustionEvent.ExhaustionReason.WALK_UNDERWATER); // CraftBukkit - EntityExhaustionEvent } } else if (this.isInWater()) { i = Math.round(MathHelper.sqrt(d0 * d0 + d2 * d2) * 100.0F); if (i > 0) { this.a(StatisticList.WALK_ON_WATER_ONE_CM, i); - this.applyExhaustion(0.01F * (float) i * 0.01F); + this.applyExhaustion(0.01F * (float) i * 0.01F, EntityExhaustionEvent.ExhaustionReason.WALK_ON_WATER); // CraftBukkit - EntityExhaustionEvent } } else if (this.isClimbing()) { if (d1 > 0.0D) { @@ -1311,13 +1441,13 @@ if (i > 0) { if (this.isSprinting()) { this.a(StatisticList.SPRINT_ONE_CM, i); - this.applyExhaustion(0.1F * (float) i * 0.01F); + this.applyExhaustion(0.1F * (float) i * 0.01F, EntityExhaustionEvent.ExhaustionReason.SPRINT); // CraftBukkit - EntityExhaustionEvent } else if (this.bz()) { this.a(StatisticList.CROUCH_ONE_CM, i); - this.applyExhaustion(0.0F * (float) i * 0.01F); + this.applyExhaustion(0.0F * (float) i * 0.01F, EntityExhaustionEvent.ExhaustionReason.CROUCH); // CraftBukkit - EntityExhaustionEvent } else { this.a(StatisticList.WALK_ONE_CM, i); - this.applyExhaustion(0.0F * (float) i * 0.01F); + this.applyExhaustion(0.0F * (float) i * 0.01F, EntityExhaustionEvent.ExhaustionReason.WALK); // CraftBukkit - EntityExhaustionEvent } } } else if (this.isGliding()) { @@ -1383,12 +1513,24 @@ } public void startGliding() { - this.setFlag(7, true); + // CraftBukkit start + if (!org.bukkit.craftbukkit.event.CraftEventFactory.callToggleGlideEvent(this, true).isCancelled()) { + this.setFlag(7, true); + } else { + // SPIGOT-5542: must toggle like below + this.setFlag(7, true); + this.setFlag(7, false); + } + // CraftBukkit end } public void stopGliding() { + // CraftBukkit start + if (!org.bukkit.craftbukkit.event.CraftEventFactory.callToggleGlideEvent(this, false).isCancelled()) { this.setFlag(7, true); this.setFlag(7, false); + } + // CraftBukkit end } @Override @@ -1478,10 +1620,21 @@ return this.expLevel >= 30 ? 112 + (this.expLevel - 30) * 9 : (this.expLevel >= 15 ? 37 + (this.expLevel - 15) * 5 : 7 + this.expLevel * 2); } + // CraftBukkit start public void applyExhaustion(float f) { + this.applyExhaustion(f, EntityExhaustionEvent.ExhaustionReason.UNKNOWN); + } + + public void applyExhaustion(float f, EntityExhaustionEvent.ExhaustionReason reason) { + // CraftBukkit end if (!this.abilities.isInvulnerable) { if (!this.world.isClientSide) { - this.foodData.a(f); + // CraftBukkit start + EntityExhaustionEvent event = CraftEventFactory.callPlayerExhaustionEvent(this, reason, f); + if (!event.isCancelled()) { + this.foodData.a(event.getExhaustion()); // PAIL rename addExhaustion + } + // CraftBukkit end } } @@ -1555,14 +1708,21 @@ @Override public void setSlot(EnumItemSlot enumitemslot, ItemStack itemstack) { + // CraftBukkit start + this.setSlot(enumitemslot, itemstack, false); + } + + @Override + public void setSlot(EnumItemSlot enumitemslot, ItemStack itemstack, boolean silent) { + // CraftBukkit end if (enumitemslot == EnumItemSlot.MAINHAND) { - this.playEquipSound(itemstack); + this.playEquipSound(itemstack, silent); // CraftBukkit this.inventory.items.set(this.inventory.itemInHandIndex, itemstack); } else if (enumitemslot == EnumItemSlot.OFFHAND) { - this.playEquipSound(itemstack); + this.playEquipSound(itemstack, silent); // CraftBukkit this.inventory.extraSlots.set(0, itemstack); } else if (enumitemslot.a() == EnumItemSlot.Function.ARMOR) { - this.playEquipSound(itemstack); + this.playEquipSound(itemstack, silent); // CraftBukkit this.inventory.armor.set(enumitemslot.b(), itemstack); } @@ -1603,26 +1763,31 @@ protected void releaseShoulderEntities() { if (this.e + 20L < this.world.getTime()) { - this.spawnEntityFromShoulder(this.getShoulderEntityLeft()); - this.setShoulderEntityLeft(new NBTTagCompound()); - this.spawnEntityFromShoulder(this.getShoulderEntityRight()); - this.setShoulderEntityRight(new NBTTagCompound()); + // CraftBukkit start + if (this.spawnEntityFromShoulder(this.getShoulderEntityLeft())) { + this.setShoulderEntityLeft(new NBTTagCompound()); + } + if (this.spawnEntityFromShoulder(this.getShoulderEntityRight())) { + this.setShoulderEntityRight(new NBTTagCompound()); + } + // CraftBukkit end } } - private void spawnEntityFromShoulder(NBTTagCompound nbttagcompound) { + private boolean spawnEntityFromShoulder(NBTTagCompound nbttagcompound) { // CraftBukkit void->boolean if (!this.world.isClientSide && !nbttagcompound.isEmpty()) { - EntityTypes.a(nbttagcompound, this.world).ifPresent((entity) -> { + return EntityTypes.a(nbttagcompound, this.world).map((entity) -> { // CraftBukkit if (entity instanceof EntityTameableAnimal) { ((EntityTameableAnimal) entity).setOwnerUUID(this.uniqueID); } entity.setPosition(this.locX(), this.locY() + 0.699999988079071D, this.locZ()); - ((WorldServer) this.world).addEntitySerialized(entity); - }); + return ((WorldServer) this.world).addEntitySerialized(entity, CreatureSpawnEvent.SpawnReason.SHOULDER_ENTITY); // CraftBukkit + }).orElse(true); // CraftBukkit } + return true; // CraftBukkit } @Override