本文整理汇总了Java中org.bukkit.event.Listener类的典型用法代码示例。如果您正苦于以下问题:Java Listener类的具体用法?Java Listener怎么用?Java Listener使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Listener类属于org.bukkit.event包,在下文中一共展示了Listener类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: registerEvent
import org.bukkit.event.Listener; //导入依赖的package包/类
@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));
}
}
示例2: execute
import org.bukkit.event.Listener; //导入依赖的package包/类
@Override
// 计算调用次数和花费总时间以及花费最多的时间
public void execute(Listener listener, Event e) throws EventException {
if (e.isAsynchronous()) {
this.eventExecutor.execute(listener, e);
} else {
long startTime = System.nanoTime();
try {
try{
this.eventExecutor.execute(listener, e);
}catch(Throwable ex){
MonitorUtils.AExceptionCatcher(plugin, ex);
}
} finally {
long endTime = System.nanoTime();
long executeTime = endTime - startTime;
if(ConfigFunction.MonitorPluginLagWarningenable){
if(executeTime/1000000 > ConfigFunction.MonitorPluginLagWarningPeriod){
AzureAPI.log("警告!服务器主线程陷入停顿超过配置设定值!因为插件" + this.getPlugin().getName() + " 执行了一次耗时 " + executeTime/1000000 + " 毫秒的位于 " + listener.getClass().getName() + " 的监听器 " + e.getEventName() + " 的操作!");
}
}
this.record(e.getEventName(), executeTime);
}
}
}
示例3: onEnable
import org.bukkit.event.Listener; //导入依赖的package包/类
@Override
protected void onEnable() {
EventExecutor.getInstance().register(this);
// load config
this.config = Moo.getInstance().loadConfig(Lightning.getInstance().getDataFolder());
this.languageManager = new LanguageManager(Lightning.getInstance().getDataFolder(),
s -> ChatColor.translateAlternateColorCodes('&', s));
this.languageManager.load("language");
// if disabled stop enabling
if(!((boolean) config.get("activated"))) {
Lightning.getInstance().getLogs().info("*** Lightning disabled. ***");
return;
}
// register handler
Moo.getInstance().registerHandler(o -> {
if(o instanceof Listener) Bukkit.getServer().getPluginManager().registerEvents((Listener) o, Lightning.getInstance());
}, new ChatListener(), new ServerListener());
// connect to cloud
if(config.isLoaded()) {
Moo.getInstance().connect(config.get("group-id"), ClientType.SERVER, config.get("cloud-ip"), config.get("cloud-port"));
}
}
示例4: generateClass
import org.bukkit.event.Listener; //导入依赖的package包/类
public static Class<?> generateClass() throws NotFoundException, CannotCompileException {
ClassPool classpath = ClassPool.getDefault();
classpath.insertClassPath(new ClassClassPath(EventCallbackGenerator.class));
CtClass eventClass = classpath.makeClass("online.pizzacrust.lukkitplus" +
".EventCallback");
eventClass.addInterface(classpath.get(Listener.class.getName()));
for (Class<? extends Event> event : LukkitPlus.BUKKIT_EVENTS) {
if (containsStaticHandlerList(event)) {
CtMethod eventMethod = CtNewMethod.make(CtClass.voidType, "on" + event.getSimpleName
(), new CtClass[]{classpath.get(event.getName())}, new CtClass[0], "online" +
".pizzacrust.lukkitplus.EventCallbackGenerator.call($1);", eventClass);
eventClass.addMethod(eventMethod);
AnnotationsAttribute attribute = new AnnotationsAttribute(eventClass.getClassFile()
.getConstPool(), AnnotationsAttribute.visibleTag);
Annotation eventHandlerAnnt = new Annotation(EventHandler.class.getName(), eventClass
.getClassFile().getConstPool());
attribute.addAnnotation(eventHandlerAnnt);
eventMethod.getMethodInfo().addAttribute(attribute);
}
}
return eventClass.toClass(LukkitPlus.class.getClassLoader());
}
示例5: register
import org.bukkit.event.Listener; //导入依赖的package包/类
/**
* Registers the given spell
*/
public void register(Spell spell) {
spells.put(spell.getName(), spell);
MystiCraft.getCraftingManager().addRecipe(spell.getRecipe());
if (spell instanceof Listener) {
MystiCraft.getInstance().getServer().getPluginManager().registerEvents((Listener) spell, MystiCraft.getInstance());
}
if (spell instanceof Configurable) {
if (!spellConfig.getConfig().has(spell.getName())) {
JsonObject obj = new JsonObject();
((Configurable) spell).setDefaults(obj);
spellConfig.getConfig().add(spell.getName(), obj);
spellConfig.saveConfig();
((Configurable) spell).load(obj);
} else {
((Configurable) spell).load(spellConfig.getConfig().get(spell.getName()).getAsJsonObject());
}
}
}
示例6: callEvent
import org.bukkit.event.Listener; //导入依赖的package包/类
@Override
public void callEvent(Event event) throws IllegalStateException
{
events.add(event);
for (Listener listener : eventListeners.values())
{
for (Method method : listener.getClass().getMethods())
{
if (method.isAnnotationPresent(EventHandler.class) && method.getParameterCount() == 1
&& method.getParameters()[0].getType().isInstance(event))
{
try
{
method.invoke(listener, event);
}
catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e)
{
throw new RuntimeException(e);
}
}
}
}
}
示例7: execute
import org.bukkit.event.Listener; //导入依赖的package包/类
@Override
// 计算调用次数和花费总时间以及花费最多的时间
public void execute(Listener listener, Event e) throws EventException {
if (e.isAsynchronous()) {
this.eventExecutor.execute(listener, e);
} else {
long startTime = System.nanoTime();
try {
this.eventExecutor.execute(listener, e);
} finally {
long endTime = System.nanoTime();
long executeTime = endTime - startTime;
this.record(e.getEventName(), executeTime);
}
}
}
示例8: unregister
import org.bukkit.event.Listener; //导入依赖的package包/类
public void unregister(@Nonnull Listener listener, @Nonnull Game game) {
//noinspection unchecked
Arrays.stream(listener.getClass().getMethods())
.filter((method -> method.isAnnotationPresent(GameEvent.class)))
.filter(method -> method.getParameterCount() != 1 || method.getParameterCount() != 2)
.filter(method -> Event.class.isAssignableFrom(method.getParameterTypes()[0]))
.map(method -> (Class<Event>) method.getParameterTypes()[0]).forEach(
eventClass -> activeEvents.get(eventClass).removeIf(registeredListener -> registeredListener.getListener().equals(listener)));
if (activeListeners.containsKey(game.getUuid())) {
activeListeners.get(game.getUuid()).removeIf(registeredListener -> registeredListener.getListener().equals(listener));
if (activeListeners.get(game.getUuid()).size() == 0) {
activeListeners.remove(game.getUuid());
}
}
HandlerList.unregisterAll(listener);
}
示例9: listener
import org.bukkit.event.Listener; //导入依赖的package包/类
@Override
protected Listener listener(Consumer<Block> callback) {
return new Listener() {
@EventHandler
public void onClick(PlayerInteractEvent event) {
Player clicker = event.getPlayer();
EquipmentSlot hand = event.getHand();
Block block = event.getClickedBlock();
if (block != null && clicker.equals(player)
&& (!MCUtils.isOffHandSupport() || hand == EquipmentSlot.HAND)) {
clicker.sendMessage(MCUtils.colorize(messageFunc.apply(block)));
callback.accept(block);
event.setCancelled(true);
}
}
};
}
示例10: init
import org.bukkit.event.Listener; //导入依赖的package包/类
public static void init(MCLibrary plugin) {
Bukkit.getPluginManager().registerEvents(new Listener() {
@EventHandler
@SuppressWarnings("deprecation")
public void onInteract(PlayerInteractEvent event) {
if (MCUtils.isOffHandSupport() && event.getHand() != EquipmentSlot.HAND)
return;
Player player = event.getPlayer();
Block block = event.getClickedBlock();
if (block == null || !INFO_LISTENERS.contains(player))
return;
Location loc = block.getLocation();
CommandSenderWrapper wrapper = new CommandSenderWrapper(player);
wrapper.sendMessage("---------------------------------------------");
wrapper.sendMessage("&eworld: &f" + loc.getWorld().getName());
wrapper.sendMessage(String.format("&ex: &f%s (%s)", loc.getBlockX(), loc.getX()));
wrapper.sendMessage(String.format("&ey: &f%s (%s)", loc.getBlockY(), loc.getY()));
wrapper.sendMessage(String.format("&ez: &f%s (%s)", loc.getBlockZ(), loc.getZ()));
wrapper.sendMessage(String.format("&eblock: &f%s:%s (%s)", block.getTypeId(), block.getData(), block.getType().name()));
}
}, plugin);
}
示例11: interactListener
import org.bukkit.event.Listener; //导入依赖的package包/类
public Listener interactListener() {
return new Listener() {
@EventHandler
public void onInteract(PlayerInteractEvent e) {
if (!(e.getPlayer().getEquipment().getItemInMainHand() == null)) { return; }
if (!(e.getItem() == null)) { return; }
if (!(e.getItem().getType() == null)) { return; }
if (!(e.getItem().getType() == Material.AIR)) { return; }
if (!e.getPlayer().getEquipment().getItemInMainHand().getItemMeta().getLore().contains(ChatColor.RED + getName())) { return; }
if (!applicableTo().contains(e.getItem().getType())) { return; }
if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
getRun().run(e.getPlayer(), e.getClickedBlock());
} else if (e.getAction() == Action.RIGHT_CLICK_AIR) {
getRun().run(e.getPlayer(), null);
}
}
};
}
示例12: register
import org.bukkit.event.Listener; //导入依赖的package包/类
@Override
public void register(final Trigger trigger) {
// if (types == null) {
// final BlockLocation l = new BlockLocation(world, x, y, z);
// List<Trigger> ts = blockTriggers.get(l);
// if (ts == null)
// blockTriggers.put(l, ts = new ArrayList<Trigger>());
// ts.add(trigger);
// } else {
for (final ItemType t : types) {
for (final ItemData d : t) {
if (d.getId() > Skript.MAXBLOCKID)
continue;
List<Trigger> ts = itemTypeTriggers.get(d.getId());
if (ts == null)
itemTypeTriggers.put(d.getId(), ts = new ArrayList<Trigger>());
ts.add(trigger);
}
}
// }
if (!registeredExecutor) {
Bukkit.getPluginManager().registerEvent(PlayerMoveEvent.class, new Listener() {}, SkriptConfig.defaultEventPriority.value(), executor, Skript.getInstance(), true);
registeredExecutor = true;
}
}
示例13: init
import org.bukkit.event.Listener; //导入依赖的package包/类
public static void init(JavaPlugin plugin) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
try {
BufferedReader reader = URLs.IP_CHECKER.openBufferedReader();
address = reader.readLine();
} catch (IOException e) {
// Ignore
}
});
Bukkit.getPluginManager().registerEvents(new Listener() {
@EventHandler
public void onLogin(PlayerLoginEvent event) throws UnknownHostException {
String host = event.getHostname();
String addrStr = host.substring(0, host.indexOf(':'));
InetAddress addr = InetAddress.getByName(addrStr);
if (!addr.isLoopbackAddress() && !addr.isSiteLocalAddress()) {
address = addrStr;
event.getHandlers().unregister(this);
}
}
}, plugin);
}
示例14: registerEvent
import org.bukkit.event.Listener; //导入依赖的package包/类
/**
* 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");
}
if (useTimings) {
getEventListeners(event).register(new TimedRegisteredListener(listener, executor, priority, plugin, ignoreCancelled));
} else {
getEventListeners(event).register(new RegisteredListener(listener, executor, priority, plugin, ignoreCancelled));
}
}
示例15: load
import org.bukkit.event.Listener; //导入依赖的package包/类
public static void load(CSCoreLib plugin) {
if (ReflectionUtils.getVersion().startsWith("v1_9_")) {
plugin.getServer().getPluginManager().registerEvents(new CustomBookOverlay1_9(), plugin);
}
else if (ReflectionUtils.getVersion().startsWith("v1_10_")) {
plugin.getServer().getPluginManager().registerEvents(new CustomBookOverlay1_9(), plugin);
}
else if (ReflectionUtils.getVersion().startsWith("v1_11_")) {
plugin.getServer().getPluginManager().registerEvents(new CustomBookOverlay1_9(), plugin);
}
else if (ReflectionUtils.getVersion().startsWith("v1_12_")) {
plugin.getServer().getPluginManager().registerEvents(new CustomBookOverlay1_9(), plugin);
}
plugin.getServer().getPluginManager().registerEvents(new Listener() {
@EventHandler
public void onDrop(PlayerDropItemEvent e) {
if (opening.contains(e.getPlayer().getUniqueId())) e.setCancelled(true);
}
}, plugin);
}