Improve death events

This adds the ability to cancel the death events and to modify the sound
an entity makes when dying. (In cases were no sound should it will be
called with shouldPlaySound set to false allowing unsilencing of silent
entities)

It makes handling of entity deaths a lot nicer as you no longer need
to listen on the damage event and calculate if the entity dies yourself
to cancel the death which has the benefit of also receiving the dropped
items and experience which is otherwise only properly possible by using
internal code.

== AT ==
public net.minecraft.world.entity.LivingEntity getDeathSound()Lnet/minecraft/sounds/SoundEvent;
public net.minecraft.world.entity.LivingEntity getSoundVolume()F
This commit is contained in:
Phoenix616
2018-08-21 01:39:35 +01:00
parent 0e656d111b
commit 04afedcccf
8 changed files with 495 additions and 191 deletions

View File

@@ -114,11 +114,15 @@
@Nullable
private Vec3 startingToFallPosition;
@Nullable
@@ -258,7 +293,27 @@
@@ -258,7 +293,31 @@
private final CommandSource commandSource;
private int containerCounter;
public boolean wonGame;
+ private int containerUpdateDelay; // Paper - Configurable container update tick rate
+ // Paper start - cancellable death event
+ public boolean queueHealthUpdatePacket;
+ public net.minecraft.network.protocol.game.ClientboundSetHealthPacket queuedHealthUpdatePacket;
+ // Paper end - cancellable death event
+ // CraftBukkit start
+ public CraftPlayer.TransferCookieConnection transferCookieConnection;
@@ -142,7 +146,7 @@
public ServerPlayer(MinecraftServer server, ServerLevel world, GameProfile profile, ClientInformation clientOptions) {
super(world, world.getSharedSpawnPos(), world.getSharedSpawnAngle(), profile);
this.chatVisibility = ChatVisiblity.FULL;
@@ -266,7 +321,7 @@
@@ -266,7 +325,7 @@
this.canChatColor = true;
this.lastActionTime = Util.getMillis();
this.requestedViewDistance = 2;
@@ -151,7 +155,7 @@
this.lastSectionPos = SectionPos.of(0, 0, 0);
this.chunkTrackingView = ChunkTrackingView.EMPTY;
this.respawnDimension = Level.OVERWORLD;
@@ -340,6 +395,13 @@
@@ -340,6 +399,13 @@
public void sendSystemMessage(Component message) {
ServerPlayer.this.sendSystemMessage(message);
}
@@ -165,7 +169,7 @@
};
this.textFilter = server.createTextFilterForPlayer(this);
this.gameMode = server.createGameModeForPlayer(this);
@@ -352,14 +414,68 @@
@@ -352,14 +418,68 @@
this.moveTo(this.adjustSpawnLocation(world, world.getSharedSpawnPos()).getBottomCenter(), 0.0F, 0.0F);
this.updateOptions(clientOptions);
this.object = null;
@@ -235,7 +239,7 @@
int i = Math.max(0, this.server.getSpawnRadius(world));
int j = Mth.floor(world.getWorldBorder().getDistanceToBorder((double) basePos.getX(), (double) basePos.getZ()));
@@ -395,14 +511,20 @@
@@ -395,14 +515,20 @@
Objects.requireNonNull(basePos);
crashreportsystemdetails.setDetail("Origin", basePos::toString);
@@ -258,7 +262,7 @@
});
throw new ReportedException(crashreport);
}
@@ -440,7 +562,7 @@
@@ -440,7 +566,7 @@
dataresult = WardenSpawnTracker.CODEC.parse(new Dynamic(NbtOps.INSTANCE, nbt.get("warden_spawn_tracker")));
logger = ServerPlayer.LOGGER;
Objects.requireNonNull(logger);
@@ -267,7 +271,7 @@
this.wardenSpawnTracker = wardenspawntracker;
});
}
@@ -457,17 +579,26 @@
@@ -457,17 +583,26 @@
return this.server.getRecipeManager().byKey(resourcekey).isPresent();
});
}
@@ -295,7 +299,7 @@
Logger logger1 = ServerPlayer.LOGGER;
Objects.requireNonNull(logger1);
@@ -482,7 +613,7 @@
@@ -482,7 +617,7 @@
dataresult = BlockPos.CODEC.parse(NbtOps.INSTANCE, nbtbase);
logger = ServerPlayer.LOGGER;
Objects.requireNonNull(logger);
@@ -304,7 +308,7 @@
this.raidOmenPosition = blockposition;
});
}
@@ -492,7 +623,7 @@
@@ -492,7 +627,7 @@
@Override
public void addAdditionalSaveData(CompoundTag nbt) {
super.addAdditionalSaveData(nbt);
@@ -313,7 +317,7 @@
Logger logger = ServerPlayer.LOGGER;
Objects.requireNonNull(logger);
@@ -526,6 +657,7 @@
@@ -526,6 +661,7 @@
nbt.put("SpawnDimension", nbtbase);
});
}
@@ -321,7 +325,7 @@
nbt.putBoolean("spawn_extra_particles_on_fall", this.spawnExtraParticlesOnFall);
if (this.raidOmenPosition != null) {
@@ -544,7 +676,20 @@
@@ -544,7 +680,20 @@
Entity entity = this.getRootVehicle();
Entity entity1 = this.getVehicle();
@@ -343,7 +347,7 @@
CompoundTag nbttagcompound1 = new CompoundTag();
CompoundTag nbttagcompound2 = new CompoundTag();
@@ -598,12 +743,12 @@
@@ -598,12 +747,12 @@
if (!this.isPassenger()) {
ServerPlayer.LOGGER.warn("Couldn't reattach entity to player");
@@ -358,7 +362,7 @@
}
}
}
@@ -625,7 +770,7 @@
@@ -625,7 +774,7 @@
CompoundTag nbttagcompound1 = new CompoundTag();
entityenderpearl.save(nbttagcompound1);
@@ -367,7 +371,7 @@
Logger logger = ServerPlayer.LOGGER;
Objects.requireNonNull(logger);
@@ -651,7 +796,7 @@
@@ -651,7 +800,7 @@
nbttaglist.forEach((nbtbase1) -> {
if (nbtbase1 instanceof CompoundTag nbttagcompound) {
if (nbttagcompound.contains("ender_pearl_dimension")) {
@@ -376,10 +380,12 @@
Logger logger = ServerPlayer.LOGGER;
Objects.requireNonNull(logger);
@@ -686,6 +831,29 @@
}
@@ -684,7 +833,30 @@
}
}
+ }
+
+ // CraftBukkit start - World fallback code, either respawn location or global spawn
+ public void spawnIn(Level world) {
+ this.setLevel(world);
@@ -400,13 +406,12 @@
+ this.setPos(position);
+ }
+ this.gameMode.setLevel((ServerLevel) world);
+ }
}
+ // CraftBukkit end
+
public void setExperiencePoints(int points) {
float f = (float) this.getXpNeededForNextLevel();
float f1 = (f - 1.0F) / f;
@@ -744,6 +912,11 @@
@@ -744,6 +916,11 @@
@Override
public void tick() {
@@ -418,7 +423,7 @@
this.tickClientLoadTimeout();
this.gameMode.tick();
this.wardenSpawnTracker.tick();
@@ -751,9 +924,13 @@
@@ -751,9 +928,13 @@
--this.invulnerableTime;
}
@@ -434,7 +439,7 @@
this.containerMenu = this.inventoryMenu;
}
@@ -820,7 +997,7 @@
@@ -820,7 +1001,7 @@
}
if (this.getHealth() != this.lastSentHealth || this.lastSentFood != this.foodData.getFoodLevel() || this.foodData.getSaturationLevel() == 0.0F != this.lastFoodSaturationZero) {
@@ -443,7 +448,7 @@
this.lastSentHealth = this.getHealth();
this.lastSentFood = this.foodData.getFoodLevel();
this.lastFoodSaturationZero = this.foodData.getSaturationLevel() == 0.0F;
@@ -851,6 +1028,12 @@
@@ -851,6 +1032,12 @@
this.updateScoreForCriteria(ObjectiveCriteria.EXPERIENCE, Mth.ceil((float) this.lastRecordedExperience));
}
@@ -456,7 +461,7 @@
if (this.experienceLevel != this.lastRecordedLevel) {
this.lastRecordedLevel = this.experienceLevel;
this.updateScoreForCriteria(ObjectiveCriteria.LEVEL, Mth.ceil((float) this.lastRecordedLevel));
@@ -865,6 +1048,20 @@
@@ -865,6 +1052,20 @@
CriteriaTriggers.LOCATION.trigger(this);
}
@@ -477,7 +482,7 @@
} catch (Throwable throwable) {
CrashReport crashreport = CrashReport.forThrowable(throwable, "Ticking player");
CrashReportCategory crashreportsystemdetails = crashreport.addCategory("Player being ticked");
@@ -893,7 +1090,7 @@
@@ -893,7 +1094,7 @@
if (this.level().getDifficulty() == Difficulty.PEACEFUL && this.serverLevel().getGameRules().getBoolean(GameRules.RULE_NATURAL_REGENERATION)) {
if (this.tickCount % 20 == 0) {
if (this.getHealth() < this.getMaxHealth()) {
@@ -486,7 +491,7 @@
}
float f = this.foodData.getSaturationLevel();
@@ -946,7 +1143,8 @@
@@ -946,19 +1147,63 @@
}
private void updateScoreForCriteria(ObjectiveCriteria criterion, int score) {
@@ -496,9 +501,11 @@
scoreaccess.set(score);
});
}
@@ -955,10 +1153,43 @@
@Override
public void die(DamageSource damageSource) {
this.gameEvent(GameEvent.ENTITY_DIE);
- this.gameEvent(GameEvent.ENTITY_DIE);
+ // this.gameEvent(GameEvent.ENTITY_DIE); // Paper - move below event cancellation check
boolean flag = this.serverLevel().getGameRules().getBoolean(GameRules.RULE_SHOWDEATHMESSAGES);
+ // CraftBukkit start - fire PlayerDeathEvent
+ if (this.isRemoved()) {
@@ -528,21 +535,31 @@
+ String deathmessage = defaultMessage.getString();
+ this.keepLevel = keepInventory; // SPIGOT-2222: pre-set keepLevel
+ org.bukkit.event.entity.PlayerDeathEvent event = CraftEventFactory.callPlayerDeathEvent(this, damageSource, loot, PaperAdventure.asAdventure(defaultMessage), keepInventory); // Paper - Adventure
+
+ // Paper start - cancellable death event
+ if (event.isCancelled()) {
+ // make compatible with plugins that might have already set the health in an event listener
+ if (this.getHealth() <= 0) {
+ this.setHealth((float) event.getReviveHealth());
+ }
+ return;
+ }
+ this.gameEvent(GameEvent.ENTITY_DIE); // moved from the top of this method
+ // Paper end
+ // SPIGOT-943 - only call if they have an inventory open
+ if (this.containerMenu != this.inventoryMenu) {
+ this.closeContainer(org.bukkit.event.inventory.InventoryCloseEvent.Reason.DEATH); // Paper - Inventory close reason
+ }
+
+ net.kyori.adventure.text.Component deathMessage = event.deathMessage() != null ? event.deathMessage() : net.kyori.adventure.text.Component.empty(); // Paper - Adventure
+
+ if (deathMessage != null && deathMessage != net.kyori.adventure.text.Component.empty() && flag) { // Paper - Adventure // TODO: allow plugins to override?
+ Component ichatbasecomponent = PaperAdventure.asVanilla(deathMessage); // Paper - Adventure
+
this.connection.send(new ClientboundPlayerCombatKillPacket(this.getId(), ichatbasecomponent), PacketSendListener.exceptionallySend(() -> {
boolean flag1 = true;
String s = ichatbasecomponent.getString(256);
@@ -988,12 +1219,18 @@
@@ -988,12 +1233,18 @@
if (this.serverLevel().getGameRules().getBoolean(GameRules.RULE_FORGIVE_DEAD_PLAYERS)) {
this.tellNeutralMobsThatIDied();
}
@@ -565,7 +582,7 @@
LivingEntity entityliving = this.getKillCredit();
if (entityliving != null) {
@@ -1028,10 +1265,12 @@
@@ -1028,10 +1279,12 @@
public void awardKillScore(Entity entityKilled, DamageSource damageSource) {
if (entityKilled != this) {
super.awardKillScore(entityKilled, damageSource);
@@ -581,7 +598,7 @@
} else {
this.awardStat(Stats.MOB_KILLS);
}
@@ -1049,7 +1288,8 @@
@@ -1049,7 +1302,8 @@
int i = scoreboardteam.getColor().getId();
if (i >= 0 && i < criterions.length) {
@@ -591,7 +608,7 @@
}
}
@@ -1062,8 +1302,8 @@
@@ -1062,8 +1316,8 @@
} else {
Entity entity = source.getEntity();
@@ -602,7 +619,7 @@
if (!this.canHarmPlayer(entityhuman)) {
return false;
@@ -1074,8 +1314,8 @@
@@ -1074,8 +1328,8 @@
AbstractArrow entityarrow = (AbstractArrow) entity;
Entity entity1 = entityarrow.getOwner();
@@ -613,7 +630,23 @@
if (!this.canHarmPlayer(entityhuman1)) {
return false;
@@ -1088,33 +1328,63 @@
@@ -1083,38 +1337,78 @@
}
}
- return super.hurtServer(world, source, amount);
+ // Paper start - cancellable death events
+ // return super.hurtServer(world, source, amount);
+ this.queueHealthUpdatePacket = true;
+ boolean damaged = super.hurtServer(world, source, amount);
+ this.queueHealthUpdatePacket = false;
+ if (this.queuedHealthUpdatePacket != null) {
+ this.connection.send(this.queuedHealthUpdatePacket);
+ this.queuedHealthUpdatePacket = null;
+ }
+ return damaged;
+ // Paper end - cancellable death events
}
}
@Override
@@ -684,7 +717,7 @@
}
public static Optional<ServerPlayer.RespawnPosAngle> findRespawnAndUseSpawnBlock(ServerLevel world, BlockPos pos, float spawnAngle, boolean spawnForced, boolean alive) {
@@ -1129,11 +1399,11 @@
@@ -1129,11 +1423,11 @@
}
return optional.map((vec3d) -> {
@@ -698,7 +731,7 @@
});
} else if (!spawnForced) {
return Optional.empty();
@@ -1142,7 +1412,7 @@
@@ -1142,7 +1436,7 @@
BlockState iblockdata1 = world.getBlockState(pos.above());
boolean flag3 = iblockdata1.getBlock().isPossibleToRespawnInThis(iblockdata1);
@@ -707,7 +740,7 @@
}
}
@@ -1160,6 +1430,7 @@
@@ -1160,6 +1454,7 @@
@Nullable
@Override
public ServerPlayer teleport(TeleportTransition teleportTarget) {
@@ -715,7 +748,7 @@
if (this.isRemoved()) {
return null;
} else {
@@ -1169,39 +1440,78 @@
@@ -1169,39 +1464,78 @@
ServerLevel worldserver = teleportTarget.newLevel();
ServerLevel worldserver1 = this.serverLevel();
@@ -802,7 +835,7 @@
this.connection.resetPosition();
worldserver.addDuringTeleport(this);
gameprofilerfiller.pop();
@@ -1215,12 +1525,30 @@
@@ -1215,12 +1549,30 @@
this.lastSentExp = -1;
this.lastSentHealth = -1.0F;
this.lastSentFood = -1;
@@ -833,7 +866,7 @@
public void forceSetRotation(float yaw, float pitch) {
this.connection.send(new ClientboundPlayerRotationPacket(yaw, pitch));
}
@@ -1228,13 +1556,21 @@
@@ -1228,13 +1580,21 @@
public void triggerDimensionChangeTriggers(ServerLevel origin) {
ResourceKey<Level> resourcekey = origin.dimension();
ResourceKey<Level> resourcekey1 = this.level().dimension();
@@ -858,7 +891,7 @@
this.enteredNetherPosition = null;
}
@@ -1251,36 +1587,63 @@
@@ -1251,36 +1611,63 @@
this.containerMenu.broadcastChanges();
}
@@ -937,7 +970,7 @@
this.awardStat(Stats.SLEEP_IN_BED);
CriteriaTriggers.SLEPT_IN_BED.trigger(this);
});
@@ -1293,9 +1656,8 @@
@@ -1293,9 +1680,8 @@
return either;
}
}
@@ -948,7 +981,7 @@
}
@Override
@@ -1322,13 +1684,31 @@
@@ -1322,13 +1708,31 @@
@Override
public void stopSleepInBed(boolean skipSleepTimer, boolean updateSleepingPlayers) {
@@ -981,7 +1014,7 @@
}
}
@@ -1387,8 +1767,9 @@
@@ -1387,8 +1791,9 @@
this.connection.send(new ClientboundOpenSignEditorPacket(sign.getBlockPos(), front));
}
@@ -992,7 +1025,7 @@
}
@Override
@@ -1396,13 +1777,35 @@
@@ -1396,13 +1801,35 @@
if (factory == null) {
return OptionalInt.empty();
} else {
@@ -1028,7 +1061,7 @@
if (container == null) {
if (this.isSpectator()) {
this.displayClientMessage(Component.translatable("container.spectatorCantOpen").withStyle(ChatFormatting.RED), true);
@@ -1410,9 +1813,11 @@
@@ -1410,9 +1837,11 @@
return OptionalInt.empty();
} else {
@@ -1042,7 +1075,7 @@
return OptionalInt.of(this.containerCounter);
}
}
@@ -1425,15 +1830,26 @@
@@ -1425,15 +1854,26 @@
@Override
public void openHorseInventory(AbstractHorse horse, Container inventory) {
@@ -1072,7 +1105,7 @@
this.initMenu(this.containerMenu);
}
@@ -1456,6 +1872,13 @@
@@ -1456,6 +1896,13 @@
@Override
public void closeContainer() {
@@ -1086,7 +1119,7 @@
this.connection.send(new ClientboundContainerClosePacket(this.containerMenu.containerId));
this.doCloseContainer();
}
@@ -1485,19 +1908,19 @@
@@ -1485,19 +1932,19 @@
i = Math.round((float) Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ) * 100.0F);
if (i > 0) {
this.awardStat(Stats.SWIM_ONE_CM, i);
@@ -1109,7 +1142,7 @@
}
} else if (this.onClimbable()) {
if (deltaY > 0.0D) {
@@ -1508,13 +1931,13 @@
@@ -1508,13 +1955,13 @@
if (i > 0) {
if (this.isSprinting()) {
this.awardStat(Stats.SPRINT_ONE_CM, i);
@@ -1126,7 +1159,7 @@
}
}
} else if (this.isFallFlying()) {
@@ -1557,7 +1980,7 @@
@@ -1557,7 +2004,7 @@
@Override
public void awardStat(Stat<?> stat, int amount) {
this.stats.increment(this, stat, amount);
@@ -1135,7 +1168,7 @@
scoreaccess.add(amount);
});
}
@@ -1565,7 +1988,7 @@
@@ -1565,7 +2012,7 @@
@Override
public void resetStat(Stat<?> stat) {
this.stats.setValue(this, stat, 0);
@@ -1144,7 +1177,7 @@
}
@Override
@@ -1597,9 +2020,9 @@
@@ -1597,9 +2044,9 @@
super.jumpFromGround();
this.awardStat(Stats.JUMP);
if (this.isSprinting()) {
@@ -1156,7 +1189,7 @@
}
}
@@ -1625,6 +2048,7 @@
@@ -1625,6 +2072,7 @@
public void resetSentInfo() {
this.lastSentHealth = -1.0E8F;
@@ -1164,7 +1197,7 @@
}
@Override
@@ -1661,7 +2085,7 @@
@@ -1661,7 +2109,7 @@
this.onUpdateAbilities();
if (alive) {
this.getAttributes().assignBaseValues(oldPlayer.getAttributes());
@@ -1173,7 +1206,7 @@
this.setHealth(oldPlayer.getHealth());
this.foodData = oldPlayer.foodData;
Iterator iterator = oldPlayer.getActiveEffects().iterator();
@@ -1669,7 +2093,7 @@
@@ -1669,7 +2117,7 @@
while (iterator.hasNext()) {
MobEffectInstance mobeffect = (MobEffectInstance) iterator.next();
@@ -1182,7 +1215,7 @@
}
this.getInventory().replaceWith(oldPlayer.getInventory());
@@ -1680,7 +2104,7 @@
@@ -1680,7 +2128,7 @@
this.portalProcess = oldPlayer.portalProcess;
} else {
this.getAttributes().assignBaseValues(oldPlayer.getAttributes());
@@ -1191,7 +1224,7 @@
if (this.serverLevel().getGameRules().getBoolean(GameRules.RULE_KEEPINVENTORY) || oldPlayer.isSpectator()) {
this.getInventory().replaceWith(oldPlayer.getInventory());
this.experienceLevel = oldPlayer.experienceLevel;
@@ -1696,7 +2120,7 @@
@@ -1696,7 +2144,7 @@
this.lastSentExp = -1;
this.lastSentHealth = -1.0F;
this.lastSentFood = -1;
@@ -1200,7 +1233,7 @@
this.seenCredits = oldPlayer.seenCredits;
this.enteredNetherPosition = oldPlayer.enteredNetherPosition;
this.chunkTrackingView = oldPlayer.chunkTrackingView;
@@ -1752,19 +2176,19 @@
@@ -1752,19 +2200,19 @@
}
@Override
@@ -1224,7 +1257,7 @@
}
return flag1;
@@ -1861,8 +2285,13 @@
@@ -1861,8 +2309,13 @@
}
public void sendChatMessage(OutgoingChatMessage message, boolean filterMaskEnabled, ChatType.Bound params) {
@@ -1239,7 +1272,7 @@
}
}
@@ -1878,7 +2307,18 @@
@@ -1878,7 +2331,18 @@
}
public void updateOptions(ClientInformation clientOptions) {
@@ -1258,7 +1291,7 @@
this.requestedViewDistance = clientOptions.viewDistance();
this.chatVisibility = clientOptions.chatVisibility();
this.canChatColor = clientOptions.chatColors();
@@ -1962,7 +2402,7 @@
@@ -1962,7 +2426,7 @@
if (world instanceof ServerLevel) {
ServerLevel worldserver = (ServerLevel) world;
@@ -1267,7 +1300,7 @@
}
if (entity != null) {
@@ -1999,11 +2439,11 @@
@@ -1999,11 +2463,11 @@
@Nullable
public Component getTabListDisplayName() {
@@ -1281,7 +1314,7 @@
}
@Override
@@ -2046,17 +2486,43 @@
@@ -2046,17 +2510,43 @@
}
public void setRespawnPosition(ResourceKey<Level> dimension, @Nullable BlockPos pos, float angle, boolean forced, boolean sendMessage) {
@@ -1332,7 +1365,7 @@
} else {
this.respawnPosition = null;
this.respawnDimension = Level.OVERWORLD;
@@ -2088,18 +2554,44 @@
@@ -2088,18 +2578,44 @@
}
@Override
@@ -1381,7 +1414,7 @@
}
this.awardStat(Stats.DROP);
@@ -2375,16 +2867,160 @@
@@ -2375,16 +2891,160 @@
return TicketType.ENDER_PEARL.timeout();
}