⚠️ 请勿将此插件与 StarCore 同时使用。StarCore 已提供 StarEvents,因此本插件不再需要。
这只是一个插件版本,旨在让其他开发者以及我的其他项目能够使用它,而无需依赖 StarCore(因为并非总是需要 StarCore)。
总共有三种构建产物:
- 依赖包 Artifact:这是基础包,也是用于发布 Maven 依赖的版本。请使用此版本进行开发。
- 插件包 Artifact:这是一个插件包,但它不包含所有必需的依赖项,需与其他库插件(如 StarMCLib)配合使用。此包仅包含 StarEvents 的代码。
- 独立包 Artifact:这是你在 SpigotMC 上下载的版本,是一个完全独立的版本,其所需的所有依赖项都已打包在插件中。
StarEvents 是一个将 Bukkit 事件传递到 StarLib IEventBus 的库。这使得事件系统具有广泛的灵活性。
主要用途以及我创建它的原因是为了允许监听那些未定义 HandlerList 的父级事件。例如,你可以监听基础的 Event 类,只需几行代码即可创建一个监听所有事件的监听器。
这也使得在 Lambda 表达式中使用监听器成为可能。
再次强调,这是插件版本,因此无需直接调用 StarEvents.init(),插件已经完成了初始化并处理了监听器的注册。在你的项目中直接使用此插件时,遵循标准的用法即可。
首先,存在一个名为 EventListener 的函数式接口,它允许为监听器创建 Lambda 表达式。
以下是一个示例:
StarEvents.registerListener(PlayerJoinEvent.class, e -> e.getPlayer().sendMessage("Hello from the lambda universe!"));
第一个参数是事件的类。这样在使用该特定事件中的方法时就不需要进行类型转换。
第二个参数是 Lambda 表达式。
从 StarEvents 0.2.0 开始,所有的事件检测都是动态的。具体过程是:首先检查 Spigot 和 Paper 的 Jar 文件,检测所有继承自 org.bukkit.event.Event、拥有静态 getHandlerList() 方法且未被标记为废弃的类,并将它们动态添加到加载插件的目录/codegen/StarEventsSpigotEventListener.class 中。每次通过独立插件或像 StarCore 这样的其他插件加载时,都会生成这些监听器。这些监听器会忽略取消标志,并具有 MONITOR 事件优先级。请在自定义监听器中使用 StarLib 的 @SubscribeEvent 注解来指定这一点。
然后,StarEvents 会查找除加载 StarEvents 自身的插件之外的所有插件,并按照上述要求搜索相同的事件类。因此,StarEvents 会检测其他插件的事件,每个插件都会获得自己的监听器,命名模式为 StarEvents%PLUGINNAME%Listener.class,位于同一文件夹中。仅当检测到相应插件时,才会重新生成并加载这些监听器。控制台可能会显示与此相关的警告。以下是一个警告示例:
[PlaceholderAPI] Loaded class com.stardevllc.starevents.BukkitEventListener from StarEvents v0.2.0 which is not a depend or softdepend of this plugin.
目前并没有简单的方法来解决这个问题,所以只能如此。
本插件免费提供的,并在 MIT 开源许可证下授权。我绝不会在任何地方出售这个库。如果你看到有人在售卖它,那不是我做的,请直接向我报告此类情况。
⚠️ Do not use this plugin AND StarCore together. StarCore already provides StarEvents and this plugin is not needed.
This is simply a plugin version to allow other developers and other projects of mine to use it without having to depend on StarCore as it is not always needed.
There are a total of three artifacts
- Dependency Artifact: This is the base artifact and is what is used for the published maven dependency. Use this to develop against
- Plugin Artifact: This artifact is a plugin, however it does not have all of the required dependencies and is to be used with the other library plugins like StarMCLib. This only contains the StarEvents code
- Standalone Artifact: This artifact is what you download here on SpigotMC and is a completely standalone version that has all the dependencies that it requires bundled into the plugin
StarEvents is a library that passes the Bukkit Events into a StarLib IEventBus. This allows for a wide range of flexibility in the event system.
The primary use case and the reason why I made it was to allow listening to parent events that do not have a HandlerList defined. For example, you can listen to the base Event class and you get a listener that listens for all events in just a few lines of code.
This also allows for the use case of having listeners in lambdas.
Again, this is the plugin version, so there is no need to directly call StarEvents.init() and the plugin does that already and handles the listener registration. To use this plugin in your own, it is just the standard way of using it.
The first thing is that there is a Functional Interface called EventListener that adds the ability to create lambdas for a listener.
Below is an example.
StarEvents.registerListener(PlayerJoinEvent.class, e -> e.getPlayer().sendMessage("Hello from the lambda universe!"));
The first argument is the class of the event. This makes it so that casts aren't needed to use the methods within that specific event.
The second argument is the lambda expression.
As of StarEvents 0.2.0, all event detection is dynamic. What happens is that the Jar files for Spigot and Paper are looked at first and all classes that extend org.bukkit.event.Event, have a static getHandlerList() method and are not deprecated are detected and dynamically added to the loader plugin's directory/codegen/StarEventsSpigotEventListener.class. This is generated each time that it is loaded either by the standalone plugin or another plugin like StarCore. These listeners ignore the cancelled flag and are of the MONITOR event priority. Use the StarLib @SubscribeEvent annotation in a custom listener to specify this.
Then StarEvents looks at all plugins except for the plugin that loads StarEvents itself and searches for the same requirements of an Event as above. So StarEvents detects other plugin events and each plugin gets their own listener with the pattern StarEvents%PLUGINNAME%Listener.class in the same folder. These are only regenerated and loaded if the plugin is detected. There will be warnings in console related to this though. The warning below is an example
[PlaceholderAPI] Loaded class com.stardevllc.starevents.BukkitEventListener from StarEvents v0.2.0 which is not a depend or softdepend of this plugin.
There is not an easy way to fix this though, so it will just have to exist.
This plugin is provided free of charge and under the MIT Open Source License. I will never sell this library anywhere. If you see it being sold, it is not by me and please report any of these cases to me directly.
评论 0
登录后参与评分
还没有评论,来抢沙发吧