-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements the PrePlayerAttackEntityEvent (#2473)
* PrePlayerAttackEntityEvent Implements the PrePlayerAttackEntityEvent, which fires when the player tries to attack an entity. * Updated both files * REMOVES THE STRAY NEW LINE * Update PrePlayerAttackEntityScriptEvent.java Removed useless import, updated event line & updated matcher accordingly, moved context for tags into triggers, and fixed formatting issue. * Update PrePlayerAttackEntityScriptEvent.java * Modified event line * Modified player link meta
- Loading branch information
Showing
2 changed files
with
83 additions
and
0 deletions.
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
80 changes: 80 additions & 0 deletions
80
...rc/main/java/com/denizenscript/denizen/paper/events/PrePlayerAttackEntityScriptEvent.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,80 @@ | ||
package com.denizenscript.denizen.paper.events; | ||
|
||
import com.denizenscript.denizen.events.BukkitScriptEvent; | ||
import com.denizenscript.denizen.objects.EntityTag; | ||
import com.denizenscript.denizen.objects.ItemTag; | ||
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData; | ||
import com.denizenscript.denizencore.objects.ObjectTag; | ||
import com.denizenscript.denizencore.objects.core.ElementTag; | ||
import com.denizenscript.denizencore.scripts.ScriptEntryData; | ||
import io.papermc.paper.event.player.PrePlayerAttackEntityEvent; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
|
||
public class PrePlayerAttackEntityScriptEvent extends BukkitScriptEvent implements Listener { | ||
|
||
// <--[event] | ||
// @Events | ||
// player tries to attack <entity> | ||
// | ||
// @Plugin Paper | ||
// | ||
// @Group Paper | ||
// | ||
// @Cancellable true | ||
// | ||
// @Switch with:<item> to only process the event if the player attacks the entity with the specified item. | ||
// | ||
// @Triggers when the player tries to attack an entity. This occurs before any of the damage logic, so cancelling this event will prevent any sort of sounds from being played when attacking. | ||
// | ||
// @Context | ||
// <context.entity> returns the entity that was attacked in this event. | ||
// <context.attacked> returns whether this entity would be attacked normally. | ||
// Entities like falling sand would return false because their entity type does not allow them to be attacked. | ||
// Note: there may be other factors (invulnerability, etc.) that will prevent this entity from being attacked that this event will not cover. | ||
// | ||
// @Player Always. | ||
// | ||
// --> | ||
|
||
public PrePlayerAttackEntityScriptEvent() { | ||
registerCouldMatcher("player tries to attack <entity>"); | ||
registerSwitches("with"); | ||
} | ||
|
||
public PrePlayerAttackEntityEvent event; | ||
public EntityTag entity; | ||
public ItemTag item; | ||
|
||
@Override | ||
public boolean matches(ScriptPath path) { | ||
if (!path.tryArgObject(4, entity)) { | ||
return false; | ||
} | ||
if (!path.tryObjectSwitch("with", new ItemTag(event.getPlayer().getEquipment().getItemInMainHand()))) { | ||
return false; | ||
} | ||
return super.matches(path); | ||
} | ||
|
||
@Override | ||
public ScriptEntryData getScriptEntryData() { | ||
return new BukkitScriptEntryData(event.getPlayer()); | ||
} | ||
|
||
@Override | ||
public ObjectTag getContext(String name) { | ||
return switch (name) { | ||
case "entity" -> entity; | ||
case "attacked" -> new ElementTag(event.willAttack()); | ||
default -> super.getContext(name); | ||
}; | ||
} | ||
|
||
@EventHandler | ||
public void onPrePlayerAttackEntity(PrePlayerAttackEntityEvent event) { | ||
this.event = event; | ||
entity = new EntityTag(event.getAttacked()); | ||
fire(event); | ||
} | ||
} |