/
ProjectileHitsScriptEvent.java
174 lines (152 loc) · 6.25 KB
/
ProjectileHitsScriptEvent.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package net.aufdemrand.denizen.events.entity;
import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.events.BukkitScriptEvent;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.objects.dMaterial;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.scripts.ScriptEntryData;
import net.aufdemrand.denizencore.scripts.containers.ScriptContainer;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import org.bukkit.Bukkit;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.ProjectileHitEvent;
import org.bukkit.util.BlockIterator;
import java.util.HashMap;
public class ProjectileHitsScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// projectile hits block (in <area>)
// projectile hits <material> (in <area>)
// <projectile> hits block (in <area>)
// <projectile> hits <material> (in <area>)
//
// @Cancellable false
//
// @Triggers when a projectile hits a block.
//
// @Context
// <context.projectile> returns the dEntity of the projectile.
// <context.shooter> returns the dEntity of the shooter, if there is one.
// <context.location> returns the dLocation of the block that was hit.
//
// -->
// <--[event]
// @Events
// entity shoots block (in <area>)
// entity shoots <material> (with <projectile>) (in <area>)
// <entity> shoots block (in <area>)
// <entity> shoots <material> (with <projectile>) (in <area>)
//
// @Triggers when a projectile shot by an entity hits a block.
//
// @Context
// <context.projectile> returns the dEntity of the projectile.
// <context.shooter> returns the dEntity of the shooter, if there is one.
// <context.location> returns the dLocation of the block that was hit.
//
// -->
public ProjectileHitsScriptEvent() {
instance = this;
}
public static ProjectileHitsScriptEvent instance;
public dEntity projectile;
public dEntity shooter;
public dLocation location;
private dMaterial material;
public ProjectileHitEvent event;
@Override
public boolean couldMatch(ScriptContainer scriptContainer, String s) {
String lower = CoreUtilities.toLowerCase(s);
String cmd = CoreUtilities.getXthArg(1, lower);
return cmd.equals("hits") || cmd.equals("shoots");
}
@Override
public boolean matches(ScriptContainer scriptContainer, String s) {
String lower = CoreUtilities.toLowerCase(s);
String cmd = CoreUtilities.getXthArg(1, lower);
String pTest = cmd.equals("hits") ? CoreUtilities.getXthArg(0, lower): CoreUtilities.getXthArg(4, lower);
if (pTest.length() > 0 && !projectile.matchesEntity(pTest)) {
return false;
}
String mat = CoreUtilities.getXthArg(2, lower);
if (!tryMaterial(material, mat)) {
return false;
}
String sTest = cmd.equals("shoots") ? CoreUtilities.getXthArg(0, lower) : "";
if (shooter != null && sTest.length() > 0 && !shooter.matchesEntity(sTest)) {
return false;
}
if (!runInCheck(scriptContainer, s, lower, location)) {
return false;
}
return true;
}
@Override
public String getName() {
return "ProjectileHits";
}
@Override
public void init() {
Bukkit.getServer().getPluginManager().registerEvents(this, DenizenAPI.getCurrentInstance());
}
@Override
public void destroy() {
ProjectileHitEvent.getHandlerList().unregister(this);
}
@Override
public boolean applyDetermination(ScriptContainer container, String determination) {
return super.applyDetermination(container, determination);
}
@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(shooter != null && shooter.isPlayer() ? dEntity.getPlayerFrom(event.getEntity()) : null,
shooter != null && shooter.isCitizensNPC() ? dEntity.getNPCFrom(event.getEntity()) : null);
}
@Override
public HashMap<String, dObject> getContext() {
HashMap<String, dObject> context = super.getContext();
context.put("projectile", projectile);
context.put("location", location);
if (shooter != null ) {
context.put("shooter", shooter);
}
return context;
}
@EventHandler
public void onProjectileHits(ProjectileHitEvent event) {
projectile = new dEntity(event.getEntity());
if (!projectile.getShooter().isPlayer()) return;
if (projectile.getLocation() == null)
return; // No, I can't explain how or why this would ever happen... nonetheless, it appears it does happen sometimes.
if (Double.isNaN(projectile.getLocation().getDirection().normalize().getX()))
return; // I can't explain this one either. It also chooses to happen whenever it pleases.
Block block = null;
try {
BlockIterator bi = new BlockIterator(projectile.getLocation().getWorld(),
projectile.getLocation().toVector(), projectile.getLocation().getDirection().normalize(), 0, 4);
while (bi.hasNext()) {
block = bi.next();
if (block.getTypeId() != 0) {
break;
}
}
}
catch (IllegalStateException ex) {
// This happens because it can. Also not explainable whatsoever.
// As this error happens on no fault of the user, display no error message... just cancel the event.
return;
}
if (block == null) {
return;
}
material = dMaterial.getMaterialFrom(block.getType(), block.getData());
shooter = projectile.getShooter();
location = new dLocation(event.getEntity().getLocation());
this.event = event;
fire();
}
}