Search in sources :

Example 1 with Event

use of org.bukkit.event.Event in project Conquer by xXAndrew28Xx.

the class LFEventForwarder method onFactionsChange.

@EventHandler
public void onFactionsChange(EventFactionsChange event) {
    Event forwardEvent;
    if (event.getFactionNew().isWilderness()) {
        forwardEvent = new ConquerFactionLeaveEvent(LFFaction.get(plugin, event.getFactionOld()), LFPlayer.get(plugin, event.getFPlayer()));
    } else {
        forwardEvent = new ConquerFactionJoinEvent(LFFaction.get(plugin, event.getFactionNew()), LFPlayer.get(plugin, event.getFPlayer()));
    }
    callEvent(forwardEvent);
    if (((Cancellable) forwardEvent).isCancelled()) {
        event.setCancelled(true);
    }
}
Also used : ConquerFactionJoinEvent(me.andrew28.addons.conquer.api.events.ConquerFactionJoinEvent) Cancellable(org.bukkit.event.Cancellable) Event(org.bukkit.event.Event) ConquerFactionRelationEvent(me.andrew28.addons.conquer.api.events.ConquerFactionRelationEvent) ConquerFactionLeaveEvent(me.andrew28.addons.conquer.api.events.ConquerFactionLeaveEvent) ConquerPowerLossEvent(me.andrew28.addons.conquer.api.events.ConquerPowerLossEvent) ConquerFactionDisbandEvent(me.andrew28.addons.conquer.api.events.ConquerFactionDisbandEvent) ConquerFactionRelationWishEvent(me.andrew28.addons.conquer.api.events.ConquerFactionRelationWishEvent) ConquerLandClaimEvent(me.andrew28.addons.conquer.api.events.ConquerLandClaimEvent) ConquerFactionJoinEvent(me.andrew28.addons.conquer.api.events.ConquerFactionJoinEvent) ConquerFactionCreateEvent(me.andrew28.addons.conquer.api.events.ConquerFactionCreateEvent) ConquerFactionLeaveEvent(me.andrew28.addons.conquer.api.events.ConquerFactionLeaveEvent) EventHandler(org.bukkit.event.EventHandler)

Example 2 with Event

use of org.bukkit.event.Event in project Conquer by xXAndrew28Xx.

the class CondWarpPassValid method check.

@Override
public boolean check(Event e) {
    String password = this.password.getSingle(e);
    ConquerFaction faction = this.faction.getSingle(e);
    return password != null && faction != null && names.check(e, warp -> faction.isWarpPassword(warp, password), isNegated());
}
Also used : Condition(ch.njol.skript.lang.Condition) Expression(ch.njol.skript.lang.Expression) Examples(ch.njol.skript.doc.Examples) Name(ch.njol.skript.doc.Name) Event(org.bukkit.event.Event) Skript(ch.njol.skript.Skript) Description(ch.njol.skript.doc.Description) Kleenean(ch.njol.util.Kleenean) SkriptParser(ch.njol.skript.lang.SkriptParser) ConquerFaction(me.andrew28.addons.conquer.api.ConquerFaction) ConquerFaction(me.andrew28.addons.conquer.api.ConquerFaction)

Example 3 with Event

use of org.bukkit.event.Event in project AuthMeReloaded by AuthMe.

the class PasswordSecurityTest method setUpMocks.

@BeforeInjecting
public void setUpMocks() {
    caughtClassInEvent = null;
    // When the password encryption event is emitted, replace the encryption method with our mock.
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            Object[] arguments = invocation.getArguments();
            if (arguments[0] instanceof PasswordEncryptionEvent) {
                PasswordEncryptionEvent event = (PasswordEncryptionEvent) arguments[0];
                caughtClassInEvent = event.getMethod() == null ? null : event.getMethod().getClass();
                event.setMethod(method);
            }
            return null;
        }
    }).when(pluginManager).callEvent(any(Event.class));
    given(settings.getProperty(SecuritySettings.PASSWORD_HASH)).willReturn(HashAlgorithm.BCRYPT);
    given(settings.getProperty(SecuritySettings.LEGACY_HASHES)).willReturn(Collections.emptySet());
    given(settings.getProperty(HooksSettings.BCRYPT_LOG2_ROUND)).willReturn(8);
    Injector injector = new InjectorBuilder().addDefaultHandlers("fr.xephi.authme.security.crypts").create();
    injector.register(Settings.class, settings);
    given(hashAlgorithmFactory.newInstance(any(Class.class))).willAnswer(invocation -> {
        Object o = injector.createIfHasDependencies(invocation.getArgument(0));
        if (o == null) {
            throw new IllegalArgumentException("Cannot create object of class '" + invocation.getArgument(0) + "': missing class that needs to be provided?");
        }
        return o;
    });
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) Injector(ch.jalu.injector.Injector) PasswordEncryptionEvent(fr.xephi.authme.events.PasswordEncryptionEvent) Event(org.bukkit.event.Event) PasswordEncryptionEvent(fr.xephi.authme.events.PasswordEncryptionEvent) BeforeClass(org.junit.BeforeClass) InjectorBuilder(ch.jalu.injector.InjectorBuilder) BeforeInjecting(ch.jalu.injector.testing.BeforeInjecting)

Example 4 with Event

use of org.bukkit.event.Event in project Glowstone by GlowstoneMC.

the class GlowFishingHookTest method setUp.

@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    PowerMockito.mockStatic(Bukkit.class);
    when(Bukkit.getServer()).thenReturn(server);
    eventsFired.removeAll(PlayerFishEvent.class);
    when(world.getBlockAt(any(Location.class))).thenReturn(block);
    when(world.getBlockAt(anyInt(), anyInt(), anyInt())).thenReturn(block);
    when(block.getType()).thenReturn(Material.WATER);
    fishingRewardManager = new FishingRewardManager();
    when(server.getFishingRewardManager()).thenReturn(fishingRewardManager);
    when(player.getLocation()).thenReturn(location);
    when(eventFactory.callEvent(any(Event.class))).thenAnswer(invocation -> {
        Event e = invocation.getArgument(0);
        eventsFired.put(e.getClass(), e);
        return e;
    });
    when(eventFactory.onEntityDamage(any(EntityDamageEvent.class))).thenAnswer(returnsFirstArg());
}
Also used : FishingRewardManager(net.glowstone.entity.FishingRewardManager) Event(org.bukkit.event.Event) PlayerFishEvent(org.bukkit.event.player.PlayerFishEvent) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent) Location(org.bukkit.Location) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent) Before(org.junit.Before)

Example 5 with Event

use of org.bukkit.event.Event in project Bukkit by Bukkit.

the class JavaPluginLoader method createRegisteredListeners.

public Map<Class<? extends Event>, Set<RegisteredListener>> createRegisteredListeners(Listener listener, final Plugin plugin) {
    Validate.notNull(plugin, "Plugin can not be null");
    Validate.notNull(listener, "Listener can not be null");
    boolean useTimings = server.getPluginManager().useTimings();
    Map<Class<? extends Event>, Set<RegisteredListener>> ret = new HashMap<Class<? extends Event>, Set<RegisteredListener>>();
    Set<Method> methods;
    try {
        Method[] publicMethods = listener.getClass().getMethods();
        methods = new HashSet<Method>(publicMethods.length, Float.MAX_VALUE);
        for (Method method : publicMethods) {
            methods.add(method);
        }
        for (Method method : listener.getClass().getDeclaredMethods()) {
            methods.add(method);
        }
    } catch (NoClassDefFoundError e) {
        plugin.getLogger().severe("Plugin " + plugin.getDescription().getFullName() + " has failed to register events for " + listener.getClass() + " because " + e.getMessage() + " does not exist.");
        return ret;
    }
    for (final Method method : methods) {
        final EventHandler eh = method.getAnnotation(EventHandler.class);
        if (eh == null)
            continue;
        final Class<?> checkClass;
        if (method.getParameterTypes().length != 1 || !Event.class.isAssignableFrom(checkClass = method.getParameterTypes()[0])) {
            plugin.getLogger().severe(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);
        Set<RegisteredListener> eventSet = ret.get(eventClass);
        if (eventSet == null) {
            eventSet = new HashSet<RegisteredListener>();
            ret.put(eventClass, eventSet);
        }
        for (Class<?> clazz = eventClass; Event.class.isAssignableFrom(clazz); clazz = clazz.getSuperclass()) {
            // This loop checks for extending deprecated events
            if (clazz.getAnnotation(Deprecated.class) != null) {
                Warning warning = clazz.getAnnotation(Warning.class);
                WarningState warningState = server.getWarningState();
                if (!warningState.printFor(warning)) {
                    break;
                }
                plugin.getLogger().log(Level.WARNING, String.format("\"%s\" has registered a listener for %s on method \"%s\", but the event is Deprecated." + " \"%s\"; please notify the authors %s.", plugin.getDescription().getFullName(), clazz.getName(), method.toGenericString(), (warning != null && warning.reason().length() != 0) ? warning.reason() : "Server performance will be affected", Arrays.toString(plugin.getDescription().getAuthors().toArray())), warningState == WarningState.ON ? new AuthorNagException(null) : null);
                break;
            }
        }
        EventExecutor executor = new EventExecutor() {

            public void execute(Listener listener, Event event) throws EventException {
                try {
                    if (!eventClass.isAssignableFrom(event.getClass())) {
                        return;
                    }
                    method.invoke(listener, event);
                } catch (InvocationTargetException ex) {
                    throw new EventException(ex.getCause());
                } catch (Throwable t) {
                    throw new EventException(t);
                }
            }
        };
        if (useTimings) {
            eventSet.add(new TimedRegisteredListener(listener, executor, eh.priority(), plugin, eh.ignoreCancelled()));
        } else {
            eventSet.add(new RegisteredListener(listener, executor, eh.priority(), plugin, eh.ignoreCancelled()));
        }
    }
    return ret;
}
Also used : Warning(org.bukkit.Warning) HashSet(java.util.HashSet) Set(java.util.Set) WarningState(org.bukkit.Warning.WarningState) TimedRegisteredListener(org.bukkit.plugin.TimedRegisteredListener) Listener(org.bukkit.event.Listener) RegisteredListener(org.bukkit.plugin.RegisteredListener) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) EventHandler(org.bukkit.event.EventHandler) TimedRegisteredListener(org.bukkit.plugin.TimedRegisteredListener) TimedRegisteredListener(org.bukkit.plugin.TimedRegisteredListener) RegisteredListener(org.bukkit.plugin.RegisteredListener) AuthorNagException(org.bukkit.plugin.AuthorNagException) EventException(org.bukkit.event.EventException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) EventExecutor(org.bukkit.plugin.EventExecutor) Event(org.bukkit.event.Event) PluginEnableEvent(org.bukkit.event.server.PluginEnableEvent) PluginDisableEvent(org.bukkit.event.server.PluginDisableEvent)

Aggregations

Event (org.bukkit.event.Event)26 Listener (org.bukkit.event.Listener)8 Test (org.junit.Test)7 EventException (org.bukkit.event.EventException)6 EventHandler (org.bukkit.event.EventHandler)6 EventExecutor (org.bukkit.plugin.EventExecutor)6 PluginManager (org.bukkit.plugin.PluginManager)6 RegisteredListener (org.bukkit.plugin.RegisteredListener)5 Cancellable (org.bukkit.event.Cancellable)4 TestEvent (org.bukkit.event.TestEvent)4 Field (java.lang.reflect.Field)3 Map (java.util.Map)3 Set (java.util.Set)3 EventPriority (org.bukkit.event.EventPriority)3 File (java.io.File)2 Method (java.lang.reflect.Method)2 List (java.util.List)2 ConquerFaction (me.andrew28.addons.conquer.api.ConquerFaction)2 ConquerPlayer (me.andrew28.addons.conquer.api.ConquerPlayer)2 ConquerFactionCreateEvent (me.andrew28.addons.conquer.api.events.ConquerFactionCreateEvent)2