-
-
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
Open
SoSeDiK
wants to merge
3
commits into
PaperMC:main
Choose a base branch
from
SoSeDiK:combat-tracker
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add combat tracker API #11853
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
paper-api/src/main/java/io/papermc/paper/world/damagesource/CombatEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 FallLocationType 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, FallLocationType, 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 fallLocationType fall location type | ||
* @param fallDistance fall distance | ||
* @return a new combat entry | ||
* @see LivingEntity#calculateFallLocationType() | ||
* @see Entity#getFallDistance() | ||
*/ | ||
static CombatEntry combatEntry(DamageSource damageSource, float damage, @Nullable FallLocationType fallLocationType, float fallDistance) { | ||
return InternalAPIBridge.get().createCombatEntry(damageSource, damage, fallLocationType, fallDistance); | ||
} | ||
|
||
} |
106 changes: 106 additions & 0 deletions
106
paper-api/src/main/java/io/papermc/paper/world/damagesource/CombatTracker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
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> | ||
* The returned list is a copy, so any modifications | ||
* to its contents won't have an effect on this entity's | ||
* combat history. | ||
* | ||
* @return the list of combat entries | ||
* @see #setCombatEntries(List) | ||
*/ | ||
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); | ||
|
||
/** | ||
* 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(); | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
paper-api/src/main/java/io/papermc/paper/world/damagesource/FallLocationType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 FallLocationType extends Translatable permits FallLocationTypeImpl { | ||
|
||
/** | ||
* 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 | ||
*/ | ||
FallLocationType GENERIC = new FallLocationTypeImpl("generic"); | ||
/** | ||
* The entity is within the ladder | ||
*/ | ||
FallLocationType LADDER = new FallLocationTypeImpl("ladder"); | ||
/** | ||
* The entity is in vines | ||
*/ | ||
FallLocationType VINES = new FallLocationTypeImpl("vines"); | ||
/** | ||
* The entity is in weeping wines | ||
*/ | ||
FallLocationType WEEPING_VINES = new FallLocationTypeImpl("weeping_vines"); | ||
/** | ||
* The entity is in twisting vines | ||
*/ | ||
FallLocationType TWISTING_VINES = new FallLocationTypeImpl("twisting_vines"); | ||
/** | ||
* The entity is in scaffolding | ||
*/ | ||
FallLocationType SCAFFOLDING = new FallLocationTypeImpl("scaffolding"); | ||
/** | ||
* The entity is within some other climable block | ||
*/ | ||
FallLocationType OTHER_CLIMBABLE = new FallLocationTypeImpl("other_climbable"); | ||
/** | ||
* The entity is in water | ||
*/ | ||
FallLocationType WATER = new FallLocationTypeImpl("water"); | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
paper-api/src/main/java/io/papermc/paper/world/damagesource/FallLocationTypeImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 FallLocationTypeImpl(String id) implements FallLocationType { | ||
|
||
@Override | ||
public String translationKey() { | ||
// Same as net.minecraft.world.damagesource.FallLocation#languageKey | ||
return "death.fell.accident." + this.id; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What's the use of this?
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.
"Sneakily" setting prior combat history without overriding/resetting active combat state.
It's a neat shortcut for providing fake pre-made combat history scenarios and producing specific death messages.