private Class<? extends Event> getRegistrationClass(Class<? extends Event> clazz) { try { clazz.getDeclaredMethod("getHandlerList"); return clazz; } catch (NoSuchMethodException e) { if ((clazz.getSuperclass() != null) && (!clazz.getSuperclass().equals(Event.class)) && (Event.class.isAssignableFrom(clazz.getSuperclass()))) { return getRegistrationClass(clazz.getSuperclass().asSubclass( Event.class)); } } throw new IllegalPluginAccessException( "Unable to find handler list for event " + clazz.getName()); }
@Override public void registerEvent(Class<? extends Event> event, Listener listener, EventPriority priority, EventExecutor executor, Plugin plugin, boolean ignoreCancelled) { Validate.notNull(listener, "Listener cannot be null"); Validate.notNull(priority, "Priority cannot be null"); Validate.notNull(executor, "Executor cannot be null"); Validate.notNull(plugin, "Plugin cannot be null"); if (!plugin.isEnabled()) { throw new IllegalPluginAccessException("Plugin attempted to register " + event + " while not enabled"); } if (useTimings) { getEventListeners(event).register(new TimedRegisteredListener(listener, executor, priority, plugin, ignoreCancelled)); } else { getEventListeners(event).register(new RegisteredListener(listener, executor, priority, plugin, ignoreCancelled)); } }
private static void validate(final Plugin plugin, final Object task) { Validate.notNull(plugin, "Plugin cannot be null"); Validate.notNull(task, "Task cannot be null"); if (!plugin.isEnabled()) { throw new IllegalPluginAccessException("Plugin attempted to register task while disabled"); } }
private HandlerList getEventListeners(Class<? extends Event> type) { try { Method method = getRegistrationClass(type).getDeclaredMethod("getHandlerList"); method.setAccessible(true); return (HandlerList) method.invoke(null); } catch (Exception e) { throw new IllegalPluginAccessException(e.toString()); } }
private Class<? extends Event> getRegistrationClass(Class<? extends Event> clazz) { try { clazz.getDeclaredMethod("getHandlerList"); return clazz; } catch (NoSuchMethodException e) { if (clazz.getSuperclass() != null && !clazz.getSuperclass().equals(Event.class) && Event.class.isAssignableFrom(clazz.getSuperclass())) { return getRegistrationClass(clazz.getSuperclass().asSubclass(Event.class)); } else { throw new IllegalPluginAccessException("Unable to find handler list for event " + clazz.getName() + ". Static getHandlerList method required!"); } } }
public void registerEvents(Listener listener, Plugin plugin) { if (!plugin.isEnabled()) { throw new IllegalPluginAccessException("Plugin attempted to register " + listener + " while not enabled"); } for (Map.Entry<Class<? extends Event>, Set<RegisteredListener>> entry : plugin.getPluginLoader() .createRegisteredListeners(listener, plugin).entrySet()) { getEventListeners(getRegistrationClass(entry.getKey())).registerAll(entry.getValue()); } }
/** * Gets event listeners * * @param type event type * @return handler list */ private HandlerList getEventListeners(Class<? extends Event> type) { try { Method method = Reflections.getMethod(type, "getHandlerList"); method.setAccessible(true); return (HandlerList) method.invoke(null); } catch(Exception e) { throw new IllegalPluginAccessException(e.toString()); } }
/** * Gets registration class * * @param clazz event class * @return the class */ private Class<? extends Event> getRegistrationClass(Class<? extends Event> clazz) { try { clazz.getDeclaredMethod("getHandlerList"); return clazz; } catch(NoSuchMethodException e) { if((clazz.getSuperclass() != null) && (!clazz.getSuperclass().equals(Event.class)) && (Event.class.isAssignableFrom(clazz.getSuperclass()))) { return getRegistrationClass(clazz.getSuperclass().asSubclass(Event.class)); } } throw new IllegalPluginAccessException("Unable to find handler list for event " + clazz.getName()); }
/** * Gets a Bukkit plugin. * * @param plugin The name of the plugin to get. * @return A Bukkit plugin. */ public static Plugin getPlugin(final String plugin) { if (!Bukkit.getPluginManager().isPluginEnabled(plugin)) { throw new IllegalPluginAccessException(String.format("The plugin [%s] is not enabled!", plugin)); } return Bukkit.getPluginManager().getPlugin(plugin); }
@Test(expected = IllegalPluginAccessException.class) public void testGetPluginNotFound() { final String pluginName = "Test plugin"; final PluginManager pluginManager = mock(PluginManager.class); when(pluginManager.isPluginEnabled(pluginName)).thenReturn(false); mockStatic(Bukkit.class); when(Bukkit.getPluginManager()).thenReturn(pluginManager); try { BukkitUtil.getPlugin(pluginName); } finally { verify(pluginManager).isPluginEnabled(pluginName); verifyStatic(); } }
private static Class<? extends Event> getRegistrationClass(Class<? extends Event> clazz) { try { clazz.getDeclaredMethod("getHandlerList"); return clazz; } catch (NoSuchMethodException e) { if (clazz.getSuperclass() != null && !clazz.getSuperclass().equals(Event.class) && Event.class.isAssignableFrom(clazz.getSuperclass())) { return getRegistrationClass(clazz.getSuperclass().asSubclass(Event.class)); } else { throw new IllegalPluginAccessException("Unable to find handler list for event " + clazz.getName()); } } }
private static HandlerList getHandlerList(Class<? extends Event> type) { try { Method method = getRegistrationClass(type).getDeclaredMethod("getHandlerList"); method.setAccessible(true); return (HandlerList) method.invoke(null); } catch (Exception e) { throw new IllegalPluginAccessException(e.toString()); } }
private static void validate(Plugin plugin, Object task) { Preconditions.checkState(plugin != null, "Plugin is null"); Preconditions.checkNotNull(task, "task"); //noinspection ConstantConditions if (!plugin.isEnabled()) { throw new IllegalPluginAccessException("Plugin attempted to register task while disabled"); } }
public void setState(Player p, UserState state) { p.teleport(state.getLocation()); p.setFallDistance(state.getFallDistance()); p.setGameMode(state.getGameMode()); p.setAllowFlight(state.getAllowFlight()); p.setFlying(state.isFlying()); p.setLevel(state.getLevel()); p.setExp(state.getExp()); p.setFireTicks(state.getFireTicks()); p.setMaxHealth(state.getMaxHealth()); p.setHealth(state.getHealth()); p.setFoodLevel(state.getFoodLevel()); p.setWalkSpeed(state.getWalkSpeed()); p.setFlySpeed(state.getFlySpeed()); for (Iterator<PotionEffect> i = p.getActivePotionEffects().iterator(); i.hasNext();) { p.removePotionEffect(i.next().getType()); } p.addPotionEffects(state.getActivePotionEffects()); p.getInventory().setHeldItemSlot(state.getHeldItemSlot()); final String name = p.getName(); final ItemStack[] contents = state.getContents(); final ItemStack[] armorcontents = state.getArmorContents(); try { Bukkit.getScheduler().scheduleSyncDelayedTask(SurvivalGames.instance, new Runnable() { public void run() { Player fp = Bukkit.getPlayer(name); if(fp != null) { fp.getInventory().setContents(contents); fp.getInventory().setArmorContents(armorcontents); fp.updateInventory(); } } }, 2L); } catch(IllegalPluginAccessException e) { p.getInventory().setContents(contents); p.getInventory().setArmorContents(armorcontents); p.updateInventory(); } }
/** * Registers the given event to the specified listener using a directly * passed EventExecutor * * @param event * Event class to register * @param listener * PlayerListener to register * @param priority * Priority of this event * @param executor * EventExecutor to register * @param plugin * Plugin to register * @param ignoreCancelled * Do not call executor if event was already cancelled */ public void registerEvent(Class<? extends Event> event, Listener listener, EventPriority priority, EventExecutor executor, Plugin plugin, boolean ignoreCancelled) { Validate.notNull(listener, "Listener cannot be null"); Validate.notNull(priority, "Priority cannot be null"); Validate.notNull(executor, "Executor cannot be null"); Validate.notNull(plugin, "Plugin cannot be null"); if (!plugin.isEnabled()) { throw new IllegalPluginAccessException("Plugin attempted to register " + event + " while not enabled"); } getEventListeners(event) .register(new RegisteredListener(listener, executor, priority, plugin, ignoreCancelled)); }