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
ae6a6abc
Commit
ae6a6abc
authored
Apr 26, 2022
by
KingRainbow44
Browse files
woo back to generics
parent
74929335
Changes
3
Show whitespace changes
Inline
Side-by-side
src/main/java/emu/grasscutter/plugin/PluginManager.java
View file @
ae6a6abc
...
...
@@ -21,7 +21,7 @@ import java.util.jar.JarFile;
*/
public
final
class
PluginManager
{
private
final
Map
<
String
,
Plugin
>
plugins
=
new
HashMap
<>();
private
final
List
<
EventHandler
>
listeners
=
new
LinkedList
<>();
private
final
List
<
EventHandler
<?
extends
Event
>
>
listeners
=
new
LinkedList
<>();
public
PluginManager
()
{
this
.
loadPlugins
();
// Load all plugins from the plugins directory.
...
...
@@ -131,7 +131,7 @@ public final class PluginManager {
* Registers a plugin's event listener.
* @param listener The event listener.
*/
public
void
registerListener
(
EventHandler
listener
)
{
public
void
registerListener
(
EventHandler
<?
extends
Event
>
listener
)
{
this
.
listeners
.
add
(
listener
);
}
...
...
@@ -161,9 +161,10 @@ public final class PluginManager {
* @param event The event passed through to the handler.
* @param handler The handler to invoke.
*/
private
void
invokeHandler
(
Event
event
,
EventHandler
handler
)
{
@SuppressWarnings
(
"unchecked"
)
private
<
T
extends
Event
>
void
invokeHandler
(
Event
event
,
EventHandler
<
T
>
handler
)
{
if
(!
event
.
isCanceled
()
||
(
event
.
isCanceled
()
&&
handler
.
ignoresCanceled
())
)
handler
.
getCallback
().
consume
(
event
);
)
handler
.
getCallback
().
consume
(
(
T
)
event
);
}
}
\ No newline at end of file
src/main/java/emu/grasscutter/server/event/EventHandler.java
View file @
ae6a6abc
...
...
@@ -3,39 +3,32 @@ package emu.grasscutter.server.event;
import
emu.grasscutter.Grasscutter
;
import
emu.grasscutter.utils.EventConsumer
;
public
final
class
EventHandler
{
private
final
Class
<?
extends
Event
>
event
;
import
java.lang.reflect.ParameterizedType
;
/**
* Creates an instance of {@link EventHandler} for the specified event.
* @param event The event to handle.
* @return An instance of {@link EventHandler}.
*/
public
static
EventHandler
forEvent
(
Class
<?
extends
Event
>
event
)
{
return
new
EventHandler
(
event
);
}
private
EventHandler
(
Class
<?
extends
Event
>
event
)
{
this
.
event
=
event
;
}
private
EventConsumer
listener
;
public
final
class
EventHandler
<
T
extends
Event
>
{
private
final
Class
<
T
>
eventClass
;
private
EventConsumer
<
T
>
listener
;
private
HandlerPriority
priority
;
private
boolean
handleCanceled
;
@SuppressWarnings
(
"unchecked"
)
public
EventHandler
()
{
this
.
eventClass
=
(
Class
<
T
>)
((
ParameterizedType
)
(
getClass
().
getGenericSuperclass
())).
getActualTypeArguments
()[
0
];
}
/**
* Gets which event this handler is handling.
* @return An event class.
*/
public
Class
<
?
extends
Event
>
handles
()
{
return
this
.
event
;
public
Class
<
T
>
handles
()
{
return
this
.
event
Class
;
}
/**
* Returns the callback for the handler.
* @return A consumer callback.
*/
public
EventConsumer
getCallback
()
{
public
EventConsumer
<
T
>
getCallback
()
{
return
this
.
listener
;
}
...
...
@@ -60,7 +53,7 @@ public final class EventHandler {
* @param listener An event handler method.
* @return Method chaining.
*/
public
EventHandler
listener
(
EventConsumer
listener
)
{
public
EventHandler
<
T
>
listener
(
EventConsumer
<
T
>
listener
)
{
this
.
listener
=
listener
;
return
this
;
}
...
...
@@ -69,7 +62,7 @@ public final class EventHandler {
* @param priority The priority of the handler.
* @return Method chaining.
*/
public
EventHandler
priority
(
HandlerPriority
priority
)
{
public
EventHandler
<
T
>
priority
(
HandlerPriority
priority
)
{
this
.
priority
=
priority
;
return
this
;
}
...
...
@@ -78,7 +71,7 @@ public final class EventHandler {
* @param ignore If the handler should ignore cancelled events.
* @return Method chaining.
*/
public
EventHandler
ignore
(
boolean
ignore
)
{
public
EventHandler
<
T
>
ignore
(
boolean
ignore
)
{
this
.
handleCanceled
=
ignore
;
return
this
;
}
...
...
src/main/java/emu/grasscutter/utils/EventConsumer.java
View file @
ae6a6abc
...
...
@@ -2,6 +2,6 @@ package emu.grasscutter.utils;
import
emu.grasscutter.server.event.Event
;
public
interface
EventConsumer
{
<
T
extends
Event
>
void
consume
(
T
event
);
public
interface
EventConsumer
<
T
extends
Event
>
{
void
consume
(
T
event
);
}
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