-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathPythonPlugin.java
More file actions
436 lines (374 loc) · 13.2 KB
/
PythonPlugin.java
File metadata and controls
436 lines (374 loc) · 13.2 KB
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
434
435
436
/**
*
*/
package net.lahwran.bukkit.jython;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.Server;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.generator.ChunkGenerator;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginLoader;
import org.bukkit.plugin.PluginLogger;
import org.bukkit.plugin.java.JavaPlugin;
import org.python.util.PythonInterpreter;
import org.python.core.util.FileUtil;
import org.python.core.PyFile;
import org.python.core.PyString;
import com.avaje.ebean.EbeanServer;
import com.avaje.ebeaninternal.api.SpiEbeanServer;
import com.avaje.ebeaninternal.server.ddl.DdlGenerator;
/**
* @author lahwran
*
*/
public class PythonPlugin implements Plugin {
private boolean isEnabled = false;
private boolean initialized = false;
private PluginLoader loader = null;
private Server server = null;
private File file = null;
private PluginDescriptionFile description = null;
private File dataFolder = null;
//private ClassLoader classLoader = null;
private Configuration config = null;
private boolean naggable = true;
private EbeanServer ebean = null;
private FileConfiguration newConfig = null;
private File configFile = null;
private PluginLogger logger = null;
private PluginDataFile dataFile = null; //data file used for retrieving resources
/**
* Listener to handle all PythonHooks events for this plugin.
*/
PythonListener listener = new PythonListener();
/**
* PythonHooks registered on startup.
*/
PythonHooks hooks;
/**
* interpreter that was used to load this plugin.
*/
PythonInterpreter interp;
/**
* Returns the folder that the plugin data's files are located in. The
* folder might not yet exist.
*
* @return data folder
*/
public File getDataFolder() {
return dataFolder;
}
/**
* Gets the associated PluginLoader responsible for this plugin
*
* @return PluginLoader that controls this plugin
*/
public final PluginLoader getPluginLoader() {
return loader;
}
/**
* Returns the Server instance currently running this plugin
*
* @return Server running this plugin
*/
public final Server getServer() {
return server;
}
/**
* Returns a value indicating whether or not this plugin is currently enabled
*
* @return true if this plugin is enabled, otherwise false
*/
public final boolean isEnabled() {
return isEnabled;
}
/**
* Returns the file which contains this plugin
*
* @return File containing this plugin
*/
protected File getFile() {
return file;
}
/**
* Returns the plugin.yaml file containing the details for this plugin
*
* @return Contents of the plugin.yaml file
*/
public PluginDescriptionFile getDescription() {
return description;
}
/**
* Returns the main configuration located at
* <plugin name>/config.yml and loads the file. If the configuration file
* does not exist and it cannot be loaded, no error will be emitted and
* the configuration file will have no values.
*
* @return The configuration.
* @deprecated See the new
*/
@Deprecated
public Configuration getConfiguration() {
if (config == null) {
config = YamlConfiguration.loadConfiguration(configFile);
}
return config;
}
public FileConfiguration getConfig() {
if (newConfig == null) {
reloadConfig();
}
return newConfig;
}
/**
* Sets the enabled state of this plugin
*
* @param enabled true if enabled, otherwise false
*/
protected void setEnabled(final boolean enabled) {
if (isEnabled != enabled) {
isEnabled = enabled;
if (isEnabled) {
if (hooks.onEnable != null)
hooks.onEnable.__call__();
hooks.doRegistrations(this);
onEnable();
} else {
if (hooks.onDisable != null)
hooks.onDisable.__call__();
onDisable();
}
}
}
/**
* Initializes this plugin with the given variables.
*
* This method should never be called manually.
*
* @param loader PluginLoader that is responsible for this plugin
* @param server Server instance that is running this plugin
* @param description PluginDescriptionFile containing metadata on this plugin
* @param dataFolder Folder containing the plugin's data
* @param file File containing this plugin
* @param classLoader ClassLoader which holds this plugin
*/
protected final void initialize(PluginLoader loader, Server server,
PluginDescriptionFile description, File dataFolder, File file ) { //,
//ClassLoader classLoader) {
if (!initialized) {
this.initialized = true;
this.loader = loader;
this.server = server;
this.file = file;
this.description = description;
this.dataFolder = dataFolder;
//this.classLoader = classLoader;
this.config = YamlConfiguration.loadConfiguration(new File(dataFolder, "config.yml"));
/*if (description.isDatabaseEnabled()) {
ServerConfig db = new ServerConfig();
db.setDefaultServer(false);
db.setRegister(false);
db.setClasses(getDatabaseClasses());
db.setName(description.getName());
server.configureDbConfig(db);
DataSourceConfig ds = db.getDataSourceConfig();
ds.setUrl(replaceDatabaseString(ds.getUrl()));
getDataFolder().mkdirs();
ClassLoader previous = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(classLoader);
ebean = EbeanServerFactory.create(db);
Thread.currentThread().setContextClassLoader(previous);
}*/
}
}
/**
* Provides a list of all classes that should be persisted in the database
*
* @return List of Classes that are Ebeans
*/
public List<Class<?>> getDatabaseClasses() {
return new ArrayList<Class<?>>();
}
// private String replaceDatabaseString(String input) {
// input = input.replaceAll("\\{DIR\\}", getDataFolder().getPath().replaceAll("\\\\", "/") + "/");
// input = input.replaceAll("\\{NAME\\}", getDescription().getName().replaceAll("[^\\w_-]", ""));
// return input;
// }
/**
* Gets the initialization status of this plugin
*
* @return true if this plugin is initialized, otherwise false
*/
public boolean isInitialized() {
return initialized;
}
/**
* {@inheritDoc}
*/
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
return false;
}
/**
* Gets the command with the given name, specific to this plugin
*
* @param name Name or alias of the command
* @return PluginCommand if found, otherwise null
*/
public PluginCommand getCommand(String name) {
String alias = name.toLowerCase();
PluginCommand command = getServer().getPluginCommand(alias);
if ((command != null) && (command.getPlugin() != this)) {
command = getServer().getPluginCommand(getDescription().getName().toLowerCase() + ":" + alias);
}
if ((command != null) && (command.getPlugin() == this)) {
return command;
} else {
return null;
}
}
/**
* Overridable, is called after all plugins have been instantiated.
*/
public void onLoad() {}
public void onLoaded() {
if (hooks.onLoaded != null)
hooks.onLoaded.__call__();
}
public ChunkGenerator getDefaultWorldGenerator(String worldName, String id) {
getServer().getLogger().severe("Plugin " + getDescription().getFullName() + " does not contain any generators that may be used in the default world!");
return null;
}
public final boolean isNaggable() {
return naggable;
}
public final void setNaggable(boolean canNag) {
this.naggable = canNag;
}
public EbeanServer getDatabase() {
return ebean;
}
protected void installDDL() {
SpiEbeanServer serv = (SpiEbeanServer) getDatabase();
DdlGenerator gen = serv.getDdlGenerator();
gen.runScript(false, gen.generateCreateDdl());
}
protected void removeDDL() {
SpiEbeanServer serv = (SpiEbeanServer) getDatabase();
DdlGenerator gen = serv.getDdlGenerator();
gen.runScript(true, gen.generateDropDdl());
}
public Logger getLogger() {
if (logger == null) {
logger = new PluginLogger(this);
}
return logger;
}
@Override
public String toString() {
return getDescription().getFullName();
}
public void onEnable() { }
public void onDisable() { }
@Override
public InputStream getResource(String filename) {
if(filename == null) {
throw new IllegalArgumentException("Filename cannot be null");
}
try {
return dataFile.getStream(filename);
} catch (IOException e) {
//just return null and do not print stack trace as JavaPlugin's getResource does not print it either
return null;
}
}
public PyFile getPyResource(PyString filename) {
if(filename == null) {
throw new IllegalArgumentException("Filename cannot be null");
}
try {
return FileUtil.wrap(dataFile.getStream(filename.asString()));
} catch (IOException e) {
//just return null and do not print stack trace as JavaPlugin's getResource does not print it either
return null;
}
}
@Override
public void saveResource(String resourcePath, boolean replace) {
if (resourcePath == null || resourcePath.equals("")) {
throw new IllegalArgumentException("ResourcePath cannot be null or empty");
}
// TODO is this necessary for use with PluginDataFile?
resourcePath = resourcePath.replace('\\', '/');
InputStream in = getResource(resourcePath);
if (in == null) {
throw new IllegalArgumentException("The embedded resource '" + resourcePath + "' cannot be found in " + getFile());
}
File outFile = new File(getDataFolder(), resourcePath);
int lastIndex = resourcePath.lastIndexOf('/');
File outDir = new File(getDataFolder(), resourcePath.substring(0, lastIndex >= 0 ? lastIndex : 0));
if (!outDir.exists()) {
outDir.mkdirs();
}
try {
if (!outFile.exists() || replace) {
OutputStream out = new FileOutputStream(outFile);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.close();
in.close();
} else {
Logger.getLogger(JavaPlugin.class.getName()).log(Level.WARNING, "Could not save " + outFile.getName() + " to " + outFile + " because " + outFile.getName() + " already exists.");
}
} catch (IOException ex) {
Logger.getLogger(JavaPlugin.class.getName()).log(Level.SEVERE, "Could not save " + outFile.getName() + " to " + outFile, ex);
}
}
public void reloadConfig() {
newConfig = YamlConfiguration.loadConfiguration(configFile);
InputStream defConfigStream = getResource("config.yml");
if (defConfigStream != null) {
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
newConfig.setDefaults(defConfig);
}
}
public void saveConfig() {
try {
getConfig().save(configFile);
} catch (IOException ex) {
Logger.getLogger(PythonPlugin.class.getName()).log(Level.SEVERE, "Could not save config to " + configFile, ex);
}
}
public void saveDefaultConfig() {
saveResource("config.yml", false);
}
protected void setDataFile(PluginDataFile file) {
this.dataFile = file;
}
@Override
public String getName() {
// TODO Auto-generated method stub
return "PythonPlugin";
}
@Override
public List<String> onTabComplete(CommandSender sender, Command cmd,
String alias, String[] args) {
return null;
}
}