SPIGOT-7615: PlayerLeashEntityEvent cancelled eats lead

By: DerFrZocker <derrieple@gmail.com>
This commit is contained in:
CraftBukkit/Spigot 2024-04-28 07:27:19 +10:00
parent 44c4305918
commit 0222693862
2 changed files with 77 additions and 66 deletions

View file

@ -1,6 +1,6 @@
--- a/net/minecraft/server/level/EntityPlayer.java
+++ b/net/minecraft/server/level/EntityPlayer.java
@@ -165,6 +165,38 @@
@@ -165,6 +165,39 @@
import net.minecraft.world.scores.criteria.IScoreboardCriteria;
import org.slf4j.Logger;
@ -8,6 +8,7 @@
+import net.minecraft.nbt.NBTBase;
+import net.minecraft.world.damagesource.CombatTracker;
+import net.minecraft.world.food.FoodMetaData;
+import net.minecraft.world.inventory.ContainerPlayer;
+import net.minecraft.world.item.enchantment.EnchantmentManager;
+import net.minecraft.world.level.block.BlockChest;
+import net.minecraft.world.level.dimension.WorldDimension;
@ -39,7 +40,7 @@
public class EntityPlayer extends EntityHuman {
private static final Logger LOGGER = LogUtils.getLogger();
@@ -203,7 +235,7 @@
@@ -203,7 +236,7 @@
private int levitationStartTime;
private boolean disconnected;
private int requestedViewDistance;
@ -48,7 +49,7 @@
@Nullable
private Vec3D startingToFallPosition;
@Nullable
@@ -233,6 +265,21 @@
@@ -233,6 +266,21 @@
private int containerCounter;
public boolean wonGame;
@ -70,7 +71,7 @@
public EntityPlayer(MinecraftServer minecraftserver, WorldServer worldserver, GameProfile gameprofile, ClientInformation clientinformation) {
super(worldserver, worldserver.getSharedSpawnPos(), worldserver.getSharedSpawnAngle(), gameprofile);
this.chatVisibility = EnumChatVisibility.FULL;
@@ -299,12 +346,56 @@
@@ -299,12 +347,65 @@
this.fudgeSpawnLocation(worldserver);
this.updateOptions(clientinformation);
this.object = null;
@ -79,8 +80,17 @@
+ this.displayName = this.getScoreboardName();
+ this.bukkitPickUpLoot = true;
+ this.maxHealthCache = this.getMaxHealth();
}
+ }
+
+ // Use method to resend items in hands in case of client desync, because the item use got cancelled.
+ // For example, when cancelling the leash event
+ public void resendItemInHands() {
+ containerMenu.findSlot(getInventory(), getInventory().selected).ifPresent(s -> {
+ containerSynchronizer.sendSlotChange(containerMenu, s, getMainHandItem());
+ });
+ containerSynchronizer.sendSlotChange(inventoryMenu, ContainerPlayer.SHIELD_SLOT, getOffhandItem());
+ }
+
+ // Yes, this doesn't match Vanilla, but it's the best we can do for now.
+ // If this is an issue, PRs are welcome
+ public final BlockPosition getSpawnPoint(WorldServer worldserver) {
@ -117,9 +127,9 @@
+ }
+
+ return blockposition;
+ }
}
+ // CraftBukkit end
+
private void fudgeSpawnLocation(WorldServer worldserver) {
BlockPosition blockposition = worldserver.getSharedSpawnPos();
@ -128,7 +138,7 @@
int i = Math.max(0, this.server.getSpawnRadius(worldserver));
int j = MathHelper.floor(worldserver.getWorldBorder().getDistanceToBorder((double) blockposition.getX(), (double) blockposition.getZ()));
@@ -359,7 +450,7 @@
@@ -359,7 +460,7 @@
dataresult = WardenSpawnTracker.CODEC.parse(new Dynamic(DynamicOpsNBT.INSTANCE, nbttagcompound.get("warden_spawn_tracker")));
logger = EntityPlayer.LOGGER;
Objects.requireNonNull(logger);
@ -137,7 +147,7 @@
this.wardenSpawnTracker = wardenspawntracker;
});
}
@@ -374,17 +465,26 @@
@@ -374,17 +475,26 @@
if (nbttagcompound.contains("recipeBook", 10)) {
this.recipeBook.fromNbt(nbttagcompound.getCompound("recipeBook"), this.server.getRecipeManager());
}
@ -165,7 +175,7 @@
Logger logger1 = EntityPlayer.LOGGER;
Objects.requireNonNull(logger1);
@@ -399,7 +499,7 @@
@@ -399,7 +509,7 @@
dataresult = BlockPosition.CODEC.parse(DynamicOpsNBT.INSTANCE, nbtbase);
logger = EntityPlayer.LOGGER;
Objects.requireNonNull(logger);
@ -174,7 +184,7 @@
this.raidOmenPosition = blockposition;
});
}
@@ -409,7 +509,7 @@
@@ -409,7 +519,7 @@
@Override
public void addAdditionalSaveData(NBTTagCompound nbttagcompound) {
super.addAdditionalSaveData(nbttagcompound);
@ -183,7 +193,7 @@
Logger logger = EntityPlayer.LOGGER;
Objects.requireNonNull(logger);
@@ -430,7 +530,20 @@
@@ -430,7 +540,20 @@
Entity entity = this.getRootVehicle();
Entity entity1 = this.getVehicle();
@ -205,7 +215,7 @@
NBTTagCompound nbttagcompound2 = new NBTTagCompound();
NBTTagCompound nbttagcompound3 = new NBTTagCompound();
@@ -455,6 +568,7 @@
@@ -455,6 +578,7 @@
nbttagcompound.put("SpawnDimension", nbtbase);
});
}
@ -213,7 +223,7 @@
nbttagcompound.putBoolean("spawn_extra_particles_on_fall", this.spawnExtraParticlesOnFall);
if (this.raidOmenPosition != null) {
@@ -468,6 +582,29 @@
@@ -468,6 +592,29 @@
}
@ -243,7 +253,7 @@
public void setExperiencePoints(int i) {
float f = (float) this.getXpNeededForNextLevel();
float f1 = (f - 1.0F) / f;
@@ -526,6 +663,11 @@
@@ -526,6 +673,11 @@
@Override
public void tick() {
@ -255,7 +265,7 @@
this.gameMode.tick();
this.wardenSpawnTracker.tick();
--this.spawnInvulnerableTime;
@@ -606,7 +748,7 @@
@@ -606,7 +758,7 @@
}
if (this.getHealth() != this.lastSentHealth || this.lastSentFood != this.foodData.getFoodLevel() || this.foodData.getSaturationLevel() == 0.0F != this.lastFoodSaturationZero) {
@ -264,7 +274,7 @@
this.lastSentHealth = this.getHealth();
this.lastSentFood = this.foodData.getFoodLevel();
this.lastFoodSaturationZero = this.foodData.getSaturationLevel() == 0.0F;
@@ -637,6 +779,12 @@
@@ -637,6 +789,12 @@
this.updateScoreForCriteria(IScoreboardCriteria.EXPERIENCE, MathHelper.ceil((float) this.lastRecordedExperience));
}
@ -277,7 +287,7 @@
if (this.experienceLevel != this.lastRecordedLevel) {
this.lastRecordedLevel = this.experienceLevel;
this.updateScoreForCriteria(IScoreboardCriteria.LEVEL, MathHelper.ceil((float) this.lastRecordedLevel));
@@ -651,6 +799,20 @@
@@ -651,6 +809,20 @@
CriterionTriggers.LOCATION.trigger(this);
}
@ -298,7 +308,7 @@
} catch (Throwable throwable) {
CrashReport crashreport = CrashReport.forThrowable(throwable, "Ticking player");
CrashReportSystemDetails crashreportsystemdetails = crashreport.addCategory("Player being ticked");
@@ -696,7 +858,8 @@
@@ -696,7 +868,8 @@
}
private void updateScoreForCriteria(IScoreboardCriteria iscoreboardcriteria, int i) {
@ -308,7 +318,7 @@
scoreaccess.set(i);
});
}
@@ -705,9 +868,47 @@
@@ -705,9 +878,47 @@
public void die(DamageSource damagesource) {
this.gameEvent(GameEvent.ENTITY_DIE);
boolean flag = this.level().getGameRules().getBoolean(GameRules.RULE_SHOWDEATHMESSAGES);
@ -358,7 +368,7 @@
this.connection.send(new ClientboundPlayerCombatKillPacket(this.getId(), ichatbasecomponent), PacketSendListener.exceptionallySend(() -> {
boolean flag1 = true;
@@ -738,12 +939,18 @@
@@ -738,12 +949,18 @@
if (this.level().getGameRules().getBoolean(GameRules.RULE_FORGIVE_DEAD_PLAYERS)) {
this.tellNeutralMobsThatIDied();
}
@ -381,7 +391,7 @@
EntityLiving entityliving = this.getKillCredit();
if (entityliving != null) {
@@ -778,10 +985,12 @@
@@ -778,10 +995,12 @@
if (entity != this) {
super.awardKillScore(entity, i, damagesource);
this.increaseScore(i);
@ -396,7 +406,7 @@
} else {
this.awardStat(StatisticList.MOB_KILLS);
}
@@ -799,7 +1008,8 @@
@@ -799,7 +1018,8 @@
int i = scoreboardteam.getColor().getId();
if (i >= 0 && i < aiscoreboardcriteria.length) {
@ -406,7 +416,7 @@
}
}
@@ -849,18 +1059,20 @@
@@ -849,18 +1069,20 @@
}
private boolean isPvpAllowed() {
@ -430,7 +440,7 @@
} else {
return shapedetectorshape;
}
@@ -869,11 +1081,20 @@
@@ -869,11 +1091,20 @@
@Nullable
@Override
public Entity changeDimension(WorldServer worldserver) {
@ -454,7 +464,7 @@
this.unRide();
this.serverLevel().removePlayerImmediately(this, Entity.RemovalReason.CHANGED_DIMENSION);
if (!this.wonGame) {
@@ -884,6 +1105,8 @@
@@ -884,6 +1115,8 @@
return this;
} else {
@ -463,7 +473,7 @@
WorldData worlddata = worldserver.getLevelData();
this.connection.send(new PacketPlayOutRespawn(this.createCommonSpawnInfo(worldserver), (byte) 3));
@@ -893,20 +1116,50 @@
@@ -893,20 +1126,50 @@
playerlist.sendPlayerPermissionLevel(this);
worldserver1.removePlayerImmediately(this, Entity.RemovalReason.CHANGED_DIMENSION);
this.unsetRemoved();
@ -519,7 +529,7 @@
this.connection.resetPosition();
worldserver.addDuringPortalTeleport(this);
worldserver1.getProfiler().pop();
@@ -926,39 +1179,66 @@
@@ -926,39 +1189,66 @@
this.lastSentExp = -1;
this.lastSentHealth = -1.0F;
this.lastSentFood = -1;
@ -592,7 +602,7 @@
}
return optional1;
@@ -968,13 +1248,21 @@
@@ -968,13 +1258,21 @@
public void triggerDimensionChangeTriggers(WorldServer worldserver) {
ResourceKey<World> resourcekey = worldserver.dimension();
ResourceKey<World> resourcekey1 = this.level().dimension();
@ -617,7 +627,7 @@
this.enteredNetherPosition = null;
}
@@ -991,19 +1279,17 @@
@@ -991,19 +1289,17 @@
this.containerMenu.broadcastChanges();
}
@ -641,7 +651,7 @@
if (this.level().isDay()) {
return Either.left(EntityHuman.EnumBedResult.NOT_POSSIBLE_NOW);
} else {
@@ -1020,7 +1306,36 @@
@@ -1020,7 +1316,36 @@
}
}
@ -679,7 +689,7 @@
this.awardStat(StatisticList.SLEEP_IN_BED);
CriterionTriggers.SLEPT_IN_BED.trigger(this);
});
@@ -1033,9 +1348,8 @@
@@ -1033,9 +1358,8 @@
return either;
}
}
@ -690,7 +700,7 @@
}
@Override
@@ -1062,13 +1376,31 @@
@@ -1062,13 +1386,31 @@
@Override
public void stopSleepInBed(boolean flag, boolean flag1) {
@ -723,7 +733,7 @@
}
}
@@ -1135,8 +1467,9 @@
@@ -1135,8 +1477,9 @@
this.connection.send(new PacketPlayOutOpenSignEditor(tileentitysign.getBlockPos(), flag));
}
@ -734,7 +744,7 @@
}
@Override
@@ -1144,13 +1477,35 @@
@@ -1144,13 +1487,35 @@
if (itileinventory == null) {
return OptionalInt.empty();
} else {
@ -770,7 +780,7 @@
if (container == null) {
if (this.isSpectator()) {
this.displayClientMessage(IChatBaseComponent.translatable("container.spectatorCantOpen").withStyle(EnumChatFormat.RED), true);
@@ -1158,9 +1513,11 @@
@@ -1158,9 +1523,11 @@
return OptionalInt.empty();
} else {
@ -784,7 +794,7 @@
return OptionalInt.of(this.containerCounter);
}
}
@@ -1173,13 +1530,24 @@
@@ -1173,13 +1540,24 @@
@Override
public void openHorseInventory(EntityHorseAbstract entityhorseabstract, IInventory iinventory) {
@ -811,7 +821,7 @@
this.initMenu(this.containerMenu);
}
@@ -1202,6 +1570,7 @@
@@ -1202,6 +1580,7 @@
@Override
public void closeContainer() {
@ -819,7 +829,7 @@
this.connection.send(new PacketPlayOutCloseWindow(this.containerMenu.containerId));
this.doCloseContainer();
}
@@ -1224,6 +1593,16 @@
@@ -1224,6 +1603,16 @@
}
this.jumping = flag;
@ -836,7 +846,7 @@
this.setShiftKeyDown(flag1);
}
@@ -1257,19 +1636,19 @@
@@ -1257,19 +1646,19 @@
i = Math.round((float) Math.sqrt(d0 * d0 + d1 * d1 + d2 * d2) * 100.0F);
if (i > 0) {
this.awardStat(StatisticList.SWIM_ONE_CM, i);
@ -859,7 +869,7 @@
}
} else if (this.onClimbable()) {
if (d1 > 0.0D) {
@@ -1280,13 +1659,13 @@
@@ -1280,13 +1669,13 @@
if (i > 0) {
if (this.isSprinting()) {
this.awardStat(StatisticList.SPRINT_ONE_CM, i);
@ -876,7 +886,7 @@
}
}
} else if (this.isFallFlying()) {
@@ -1329,7 +1708,7 @@
@@ -1329,7 +1718,7 @@
@Override
public void awardStat(Statistic<?> statistic, int i) {
this.stats.increment(this, statistic, i);
@ -885,7 +895,7 @@
scoreaccess.add(i);
});
}
@@ -1337,7 +1716,7 @@
@@ -1337,7 +1726,7 @@
@Override
public void resetStat(Statistic<?> statistic) {
this.stats.setValue(this, statistic, 0);
@ -894,7 +904,7 @@
}
@Override
@@ -1385,6 +1764,7 @@
@@ -1385,6 +1774,7 @@
public void resetSentInfo() {
this.lastSentHealth = -1.0E8F;
@ -902,7 +912,7 @@
}
@Override
@@ -1442,7 +1822,7 @@
@@ -1442,7 +1832,7 @@
this.lastSentExp = -1;
this.lastSentHealth = -1.0F;
this.lastSentFood = -1;
@ -911,7 +921,7 @@
this.seenCredits = entityplayer.seenCredits;
this.enteredNetherPosition = entityplayer.enteredNetherPosition;
this.chunkTrackingView = entityplayer.chunkTrackingView;
@@ -1493,6 +1873,12 @@
@@ -1493,6 +1883,12 @@
@Override
public boolean teleportTo(WorldServer worldserver, double d0, double d1, double d2, Set<RelativeMovement> set, float f, float f1) {
@ -924,7 +934,7 @@
ChunkCoordIntPair chunkcoordintpair = new ChunkCoordIntPair(BlockPosition.containing(d0, d1, d2));
worldserver.getChunkSource().addRegionTicket(TicketType.POST_TELEPORT, chunkcoordintpair, 1, this.getId());
@@ -1502,9 +1888,9 @@
@@ -1502,9 +1898,9 @@
}
if (worldserver == this.level()) {
@ -936,7 +946,7 @@
}
this.setYHeadRot(f);
@@ -1606,6 +1992,16 @@
@@ -1606,6 +2002,16 @@
}
public void updateOptions(ClientInformation clientinformation) {
@ -953,7 +963,7 @@
this.language = clientinformation.language();
this.requestedViewDistance = clientinformation.viewDistance();
this.chatVisibility = clientinformation.chatVisibility();
@@ -1689,7 +2085,7 @@
@@ -1689,7 +2095,7 @@
if (world instanceof WorldServer) {
WorldServer worldserver = (WorldServer) world;
@ -962,7 +972,7 @@
}
if (entity != null) {
@@ -1726,7 +2122,7 @@
@@ -1726,7 +2132,7 @@
@Nullable
public IChatBaseComponent getTabListDisplayName() {
@ -971,7 +981,7 @@
}
@Override
@@ -1747,9 +2143,16 @@
@@ -1747,9 +2153,16 @@
return this.advancements;
}
@ -988,7 +998,7 @@
if (worldserver == this.level()) {
this.connection.teleport(d0, d1, d2, f, f1);
} else {
@@ -1769,6 +2172,9 @@
@@ -1769,6 +2182,9 @@
this.server.getPlayerList().sendLevelInfo(this, worldserver);
this.server.getPlayerList().sendAllPlayerInfo(this);
}
@ -998,7 +1008,7 @@
}
@@ -1790,6 +2196,32 @@
@@ -1790,6 +2206,32 @@
}
public void setRespawnPosition(ResourceKey<World> resourcekey, @Nullable BlockPosition blockposition, float f, boolean flag, boolean flag1) {
@ -1031,7 +1041,7 @@
if (blockposition != null) {
boolean flag2 = blockposition.equals(this.respawnPosition) && resourcekey.equals(this.respawnDimension);
@@ -2011,4 +2443,147 @@
@@ -2011,4 +2453,147 @@
public BlockPosition getRaidOmenPosition() {
return this.raidOmenPosition;
}

View file

@ -259,12 +259,13 @@
this.dropLeash(true, !entityhuman.hasInfiniteMaterials());
this.gameEvent(GameEvent.ENTITY_INTERACT, entityhuman);
return EnumInteractionResult.sidedSuccess(this.level().isClientSide);
@@ -1336,6 +1434,12 @@
@@ -1336,6 +1434,13 @@
ItemStack itemstack = entityhuman.getItemInHand(enumhand);
if (itemstack.is(Items.LEAD) && this.canBeLeashed(entityhuman)) {
+ // CraftBukkit start - fire PlayerLeashEntityEvent
+ if (CraftEventFactory.callPlayerLeashEntityEvent(this, entityhuman, entityhuman, enumhand).isCancelled()) {
+ ((EntityPlayer) entityhuman).resendItemInHands(); // SPIGOT-7615: Resend to fix client desync with used item
+ ((EntityPlayer) entityhuman).connection.send(new PacketPlayOutAttachEntity(this, this.getLeashHolder()));
+ return EnumInteractionResult.PASS;
+ }
@ -272,7 +273,7 @@
this.setLeashedTo(entityhuman, true);
itemstack.shrink(1);
return EnumInteractionResult.sidedSuccess(this.level().isClientSide);
@@ -1351,7 +1455,7 @@
@@ -1351,7 +1456,7 @@
if (itemstack.getItem() instanceof ItemMonsterEgg) {
if (this.level() instanceof WorldServer) {
ItemMonsterEgg itemmonsteregg = (ItemMonsterEgg) itemstack.getItem();
@ -281,7 +282,7 @@
optional.ifPresent((entityinsentient) -> {
this.onOffspringSpawnedFromEgg(entityhuman, entityinsentient);
@@ -1401,12 +1505,19 @@
@@ -1401,12 +1506,19 @@
return this.restrictRadius != -1.0F;
}
@ -302,7 +303,7 @@
if (t0 == null) {
return null;
@@ -1440,7 +1551,12 @@
@@ -1440,7 +1552,12 @@
}
}
@ -316,7 +317,7 @@
if (this.isPassenger()) {
Entity entity = this.getVehicle();
@@ -1448,7 +1564,7 @@
@@ -1448,7 +1565,7 @@
t0.startRiding(entity, true);
}
@ -325,7 +326,7 @@
return t0;
}
}
@@ -1461,7 +1577,8 @@
@@ -1461,7 +1578,8 @@
if (this.leashHolder != null) {
if (!this.isAlive() || !this.leashHolder.isAlive()) {
@ -335,7 +336,7 @@
}
}
@@ -1473,7 +1590,9 @@
@@ -1473,7 +1591,9 @@
this.delayedLeashInfo = null;
this.clearRestriction();
if (!this.level().isClientSide && flag1) {
@ -345,7 +346,7 @@
}
if (!this.level().isClientSide && flag && this.level() instanceof WorldServer) {
@@ -1527,6 +1646,7 @@
@@ -1527,6 +1647,7 @@
boolean flag1 = super.startRiding(entity, flag);
if (flag1 && this.isLeashed()) {
@ -353,7 +354,7 @@
this.dropLeash(true, true);
}
@@ -1555,7 +1675,9 @@
@@ -1555,7 +1676,9 @@
}
if (this.tickCount > 100) {
@ -363,7 +364,7 @@
this.delayedLeashInfo = null;
}
}
@@ -1638,14 +1760,21 @@
@@ -1638,14 +1761,21 @@
int i = EnchantmentManager.getFireAspect(this);
if (i > 0) {
@ -387,7 +388,7 @@
this.setDeltaMovement(this.getDeltaMovement().multiply(0.6D, 1.0D, 0.6D));
}
@@ -1695,6 +1824,7 @@
@@ -1695,6 +1825,7 @@
@Override
protected void removeAfterChangingDimensions() {
super.removeAfterChangingDimensions();