-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add combat tracker API #11853
base: main
Are you sure you want to change the base?
Add combat tracker API #11853
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package io.papermc.paper.world.damagesource; | ||
|
||
import io.papermc.paper.InternalAPIBridge; | ||
import org.bukkit.damage.DamageSource; | ||
import org.bukkit.entity.Entity; | ||
import org.bukkit.entity.LivingEntity; | ||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jspecify.annotations.NullMarked; | ||
import org.jspecify.annotations.Nullable; | ||
|
||
/** | ||
* Represents a combat entry | ||
* | ||
* @since 1.21.4 | ||
*/ | ||
@NullMarked | ||
@ApiStatus.Experimental | ||
@ApiStatus.NonExtendable | ||
public interface CombatEntry { | ||
|
||
/** | ||
* Gets the damage source | ||
* | ||
* @return the damage source | ||
*/ | ||
DamageSource getDamageSource(); | ||
|
||
/** | ||
* Gets the amount of damage caused | ||
* | ||
* @return the amount of damage caused | ||
*/ | ||
float getDamage(); | ||
|
||
/** | ||
* Gets the fall location at the time of this damage | ||
* | ||
* @return the fall location | ||
*/ | ||
@Nullable FallLocation getFallLocation(); | ||
|
||
/** | ||
* Gets the fall distance at the time of this damage | ||
* | ||
* @return the fall distance | ||
*/ | ||
float getFallDistance(); | ||
|
||
/** | ||
* Creates a new combat entry. | ||
* <br> | ||
* The fall location and fall distance will be calculated from the entity's current state. | ||
* | ||
* @param entity entity | ||
* @param damageSource damage source | ||
* @param damage damage amount | ||
* @return combat entry | ||
* @see #combatEntry(DamageSource, float, FallLocation, float) | ||
*/ | ||
static CombatEntry combatEntry(LivingEntity entity, DamageSource damageSource, float damage) { | ||
return InternalAPIBridge.get().createCombatEntry(entity, damageSource, damage); | ||
} | ||
|
||
/** | ||
* Creates a new combat entry | ||
* | ||
* @param damageSource damage source | ||
* @param damage damage amount | ||
* @param fallLocation fall location | ||
* @param fallDistance fall distance | ||
* @return a new combat entry | ||
* @see LivingEntity#calculateFallLocation() | ||
* @see Entity#getFallDistance() | ||
*/ | ||
static CombatEntry combatEntry(DamageSource damageSource, float damage, @Nullable FallLocation fallLocation, float fallDistance) { | ||
return InternalAPIBridge.get().createCombatEntry(damageSource, damage, fallLocation, fallDistance); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package io.papermc.paper.world.damagesource; | ||
|
||
import net.kyori.adventure.text.Component; | ||
import org.bukkit.entity.LivingEntity; | ||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jspecify.annotations.NullMarked; | ||
import org.jspecify.annotations.Nullable; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Represents entity's combat tracker | ||
* | ||
* @since 1.21.4 | ||
*/ | ||
@NullMarked | ||
@ApiStatus.Experimental | ||
@ApiStatus.NonExtendable | ||
public interface CombatTracker { | ||
|
||
/** | ||
* Gets the entity behind this combat tracker | ||
* | ||
* @return the entity behind this combat tracker | ||
*/ | ||
LivingEntity getEntity(); | ||
|
||
/** | ||
* Gets the list of recorded combat entries. | ||
* <br> | ||
* Mutating the list is safe and won't have | ||
* an effect on this entity's combat history. | ||
* | ||
* @return the list of combat entries | ||
*/ | ||
List<CombatEntry> getCombatEntries(); | ||
|
||
/** | ||
* Sets the entity's combat history. | ||
* <br> | ||
* Note that overriding the entity's combat history won't | ||
* have an effect on the entity's current or new combat state. | ||
* Reset the current combat state and register new combat entries instead | ||
* if you want the new history to have an effect on the combat state. | ||
* | ||
* @param combatEntries combat entries | ||
* @see #resetCombatState() | ||
* @see #addCombatEntry(CombatEntry) | ||
*/ | ||
void setCombatEntries(List<CombatEntry> combatEntries); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the use of this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Sneakily" setting prior combat history without overriding/resetting active combat state. |
||
|
||
/** | ||
* Calculates the most significant fall damage entry | ||
* | ||
* @return the most significant fall damage entry | ||
*/ | ||
@Nullable CombatEntry getMostSignificantFall(); | ||
|
||
/** | ||
* Checks whether the entity is in combat, | ||
* i.e. has taken damage from an entity | ||
* since the combat tracking has begun | ||
* | ||
* @return whether the entity is in combat | ||
*/ | ||
boolean isInCombat(); | ||
|
||
/** | ||
* Checks whether the entity has started recording damage, | ||
* i.e. its combat tracking is active | ||
* | ||
* @return whether the entity has started recording damage | ||
*/ | ||
boolean isTakingDamage(); | ||
|
||
/** | ||
* Gets the last or current combat duration | ||
* | ||
* @return the combat duration | ||
* @see #isInCombat() | ||
*/ | ||
int getCombatDuration(); | ||
|
||
/** | ||
* Adds a new entry the pool of combat entries, | ||
* updating the entity's combat state | ||
* | ||
* @param combatEntry combat entry | ||
*/ | ||
void addCombatEntry(CombatEntry combatEntry); | ||
|
||
/** | ||
* Constructs a death message based on the current combat history | ||
* | ||
* @return a death message | ||
*/ | ||
Component getDeathMessage(); | ||
|
||
/** | ||
* Resets entity's combat state, clearing combat history | ||
*/ | ||
void resetCombatState(); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package io.papermc.paper.world.damagesource; | ||
|
||
import net.kyori.adventure.translation.Translatable; | ||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jspecify.annotations.NullMarked; | ||
|
||
/** | ||
* Represents a type of location from which the entity fell | ||
* | ||
* @since 1.21.4 | ||
*/ | ||
@NullMarked | ||
@ApiStatus.Experimental | ||
public sealed interface FallLocation extends Translatable permits FallLocationImpl { | ||
SoSeDiK marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Gets the fall location id | ||
* | ||
* @return the fall location id | ||
*/ | ||
String id(); | ||
|
||
/** | ||
* Gets the translation key used for a fall death message | ||
* caused by falling from this location | ||
* | ||
* @return the translation key | ||
*/ | ||
@Override | ||
String translationKey(); | ||
|
||
/** | ||
* The entity is not within a special fall location | ||
*/ | ||
FallLocation GENERIC = new FallLocationImpl("generic"); | ||
/** | ||
* The entity is within the ladder | ||
*/ | ||
FallLocation LADDER = new FallLocationImpl("ladder"); | ||
/** | ||
* The entity is in vines | ||
*/ | ||
FallLocation VINES = new FallLocationImpl("vines"); | ||
/** | ||
* The entity is in weeping wines | ||
*/ | ||
FallLocation WEEPING_VINES = new FallLocationImpl("weeping_vines"); | ||
/** | ||
* The entity is in twisting vines | ||
*/ | ||
FallLocation TWISTING_VINES = new FallLocationImpl("twisting_vines"); | ||
/** | ||
* The entity is in scaffolding | ||
*/ | ||
FallLocation SCAFFOLDING = new FallLocationImpl("scaffolding"); | ||
/** | ||
* The entity is within some other climable block | ||
*/ | ||
FallLocation OTHER_CLIMBABLE = new FallLocationImpl("other_climbable"); | ||
/** | ||
* The entity is in water | ||
*/ | ||
FallLocation WATER = new FallLocationImpl("water"); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.papermc.paper.world.damagesource; | ||
|
||
import org.jspecify.annotations.NullMarked; | ||
|
||
@NullMarked | ||
record FallLocationImpl(String id) implements FallLocation { | ||
|
||
@Override | ||
public String translationKey() { | ||
// Same as net.minecraft.world.damagesource.FallLocation#languageKey | ||
return "death.fell.accident." + this.id; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should just be immutable/say it's a copy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the wording to state it's a copy.
Haven't made this immutable because it's returning a copy internally each time, so modifications are safe. Otherwise, users would need to make a copy of a copy.