Skip to content

Conversation

@MrScopes
Copy link
Contributor

Problem

This event could be useful in some situations.
https://jd.papermc.io/paper/1.21.11/io/papermc/paper/event/player/PrePlayerAttackEntityEvent.html

Solution

Added [player] pre attack[ing] [an] [entity]
Added [the] (entity|victim) (:will|will not|won't) be attack(ed|ing)

Testing Completed

on player pre attack entity:
    if victim will be attacked:
        send "attack on %entity% was canceled"
        cancel event
    else:
        # tested with 'listen to cancelled events by default: true'
        send "%victim% will not be attacked"
    send "event is cancelled" if event is cancelled else "event is not cancelled"
image

Supporting Information

Examples and descriptions need some work.
Honestly the will/won't be attacked can probably be removed. It's really not too useful from what I can tell and the event already won't be called unless you enable the config setting to listen to canceled events, since it's sent as canceled already if the entity is non-living and won't be attacked.
For the condition, I went with (entity|victim) instead of an expression since the event itself just has willAttack(boolean). only concern is I'm not sure if this will cause any conflicts. Whole thing needs some work but it's a start

Since it's a player event, I set the event value for command sender to be the player in the event
event-entity is also set to the getAttacked() of the event
also edited ExprAttacked/victim expression to support this so the event could use

on player pre attack entity:
    kill victim
    # or
    kill entity

Completes: 8266
Related: none
AI assistance: none

@MrScopes MrScopes requested a review from a team as a code owner December 23, 2025 08:26
@MrScopes MrScopes requested review from Pesekjak and TheMug06 and removed request for a team December 23, 2025 08:26
@skriptlang-automation skriptlang-automation bot added the needs reviews A PR that needs additional reviews label Dec 23, 2025
@sovdeeth
Copy link
Member

Note that listening to cancelled events does not require the config, you can simply do on cancelled X or on any X

@MrScopes
Copy link
Contributor Author

MrScopes commented Dec 23, 2025

Just a thought, could maybe still take an expression but not do anything with it for the condition, or maybe [event-](entity|victim) etc. But obviously could lead to some confusion

@MrScopes
Copy link
Contributor Author

Note that listening to cancelled events does not require the config, you can simply do on cancelled X or on any X

Good to know. I can update the examples and description with that + whatever else is decided

@sovdeeth sovdeeth added the feature Pull request adding a new feature. label Dec 24, 2025
@Name("Will Be Attacked")
@Description("""
Checks if the entity in <a href='#Evt'>Player Pre Attack Entity Event</a> will be attacked.
Returns false for non-living entities. However, the event won't be called for them unless explicitly listened to.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should clarify what "explicitly listened to" means


static {
Skript.registerCondition(CondWillBeAttacked.class,
"[the] [event-](entity|victim) (:will|will not|won't) be attacked"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i do not like using event-entity and victim literally in syntaxes, since readers of code will assume they are expressions like they normally are.
If this is always equal to isCancelled, as the javadocs imply, I would say you can just remove the condition entirely.

Comment on lines +43 to +48
public boolean check(Event e) {
return (e instanceof PrePlayerAttackEntityEvent && ((PrePlayerAttackEntityEvent) e).willAttack()) ^ isNegated();
}

@Override
public String toString(@Nullable Event e, boolean debug) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use event, not e


@Override
public boolean check(Event e) {
return (e instanceof PrePlayerAttackEntityEvent && ((PrePlayerAttackEntityEvent) e).willAttack()) ^ isNegated();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use pattern variable here

.since("2.12");

Skript.registerEvent("Pre Player Attack Entity Event", SimpleEvent.class, PrePlayerAttackEntityEvent.class,
"[player] pre attack[ing] [an] [entity]")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you come up with a better syntax than pre attacking? it's really clunky as is but I don't know if I have better suggestions
maybe something along the lines of before attack?

.examples("""
on player pre attack entity:
cancel event
send "Your attack was canceled!"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would (should) cause a parse error
You will need to update the event value for event-entity to add the pre-attack event as a case to use attacker/victim.
Also need to update ExprAttacker to support attacker in the pre attack event.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would (should) cause a parse error You will need to update the event value for event-entity to add the pre-attack event as a case to use attacker/victim. Also need to update ExprAttacker to support attacker in the pre attack event.

the reason I didn't make it attacker is since it's always going to be a player event, but yeah making it attacker would be easy. will do tomorrow. command sender is set to player in the event so it won't have any errors in my example

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah the issue is that what if a player attacks another player? now there's 2 event-players and it's ambiguous which one the user meant

Comment on lines +88 to +89
else if (e instanceof PrePlayerAttackEntityEvent)
entity = ((PrePlayerAttackEntityEvent) e).getAttacked();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please update the expression to use event instead of e and to use pattern variables while you're here.
and the {} for if else
and any other violations of our coding conventions exist in this class

Comment on lines +350 to +351
EventValues.registerEventValue(PrePlayerAttackEntityEvent.class, Entity.class, PrePlayerAttackEntityEvent::getAttacked);
EventValues.registerEventValue(PrePlayerAttackEntityEvent.class, CommandSender.class, PrePlayerAttackEntityEvent::getPlayer);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

neither of these are necessary, since the user should be using attacker and victim

Copy link
Member

@Efnilite Efnilite left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good after sovde's changes


@Name("Will Be Attacked")
@Description("""
Checks if the entity in <a href='#Evt'>Player Pre Attack Entity Event</a> will be attacked.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Checks if the entity in <a href='#Evt'>Player Pre Attack Entity Event</a> will be attacked.
Checks if the entity in <a href='#PlayerPreAttackEntity'>Player Pre Attack Entity Event</a> will be attacked.

"[player] pre attack[ing] [an] [entity]")
.description(
"Called when a player tries to attack an entity.",
"Canceling this event will prevent the attack and any sounds being played when attacking.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"Canceling this event will prevent the attack and any sounds being played when attacking.",
"Canceling this event will prevent the attack and any sounds from being played when attacking.",

@skriptlang-automation skriptlang-automation bot added needs reviews A PR that needs additional reviews and removed needs reviews A PR that needs additional reviews labels Dec 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Pull request adding a new feature. needs reviews A PR that needs additional reviews

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants