Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
ziqian zhang
Grasscutter
Commits
6056e962
Commit
6056e962
authored
Apr 23, 2022
by
KingRainbow44
Browse files
Implement an event handler
parent
7a3fbcdc
Changes
6
Hide whitespace changes
Inline
Side-by-side
src/main/java/emu/grasscutter/plugin/PluginManager.java
View file @
6056e962
package
emu.grasscutter.plugin
;
import
emu.grasscutter.Grasscutter
;
import
emu.grasscutter.server.event.Event
;
import
emu.grasscutter.server.event.EventHandler
;
import
emu.grasscutter.server.event.Listener
;
import
emu.grasscutter.utils.Utils
;
import
org.reflections.Reflections
;
import
java.io.File
;
import
java.io.InputStreamReader
;
import
java.lang.reflect.Method
;
import
java.net.URL
;
import
java.net.URLClassLoader
;
import
java.util.Arrays
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.*
;
/**
* Manages the server's plugins & the event system.
*/
public
final
class
PluginManager
{
private
final
Map
<
String
,
Plugin
>
plugins
=
new
HashMap
<>();
private
final
Map
<
Plugin
,
List
<
Listener
>>
listeners
=
new
HashMap
<>();
public
PluginManager
()
{
this
.
loadPlugins
();
// Load all plugins from the plugins directory.
...
...
@@ -113,4 +115,38 @@ public final class PluginManager {
plugin
.
onDisable
();
});
}
/**
* Registers a plugin's event listener.
* @param plugin The plugin instance.
* @param listener The event listener.
*/
public
void
registerListener
(
Plugin
plugin
,
Listener
listener
)
{
this
.
listeners
.
computeIfAbsent
(
plugin
,
k
->
new
ArrayList
<>()).
add
(
listener
);
}
/**
* Invoke the provided event on all registered event listeners.
* @param event The event to invoke.
*/
public
void
invokeEvent
(
Event
event
)
{
this
.
listeners
.
values
().
stream
()
.
flatMap
(
Collection:
:
stream
)
.
forEach
(
listener
->
this
.
invokeOnListener
(
listener
,
event
));
}
/**
* Attempts to invoke the event on the provided listener.
*/
private
void
invokeOnListener
(
Listener
listener
,
Event
event
)
{
try
{
Class
<?>
listenerClass
=
listener
.
getClass
();
Method
[]
methods
=
listenerClass
.
getMethods
();
for
(
Method
method
:
methods
)
{
if
(!
method
.
isAnnotationPresent
(
EventHandler
.
class
))
return
;
if
(!
method
.
getParameterTypes
()[
0
].
isAssignableFrom
(
event
.
getClass
()))
return
;
method
.
invoke
(
listener
,
event
);
}
}
catch
(
Exception
ignored
)
{
}
}
}
\ No newline at end of file
src/main/java/emu/grasscutter/server/event/Cancellable.java
0 → 100644
View file @
6056e962
package
emu.grasscutter.server.event
;
/**
* Implementing this interface marks an event as cancellable.
*/
public
interface
Cancellable
{
void
cancel
();
}
src/main/java/emu/grasscutter/server/event/Event.java
0 → 100644
View file @
6056e962
package
emu.grasscutter.server.event
;
import
emu.grasscutter.Grasscutter
;
/**
* A generic server event.
*/
public
abstract
class
Event
{
private
boolean
cancelled
=
false
;
/**
* Return the cancelled state of the event.
*/
public
boolean
isCanceled
()
{
return
this
.
cancelled
;
}
/**
* Cancels the event if possible.
*/
public
void
cancel
()
throws
IllegalAccessException
{
if
(!(
this
instanceof
Cancellable
))
throw
new
IllegalAccessException
(
"Event is not cancellable."
);
this
.
cancelled
=
true
;
}
/**
* Pushes this event to all listeners.
*/
public
void
call
()
{
Grasscutter
.
getPluginManager
().
invokeEvent
(
this
);
}
}
src/main/java/emu/grasscutter/server/event/EventHandler.java
0 → 100644
View file @
6056e962
package
emu.grasscutter.server.event
;
import
java.lang.annotation.Retention
;
import
java.lang.annotation.RetentionPolicy
;
/**
* Declares a class as an event listener/handler.
*/
@Retention
(
RetentionPolicy
.
RUNTIME
)
public
@interface
EventHandler
{
}
\ No newline at end of file
src/main/java/emu/grasscutter/server/event/Listener.java
0 → 100644
View file @
6056e962
package
emu.grasscutter.server.event
;
/**
* Implementing this interface declares a class as an event listener.
*/
public
interface
Listener
{
}
src/main/java/emu/grasscutter/server/event/ServerEvent.java
0 → 100644
View file @
6056e962
package
emu.grasscutter.server.event
;
/**
* An event that is related to the internals of the server.
*/
public
abstract
class
ServerEvent
extends
Event
{
protected
final
Type
type
;
public
ServerEvent
(
Type
type
)
{
this
.
type
=
type
;
}
public
enum
Type
{
DISPATCH
,
GAME
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment