Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
M
Mirai
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
Mirai
Commits
a9072679
Commit
a9072679
authored
Aug 08, 2019
by
Him188moe
Browse files
Options
Browse Files
Download
Plain Diff
Merge remote-tracking branch 'origin/master'
parents
dbfe12dd
78826c56
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
115 additions
and
5 deletions
+115
-5
mirai-core/src/main/java/net/mamoe/mirai/event/Cancelable.java
...-core/src/main/java/net/mamoe/mirai/event/Cancelable.java
+7
-1
mirai-core/src/main/java/net/mamoe/mirai/event/MiraiEvent.java
...-core/src/main/java/net/mamoe/mirai/event/MiraiEvent.java
+23
-0
mirai-core/src/main/java/net/mamoe/mirai/event/MiraiEventManager.java
...rc/main/java/net/mamoe/mirai/event/MiraiEventManager.java
+32
-4
mirai-core/src/main/java/net/mamoe/mirai/utils/EventException.java
...e/src/main/java/net/mamoe/mirai/utils/EventException.java
+53
-0
No files found.
mirai-core/src/main/java/net/mamoe/mirai/event/Cancelable.java
View file @
a9072679
package
net.mamoe.mirai.event
;
package
net.mamoe.mirai.event
;
import
lombok.Getter
;
interface
Cancelable
{
interface
Cancelable
{
void
setCancel
(
boolean
value
);
boolean
isCancelled
();
void
setCancelled
();
void
setCancelled
(
boolean
forceCancel
);
}
}
mirai-core/src/main/java/net/mamoe/mirai/event/MiraiEvent.java
View file @
a9072679
package
net.mamoe.mirai.event
;
package
net.mamoe.mirai.event
;
import
net.mamoe.jpre.event.Cancellable
;
import
net.mamoe.mirai.utils.EventException
;
public
abstract
class
MiraiEvent
{
public
abstract
class
MiraiEvent
{
private
boolean
cancelled
;
public
boolean
isCancelled
()
{
if
(!(
this
instanceof
Cancellable
))
{
throw
new
EventException
(
"Event is not Cancellable"
);
}
return
this
.
cancelled
;
}
public
void
setCancelled
()
{
setCancelled
(
true
);
}
public
void
setCancelled
(
boolean
value
)
{
if
(!(
this
instanceof
Cancellable
))
{
throw
new
EventException
(
"Event is not Cancellable"
);
}
this
.
cancelled
=
value
;
}
}
}
mirai-core/src/main/java/net/mamoe/mirai/event/MiraiEventManager.java
View file @
a9072679
...
@@ -3,12 +3,12 @@ package net.mamoe.mirai.event;
...
@@ -3,12 +3,12 @@ package net.mamoe.mirai.event;
import
lombok.AllArgsConstructor
;
import
lombok.AllArgsConstructor
;
import
lombok.Data
;
import
lombok.Data
;
import
java.util.ArrayList
;
import
java.util.*
;
import
java.util.HashMap
;
import
java.util.concurrent.locks.Lock
;
import
java.util.List
;
import
java.util.concurrent.locks.ReentrantLock
;
import
java.util.Map
;
import
java.util.function.Consumer
;
import
java.util.function.Consumer
;
import
java.util.function.Predicate
;
import
java.util.function.Predicate
;
import
java.util.stream.Collectors
;
public
class
MiraiEventManager
{
public
class
MiraiEventManager
{
private
MiraiEventManager
(){
private
MiraiEventManager
(){
...
@@ -24,6 +24,7 @@ public class MiraiEventManager {
...
@@ -24,6 +24,7 @@ public class MiraiEventManager {
return
MiraiEventManager
.
instance
;
return
MiraiEventManager
.
instance
;
}
}
Lock
hooksLock
=
new
ReentrantLock
();
private
Map
<
Class
<?
extends
MiraiEvent
>,
List
<
MiraiEventConsumer
<?
extends
MiraiEvent
>>>
hooks
=
new
HashMap
<>();
private
Map
<
Class
<?
extends
MiraiEvent
>,
List
<
MiraiEventConsumer
<?
extends
MiraiEvent
>>>
hooks
=
new
HashMap
<>();
public
<
D
extends
MiraiEvent
>
void
registerUntil
(
MiraiEventHook
<
D
>
hook
,
Predicate
<
D
>
toRemove
){
public
<
D
extends
MiraiEvent
>
void
registerUntil
(
MiraiEventHook
<
D
>
hook
,
Predicate
<
D
>
toRemove
){
...
@@ -39,11 +40,38 @@ public class MiraiEventManager {
...
@@ -39,11 +40,38 @@ public class MiraiEventManager {
this
.
registerUntil
(
hook
,(
a
)
->
false
);
this
.
registerUntil
(
hook
,(
a
)
->
false
);
}
}
public
void
boardcastEvent
(
MiraiEvent
event
){
hooksLock
.
lock
();
if
(
hooks
.
containsKey
(
event
.
getClass
())){
hooks
.
put
(
event
.
getClass
(),
hooks
.
get
(
event
.
getClass
())
.
stream
()
.
sorted
(
Comparator
.
comparingInt
(
MiraiEventConsumer:
:
getPriority
))
.
dropWhile
(
a
->
a
.
accept
(
event
))
.
collect
(
Collectors
.
toList
())
);
}
hooksLock
.
unlock
();
}
}
}
@Data
@Data
@AllArgsConstructor
@AllArgsConstructor
class
MiraiEventConsumer
<
T
extends
MiraiEvent
>{
class
MiraiEventConsumer
<
T
extends
MiraiEvent
>{
private
MiraiEventHook
<
T
>
hook
;
private
MiraiEventHook
<
T
>
hook
;
private
Predicate
<
T
>
remove
;
private
Predicate
<
T
>
remove
;
public
int
getPriority
(){
return
hook
.
getPreferences
().
getPriority
();
}
@SuppressWarnings
(
"unchecked"
)
public
boolean
accept
(
MiraiEvent
event
)
{
if
(!(
event
instanceof
Cancelable
&&
event
.
isCancelled
()
&&
hook
.
getPreferences
().
isIgnoreCanceled
())){
hook
.
getHandler
().
accept
((
T
)
event
);
}
return
remove
.
test
((
T
)
event
);
}
}
}
mirai-core/src/main/java/net/mamoe/mirai/utils/EventException.java
0 → 100644
View file @
a9072679
package
net.mamoe.mirai.utils
;
public
class
EventException
extends
RuntimeException
{
private
final
Throwable
cause
;
/**
* Constructs a new EventException based on the given Exception
*
* @param throwable Exception that triggered this Exception
*/
public
EventException
(
Throwable
throwable
)
{
cause
=
throwable
;
}
/**
* Constructs a new EventException
*/
public
EventException
()
{
cause
=
null
;
}
/**
* Constructs a new EventException with the given message
*
* @param cause The exception that caused this
* @param message The message
*/
public
EventException
(
Throwable
cause
,
String
message
)
{
super
(
message
);
this
.
cause
=
cause
;
}
/**
* Constructs a new EventException with the given message
*
* @param message The message
*/
public
EventException
(
String
message
)
{
super
(
message
);
cause
=
null
;
}
/**
* If applicable, returns the Exception that triggered this Exception
*
* @return Inner exception, or null if one does not exist
*/
@Override
public
Throwable
getCause
()
{
return
cause
;
}
}
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