Search in sources :

Example 86 with EventHandler

use of org.bukkit.event.EventHandler in project Denizen-For-Bukkit by DenizenScript.

the class dNPCRegistry method onSpawn.

// <--[action]
// @Actions
// spawn
// @Triggers when the NPC is spawned.
// This will fire whenever an NPC's chunk is loaded, or a spawn command is issued.
//
// @Context
// None
// -->
/**
     * Fires the 'On Spawn:' action in an NPCs Assignment, if set.
     *
     * @param event NPCSpawnEvent
     */
@EventHandler
public void onSpawn(NPCSpawnEvent event) {
    if (event.getNPC() == null) {
        dB.echoError("Null NPC spawned!");
        return;
    }
    _registerNPC(event.getNPC());
    // Do world script event 'On NPC spawns'
    OldEventManager.doEvents(Arrays.asList("npc spawns"), new BukkitScriptEntryData(null, dNPC.mirrorCitizensNPC(event.getNPC())), null);
    // On Spawn action
    new dNPC(event.getNPC()).action("spawn", null);
}
Also used : net.aufdemrand.denizen.objects.dNPC(net.aufdemrand.denizen.objects.dNPC) BukkitScriptEntryData(net.aufdemrand.denizen.BukkitScriptEntryData) EventHandler(org.bukkit.event.EventHandler)

Example 87 with EventHandler

use of org.bukkit.event.EventHandler in project Denizen-For-Bukkit by DenizenScript.

the class ListenerRegistry method playerJoin.

@EventHandler
public void playerJoin(PlayerJoinEvent event) {
    Denizen denizen = DenizenAPI.getCurrentInstance();
    dPlayer player = new dPlayer(event.getPlayer());
    // Any saves quest listeners in progress?
    if (!denizen.getSaves().contains("Listeners." + player.getSaveName())) {
        return;
    }
    Set<String> inProgress = denizen.getSaves().getConfigurationSection("Listeners." + player.getSaveName()).getKeys(false);
    // If empty, no quest listeners to load.
    if (inProgress.isEmpty()) {
        return;
    }
    // TODO: Players.SAVENAME.Listeners?
    String path = "Listeners." + player.getSaveName() + ".";
    // If not empty, let's do the loading process for each.
    for (String listenerId : inProgress) {
        // People tend to worry when they see long-ass stacktraces.. let's catch them.
        try {
            String type = denizen.getSaves().getString(path + listenerId + ".Listener Type");
            dNPC npc = null;
            if (denizen.getSaves().contains(path + listenerId + ".Linked NPCID")) {
                npc = DenizenAPI.getDenizenNPC(CitizensAPI.getNPCRegistry().getById(denizen.getSaves().getInt(path + listenerId + ".Linked NPCID")));
            }
            if (get(type) == null) {
                return;
            }
            dB.log(event.getPlayer().getName() + " has a LISTENER in progress. Loading '" + listenerId + "'.");
            get(type).createInstance(dPlayer.mirrorBukkitPlayer(event.getPlayer()), listenerId).load(dPlayer.mirrorBukkitPlayer(event.getPlayer()), npc, listenerId, type);
        } catch (Exception e) {
            dB.log(event.getPlayer() + " has a saved listener named '" + listenerId + "' that may be corrupt. Skipping for now, but perhaps check the contents of your saves.yml for problems?");
        }
    }
}
Also used : net.aufdemrand.denizen.objects.dNPC(net.aufdemrand.denizen.objects.dNPC) Denizen(net.aufdemrand.denizen.Denizen) net.aufdemrand.denizen.objects.dPlayer(net.aufdemrand.denizen.objects.dPlayer) EventHandler(org.bukkit.event.EventHandler)

Example 88 with EventHandler

use of org.bukkit.event.EventHandler in project Denizen-For-Bukkit by DenizenScript.

the class BlockListenerInstance method listenBreak.

@EventHandler
public void listenBreak(BlockBreakEvent event) {
    //Check if event references proper player.
    if (event.getPlayer() != player.getPlayerEntity()) {
        return;
    }
    //Check if region is specified, and if so, is the player in it.
    if (region != null) {
    //if (!WorldGuardUtilities.inRegion(player.getPlayerEntity().getLocation(), region)) return;
    }
    // Same with the CUBOID argument...
    if (cuboid != null) {
        if (!cuboid.isInsideCuboid(player.getLocation())) {
            return;
        }
    }
    //Type BREAK
    if (type == BlockType.BREAK) {
        dB.log("...BREAK listener");
        //if catch-all specified, count it!
        if (blocks.contains("*")) {
            blocks_so_far++;
            dB.log(ChatColor.YELLOW + "// " + player.getName() + " broke a(n) " + event.getBlock().getType().toString() + ".");
            check();
            return;
        }
        //check if block is specified and if so, count it!
        for (String item_value : blocks) {
            dB.log("...checking value: " + item_value);
            dMaterial mat = dMaterial.valueOf(item_value);
            if (event.getBlock().getState().getType() == mat.getMaterial() && event.getBlock().getState().getData().equals(mat.getMaterialData())) {
                blocks_so_far++;
                dB.log(ChatColor.YELLOW + "// " + player.getName() + " broke a(n) " + event.getBlock().getType().toString() + ".");
                check();
            }
        }
    }
}
Also used : net.aufdemrand.denizen.objects.dMaterial(net.aufdemrand.denizen.objects.dMaterial) EventHandler(org.bukkit.event.EventHandler)

Example 89 with EventHandler

use of org.bukkit.event.EventHandler in project Towny by ElgarL.

the class TownyWarCustomListener method onCellAttackEvent.

@EventHandler(priority = EventPriority.LOWEST)
public void onCellAttackEvent(CellAttackEvent event) {
    try {
        CellUnderAttack cell = event.getData();
        TownyWar.registerAttack(cell);
    } catch (Exception e) {
        event.setCancelled(true);
        event.setReason(e.getMessage());
    }
}
Also used : CellUnderAttack(com.palmergames.bukkit.towny.war.flagwar.CellUnderAttack) EconomyException(com.palmergames.bukkit.towny.exceptions.EconomyException) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException) EventHandler(org.bukkit.event.EventHandler)

Example 90 with EventHandler

use of org.bukkit.event.EventHandler 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

EventHandler (org.bukkit.event.EventHandler)532 Player (org.bukkit.entity.Player)185 Entity (org.bukkit.entity.Entity)70 net.aufdemrand.denizen.objects.dEntity (net.aufdemrand.denizen.objects.dEntity)67 net.aufdemrand.denizen.objects.dLocation (net.aufdemrand.denizen.objects.dLocation)62 Block (org.bukkit.block.Block)62 ItemStack (org.bukkit.inventory.ItemStack)55 Location (org.bukkit.Location)54 Island (com.wasteofplastic.acidisland.Island)42 Element (net.aufdemrand.denizencore.objects.Element)38 LivingEntity (org.bukkit.entity.LivingEntity)37 MinigamePlayer (au.com.mineauz.minigames.MinigamePlayer)31 Minigame (au.com.mineauz.minigames.minigame.Minigame)29 net.aufdemrand.denizen.objects.dItem (net.aufdemrand.denizen.objects.dItem)29 net.aufdemrand.denizencore.objects.dList (net.aufdemrand.denizencore.objects.dList)28 Projectile (org.bukkit.entity.Projectile)26 MyPetPlayer (de.Keyle.MyPet.api.player.MyPetPlayer)23 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)22 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)18 ArrayList (java.util.ArrayList)18