-
-
Notifications
You must be signed in to change notification settings - Fork 302
/
LookClose.java
319 lines (273 loc) · 9.92 KB
/
LookClose.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package net.citizensnpcs.trait;
import java.util.Random;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.potion.PotionEffectType;
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.command.CommandConfigurable;
import net.citizensnpcs.api.command.CommandContext;
import net.citizensnpcs.api.command.CommandMessages;
import net.citizensnpcs.api.command.exception.CommandException;
import net.citizensnpcs.api.event.NPCLookCloseChangeTargetEvent;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.util.NMS;
import net.citizensnpcs.util.Util;
/**
* Persists the /npc lookclose metadata
*
*/
@TraitName("lookclose")
public class LookClose extends Trait implements Toggleable, CommandConfigurable {
@Persist("disablewhilenavigating")
private boolean disableWhileNavigating = Setting.DISABLE_LOOKCLOSE_WHILE_NAVIGATING.asBoolean();
@Persist("enabled")
private boolean enabled = Setting.DEFAULT_LOOK_CLOSE.asBoolean();
@Persist
private boolean enableRandomLook = Setting.DEFAULT_RANDOM_LOOK_CLOSE.asBoolean();
private Player lookingAt;
@Persist
private int randomLookDelay = Setting.DEFAULT_RANDOM_LOOK_DELAY.asInt();
@Persist
private float[] randomPitchRange = { 0, 0 };
@Persist
private float[] randomYawRange = { 0, 360 };
private double range = Setting.DEFAULT_LOOK_CLOSE_RANGE.asDouble();
@Persist("realisticlooking")
private boolean realisticLooking = Setting.DEFAULT_REALISTIC_LOOKING.asBoolean();
private int t;
public LookClose() {
super("lookclose");
}
private boolean canSee(Player player) {
if (player == null || !player.isValid())
return false;
return realisticLooking && npc.getEntity() instanceof LivingEntity
? ((LivingEntity) npc.getEntity()).hasLineOfSight(player)
: true;
}
/**
* Returns whether the target can be seen. Will use realistic line of sight if {@link #setRealisticLooking(boolean)}
* is true.
*/
public boolean canSeeTarget() {
return canSee(lookingAt);
}
@Override
public void configure(CommandContext args) throws CommandException {
try {
range = args.getFlagDouble("range", args.getFlagDouble("r", range));
} catch (NumberFormatException ex) {
throw new CommandException(CommandMessages.INVALID_NUMBER);
}
realisticLooking = args.hasFlag('r');
}
public boolean disableWhileNavigating() {
return disableWhileNavigating;
}
/**
* Finds a new look-close target
*/
public void findNewTarget() {
double min = range * range;
Player old = lookingAt;
for (Entity entity : npc.getEntity().getNearbyEntities(range, range, range)) {
if (!(entity instanceof Player))
continue;
Player player = (Player) entity;
Location location = player.getLocation(CACHE_LOCATION);
if (location.getWorld() != NPC_LOCATION.getWorld())
continue;
double dist = location.distanceSquared(NPC_LOCATION);
if (dist > min || CitizensAPI.getNPCRegistry().getNPC(entity) != null || isInvisible(player))
continue;
min = dist;
lookingAt = player;
}
if (old != lookingAt) {
NPCLookCloseChangeTargetEvent event = new NPCLookCloseChangeTargetEvent(npc, old, lookingAt);
Bukkit.getPluginManager().callEvent(event);
if (lookingAt != event.getNewTarget() && event.getNewTarget() != null && !isValid(event.getNewTarget())) {
return;
}
lookingAt = event.getNewTarget();
}
}
public int getRandomLookDelay() {
return randomLookDelay;
}
public float[] getRandomLookPitchRange() {
return randomPitchRange;
}
public float[] getRandomLookYawRange() {
return randomYawRange;
}
public double getRange() {
return range;
}
public Player getTarget() {
return lookingAt;
}
public boolean isEnabled() {
return enabled;
}
private boolean isInvisible(Player player) {
return player.getGameMode() == GameMode.SPECTATOR || player.hasPotionEffect(PotionEffectType.INVISIBILITY)
|| isPluginVanished(player) || !canSee(player);
}
private boolean isPluginVanished(Player player) {
for (MetadataValue meta : player.getMetadata("vanished")) {
if (meta.asBoolean()) {
return true;
}
}
return false;
}
public boolean isRandomLook() {
return enableRandomLook;
}
private boolean isValid(Player entity) {
return entity.isOnline() && entity.isValid() && entity.getWorld() == npc.getEntity().getWorld()
&& entity.getLocation(PLAYER_LOCATION).distanceSquared(NPC_LOCATION) < range * range
&& !isInvisible(entity);
}
@Override
public void load(DataKey key) {
range = key.getDouble("range");
}
/**
* Enables/disables the trait
*/
public void lookClose(boolean lookClose) {
enabled = lookClose;
}
@Override
public void onDespawn() {
NPCLookCloseChangeTargetEvent event = new NPCLookCloseChangeTargetEvent(npc, lookingAt, null);
Bukkit.getPluginManager().callEvent(event);
if (event.getNewTarget() != null && isValid(event.getNewTarget())) {
lookingAt = event.getNewTarget();
} else {
lookingAt = null;
}
}
private void randomLook() {
Random rand = new Random();
float pitch = isEqual(randomPitchRange) ? randomPitchRange[0]
: rand.doubles(randomPitchRange[0], randomPitchRange[1]).iterator().next().floatValue();
float yaw = isEqual(randomYawRange) ? randomYawRange[0]
: rand.doubles(randomYawRange[0], randomYawRange[1]).iterator().next().floatValue();
Util.face(npc.getEntity(), yaw, pitch);
}
@Override
public void run() {
if (!npc.isSpawned())
return;
if (enableRandomLook) {
if (!npc.getNavigator().isNavigating() && lookingAt == null && t <= 0) {
randomLook();
t = randomLookDelay;
} else {
t--;
}
}
if (!enabled)
return;
if (npc.getNavigator().isNavigating() && disableWhileNavigating())
return;
npc.getEntity().getLocation(NPC_LOCATION);
if (tryInvalidateTarget()) {
findNewTarget();
}
if (npc.getNavigator().isNavigating()) {
npc.getNavigator().setPaused(lookingAt != null);
}
if (lookingAt == null)
return;
Util.faceEntity(npc.getEntity(), lookingAt);
if (npc.getEntity().getType().name().equals("SHULKER")) {
boolean wasSilent = npc.getEntity().isSilent();
npc.getEntity().setSilent(true);
NMS.setPeekShulker(npc.getEntity(), 100 - 4 * (int) Math
.floor(npc.getStoredLocation().distanceSquared(lookingAt.getLocation(PLAYER_LOCATION))));
npc.getEntity().setSilent(wasSilent);
}
}
@Override
public void save(DataKey key) {
key.setDouble("range", range);
}
public void setDisableWhileNavigating(boolean set) {
disableWhileNavigating = set;
}
/**
* Enables random looking - will look at a random {@link Location} every so often if enabled.
*/
public void setRandomLook(boolean enableRandomLook) {
this.enableRandomLook = enableRandomLook;
}
/**
* Sets the delay between random looking in ticks
*/
public void setRandomLookDelay(int delay) {
this.randomLookDelay = delay;
}
public void setRandomLookPitchRange(float min, float max) {
this.randomPitchRange = new float[] { min, max };
}
public void setRandomLookYawRange(float min, float max) {
this.randomYawRange = new float[] { min, max };
}
/**
* Sets the maximum range in blocks to look at other Entities
*/
public void setRange(double d) {
this.range = d;
}
/**
* Enables/disables realistic looking (using line of sight checks). More computationally expensive.
*/
public void setRealisticLooking(boolean realistic) {
this.realisticLooking = realistic;
}
@Override
public boolean toggle() {
enabled = !enabled;
return enabled;
}
@Override
public String toString() {
return "LookClose{" + enabled + "}";
}
private boolean tryInvalidateTarget() {
if (lookingAt == null)
return true;
if (!isValid(lookingAt)) {
NPCLookCloseChangeTargetEvent event = new NPCLookCloseChangeTargetEvent(npc, lookingAt, null);
Bukkit.getPluginManager().callEvent(event);
if (event.getNewTarget() != null && isValid(event.getNewTarget())) {
lookingAt = event.getNewTarget();
} else {
lookingAt = null;
}
}
return lookingAt == null;
}
public boolean useRealisticLooking() {
return realisticLooking;
}
private static boolean isEqual(float[] array) {
return Math.abs(array[0] - array[1]) < 0.001;
}
private static final Location CACHE_LOCATION = new Location(null, 0, 0, 0);
private static final Location NPC_LOCATION = new Location(null, 0, 0, 0);
private static final Location PLAYER_LOCATION = new Location(null, 0, 0, 0);
}