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
82ec63c4
Commit
82ec63c4
authored
May 04, 2022
by
AnimeGitB
Committed by
Melledy
May 05, 2022
Browse files
[WIP] Command Targeting overhaul
parent
4e0ebe56
Changes
41
Show whitespace changes
Inline
Side-by-side
src/main/java/emu/grasscutter/Grasscutter.java
View file @
82ec63c4
...
...
@@ -221,7 +221,7 @@ public final class Grasscutter {
isLastInterrupted
=
false
;
try
{
CommandMap
.
getInstance
().
invoke
(
null
,
input
);
CommandMap
.
getInstance
().
invoke
(
null
,
null
,
input
);
}
catch
(
Exception
e
)
{
Grasscutter
.
getLogger
().
error
(
language
.
Command_error
,
e
);
}
...
...
src/main/java/emu/grasscutter/command/Command.java
View file @
82ec63c4
...
...
@@ -15,5 +15,7 @@ public @interface Command {
String
permission
()
default
""
;
String
permissionTargeted
()
default
""
;
boolean
threading
()
default
false
;
}
src/main/java/emu/grasscutter/command/CommandHandler.java
View file @
82ec63c4
...
...
@@ -25,6 +25,6 @@ public interface CommandHandler {
* @param sender The player/console that invoked the command.
* @param args The arguments to the command.
*/
default
void
execute
(
Player
sender
,
List
<
String
>
args
)
{
default
void
execute
(
Player
sender
,
Player
targetPlayer
,
List
<
String
>
args
)
{
}
}
src/main/java/emu/grasscutter/command/CommandMap.java
View file @
82ec63c4
...
...
@@ -12,6 +12,8 @@ import java.util.*;
public
final
class
CommandMap
{
private
final
Map
<
String
,
CommandHandler
>
commands
=
new
HashMap
<>();
private
final
Map
<
String
,
Command
>
annotations
=
new
HashMap
<>();
private
final
Map
<
String
,
Player
>
targetPlayers
=
new
HashMap
<>();
private
static
final
String
consoleId
=
"console"
;
public
CommandMap
()
{
this
(
false
);
}
...
...
@@ -104,7 +106,7 @@ public final class CommandMap {
* @param player The player invoking the command or null for the server console.
* @param rawMessage The messaged used to invoke the command.
*/
public
void
invoke
(
Player
player
,
String
rawMessage
)
{
public
void
invoke
(
Player
player
,
Player
targetPlayer
,
String
rawMessage
)
{
rawMessage
=
rawMessage
.
trim
();
if
(
rawMessage
.
length
()
==
0
)
{
CommandHandler
.
sendMessage
(
player
,
Grasscutter
.
getLanguage
().
No_command_specified
);
...
...
@@ -119,6 +121,29 @@ public final class CommandMap {
String
[]
split
=
rawMessage
.
split
(
" "
);
List
<
String
>
args
=
new
LinkedList
<>(
Arrays
.
asList
(
split
));
String
label
=
args
.
remove
(
0
);
// Check for special case
String
playerId
=
(
player
==
null
)
?
consoleId
:
player
.
getAccount
().
getId
();
if
(
label
==
"target"
)
{
// Sets or clears default targetPlayer
if
(
args
.
size
()
<
1
)
{
targetPlayers
.
remove
(
playerId
);
CommandHandler
.
sendMessage
(
player
,
Grasscutter
.
getLanguage
().
Target_cleared
);
}
else
{
try
{
String
sUid
=
args
.
get
(
0
);
int
uid
=
Integer
.
parseInt
(
sUid
);
targetPlayer
=
Grasscutter
.
getGameServer
().
getPlayerByUid
(
uid
);
if
(
targetPlayer
==
null
)
{
CommandHandler
.
sendMessage
(
player
,
Grasscutter
.
getLanguage
().
Player_not_found_or_offline
);
}
else
{
targetPlayers
.
put
(
playerId
,
targetPlayer
);
CommandHandler
.
sendMessage
(
player
,
Grasscutter
.
getLanguage
().
Target_set
.
replace
(
"{uid}"
,
sUid
));
}
}
catch
(
NumberFormatException
e
)
{
CommandHandler
.
sendMessage
(
player
,
Grasscutter
.
getLanguage
().
Invalid_UID
);
}
}
return
;
}
// Get command handler.
CommandHandler
handler
=
this
.
commands
.
get
(
label
);
if
(
handler
==
null
)
{
...
...
@@ -126,10 +151,43 @@ public final class CommandMap {
return
;
}
// If any @UID argument is present, override targetPlayer with it
for
(
int
i
=
0
;
i
<
args
.
size
();
i
++)
{
String
arg
=
args
.
get
(
i
);
if
(!
arg
.
startsWith
(
"@"
))
{
continue
;
}
else
{
arg
=
args
.
remove
(
i
).
substring
(
1
);
try
{
int
uid
=
Integer
.
parseInt
(
arg
);
targetPlayer
=
Grasscutter
.
getGameServer
().
getPlayerByUid
(
uid
);
if
(
targetPlayer
==
null
)
{
CommandHandler
.
sendMessage
(
player
,
Grasscutter
.
getLanguage
().
Player_not_found_or_offline
);
return
;
}
break
;
}
catch
(
NumberFormatException
e
)
{
CommandHandler
.
sendMessage
(
player
,
Grasscutter
.
getLanguage
().
Invalid_UID
);
return
;
}
}
}
// If there's still no targetPlayer at this point, use previously-set target
if
(
targetPlayer
==
null
)
{
targetPlayer
=
targetPlayers
.
getOrDefault
(
playerId
,
null
);
}
// Check for permission.
if
(
player
!=
null
)
{
String
permissionNode
=
this
.
annotations
.
get
(
label
).
permission
();
String
permissionNodeTargeted
=
this
.
annotations
.
get
(
label
).
permissionTargeted
();
Account
account
=
player
.
getAccount
();
if
(
player
!=
targetPlayer
)
{
// Additional permission required for targeting another player
if
(!
permissionNodeTargeted
.
isEmpty
()
&&
!
account
.
hasPermission
(
permissionNodeTargeted
))
{
CommandHandler
.
sendMessage
(
player
,
Grasscutter
.
getLanguage
().
You_not_permission_run_command
);
return
;
}
}
if
(!
permissionNode
.
isEmpty
()
&&
!
account
.
hasPermission
(
permissionNode
))
{
CommandHandler
.
sendMessage
(
player
,
Grasscutter
.
getLanguage
().
You_not_permission_run_command
);
return
;
...
...
@@ -138,7 +196,8 @@ public final class CommandMap {
// Invoke execute method for handler.
boolean
threading
=
this
.
annotations
.
get
(
label
).
threading
();
Runnable
runnable
=
()
->
handler
.
execute
(
player
,
args
);
final
Player
targetPlayerF
=
targetPlayer
;
// Is there a better way to do this?
Runnable
runnable
=
()
->
handler
.
execute
(
player
,
targetPlayerF
,
args
);
if
(
threading
)
{
Thread
command
=
new
Thread
(
runnable
);
command
.
start
();
...
...
src/main/java/emu/grasscutter/command/commands/AccountCommand.java
View file @
82ec63c4
...
...
@@ -12,7 +12,7 @@ import java.util.List;
public
final
class
AccountCommand
implements
CommandHandler
{
@Override
public
void
execute
(
Player
sender
,
List
<
String
>
args
)
{
public
void
execute
(
Player
sender
,
Player
targetPlayer
,
List
<
String
>
args
)
{
if
(
sender
!=
null
)
{
CommandHandler
.
sendMessage
(
sender
,
Grasscutter
.
getLanguage
().
This_command_can_only_run_from_console
);
return
;
...
...
src/main/java/emu/grasscutter/command/commands/BroadcastCommand.java
View file @
82ec63c4
...
...
@@ -12,7 +12,7 @@ import java.util.List;
public
final
class
BroadcastCommand
implements
CommandHandler
{
@Override
public
void
execute
(
Player
sender
,
List
<
String
>
args
)
{
public
void
execute
(
Player
sender
,
Player
targetPlayer
,
List
<
String
>
args
)
{
if
(
args
.
size
()
<
1
)
{
CommandHandler
.
sendMessage
(
sender
,
Grasscutter
.
getLanguage
().
Broadcast_command_usage
);
return
;
...
...
src/main/java/emu/grasscutter/command/commands/ChangeSceneCommand.java
View file @
82ec63c4
...
...
@@ -12,7 +12,7 @@ import java.util.List;
description
=
"Changes your scene"
,
aliases
=
{
"scene"
},
permission
=
"player.changescene"
)
public
final
class
ChangeSceneCommand
implements
CommandHandler
{
@Override
public
void
execute
(
Player
sender
,
List
<
String
>
args
)
{
public
void
execute
(
Player
sender
,
Player
targetPlayer
,
List
<
String
>
args
)
{
if
(
sender
==
null
)
{
CommandHandler
.
sendMessage
(
null
,
Grasscutter
.
getLanguage
().
Run_this_command_in_game
);
return
;
...
...
src/main/java/emu/grasscutter/command/commands/ClearCommand.java
View file @
82ec63c4
...
...
@@ -16,7 +16,7 @@ import java.util.List;
public
final
class
ClearCommand
implements
CommandHandler
{
@Override
public
void
execute
(
Player
sender
,
List
<
String
>
args
)
{
public
void
execute
(
Player
sender
,
Player
targetPlayer
,
List
<
String
>
args
)
{
int
target
;
String
cmdSwitch
=
""
;
if
(
sender
==
null
)
{
...
...
@@ -32,7 +32,6 @@ public final class ClearCommand implements CommandHandler {
cmdSwitch
=
args
.
get
(
1
);
target
=
Integer
.
parseInt
(
args
.
get
(
0
));
}
Player
targetPlayer
=
Grasscutter
.
getGameServer
().
getPlayerByUid
(
target
);
switch
(
cmdSwitch
)
{
case
"wp"
->
{
playerInventory
.
getItems
().
values
().
stream
()
...
...
@@ -99,7 +98,6 @@ public final class ClearCommand implements CommandHandler {
return
;
}
Player
targetPlayer
=
Grasscutter
.
getGameServer
().
getPlayerByUid
(
target
);
if
(
targetPlayer
==
null
)
{
CommandHandler
.
sendMessage
(
sender
,
Grasscutter
.
getLanguage
().
Player_not_found
);
return
;
...
...
src/main/java/emu/grasscutter/command/commands/CoopCommand.java
View file @
82ec63c4
...
...
@@ -11,7 +11,7 @@ import java.util.List;
description
=
"Forces someone to join the world of others"
,
permission
=
"server.coop"
)
public
final
class
CoopCommand
implements
CommandHandler
{
@Override
public
void
execute
(
Player
sender
,
List
<
String
>
args
)
{
public
void
execute
(
Player
sender
,
Player
targetPlayer
,
List
<
String
>
args
)
{
if
(
args
.
size
()
<
2
)
{
CommandHandler
.
sendMessage
(
sender
,
Grasscutter
.
getLanguage
().
Coop_usage
);
return
;
...
...
src/main/java/emu/grasscutter/command/commands/DropCommand.java
View file @
82ec63c4
...
...
@@ -16,7 +16,7 @@ import java.util.List;
public
final
class
DropCommand
implements
CommandHandler
{
@Override
public
void
execute
(
Player
sender
,
List
<
String
>
args
)
{
public
void
execute
(
Player
sender
,
Player
targetPlayer
,
List
<
String
>
args
)
{
if
(
sender
==
null
)
{
CommandHandler
.
sendMessage
(
null
,
Grasscutter
.
getLanguage
().
Run_this_command_in_game
);
return
;
...
...
src/main/java/emu/grasscutter/command/commands/EnterDungeonCommand.java
View file @
82ec63c4
...
...
@@ -11,7 +11,7 @@ import java.util.List;
description
=
"Enter a dungeon"
,
aliases
=
{
"dungeon"
},
permission
=
"player.enterdungeon"
)
public
final
class
EnterDungeonCommand
implements
CommandHandler
{
@Override
public
void
execute
(
Player
sender
,
List
<
String
>
args
)
{
public
void
execute
(
Player
sender
,
Player
targetPlayer
,
List
<
String
>
args
)
{
if
(
sender
==
null
)
{
CommandHandler
.
sendMessage
(
null
,
Grasscutter
.
getLanguage
().
Run_this_command_in_game
);
return
;
...
...
src/main/java/emu/grasscutter/command/commands/GiveAllCommand.java
View file @
82ec63c4
...
...
@@ -18,7 +18,7 @@ import java.util.*;
public
final
class
GiveAllCommand
implements
CommandHandler
{
@Override
public
void
execute
(
Player
sender
,
List
<
String
>
args
)
{
public
void
execute
(
Player
sender
,
Player
targetPlayer
,
List
<
String
>
args
)
{
int
target
,
amount
=
99999
;
switch
(
args
.
size
())
{
...
...
@@ -63,7 +63,6 @@ public final class GiveAllCommand implements CommandHandler {
return
;
}
Player
targetPlayer
=
Grasscutter
.
getGameServer
().
getPlayerByUid
(
target
);
if
(
targetPlayer
==
null
)
{
CommandHandler
.
sendMessage
(
sender
,
Grasscutter
.
getLanguage
().
Player_not_found
);
return
;
...
...
src/main/java/emu/grasscutter/command/commands/GiveArtifactCommand.java
View file @
82ec63c4
...
...
@@ -17,7 +17,7 @@ import java.util.List;
@Command
(
label
=
"giveart"
,
usage
=
"giveart [player] <artifactId> <mainPropId> [<appendPropId>[,<times>]]... [level]"
,
description
=
"Gives the player a specified artifact"
,
aliases
=
{
"gart"
},
permission
=
"player.giveart"
)
public
final
class
GiveArtifactCommand
implements
CommandHandler
{
@Override
public
void
execute
(
Player
sender
,
List
<
String
>
args
)
{
public
void
execute
(
Player
sender
,
Player
targetPlayer
,
List
<
String
>
args
)
{
int
size
=
args
.
size
(),
target
,
itemId
,
mainPropId
,
level
=
1
;
ArrayList
<
Integer
>
appendPropIdList
=
new
ArrayList
<>();
String
msg
=
Grasscutter
.
getLanguage
().
GiveArtifact_usage
;
...
...
@@ -71,7 +71,6 @@ public final class GiveArtifactCommand implements CommandHandler {
return
;
}
Player
targetPlayer
=
Grasscutter
.
getGameServer
().
getPlayerByUid
(
target
);
if
(
targetPlayer
==
null
)
{
CommandHandler
.
sendMessage
(
sender
,
Grasscutter
.
getLanguage
().
Player_not_found
);
return
;
...
...
src/main/java/emu/grasscutter/command/commands/GiveCharCommand.java
View file @
82ec63c4
...
...
@@ -15,7 +15,7 @@ import java.util.List;
public
final
class
GiveCharCommand
implements
CommandHandler
{
@Override
public
void
execute
(
Player
sender
,
List
<
String
>
args
)
{
public
void
execute
(
Player
sender
,
Player
targetPlayer
,
List
<
String
>
args
)
{
int
target
,
avatarId
,
level
=
1
,
ascension
;
if
(
sender
==
null
&&
args
.
size
()
<
2
)
{
...
...
@@ -61,7 +61,6 @@ public final class GiveCharCommand implements CommandHandler {
break
;
}
Player
targetPlayer
=
Grasscutter
.
getGameServer
().
getPlayerByUid
(
target
);
if
(
targetPlayer
==
null
)
{
CommandHandler
.
sendMessage
(
sender
,
Grasscutter
.
getLanguage
().
Player_not_found
);
return
;
...
...
src/main/java/emu/grasscutter/command/commands/GiveCommand.java
View file @
82ec63c4
...
...
@@ -18,7 +18,7 @@ import java.util.List;
public
final
class
GiveCommand
implements
CommandHandler
{
@Override
public
void
execute
(
Player
sender
,
List
<
String
>
args
)
{
public
void
execute
(
Player
sender
,
Player
targetPlayer
,
List
<
String
>
args
)
{
int
target
,
item
,
lvl
,
amount
=
1
,
refinement
=
0
;
if
(
sender
==
null
&&
args
.
size
()
<
2
)
{
CommandHandler
.
sendMessage
(
null
,
Grasscutter
.
getLanguage
().
Give_usage
);
...
...
@@ -121,7 +121,6 @@ public final class GiveCommand implements CommandHandler {
break
;
}
Player
targetPlayer
=
Grasscutter
.
getGameServer
().
getPlayerByUid
(
target
);
if
(
targetPlayer
==
null
)
{
CommandHandler
.
sendMessage
(
sender
,
Grasscutter
.
getLanguage
().
Player_not_found
);
...
...
src/main/java/emu/grasscutter/command/commands/GodModeCommand.java
View file @
82ec63c4
...
...
@@ -12,7 +12,7 @@ import java.util.List;
public
final
class
GodModeCommand
implements
CommandHandler
{
@Override
public
void
execute
(
Player
sender
,
List
<
String
>
args
)
{
public
void
execute
(
Player
sender
,
Player
targetPlayer
,
List
<
String
>
args
)
{
if
(
sender
==
null
)
{
CommandHandler
.
sendMessage
(
null
,
Grasscutter
.
getLanguage
().
Run_this_command_in_game
);
return
;
// TODO: toggle player's godmode statue from console or other players
...
...
@@ -32,7 +32,6 @@ public final class GodModeCommand implements CommandHandler {
}
else
{
target
=
sender
.
getUid
();
}
Player
targetPlayer
=
Grasscutter
.
getGameServer
().
getPlayerByUid
(
target
);
if
(
targetPlayer
==
null
)
{
CommandHandler
.
sendMessage
(
sender
,
Grasscutter
.
getLanguage
().
Player_not_found
);
return
;
...
...
src/main/java/emu/grasscutter/command/commands/HealCommand.java
View file @
82ec63c4
...
...
@@ -14,7 +14,7 @@ import java.util.List;
description
=
"Heal all characters in your current team."
,
permission
=
"player.heal"
)
public
final
class
HealCommand
implements
CommandHandler
{
@Override
public
void
execute
(
Player
sender
,
List
<
String
>
args
)
{
public
void
execute
(
Player
sender
,
Player
targetPlayer
,
List
<
String
>
args
)
{
if
(
sender
==
null
)
{
CommandHandler
.
sendMessage
(
null
,
Grasscutter
.
getLanguage
().
Run_this_command_in_game
);
return
;
...
...
src/main/java/emu/grasscutter/command/commands/HelpCommand.java
View file @
82ec63c4
...
...
@@ -13,7 +13,7 @@ import java.util.*;
public
final
class
HelpCommand
implements
CommandHandler
{
@Override
public
void
execute
(
Player
player
,
List
<
String
>
args
)
{
public
void
execute
(
Player
player
,
Player
targetPlayer
,
List
<
String
>
args
)
{
if
(
args
.
size
()
<
1
)
{
HashMap
<
String
,
CommandHandler
>
handlers
=
CommandMap
.
getInstance
().
getHandlers
();
List
<
Command
>
annotations
=
new
ArrayList
<>();
...
...
src/main/java/emu/grasscutter/command/commands/KickCommand.java
View file @
82ec63c4
...
...
@@ -12,10 +12,9 @@ import java.util.List;
public
final
class
KickCommand
implements
CommandHandler
{
@Override
public
void
execute
(
Player
sender
,
List
<
String
>
args
)
{
public
void
execute
(
Player
sender
,
Player
targetPlayer
,
List
<
String
>
args
)
{
int
target
=
Integer
.
parseInt
(
args
.
get
(
0
));
Player
targetPlayer
=
Grasscutter
.
getGameServer
().
getPlayerByUid
(
target
);
if
(
targetPlayer
==
null
)
{
CommandHandler
.
sendMessage
(
sender
,
Grasscutter
.
getLanguage
().
Player_not_found
);
return
;
...
...
src/main/java/emu/grasscutter/command/commands/KillAllCommand.java
View file @
82ec63c4
...
...
@@ -15,9 +15,8 @@ import java.util.List;
public
final
class
KillAllCommand
implements
CommandHandler
{
@Override
public
void
execute
(
Player
sender
,
List
<
String
>
args
)
{
public
void
execute
(
Player
sender
,
Player
targetPlayer
,
List
<
String
>
args
)
{
Scene
mainScene
;
Player
targetPlayer
;
try
{
switch
(
args
.
size
())
{
...
...
Prev
1
2
3
Next
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