-
-
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.
Add Paper Only TargetHitScriptEvent (#2604)
* Add Paper Only TargetHitScriptEvent * squashme: review comments * squash: more review comments * squashme: review comments * squashme: review comments * squashme: review comments * squashme: review comments * squashme: review comments * review
- Loading branch information
Showing
2 changed files
with
84 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
83 changes: 83 additions & 0 deletions
83
paper/src/main/java/com/denizenscript/denizen/paper/events/TargetBlockHitScriptEvent.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,83 @@ | ||
package com.denizenscript.denizen.paper.events; | ||
|
||
import com.denizenscript.denizen.events.BukkitScriptEvent; | ||
import com.denizenscript.denizen.objects.EntityTag; | ||
import com.denizenscript.denizen.objects.LocationTag; | ||
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.block.TargetHitEvent; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
|
||
|
||
public class TargetBlockHitScriptEvent extends BukkitScriptEvent implements Listener { | ||
|
||
// <--[event] | ||
// @Events | ||
// target block hit | ||
// | ||
// @Location true | ||
// | ||
// @Group Paper | ||
// | ||
// @Plugin Paper | ||
// | ||
// @Triggers when a target block is hit by a projectile such as an arrow. | ||
// | ||
// @Context | ||
// <context.projectile> returns an EntityTag of the projectile. | ||
// <context.hit_block> returns a LocationTag of the block that was hit. | ||
// <context.hit_face> returns a LocationTag vector of the hit normal (like '0,1,0' if the projectile hit the top of a block). | ||
// <context.shooter> returns an EntityTag of the entity that shot the projectile, if any. | ||
// <context.strength> returns a ElementTag of the emitted redstone strength. | ||
// | ||
// @Player when the shooter is a player. | ||
// | ||
// @NPC when the shooter is a npc. | ||
// --> | ||
|
||
public TargetBlockHitScriptEvent() { | ||
registerCouldMatcher("target block hit"); | ||
} | ||
|
||
public TargetHitEvent event; | ||
public LocationTag hitBlock; | ||
public EntityTag projectile; | ||
public EntityTag shooter; | ||
|
||
@Override | ||
public boolean matches(ScriptPath path) { | ||
if (!runInCheck(path, hitBlock)) { | ||
return false; | ||
} | ||
return super.matches(path); | ||
} | ||
|
||
@Override | ||
public ObjectTag getContext(String name) { | ||
return switch (name) { | ||
case "projectile" -> projectile.getDenizenObject(); | ||
case "hit_block" -> hitBlock; | ||
case "hit_face" -> event.getHitBlockFace() != null ? new LocationTag(event.getHitBlockFace().getDirection()) : null; | ||
case "shooter" -> shooter != null ? shooter.getDenizenObject() : null; | ||
case "strength" -> new ElementTag(event.getSignalStrength()); | ||
default -> super.getContext(name); | ||
}; | ||
} | ||
|
||
@Override | ||
public ScriptEntryData getScriptEntryData() { | ||
return new BukkitScriptEntryData(shooter); | ||
} | ||
|
||
@EventHandler | ||
public void onProjectileHit(TargetHitEvent event) { | ||
this.event = event; | ||
projectile = new EntityTag(event.getEntity()); | ||
hitBlock = new LocationTag(event.getHitBlock().getLocation()); | ||
shooter = projectile.getShooter(); | ||
fire(event); | ||
} | ||
} |