use of cn.nukkit.utils.PluginException in project Nukkit by Nukkit.
the class PluginManager method registerEvent.
public void registerEvent(Class<? extends Event> event, Listener listener, EventPriority priority, EventExecutor executor, Plugin plugin, boolean ignoreCancelled) throws PluginException {
if (!plugin.isEnabled()) {
throw new PluginException("Plugin attempted to register " + event + " while not enabled");
}
try {
Timing timing = Timings.getPluginEventTiming(event, listener, executor, plugin);
this.getEventListeners(event).register(new RegisteredListener(listener, executor, priority, plugin, ignoreCancelled, timing));
} catch (IllegalAccessException e) {
Server.getInstance().getLogger().logException(e);
}
}
use of cn.nukkit.utils.PluginException in project Nukkit by Nukkit.
the class MetadataStore method setMetadata.
public void setMetadata(Object subject, String metadataKey, MetadataValue newMetadataValue) {
if (newMetadataValue == null) {
throw new ServerException("Value cannot be null");
}
Plugin owningPlugin = newMetadataValue.getOwningPlugin();
if (owningPlugin == null) {
throw new PluginException("Plugin cannot be null");
}
String key = this.disambiguate((Metadatable) subject, metadataKey);
Map<Plugin, MetadataValue> entry = this.metadataMap.computeIfAbsent(key, k -> new WeakHashMap<>(1));
entry.put(owningPlugin, newMetadataValue);
}
use of cn.nukkit.utils.PluginException in project Nukkit by Nukkit.
the class PluginDescription method loadMap.
private void loadMap(Map<String, Object> plugin) throws PluginException {
this.name = ((String) plugin.get("name")).replaceAll("[^A-Za-z0-9 _.-]", "");
if (this.name.equals("")) {
throw new PluginException("Invalid PluginDescription name");
}
this.name = this.name.replace(" ", "_");
this.version = String.valueOf(plugin.get("version"));
this.main = (String) plugin.get("main");
Object api = plugin.get("api");
if (api instanceof List) {
this.api = (List<String>) api;
} else {
List<String> list = new ArrayList<>();
list.add((String) api);
this.api = list;
}
if (this.main.startsWith("cn.nukkit.")) {
throw new PluginException("Invalid PluginDescription main, cannot start within the cn.nukkit. package");
}
if (plugin.containsKey("commands") && plugin.get("commands") instanceof Map) {
this.commands = (Map<String, Object>) plugin.get("commands");
}
if (plugin.containsKey("depend")) {
this.depend = (List<String>) plugin.get("depend");
}
if (plugin.containsKey("softdepend")) {
this.softDepend = (List<String>) plugin.get("softdepend");
}
if (plugin.containsKey("loadbefore")) {
this.loadBefore = (List<String>) plugin.get("loadbefore");
}
if (plugin.containsKey("website")) {
this.website = (String) plugin.get("website");
}
if (plugin.containsKey("description")) {
this.description = (String) plugin.get("description");
}
if (plugin.containsKey("prefix")) {
this.prefix = (String) plugin.get("prefix");
}
if (plugin.containsKey("load")) {
String order = (String) plugin.get("load");
try {
this.order = PluginLoadOrder.valueOf(order);
} catch (Exception e) {
throw new PluginException("Invalid PluginDescription load");
}
}
if (plugin.containsKey("author")) {
this.authors.add((String) plugin.get("author"));
}
if (plugin.containsKey("authors")) {
this.authors.addAll((Collection<? extends String>) plugin.get("authors"));
}
if (plugin.containsKey("permissions")) {
this.permissions = Permission.loadPermissions((Map<String, Object>) plugin.get("permissions"));
}
}
use of cn.nukkit.utils.PluginException in project Nukkit by Nukkit.
the class PluginManager method registerEvents.
public void registerEvents(Listener listener, Plugin plugin) {
if (!plugin.isEnabled()) {
throw new PluginException("Plugin attempted to register " + listener.getClass().getName() + " while not enabled");
}
Map<Class<? extends Event>, Set<RegisteredListener>> ret = new HashMap<>();
Set<Method> methods;
try {
Method[] publicMethods = listener.getClass().getMethods();
Method[] privateMethods = listener.getClass().getDeclaredMethods();
methods = new HashSet<>(publicMethods.length + privateMethods.length, 1.0f);
Collections.addAll(methods, publicMethods);
Collections.addAll(methods, privateMethods);
} catch (NoClassDefFoundError e) {
plugin.getLogger().error("Plugin " + plugin.getDescription().getFullName() + " has failed to register events for " + listener.getClass() + " because " + e.getMessage() + " does not exist.");
return;
}
for (final Method method : methods) {
final EventHandler eh = method.getAnnotation(EventHandler.class);
if (eh == null)
continue;
if (method.isBridge() || method.isSynthetic()) {
continue;
}
final Class<?> checkClass;
if (method.getParameterTypes().length != 1 || !Event.class.isAssignableFrom(checkClass = method.getParameterTypes()[0])) {
plugin.getLogger().error(plugin.getDescription().getFullName() + " attempted to register an invalid EventHandler method signature \"" + method.toGenericString() + "\" in " + listener.getClass());
continue;
}
final Class<? extends Event> eventClass = checkClass.asSubclass(Event.class);
method.setAccessible(true);
for (Class<?> clazz = eventClass; Event.class.isAssignableFrom(clazz); clazz = clazz.getSuperclass()) {
// This loop checks for extending deprecated events
if (clazz.getAnnotation(Deprecated.class) != null) {
if (Boolean.valueOf(String.valueOf(this.server.getConfig("settings.deprecated-verbpse", true)))) {
this.server.getLogger().warning(this.server.getLanguage().translateString("nukkit.plugin.deprecatedEvent", plugin.getName(), clazz.getName(), listener.getClass().getName() + "." + method.getName() + "()"));
}
break;
}
}
this.registerEvent(eventClass, listener, eh.priority(), new MethodEventExecutor(method), plugin, eh.ignoreCancelled());
}
}
use of cn.nukkit.utils.PluginException in project Nukkit by Nukkit.
the class JavaPluginLoader method loadPlugin.
@Override
public Plugin loadPlugin(File file) throws Exception {
PluginDescription description = this.getPluginDescription(file);
if (description != null) {
this.server.getLogger().info(this.server.getLanguage().translateString("nukkit.plugin.load", description.getFullName()));
File dataFolder = new File(file.getParentFile(), description.getName());
if (dataFolder.exists() && !dataFolder.isDirectory()) {
throw new IllegalStateException("Projected dataFolder '" + dataFolder.toString() + "' for " + description.getName() + " exists and is not a directory");
}
String className = description.getMain();
PluginClassLoader classLoader = new PluginClassLoader(this, this.getClass().getClassLoader(), file);
this.classLoaders.put(description.getName(), classLoader);
PluginBase plugin;
try {
Class javaClass = classLoader.loadClass(className);
try {
Class<? extends PluginBase> pluginClass = javaClass.asSubclass(PluginBase.class);
plugin = pluginClass.newInstance();
this.initPlugin(plugin, description, dataFolder, file);
return plugin;
} catch (ClassCastException e) {
throw new PluginException("main class `" + description.getMain() + "' does not extend PluginBase");
} catch (InstantiationException | IllegalAccessException e) {
Server.getInstance().getLogger().logException(e);
}
} catch (ClassNotFoundException e) {
throw new PluginException("Couldn't load plugin " + description.getName() + ": main class not found");
}
}
return null;
}
Aggregations