-
Notifications
You must be signed in to change notification settings - Fork 17
/
LWCPlugin.java
426 lines (359 loc) · 13.4 KB
/
LWCPlugin.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
/**
* This file is part of LWC (https://github.com/Hidendra/LWC)
*
* 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 com.griefcraft.lwc;
import com.griefcraft.listeners.LWCBlockListener;
import com.griefcraft.listeners.LWCEntityListener;
import com.griefcraft.listeners.LWCPlayerListener;
import com.griefcraft.listeners.LWCServerListener;
import com.griefcraft.scripting.event.LWCCommandEvent;
import com.griefcraft.sql.Database;
import com.griefcraft.util.Colors;
import com.griefcraft.util.LWCResourceBundle;
import com.griefcraft.util.LocaleClassLoader;
import com.griefcraft.util.StringUtils;
import com.griefcraft.util.UTF8Control;
import com.griefcraft.util.Updater;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Event.Priority;
import org.bukkit.event.Event.Type;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockListener;
import org.bukkit.event.entity.EntityListener;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.event.server.ServerListener;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.logging.Logger;
public class LWCPlugin extends JavaPlugin {
/**
* The block listener
*/
private BlockListener blockListener;
/**
* The entity listener
*/
private EntityListener entityListener;
/**
* The player listener
*/
private PlayerListener playerListener;
/**
* The server listener
*/
private ServerListener serverListener;
/**
* The locale for LWC
*/
private LWCResourceBundle locale;
/**
* The logging object
*/
private Logger logger = Logger.getLogger("LWC");
/**
* The LWC instance
*/
private LWC lwc;
/**
* LWC updater
*/
private Updater updater;
public LWCPlugin() {
log("Loading shared objects");
updater = new Updater();
lwc = new LWC(this);
playerListener = new LWCPlayerListener(this);
blockListener = new LWCBlockListener(this);
entityListener = new LWCEntityListener(this);
serverListener = new LWCServerListener(this);
/*
* Set the SQLite native library path
*/
System.setProperty("org.sqlite.lib.path", updater.getOSSpecificFolder());
// we want to force people who used sqlite.purejava before to switch:
System.setProperty("sqlite.purejava", "");
// BUT, some can't use native, so we need to give them the option to use
// pure:
String isPureJava = System.getProperty("lwc.purejava");
if (isPureJava != null && isPureJava.equalsIgnoreCase("true")) {
System.setProperty("sqlite.purejava", "true");
}
log("Native library: " + updater.getFullNativeLibraryPath());
}
/**
* @return the locale
*/
public ResourceBundle getLocale() {
return locale;
}
/**
* @return the LWC instance
*/
public LWC getLWC() {
return lwc;
}
/**
* @return the Updater instance
*/
public Updater getUpdater() {
return updater;
}
/**
* Verify a command name
*
* @param name
* @return
*/
public boolean isValidCommand(String name) {
name = name.toLowerCase();
if (name.equals("lwc")) {
return true;
} else if (name.equals("cpublic")) {
return true;
} else if (name.equals("cpassword")) {
return true;
} else if (name.equals("cprivate")) {
return true;
} else if (name.equals("cinfo")) {
return true;
} else if (name.equals("cmodify")) {
return true;
} else if (name.equals("cunlock")) {
return true;
} else if (name.equals("cremove")) {
return true;
} else if (name.equals("climits")) {
return true;
} else if (name.equals("credstone")) {
return true;
} else if (name.equals("cmagnet")) {
return true;
} else if (name.equals("cdroptransfer")) {
return true;
} else if (name.equals("cpersist")) {
return true;
} else if (name.equals("cnospam")) {
return true;
} else if (name.equals("cexempt")) {
return true;
} else return name.equals("cadmin");
}
/**
* Load the database
*/
public void loadDatabase() {
String database = lwc.getConfiguration().getString("database.adapter");
if (database.equalsIgnoreCase("mysql")) {
Database.DefaultType = Database.Type.MySQL;
} else {
Database.DefaultType = Database.Type.SQLite;
}
}
@Override
public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
String commandName = command.getName().toLowerCase();
String argString = StringUtils.join(args, 0);
boolean isPlayer = (sender instanceof Player); // check if they're a player
if (!isValidCommand(commandName)) {
return false;
}
// these can only apply to players, not the console (who has absolute player :P)
if (isPlayer) {
// Aliases
String aliasCommand = null;
String[] aliasArgs = new String[0];
if (commandName.equals("cpublic")) {
aliasCommand = "create";
aliasArgs = new String[]{"public"};
} else if (commandName.equals("cpassword")) {
aliasCommand = "create";
aliasArgs = ("password " + argString).split(" ");
} else if (commandName.equals("cprivate")) {
aliasCommand = "create";
aliasArgs = ("private " + argString).split(" ");
} else if (commandName.equals("cmodify")) {
aliasCommand = "modify";
aliasArgs = argString.isEmpty() ? new String[0] : argString.split(" ");
} else if (commandName.equals("cinfo")) {
aliasCommand = "info";
} else if (commandName.equals("cunlock")) {
aliasCommand = "unlock";
aliasArgs = argString.isEmpty() ? new String[0] : argString.split(" ");
} else if (commandName.equals("cremove")) {
aliasCommand = "remove";
aliasArgs = new String[]{"protection"};
} else if (commandName.equals("climits")) {
aliasCommand = "limits";
aliasArgs = argString.isEmpty() ? new String[0] : argString.split(" ");
} else if (commandName.equals("cadmin")) {
aliasCommand = "admin";
aliasArgs = argString.isEmpty() ? new String[0] : argString.split(" ");
}
// Flag aliases
if (commandName.equals("credstone")) {
aliasCommand = "flag";
aliasArgs = ("redstone " + argString).split(" ");
} else if (commandName.equals("cmagnet")) {
aliasCommand = "flag";
aliasArgs = ("magnet " + argString).split(" ");
} else if (commandName.equals("cexempt")) {
aliasCommand = "flag";
aliasArgs = ("exemption " + argString).split(" ");
}
// Mode aliases
if (commandName.equals("cdroptransfer")) {
aliasCommand = "mode";
aliasArgs = ("droptransfer " + argString).split(" ");
} else if (commandName.equals("cpersist")) {
aliasCommand = "mode";
aliasArgs = ("persist " + argString).split(" ");
} else if (commandName.equals("cnospam")) {
aliasCommand = "mode";
aliasArgs = ("nospam " + argString).split(" ");
}
if (aliasCommand != null) {
lwc.getModuleLoader().dispatchEvent(new LWCCommandEvent(sender, aliasCommand, aliasArgs));
return true;
}
}
if (args.length == 0) {
lwc.sendFullHelp(sender);
return true;
}
///// Dispatch command to modules
LWCCommandEvent evt = new LWCCommandEvent(sender, args[0].toLowerCase(), args.length > 1 ? StringUtils.join(args, 1).split(" ") : new String[0]);
lwc.getModuleLoader().dispatchEvent(evt);
if (evt.isCancelled()) {
return true;
}
if (!isPlayer) {
sender.sendMessage(Colors.Red + "That LWC command is not supported through the console :-)");
return true;
}
return false;
}
public void onDisable() {
LWC.ENABLED = false;
if (lwc != null) {
lwc.destruct();
}
// cancel all tasks we created
getServer().getScheduler().cancelTasks(this);
}
public void onEnable() {
String version = getDescription().getVersion();
LWCInfo.setVersion(version);
LWC.ENABLED = true;
String localization = lwc.getConfiguration().getString("core.locale");
try {
ResourceBundle defaultBundle = null;
ResourceBundle optionalBundle = null;
// load the default locale first
defaultBundle = ResourceBundle.getBundle("lang.lwc", new Locale("en"), new UTF8Control());
// and now check if a bundled locale the same as the server's locale exists
try {
optionalBundle = ResourceBundle.getBundle("lang.lwc", new Locale(localization), new UTF8Control());
} catch (MissingResourceException e) {
}
// ensure both bundles aren't the same
if (defaultBundle == optionalBundle) {
optionalBundle = null;
}
locale = new LWCResourceBundle(defaultBundle);
if (optionalBundle != null) {
locale.addExtensionBundle(optionalBundle);
}
} catch (MissingResourceException e) {
log("We are missing the default locale in LWC.jar.. What happened to it? :-(");
throw e;
}
// located in plugins/LWC/locale/, values in that overrides the ones in the default :-)
ResourceBundle optionalBundle = null;
try {
optionalBundle = ResourceBundle.getBundle("lwc", new Locale(localization), new LocaleClassLoader(), new UTF8Control());
} catch (MissingResourceException e) {
}
if (optionalBundle != null) {
locale.addExtensionBundle(optionalBundle);
log("Loaded override bundle: " + optionalBundle.getLocale().toString());
}
int overrides = optionalBundle != null ? optionalBundle.keySet().size() : 0;
log("Loaded " + locale.keySet().size() + " locale strings (" + overrides + " overrides)");
loadDatabase();
try {
registerEvents();
} catch(NoSuchFieldError e) { }
// Load the rest of LWC
lwc.load();
// let the updater do its thang
updater.init();
log("At version: " + LWCInfo.FULL_VERSION);
}
/**
* Log a string to the console
*
* @param str
*/
private void log(String str) {
logger.info("LWC: " + str);
}
/**
* Register a hook with default priority
*
* @param listener
* @param eventType
*/
private void registerEvent(Listener listener, Type eventType) {
registerEvent(listener, eventType, Priority.Highest);
}
/**
* Register a hook
*
* @param listener
* @param eventType
* @param priority
*/
private void registerEvent(Listener listener, Type eventType, Priority priority) {
// logger.info("-> " + eventType.toString());
getServer().getPluginManager().registerEvent(eventType, listener, priority, this);
}
/**
* Register all of the events used by LWC
*/
private void registerEvents() {
/* Player events */
registerEvent(playerListener, Type.PLAYER_QUIT, Priority.Monitor);
registerEvent(playerListener, Type.PLAYER_DROP_ITEM);
registerEvent(playerListener, Type.PLAYER_INTERACT);
registerEvent(playerListener, Type.PLAYER_CHAT);
/* Entity events */
registerEvent(entityListener, Type.ENTITY_EXPLODE);
/* Block events */
registerEvent(blockListener, Type.BLOCK_BREAK);
registerEvent(blockListener, Type.BLOCK_PLACE);
registerEvent(blockListener, Type.REDSTONE_CHANGE);
registerEvent(blockListener, Type.SIGN_CHANGE);
/* Server events */
registerEvent(serverListener, Type.PLUGIN_DISABLE);
// post-1.7 event
registerEvent(blockListener, Type.BLOCK_PISTON_EXTEND);
registerEvent(blockListener, Type.BLOCK_PISTON_RETRACT);
}
}