-
-
Notifications
You must be signed in to change notification settings - Fork 87
/
SentinelPlugin.java
421 lines (381 loc) · 15 KB
/
SentinelPlugin.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package org.mcmonkey.sentinel;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.trait.TraitInfo;
import net.citizensnpcs.api.trait.trait.Owner;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.*;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.EntityType;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.mcmonkey.sentinel.commands.SentinelCommand;
import org.mcmonkey.sentinel.integration.*;
import org.mcmonkey.sentinel.metrics.BStatsMetricsLite;
import org.mcmonkey.sentinel.metrics.MetricsLite;
import org.mcmonkey.sentinel.metrics.StatsRecord;
import org.mcmonkey.sentinel.targeting.SentinelTarget;
import org.mcmonkey.sentinel.utilities.ConfigUpdater;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
/**
* The main Sentinel plugin class.
*/
public class SentinelPlugin extends JavaPlugin {
/**
* A map of of all valid event targets.
*/
public static HashSet<String> validEventTargets = new HashSet<>(
Arrays.asList("pvp", "pve", "pv", "pvnpc", "pvsentinel", "guarded_fight", "eve", "ev", "message")
);
/**
* A map of typeable target names to valid targets.
*/
public static HashMap<String, SentinelTarget> targetOptions = new HashMap<>();
/**
* A map of entity types to target types.
*/
public static HashMap<EntityType, HashSet<SentinelTarget>> entityToTargets = new HashMap<>();
/**
* A map of target prefixes to the integration object.
*/
public final static HashMap<String, SentinelIntegration> integrationPrefixMap = new HashMap<>();
/**
* All current integrations available to Sentinel.
*/
public final static ArrayList<SentinelIntegration> integrations = new ArrayList<>();
/**
* Current plugin instance.
*/
public static SentinelPlugin instance;
/**
* A list of all currently spawned Sentinel NPCs.
*/
public ArrayList<SentinelTrait> currentSentinelNPCs = new ArrayList<>();
/**
* Cleans and returns the current Sentinel NPC list.
*/
public ArrayList<SentinelTrait> cleanCurrentList() {
ArrayList<SentinelTrait> npcs = SentinelPlugin.instance.currentSentinelNPCs;
for (int i = 0; i < npcs.size(); i++) {
if (!npcs.get(i).validateOnList()) {
i--;
}
}
return npcs;
}
/**
* Permissions handler.
*/
public Permission vaultPerms;
/**
* Configuration option: maximum health value any NPC can ever have.
*/
public double maxHealth;
/**
* Configuration option: maximum duration (in ticks) an NPC can know where a hidden target is.
*/
public int cleverTicks;
/**
* Configuration option: whether the skull weapon is allowed.
*/
public boolean canUseSkull;
/**
* Configuration option: whether to block some events that may cause other plugins to have issues.
*/
public boolean blockEvents;
/**
* Configuration option: whether to use an alternative (work-around) method of applying damage.
*/
public boolean alternateDamage;
/**
* Configuration option: whether to work-around damage-giving issues.
*/
public boolean workaroundDamage;
/**
* Configuration option: minimum arrow shooting speed.
*/
public double minShootSpeed;
/**
* Configuration option: whether to work-around potential NPC item drop issues.
*/
public boolean workaroundDrops;
/**
* Configuration option: whether to enable NPC death messages.
*/
public boolean deathMessages;
/**
* Configuration option: the sound to play when using the Spectral attack.
*/
public Sound spectralSound;
/**
* Configuration option: whether to ignore invisible targets.
*/
public boolean ignoreInvisible;
/**
* Configuration option: guarding distance values.
*/
public int guardDistanceMinimum, guardDistanceSelectionRange;
/**
* Configuration option: whether to work-around a pathfinder issue.
*/
public boolean workaroundEntityChasePathfinder;
/**
* Configuration option: whether to protect the NPC from being harmed by ignored entities.
*/
public boolean protectFromIgnores;
/**
* Configuration option: standard tick-rate for NPC updates.
*/
public int tickRate = 10;
/**
* Configuration option: time to keep running away for.
*/
public int runAwayTime;
/**
* Configuration option: whether block players from damaging their own guards.
*/
public boolean noGuardDamage;
/**
* Configuration option: time until arrow cleanup.
*/
public int arrowCleanupTime;
/**
* Fills the {@code vaultPerms} object if possible.
*/
public void tryGetPerms() {
if (Bukkit.getServer().getPluginManager().getPlugin("Vault") == null) {
return;
}
try {
RegisteredServiceProvider<Permission> rsp = Bukkit.getServer().getServicesManager().getRegistration(Permission.class);
vaultPerms = rsp.getProvider();
getLogger().info("Vault linked! Group targets will work.");
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* Whether debugging is enabled.
*/
public static boolean debugMe = false;
static {
for (EntityType type : EntityType.values()) {
entityToTargets.put(type, new HashSet<>());
}
}
/**
* Registers a new integration to Sentinel.
*/
public void registerIntegration(SentinelIntegration integration) {
integrations.add(integration);
for (String prefix : integration.getTargetPrefixes()) {
integrationPrefixMap.put(prefix, integration);
}
}
/**
* Reloads the config and updates settings fields accordingly.
*/
public void loadConfigSettings() {
reloadConfig();
cleverTicks = getConfig().getInt("random.clever ticks", 10);
canUseSkull = getConfig().getBoolean("random.skull allowed", true);
blockEvents = getConfig().getBoolean("random.workaround bukkit events", false);
alternateDamage = getConfig().getBoolean("random.enforce damage", false);
workaroundDamage = getConfig().getBoolean("random.workaround damage", false);
minShootSpeed = getConfig().getDouble("random.shoot speed minimum", 20);
workaroundDrops = getConfig().getBoolean("random.workaround drops", false) || blockEvents;
deathMessages = getConfig().getBoolean("random.death messages", true);
try {
spectralSound = Sound.valueOf(getConfig().getString("random.spectral sound", "ENTITY_VILLAGER_YES"));
}
catch (Throwable e) {
getLogger().warning("Sentinel Configuration value 'random.spectral sound' is set to an invalid sound name. This is usually an ignorable issue.");
}
ignoreInvisible = getConfig().getBoolean("random.ignore invisible targets");
guardDistanceMinimum = getConfig().getInt("random.guard follow distance.minimum", 7);
guardDistanceSelectionRange = getConfig().getInt("random.guard follow distance.selection range", 4);
workaroundEntityChasePathfinder = getConfig().getBoolean("random.workaround entity chase pathfinder", false);
protectFromIgnores = getConfig().getBoolean("random.protected", false);
runAwayTime = getConfig().getInt("random.run away time");
maxHealth = getConfig().getDouble("random.max health", 2000);
noGuardDamage = getConfig().getBoolean("random.no guard damage", true);
arrowCleanupTime = getConfig().getInt("random.arrow cleanup time", 200);
tickRate = getConfig().getInt("update rate", 10);
}
/**
* Called when the plugin is enabled at server startup.
*/
@Override
public void onEnable() {
getLogger().info("Sentinel loading...");
instance = this;
CitizensAPI.getTraitFactory().registerTrait(TraitInfo.create(SentinelTrait.class).withName("sentinel"));
saveDefaultConfig();
try {
// Automatic config file update
InputStream properConfig = SentinelPlugin.class.getResourceAsStream("/config.yml");
String properConfigString = SentinelUtilities.streamToString(properConfig);
properConfig.close();
FileInputStream currentConfig = new FileInputStream(getDataFolder() + "/config.yml");
String currentConfigString = SentinelUtilities.streamToString(currentConfig);
currentConfig.close();
String updated = ConfigUpdater.updateConfig(currentConfigString, properConfigString);
if (updated != null) {
getLogger().info("Your config file is outdated. Automatically updating it...");
FileOutputStream configOutput = new FileOutputStream(getDataFolder() + "/config.yml");
OutputStreamWriter writer = new OutputStreamWriter(configOutput);
writer.write(updated);
writer.close();
configOutput.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
loadConfigSettings();
BukkitRunnable postLoad = new BukkitRunnable() {
@Override
public void run() {
for (NPC npc : CitizensAPI.getNPCRegistry()) {
if (!npc.isSpawned() && npc.hasTrait(SentinelTrait.class)) {
SentinelTrait sentinel = npc.getTrait(SentinelTrait.class);
if (sentinel.respawnTime > 0) {
if (sentinel.spawnPoint == null && npc.getStoredLocation() == null) {
getLogger().warning("NPC " + npc.getId() + " has a null spawn point and can't be spawned. Perhaps the world was deleted?");
continue;
}
npc.spawn(sentinel.spawnPoint == null ? npc.getStoredLocation() : sentinel.spawnPoint);
}
}
}
}
};
postLoad.runTaskLater(this, 40);
getLogger().info("Sentinel loaded!");
SentinelCommand.buildCommandHandler();
Bukkit.getPluginManager().registerEvents(new SentinelEventHandler(), this);
// mcstats.org
try {
MetricsLite metrics = new MetricsLite(this);
metrics.start();
}
catch (Throwable e) {
e.printStackTrace();
}
// bstats.org
try {
BStatsMetricsLite metrics = new BStatsMetricsLite(this);
}
catch (Throwable e) {
e.printStackTrace();
}
// neo.mcmonkey.org
new BukkitRunnable() {
@Override
public void run() {
if (!getConfig().getBoolean("stats_opt_out", false)) {
new StatsRecord().start();
}
}
}.runTaskTimer(this, 100, 20 * 60 * 60);
tryGetPerms();
registerIntegration(new SentinelHealth());
registerIntegration(new SentinelPermissions());
registerIntegration(new SentinelSBTeams());
registerIntegration(new SentinelSquads());
registerIntegration(new SentinelUUID());
if (Bukkit.getPluginManager().getPlugin("Towny") != null) {
try {
registerIntegration(new SentinelTowny());
getLogger().info("Sentinel found Towny! Adding support for it!");
}
catch (Throwable ex) {
ex.printStackTrace();
}
}
if (Bukkit.getPluginManager().getPlugin("Factions") != null) {
try {
registerIntegration(new SentinelFactions());
getLogger().info("Sentinel found Factions! Adding support for it!");
}
catch (Throwable ex) {
ex.printStackTrace();
}
}
if (Bukkit.getPluginManager().getPlugin("CrackShot") != null) {
try {
registerIntegration(new SentinelCrackShot());
getLogger().info("Sentinel found CrackShot! Adding support for it!");
}
catch (Throwable ex) {
ex.printStackTrace();
}
}
if (Bukkit.getPluginManager().getPlugin("SimpleClans") != null) {
try {
registerIntegration(new SentinelSimpleClans());
getLogger().info("Sentinel found SimpleClans! Adding support for it!");
}
catch (Throwable ex) {
ex.printStackTrace();
}
}
if (Bukkit.getPluginManager().getPlugin("War") != null) {
try {
registerIntegration(new SentinelWar());
getLogger().info("Sentinel found War! Adding support for it!");
}
catch (Throwable ex) {
ex.printStackTrace();
}
}
}
/**
* Called when the plugin is disabled at server shutdown.
*/
@Override
public void onDisable() {
getLogger().info("Sentinel unloading...");
getLogger().info("Sentinel unloaded!");
}
/**
* Gets the Sentinel Trait instance for a given command sender (based on their selected NPC).
*/
public SentinelTrait getSentinelFor(CommandSender sender) {
NPC npc = CitizensAPI.getDefaultNPCSelector().getSelected(sender);
if (npc == null) {
return null;
}
if (npc.hasTrait(SentinelTrait.class)) {
return npc.getTrait(SentinelTrait.class);
}
return null;
}
/**
* Handles a command given by a player or the server.
*/
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
return SentinelCommand.onCommand(this, sender, command, label, args);
}
/**
* Gets the owner identity of an NPC for output (player name or "server").
*/
public String getOwner(NPC npc) {
if (npc.getTrait(Owner.class).getOwnerId() == null) {
return npc.getTrait(Owner.class).getOwner();
}
OfflinePlayer player = Bukkit.getOfflinePlayer(npc.getTrait(Owner.class).getOwnerId());
if (player == null) {
return "Server/Unknown";
}
return player.getName();
}
}