-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
TargetBlockHitScriptEvent.java
83 lines (73 loc) · 2.81 KB
/
TargetBlockHitScriptEvent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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);
}
}