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);
}
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?");
}
}
}
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();
}
}
}
}
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());
}
}
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;
}
Aggregations