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
25c414be
Commit
25c414be
authored
Apr 18, 2022
by
KingRainbow44
Browse files
Fix registering error & implement handling
parent
9cd65046
Changes
4
Show whitespace changes
Inline
Side-by-side
src/main/java/emu/grasscutter/Grasscutter.java
View file @
25c414be
...
@@ -16,7 +16,6 @@ import com.google.gson.Gson;
...
@@ -16,7 +16,6 @@ import com.google.gson.Gson;
import
com.google.gson.GsonBuilder
;
import
com.google.gson.GsonBuilder
;
import
ch.qos.logback.classic.Logger
;
import
ch.qos.logback.classic.Logger
;
import
emu.grasscutter.commands.ServerCommands
;
import
emu.grasscutter.data.ResourceLoader
;
import
emu.grasscutter.data.ResourceLoader
;
import
emu.grasscutter.database.DatabaseManager
;
import
emu.grasscutter.database.DatabaseManager
;
import
emu.grasscutter.server.dispatch.DispatchServer
;
import
emu.grasscutter.server.dispatch.DispatchServer
;
...
@@ -103,7 +102,11 @@ public final class Grasscutter {
...
@@ -103,7 +102,11 @@ public final class Grasscutter {
String
input
;
String
input
;
try
(
BufferedReader
br
=
new
BufferedReader
(
new
InputStreamReader
(
System
.
in
)))
{
try
(
BufferedReader
br
=
new
BufferedReader
(
new
InputStreamReader
(
System
.
in
)))
{
while
((
input
=
br
.
readLine
())
!=
null
)
{
while
((
input
=
br
.
readLine
())
!=
null
)
{
try
{
CommandMap
.
getInstance
().
invoke
(
null
,
input
);
CommandMap
.
getInstance
().
invoke
(
null
,
input
);
}
catch
(
Exception
e
)
{
Grasscutter
.
getLogger
().
error
(
"Command error: "
+
e
.
getMessage
());
}
}
}
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
Grasscutter
.
getLogger
().
error
(
"An error occurred."
,
e
);
Grasscutter
.
getLogger
().
error
(
"An error occurred."
,
e
);
...
...
src/main/java/emu/grasscutter/commands/Command.java
View file @
25c414be
...
@@ -7,7 +7,7 @@ import java.lang.annotation.RetentionPolicy;
...
@@ -7,7 +7,7 @@ import java.lang.annotation.RetentionPolicy;
public
@interface
Command
{
public
@interface
Command
{
String
label
()
default
""
;
String
label
()
default
""
;
String
[]
aliases
()
default
""
;
String
[]
aliases
()
default
{
""
}
;
int
gmLevel
()
default
1
;
int
gmLevel
()
default
1
;
...
...
src/main/java/emu/grasscutter/commands/CommandMap.java
View file @
25c414be
...
@@ -21,7 +21,15 @@ public final class CommandMap {
...
@@ -21,7 +21,15 @@ public final class CommandMap {
* @return Instance chaining.
* @return Instance chaining.
*/
*/
public
CommandMap
registerCommand
(
String
label
,
CommandHandler
command
)
{
public
CommandMap
registerCommand
(
String
label
,
CommandHandler
command
)
{
this
.
commands
.
put
(
label
,
command
);
return
this
;
Grasscutter
.
getLogger
().
debug
(
"Registered command: "
+
label
);
Command
annotation
=
command
.
getClass
().
getAnnotation
(
Command
.
class
);
if
(
annotation
.
aliases
().
length
>
0
)
{
for
(
String
alias
:
annotation
.
aliases
())
this
.
commands
.
put
(
alias
,
command
);
}
this
.
commands
.
put
(
label
,
command
);
return
this
;
}
}
/**
/**
...
@@ -30,7 +38,17 @@ public final class CommandMap {
...
@@ -30,7 +38,17 @@ public final class CommandMap {
* @return Instance chaining.
* @return Instance chaining.
*/
*/
public
CommandMap
unregisterCommand
(
String
label
)
{
public
CommandMap
unregisterCommand
(
String
label
)
{
this
.
commands
.
remove
(
label
);
return
this
;
Grasscutter
.
getLogger
().
debug
(
"Unregistered command: "
+
label
);
CommandHandler
handler
=
this
.
commands
.
get
(
label
);
if
(
handler
==
null
)
return
this
;
Command
annotation
=
handler
.
getClass
().
getAnnotation
(
Command
.
class
);
if
(
annotation
.
aliases
().
length
>
0
)
{
for
(
String
alias
:
annotation
.
aliases
())
this
.
commands
.
remove
(
alias
);
}
this
.
commands
.
remove
(
label
);
return
this
;
}
}
/**
/**
...
@@ -39,13 +57,18 @@ public final class CommandMap {
...
@@ -39,13 +57,18 @@ public final class CommandMap {
* @param rawMessage The messaged used to invoke the command.
* @param rawMessage The messaged used to invoke the command.
*/
*/
public
void
invoke
(
GenshinPlayer
player
,
String
rawMessage
)
{
public
void
invoke
(
GenshinPlayer
player
,
String
rawMessage
)
{
rawMessage
=
rawMessage
.
trim
();
if
(
rawMessage
.
length
()
==
0
)
{
CommandHandler
.
sendMessage
(
player
,
"No command specified."
);
}
// Remove prefix if present.
// Remove prefix if present.
if
(!
Character
.
isLetter
(
rawMessage
.
charAt
(
0
)))
if
(!
Character
.
isLetter
(
rawMessage
.
charAt
(
0
)))
rawMessage
=
rawMessage
.
substring
(
1
);
rawMessage
=
rawMessage
.
substring
(
1
);
// Parse message.
// Parse message.
String
[]
split
=
rawMessage
.
split
(
" "
);
String
[]
split
=
rawMessage
.
split
(
" "
);
List
<
String
>
args
=
Arrays
.
asList
(
split
);
List
<
String
>
args
=
new
LinkedList
<>(
Arrays
.
asList
(
split
)
)
;
String
label
=
args
.
remove
(
0
);
String
label
=
args
.
remove
(
0
);
// Get command handler.
// Get command handler.
...
@@ -73,15 +96,17 @@ public final class CommandMap {
...
@@ -73,15 +96,17 @@ public final class CommandMap {
*/
*/
private
void
scan
()
{
private
void
scan
()
{
Reflections
reflector
=
Grasscutter
.
reflector
;
Reflections
reflector
=
Grasscutter
.
reflector
;
Set
<
?
>
classes
=
reflector
.
getTypesAnnotatedWith
(
Command
.
class
);
Set
<
Class
<?>
>
classes
=
reflector
.
getTypesAnnotatedWith
(
Command
.
class
);
classes
.
forEach
(
annotated
->
{
classes
.
forEach
(
annotated
->
{
try
{
try
{
Class
<?>
cls
=
annotated
.
getClass
();
Command
cmdData
=
annotated
.
getAnnotation
(
Command
.
class
);
Command
cmdData
=
cls
.
getAnnotation
(
Command
.
class
);
Object
object
=
annotated
.
newInstance
();
Object
object
=
cls
.
getDeclaredConstructors
()[
0
].
newInstance
();
if
(
object
instanceof
CommandHandler
)
if
(
object
instanceof
CommandHandler
)
this
.
registerCommand
(
cmdData
.
label
(),
(
CommandHandler
)
object
);
this
.
registerCommand
(
cmdData
.
label
(),
(
CommandHandler
)
object
);
}
catch
(
Exception
ignored
)
{
}
else
Grasscutter
.
getLogger
().
error
(
"Class "
+
annotated
.
getName
()
+
" is not a CommandHandler!"
);
}
catch
(
Exception
exception
)
{
Grasscutter
.
getLogger
().
error
(
"Failed to register command handler for "
+
annotated
.
getSimpleName
(),
exception
);
}
});
});
}
}
}
}
src/main/java/emu/grasscutter/game/managers/ChatManager.java
View file @
25c414be
package
emu.grasscutter.game.managers
;
package
emu.grasscutter.game.managers
;
import
emu.grasscutter.commands.CommandMap
;
import
emu.grasscutter.commands.CommandMap
;
import
emu.grasscutter.commands.PlayerCommands
;
import
emu.grasscutter.game.GenshinPlayer
;
import
emu.grasscutter.game.GenshinPlayer
;
import
emu.grasscutter.net.packet.GenshinPacket
;
import
emu.grasscutter.net.packet.GenshinPacket
;
import
emu.grasscutter.server.game.GameServer
;
import
emu.grasscutter.server.game.GameServer
;
...
...
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