| 1 | package net.minecraft.world.damagesource;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import java.util.List;
|
| 5 | import java.util.Objects;
|
| 6 | import net.minecraft.core.component.DataComponents;
|
| 7 | import net.minecraft.network.chat.ClickEvent;
|
| 8 | import net.minecraft.network.chat.Component;
|
| 9 | import net.minecraft.network.chat.ComponentUtils;
|
| 10 | import net.minecraft.network.chat.HoverEvent;
|
| 11 | import net.minecraft.network.chat.Style;
|
| 12 | import net.minecraft.tags.DamageTypeTags;
|
| 13 | import net.minecraft.util.CommonLinks;
|
| 14 | import net.minecraft.world.entity.Entity;
|
| 15 | import net.minecraft.world.entity.LivingEntity;
|
| 16 | import net.minecraft.world.item.ItemStack;
|
| 17 | import org.jspecify.annotations.Nullable;
|
| 18 |
|
| 19 | public class CombatTracker {
|
| 20 | public static final int RESET_DAMAGE_STATUS_TIME = 100;
|
| 21 | public static final int RESET_COMBAT_STATUS_TIME = 300;
|
| 22 | private static final Style INTENTIONAL_GAME_DESIGN_STYLE = Style.EMPTY
|
| 23 | .withClickEvent(new ClickEvent.OpenUrl(CommonLinks.INTENTIONAL_GAME_DESIGN_BUG))
|
| 24 | .withHoverEvent(new HoverEvent.ShowText(Component.literal("MCPE-28723")));
|
| 25 | private final List<CombatEntry> entries = Lists.newArrayList();
|
| 26 | private final LivingEntity mob;
|
| 27 | private int lastDamageTime;
|
| 28 | private int combatStartTime;
|
| 29 | private int combatEndTime;
|
| 30 | private boolean inCombat;
|
| 31 | private boolean takingDamage;
|
| 32 |
|
| 33 | public CombatTracker(final LivingEntity mob) {
|
| 34 | this.mob = mob;
|
| 35 | }
|
| 36 |
|
| 37 | public void recordDamage(final DamageSource source, final float damage) {
|
| 38 | this.recheckStatus();
|
| 39 | FallLocation fallLocation = FallLocation.getCurrentFallLocation(this.mob);
|
| 40 | CombatEntry entry = new CombatEntry(source, damage, fallLocation, (float)this.mob.fallDistance);
|
| 41 | this.entries.add(entry);
|
| 42 | this.lastDamageTime = this.mob.tickCount;
|
| 43 | this.takingDamage = true;
|
| 44 | if (!this.inCombat && this.mob.isAlive() && shouldEnterCombat(source)) {
|
| 45 | this.inCombat = true;
|
| 46 | this.combatStartTime = this.mob.tickCount;
|
| 47 | this.combatEndTime = this.combatStartTime;
|
| 48 | this.mob.onEnterCombat();
|
| 49 | }
|
| 50 | }
|
| 51 |
|
| 52 | private static boolean shouldEnterCombat(final DamageSource source) {
|
| 53 | return source.getEntity() instanceof LivingEntity;
|
| 54 | }
|
| 55 |
|
| 56 | private Component getMessageForAssistedFall(
|
| 57 | final Entity attackerEntity, final Component attackerName, final String messageWithItem, final String messageWithoutItem
|
| 58 | ) {
|
| 59 | ItemStack attackerItem = attackerEntity instanceof LivingEntity livingEntity ? livingEntity.getMainHandItem() : ItemStack.EMPTY;
|
| 60 | return !attackerItem.isEmpty() && attackerItem.has(DataComponents.CUSTOM_NAME)
|
| 61 | ? Component.translatable(messageWithItem, this.mob.getDisplayName(), attackerName, attackerItem.getDisplayName())
|
| 62 | : Component.translatable(messageWithoutItem, this.mob.getDisplayName(), attackerName);
|
| 63 | }
|
| 64 |
|
| 65 | private Component getFallMessage(final CombatEntry knockOffEntry, final @Nullable Entity killingEntity) {
|
| 66 | DamageSource knockOffSource = knockOffEntry.source();
|
| 67 | if (!knockOffSource.is(DamageTypeTags.IS_FALL) && !knockOffSource.is(DamageTypeTags.ALWAYS_MOST_SIGNIFICANT_FALL)) {
|
| 68 | Component killerName = getDisplayName(killingEntity);
|
| 69 | Entity attackerEntity = knockOffSource.getEntity();
|
| 70 | Component attackerName = getDisplayName(attackerEntity);
|
| 71 | if (attackerName != null && !attackerName.equals(killerName)) {
|
| 72 | return this.getMessageForAssistedFall(attackerEntity, attackerName, "death.fell.assist.item", "death.fell.assist");
|
| 73 | } else {
|
| 74 | return killerName != null
|
| 75 | ? this.getMessageForAssistedFall(killingEntity, killerName, "death.fell.finish.item", "death.fell.finish")
|
| 76 | : Component.translatable("death.fell.killer", this.mob.getDisplayName());
|
| 77 | }
|
| 78 | } else {
|
| 79 | FallLocation fallLocation = Objects.requireNonNullElse(knockOffEntry.fallLocation(), FallLocation.GENERIC);
|
| 80 | return Component.translatable(fallLocation.languageKey(), this.mob.getDisplayName());
|
| 81 | }
|
| 82 | }
|
| 83 |
|
| 84 | private static @Nullable Component getDisplayName(final @Nullable Entity entity) {
|
| 85 | return entity == null ? null : entity.getDisplayName();
|
| 86 | }
|
| 87 |
|
| 88 | public Component getDeathMessage() {
|
| 89 | if (this.entries.isEmpty()) {
|
| 90 | return Component.translatable("death.attack.generic", this.mob.getDisplayName());
|
| 91 | } else {
|
| 92 | CombatEntry killingBlow = this.entries.get(this.entries.size() - 1);
|
| 93 | DamageSource killingSource = killingBlow.source();
|
| 94 | CombatEntry knockOffEntry = this.getMostSignificantFall();
|
| 95 | DeathMessageType messageType = killingSource.type().deathMessageType();
|
| 96 | if (messageType == DeathMessageType.FALL_VARIANTS && knockOffEntry != null) {
|
| 97 | return this.getFallMessage(knockOffEntry, killingSource.getEntity());
|
| 98 | } else if (messageType == DeathMessageType.INTENTIONAL_GAME_DESIGN) {
|
| 99 | String deathMsg = "death.attack." + killingSource.getMsgId();
|
| 100 | Component link = ComponentUtils.wrapInSquareBrackets(Component.translatable(deathMsg + ".link")).withStyle(INTENTIONAL_GAME_DESIGN_STYLE);
|
| 101 | return Component.translatable(deathMsg + ".message", this.mob.getDisplayName(), link);
|
| 102 | } else {
|
| 103 | return killingSource.getLocalizedDeathMessage(this.mob);
|
| 104 | }
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | private @Nullable CombatEntry getMostSignificantFall() {
|
| 109 | CombatEntry result = null;
|
| 110 | CombatEntry alternative = null;
|
| 111 | float altDamage = 0.0F;
|
| 112 | float bestFall = 0.0F;
|
| 113 |
|
| 114 | for (int i = 0; i < this.entries.size(); i++) {
|
| 115 | CombatEntry entry = this.entries.get(i);
|
| 116 | CombatEntry previous = i > 0 ? this.entries.get(i - 1) : null;
|
| 117 | DamageSource source = entry.source();
|
| 118 | boolean isFakeFall = source.is(DamageTypeTags.ALWAYS_MOST_SIGNIFICANT_FALL);
|
| 119 | float fallDistance = isFakeFall ? Float.MAX_VALUE : entry.fallDistance();
|
| 120 | if ((source.is(DamageTypeTags.IS_FALL) || isFakeFall) && fallDistance > 0.0F && (result == null || fallDistance > bestFall)) {
|
| 121 | if (i > 0) {
|
| 122 | result = previous;
|
| 123 | } else {
|
| 124 | result = entry;
|
| 125 | }
|
| 126 |
|
| 127 | bestFall = fallDistance;
|
| 128 | }
|
| 129 |
|
| 130 | if (entry.fallLocation() != null && (alternative == null || entry.damage() > altDamage)) {
|
| 131 | alternative = entry;
|
| 132 | altDamage = entry.damage();
|
| 133 | }
|
| 134 | }
|
| 135 |
|
| 136 | if (bestFall > 5.0F && result != null) {
|
| 137 | return result;
|
| 138 | } else {
|
| 139 | return altDamage > 5.0F && alternative != null ? alternative : null;
|
| 140 | }
|
| 141 | }
|
| 142 |
|
| 143 | public int getCombatDuration() {
|
| 144 | return this.inCombat ? this.mob.tickCount - this.combatStartTime : this.combatEndTime - this.combatStartTime;
|
| 145 | }
|
| 146 |
|
| 147 | public void recheckStatus() {
|
| 148 | int reset = this.inCombat ? 300 : 100;
|
| 149 | if (this.takingDamage && (!this.mob.isAlive() || this.mob.tickCount - this.lastDamageTime > reset)) {
|
| 150 | boolean wasInCombat = this.inCombat;
|
| 151 | this.takingDamage = false;
|
| 152 | this.inCombat = false;
|
| 153 | this.combatEndTime = this.mob.tickCount;
|
| 154 | if (wasInCombat) {
|
| 155 | this.mob.onLeaveCombat();
|
| 156 | }
|
| 157 |
|
| 158 | this.entries.clear();
|
| 159 | }
|
| 160 | }
|
| 161 | }
|
| 162 |
|