This repository has been archived by the owner on Mar 30, 2018. It is now read-only.
/
SimpleSpleef.java
433 lines (372 loc) · 13.1 KB
/
SimpleSpleef.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
422
423
424
425
426
427
428
429
430
431
432
433
/**
* This file is part of the SimpleSpleef bukkit plugin.
* Copyright (C) 2011 Maximilian Kalus
* See http://dev.bukkit.org/server-mods/simple-spleef/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
package de.beimax.simplespleef;
import java.util.Map;
import java.util.logging.Logger;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import com.sk89q.worldedit.bukkit.WorldEditAPI;
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import de.beimax.simplespleef.command.SimpleSpleefCommandExecutor;
import de.beimax.simplespleef.command.SimpleSpleefCommandWhitelist;
import de.beimax.simplespleef.command.SimpleSpleefSignCommandExecutor;
import de.beimax.simplespleef.game.GameHandler;
import de.beimax.simplespleef.gamehelpers.OriginalPositionKeeper;
import de.beimax.simplespleef.listeners.PluginListener;
import de.beimax.simplespleef.statistics.StatisticsFactory;
import de.beimax.simplespleef.statistics.StatisticsModule;
import de.beimax.simplespleef.util.ConfigHelper;
import de.beimax.simplespleef.util.Translator;
import de.beimax.simplespleef.util.UpdateChecker;
/**
* @author mkalus
*
*/
public class SimpleSpleef extends JavaPlugin {
public static final Logger log = Logger.getLogger("Minecraft");
/**
* reference to Vault economy
*/
public static Economy economy = null;
/**
* reference to Vault permissions
*/
public static Permission permission = null;
/**
* checks permission, either Vault-based or using builtin system
* @param sender
* @return
*/
public static boolean checkPermission(CommandSender sender, String permission) {
if (SimpleSpleef.permission != null) { // use Vault to check permissions - actually does nothing different than sender.hasPermission at the moment, but might change
try {
return SimpleSpleef.permission.has(sender, permission);
} catch (NoSuchMethodError e) {
// if for some reason there is a problem with Vault, fall back to default permissions
SimpleSpleef.log.warning("[SimpleSpleef] Checking Vault permission threw an exception. Are you using the most recent version? Falling back to to default permission checking.");
}
}
// fallback to default Bukkit permission checking system
return sender.hasPermission(permission) || sender.hasPermission("simplespleef.*");
}
/**
* self reference to singleton
*/
private static SimpleSpleef simpleSpleef;
/**
* get instance
* @return
*/
public static SimpleSpleef getPlugin() {
return simpleSpleef;
}
/**
* reference to game handler
*/
private static GameHandler gameHandler;
/**
* @return the gameHandler
*/
public static GameHandler getGameHandler() {
return gameHandler;
}
/**
* reference to statistics handler
*/
private static StatisticsModule statisticsModule;
/**
* @return the statisticsModule
*/
public static StatisticsModule getStatisticsModule() {
return statisticsModule;
}
/**
* world edit API
*/
private static WorldEditAPI worldEditAPI = null;
/**
* sets the world edit API for this plugin
* @param worldEditAPI
*/
public static void setWorldEditAPI(WorldEditAPI worldEditAPI) {
SimpleSpleef.worldEditAPI = worldEditAPI;
}
/**
* return WorldEditAPI
* @return
*/
public static WorldEditAPI getWorldEditAPI() {
return SimpleSpleef.worldEditAPI;
}
/**
* keeper of original positions
*/
private static OriginalPositionKeeper originalPositionKeeper;
/**
* get originalPositionKeeper instance (singleton)
* @return
*/
public static OriginalPositionKeeper getOriginalPositionKeeper() {
if (originalPositionKeeper == null) originalPositionKeeper = new OriginalPositionKeeper();
return originalPositionKeeper;
}
/**
* Teleport a player to a location - fixes people falling through the floor
* @param player
* @param location
*/
public static void simpleSpleefTeleport(Player player, Location location) {
player.teleport(location.add(0, 0.25, 0), TeleportCause.PLUGIN); // add a quarter block to the height of the location to let the player "fall" onto the block
}
/**
* reference to command handler
*/
private SimpleSpleefCommandExecutor commandExecutor;
/**
* reference to translator
*/
private static Translator lang;
/**
* Called when enabling plugin
*/
@Override
public void onEnable() {
// initialize plugin
log.info(this.toString() + " is loading.");
// set reference
SimpleSpleef.simpleSpleef = this;
// configure plugin (configuration stuff)
configurePlugin();
// check updates, if turned on
checkForUpdate();
// create new handler
SimpleSpleef.gameHandler = new GameHandler();
SimpleSpleef.gameHandler.updateGameHandlerData();
// start tracking
SimpleSpleef.getPlugin().getServer().getScheduler().scheduleSyncRepeatingTask(this, gameHandler, 0L, 20L);
// register vault stuff
setupEconomy();
setupPermission();
// add event listeners
registerEvents();
// check for WorldEdit
checkForWorldEdit();
// create statistics handler
createStatisticsHander();
}
/**
* Called when disabling plugin
*/
public void onDisable() {
log.info(this.toString() + " is shutting down.");
// cancel all tasks for this plugin
SimpleSpleef.getPlugin().getServer().getScheduler().cancelTasks(this);
// clean memory
SimpleSpleef.worldEditAPI = null;
// stop game handler
SimpleSpleef.gameHandler = null;
SimpleSpleef.economy = null;
SimpleSpleef.originalPositionKeeper = null;
SimpleSpleef.lang = null;
SimpleSpleef.statisticsModule = null;
//TODO add if more stuff comes along
//save config to disk
//this.saveConfig();
// derefer self reference
SimpleSpleef.simpleSpleef = null;
}
/**
* Run some configuration stuff at the initialization of the plugin
*/
public void configurePlugin() {
// define that default config should be copied
this.getConfig().options().copyDefaults(true);
// check for the existance of plugin folder
if (!getDataFolder().exists()) {
getDataFolder().mkdir(); // create it!
}
// create config helper
ConfigHelper configHelper = new ConfigHelper();
// update defaults of existing arenas
configHelper.updateDefaults();
// update sample config, if needed
configHelper.updateSampleConfig();
// update language files
configHelper.updateLanguageFiles();
// initialize the translator
lang = new Translator(this, this.getConfig().getString("language", "en"));
}
/**
* reload configuration and translation
*/
public void reloadSimpleSpleefConfiguration() {
// reload the config file
this.reloadConfig();
// have the game loader reload, too
SimpleSpleef.gameHandler.updateGameHandlerData();
// re-initialize the translator
lang = new Translator(this, this.getConfig().getString("language", "en"));
}
/**
* Check for updates
*/
protected void checkForUpdate() {
// create update checker
final UpdateChecker checker = new UpdateChecker();
// possibly check for updates in the internet on startup
this.getServer().getScheduler().scheduleAsyncDelayedTask(this, new Runnable() {
public void run() {
try {
String newVersion = checker.checkForUpdate(SimpleSpleef.simpleSpleef.getDescription().getVersion());
if (newVersion != null)
log.info("[SimpleSpleef] Update found for SimpleSpleef - please go to http://dev.bukkit.org/server-mods/simple-spleef/ to download version " + newVersion + "!");
} catch (Exception e) {
log.warning("[SimpleSpleef] Could not connect to remote server to check for update. Exception said: " + e.getMessage());
}
}
}, 0L);
// also check for updates in the configuration files and update them, if needed
checker.updateConfigurationVersion(this);
}
/**
* Configure event listeners
*/
protected void registerEvents() {
// let my command handler take care of commands
this.commandExecutor = new SimpleSpleefCommandExecutor();
this.getCommand("spleef").setExecutor(commandExecutor);
// Prepare listeners
PluginManager pm = getServer().getPluginManager();
// add listener for other plugins
pm.registerEvents(new PluginListener(), this);
// Register our events
pm.registerEvents(gameHandler, this);
pm.registerEvents(new UpdateChecker(), this); // check updates
pm.registerEvents(new SimpleSpleefSignCommandExecutor(), this); // check sign actions
pm.registerEvents(new SimpleSpleefCommandWhitelist(), this); // check command whitelist
}
/**
* @param key of translation file
* @param replacers an even number of key/value pairs to replace key entries
* @return translated string
*/
public static String ll(String key, String... replacers) {
return lang.ll(key, replacers);
}
/**
* Get translation section list
* @param key
* @return
*/
public static Map<String, String> lls(String section) {
return lang.lls(section);
}
/**
* set up economy - see http://dev.bukkit.org/server-mods/vault/
* @return
*/
private boolean setupEconomy() {
if (this.getServer().getPluginManager().getPlugin("Vault") != null) {
RegisteredServiceProvider<Economy> economyProvider = getServer()
.getServicesManager().getRegistration(
net.milkbowl.vault.economy.Economy.class);
if (economyProvider != null) {
economy = economyProvider.getProvider();
}
SimpleSpleef.log.info("[SimpleSpleef] Vault hooked as economy plugin.");
return (economy != null);
}
economy = null; // if the plugin is reloaded during play, possibly kill economy
SimpleSpleef.log.info("[SimpleSpleef] Vault plugin not found - not using economy.");
return false;
}
/**
* set up permissions - see http://dev.bukkit.org/server-mods/vault/
* @return
*/
private boolean setupPermission() {
if (this.getServer().getPluginManager().getPlugin("Vault") != null) {
RegisteredServiceProvider<Permission> permissionProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.permission.Permission.class);
if (permissionProvider != null) {
permission = permissionProvider.getProvider();
}
SimpleSpleef.log.info("[SimpleSpleef] Vault hooked as permission plugin.");
return (permission != null);
}
permission = null; // if the plugin is reloaded during play, possibly kill permissions
SimpleSpleef.log.info("[SimpleSpleef] Vault plugin not found - defaulting to Bukkit permission system.");
return false;
}
/**
* make sure worldEdit is loaded - inspired by MultiversePortals
*/
private void checkForWorldEdit() {
Plugin plugin = this.getServer().getPluginManager().getPlugin("WorldEdit");
if (plugin != null) {
// check version
try {
double version = Double.parseDouble(plugin.getDescription().getVersion());
if (version < 5.0) {
SimpleSpleef.log
.warning("[SimpleSpleef] Found WorldEdit, but must be at least version 5.0 to work! Ignoring it.");
return;
}
} catch (Exception e) {} // no number, should be fine...
SimpleSpleef.setWorldEditAPI(new WorldEditAPI((WorldEditPlugin) plugin));
SimpleSpleef.log
.info("[SimpleSpleef] Found WorldEdit " + plugin.getDescription().getVersion() + ". Using it for selections.");
}
}
/**
* get a world guard instance if it exists - see http://wiki.sk89q.com/wiki/WorldGuard/Regions/API for source
* @return
*/
public WorldGuardPlugin getWorldGuard() {
Plugin plugin = getServer().getPluginManager().getPlugin("WorldGuard");
// WorldGuard may not be loaded
if (plugin == null || !(plugin instanceof WorldGuardPlugin)) {
return null; // Maybe you want throw an exception instead
}
return (WorldGuardPlugin) plugin;
}
/**
* create new statistics handler instance
*/
private void createStatisticsHander() {
if (getConfig().getBoolean("settings.enableStatistics", true))
try {
StatisticsFactory statisticsHandler = new StatisticsFactory();
statisticsModule = statisticsHandler.getStatisticsModuleByName(getConfig().getString("settings.statisticsModule", "file"),
getConfig().getConfigurationSection("settings.statisticsSettings"));
} catch (Exception e) {
SimpleSpleef.log.warning("[SimpleSpleef] Could not initiate statistics module - settings bugged somehow or database error. Disabling statistics for now.");
statisticsModule = null;
}
else statisticsModule = null;
}
}