-
Notifications
You must be signed in to change notification settings - Fork 17
/
LWCPlugin.java
361 lines (309 loc) · 12.2 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
/*
* Copyright 2011 Tyler Blair. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and contributors and should not be interpreted as representing official policies,
* either expressed or implied, of anybody else.
*/
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.StringUtil;
import com.griefcraft.util.Updater;
import com.griefcraft.util.locale.LWCResourceBundle;
import com.griefcraft.util.locale.LocaleClassLoader;
import com.griefcraft.util.locale.UTF8Control;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.jar.JarFile;
public class LWCPlugin extends JavaPlugin {
/**
* The LWC instance
*/
private LWC lwc;
/**
* The block listener
*/
private LWCBlockListener blockListener;
/**
* The entity listener
*/
private LWCEntityListener entityListener;
/**
* The player listener
*/
private LWCPlayerListener playerListener;
/**
* The server listener
*/
private LWCServerListener serverListener;
/**
* The message parser to parse messages with
*/
private MessageParser messageParser;
/**
* LWC updater
*/
private Updater updater;
@Override
public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
String commandName = command.getName().toLowerCase();
String argString = StringUtil.join(args, 0);
boolean isPlayer = (sender instanceof Player); // check if they're a player
// 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") || commandName.equals("lock")) {
aliasCommand = "create";
aliasArgs = ("private " + argString).split(" ");
} else if (commandName.equals("cdonation")) {
aliasCommand = "create";
aliasArgs = ("donation " + 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") || commandName.equals("unlock")) {
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(" ");
} else if (commandName.equals("cremoveall")) {
aliasCommand = "remove";
aliasArgs = new String[]{"allprotections"};
}
// 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(" ");
} else if (commandName.equals("cautoclose")) {
aliasCommand = "flag";
aliasArgs = ("autoclose " + argString).split(" ");
} else if (commandName.equals("callowexplosions") || commandName.equals("ctnt")) {
aliasCommand = "flag";
aliasArgs = ("allowexplosions " + 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 ? StringUtil.join(args, 1).split(" ") : new String[0]);
lwc.getModuleLoader().dispatchEvent(evt);
if (evt.isCancelled()) {
return true;
}
if (!isPlayer) {
lwc.sendLocale(sender, "lwc.commandnotsupported");
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() {
preload();
lwc = new LWC(this);
LWCInfo.setVersion(getDescription().getVersion());
LWC.ENABLED = true;
loadLocales();
loadDatabase();
try {
registerEvents();
} catch (NoSuchFieldError e) {
}
// Load the rest of LWC
lwc.load();
}
/**
* 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;
}
}
/**
* Load LWC localizations
*/
public void loadLocales() {
LWCResourceBundle locale;
String localization = getCurrentLocale();
// located in plugins/LWC/locale/, values in that overrides the ones in the default :-)
ResourceBundle optionalBundle = null;
try {
ResourceBundle defaultBundle;
// Open the LWC jar file
JarFile file = new JarFile(getFile());
// Attempt to load the default locale
defaultBundle = new PropertyResourceBundle(new InputStreamReader(file.getInputStream(file.getJarEntry("lang/lwc_en.properties")), "UTF-8"));
locale = new LWCResourceBundle(defaultBundle);
try {
optionalBundle = ResourceBundle.getBundle("lwc", new Locale(localization), new LocaleClassLoader(), new UTF8Control());
} catch (MissingResourceException e) {
}
if (optionalBundle != null) {
locale.addExtensionBundle(optionalBundle);
}
// and now check if a bundled locale the same as the server's locale exists
try {
optionalBundle = new PropertyResourceBundle(new InputStreamReader(file.getInputStream(file.getJarEntry("lang/lwc_" + localization + ".properties")), "UTF-8"));
} catch (MissingResourceException e) {
} catch (NullPointerException e) {
// file wasn't found :p - that's ok
}
// ensure both bundles aren't the same
if (defaultBundle == optionalBundle) {
optionalBundle = null;
}
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;
} catch (IOException e) {
log("Uh-oh: " + e.getMessage());
return;
}
// create the message parser
messageParser = new SimpleMessageParser(locale);
}
/**
* Load shared libraries and other misc things
*/
private void preload() {
updater = new Updater();
// Set the SQLite native library path
System.setProperty("org.sqlite.lib.path", updater.getOSSpecificFolder());
}
/**
* Log a string to the console
*
* @param str
*/
private void log(String str) {
getLogger().info(str);
}
/**
* Register all of the events used by LWC
*/
private void registerEvents() {
PluginManager pluginManager = Bukkit.getServer().getPluginManager();
pluginManager.registerEvents(playerListener = new LWCPlayerListener(this), this);
pluginManager.registerEvents(entityListener = new LWCEntityListener(this), this);
pluginManager.registerEvents(blockListener = new LWCBlockListener(this), this);
pluginManager.registerEvents(serverListener = new LWCServerListener(this), this);
}
/**
* @return the current locale in use
*/
public String getCurrentLocale() {
return lwc.getConfiguration().getString("core.locale", "en");
}
/**
* @return the LWC instance
*/
public LWC getLWC() {
return lwc;
}
/**
* Gets the message parser
* @return
*/
public MessageParser getMessageParser() {
return messageParser;
}
/**
* @return the Updater instance
*/
public Updater getUpdater() {
return updater;
}
@Override
public File getFile() {
return super.getFile();
}
}