-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
SentinelPlugin.java
384 lines (350 loc) · 14.1 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
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.configuration.file.YamlConfiguration;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.mcmonkey.sentinel.integration.*;
import org.mcmonkey.sentinel.metrics.BStatsMetricsLite;
import org.mcmonkey.sentinel.metrics.MetricsLite;
import org.mcmonkey.sentinel.metrics.StatsRecord;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
/**
* The main Sentinel plugin class.
*/
public class SentinelPlugin extends JavaPlugin implements Listener {
/**
* 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<>();
/**
* Current plugin instance.
*/
public static SentinelPlugin instance;
/**
* 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, guardDistanceMargin;
/**
* 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;
/**
* 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;
/**
* Expected configuration file version.
*/
public final static int CONFIG_VERSION;
static {
for (EntityType type : EntityType.values()) {
entityToTargets.put(type, new HashSet<>());
}
int confVer;
try {
InputStream inputConfigStream = SentinelPlugin.class.getResourceAsStream("/config.yml");
InputStreamReader inputConfigReader = new InputStreamReader(inputConfigStream);
YamlConfiguration config = YamlConfiguration.loadConfiguration(inputConfigReader);
inputConfigReader.close();
inputConfigStream.close();
confVer = config.getInt("config version");
}
catch (Throwable ex) {
ex.printStackTrace();
confVer = -1;
}
CONFIG_VERSION = confVer;
}
/**
* All current integrations available to Sentinel.
*/
public final static ArrayList<SentinelIntegration> integrations = new ArrayList<>();
/**
* 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();
int confVer = getConfig().getInt("config version", -1);
if (confVer != CONFIG_VERSION) {
if (CONFIG_VERSION == -1) {
getLogger().warning("Default config data could not be found. May be a jar file issue?");
}
else if (confVer == -1) {
getLogger().warning("Config data could not be found. May be an issue with your server plugins folder? (Check file access permissions).");
}
else {
getLogger().warning("Outdated or invalid Sentinel config - please delete it to regenerate it (keep a backup copy of the original)!"
+ " Expected version " + CONFIG_VERSION + " but you have " + confVer + ".");
}
}
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);
spectralSound = Sound.valueOf(getConfig().getString("random.spectral sound", "ENTITY_VILLAGER_YES"));
ignoreInvisible = getConfig().getBoolean("random.ignore invisible targets");
guardDistanceMinimum = getConfig().getInt("random.guard follow distance.minimum", 7);
guardDistanceMargin = getConfig().getInt("random.guard follow distance.selction range", 4);
guardDistanceSelectionRange = getConfig().getInt("random.guard follow distance.margin", 2);
workaroundEntityChasePathfinder = getConfig().getBoolean("random.workaround entity chase pathfinder", false);
protectFromIgnores = getConfig().getBoolean("random.protected", false);
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);
for (String target : new HashSet<>(sentinel.targets)) {
sentinel.targets.add(SentinelTarget.forName(target).name());
}
for (String target : new HashSet<>(sentinel.ignores)) {
sentinel.ignores.add(SentinelTarget.forName(target).name());
}
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);
}
}
}
}
};
maxHealth = getConfig().getDouble("random.max health", 2000);
postLoad.runTaskLater(this, 40);
tickRate = getConfig().getInt("update rate", 10);
getLogger().info("Sentinel loaded!");
getServer().getPluginManager().registerEvents(this, 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();
integrations.add(new SentinelHealth());
integrations.add(new SentinelPermissions());
integrations.add(new SentinelSBTeams());
integrations.add(new SentinelSquads());
if (Bukkit.getPluginManager().getPlugin("Towny") != null) {
try {
integrations.add(new SentinelTowny());
getLogger().info("Sentinel found Towny! Adding support for it!");
}
catch (Throwable ex) {
ex.printStackTrace();
}
}
if (Bukkit.getPluginManager().getPlugin("Factions") != null) {
try {
integrations.add(new SentinelFactions());
getLogger().info("Sentinel found Factions! Adding support for it!");
}
catch (Throwable ex) {
ex.printStackTrace();
}
}
if (Bukkit.getPluginManager().getPlugin("CrackShot") != null) {
try {
integrations.add(new SentinelCrackShot());
getLogger().info("Sentinel found CrackShot! Adding support for it!");
}
catch (Throwable ex) {
ex.printStackTrace();
}
}
if (Bukkit.getPluginManager().getPlugin("SimpleClans") != null) {
try {
integrations.add(new SentinelSimpleClans());
getLogger().info("Sentinel found SimpleClans! Adding support for it!");
}
catch (Throwable ex) {
ex.printStackTrace();
}
}
}
/**
* Prefix string for an inventory title.
*/
final static String InvPrefix = ChatColor.GREEN + "Sentinel ";
/**
* Called when an inventory is closed.
*/
@EventHandler
public void onInvClose(InventoryCloseEvent event) {
if (event.getInventory().getTitle().startsWith(InvPrefix)) {
int id = Integer.parseInt(event.getInventory().getTitle().substring(InvPrefix.length()));
NPC npc = CitizensAPI.getNPCRegistry().getById(id);
if (npc != null && npc.hasTrait(SentinelTrait.class)) {
ArrayList<ItemStack> its = npc.getTrait(SentinelTrait.class).drops;
its.clear();
for (ItemStack it : event.getInventory().getContents()) {
if (it != null && it.getType() != Material.AIR) {
its.add(it);
}
}
}
}
}
/**
* 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();
}
}