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
3a28e741
Commit
3a28e741
authored
May 05, 2022
by
AnimeGitB
Committed by
Melledy
May 05, 2022
Browse files
Add R# LVL# X# arg syntax to GiveCommand
parent
7d85c53e
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/main/java/emu/grasscutter/command/commands/GiveCommand.java
View file @
3a28e741
...
...
@@ -12,10 +12,23 @@ import emu.grasscutter.game.props.ActionReason;
import
java.util.LinkedList
;
import
java.util.List
;
import
java.util.regex.Pattern
;
import
java.util.regex.Matcher
;
@Command
(
label
=
"give"
,
usage
=
"give <itemId|itemName> [amount] [level]"
,
description
=
"Gives an item to you or the specified player"
,
aliases
=
{
"g"
,
"item"
,
"giveitem"
},
permission
=
"player.give"
)
public
final
class
GiveCommand
implements
CommandHandler
{
Pattern
lvlRegex
=
Pattern
.
compile
(
"l(?:vl?)?(\\d+)"
);
// Java is a joke of a proglang that doesn't have raw string literals
Pattern
refineRegex
=
Pattern
.
compile
(
"r(\\d+)"
);
Pattern
amountRegex
=
Pattern
.
compile
(
"((?<=x)\\d+|\\d+(?=x))"
);
private
int
matchIntOrNeg
(
Pattern
pattern
,
String
arg
)
{
Matcher
match
=
pattern
.
matcher
(
arg
);
if
(
match
.
find
())
{
return
Integer
.
parseInt
(
match
.
group
(
1
));
// This should be exception-safe as only \d+ can be passed to it (i.e. non-empty string of pure digits)
}
return
-
1
;
}
@Override
public
void
execute
(
Player
sender
,
Player
targetPlayer
,
List
<
String
>
args
)
{
...
...
@@ -28,6 +41,27 @@ public final class GiveCommand implements CommandHandler {
int
amount
=
1
;
int
refinement
=
0
;
for
(
int
i
=
args
.
size
()-
1
;
i
>=
0
;
i
--)
{
// Reverse iteration as we are deleting elements
String
arg
=
args
.
get
(
i
).
toLowerCase
();
boolean
deleteArg
=
false
;
int
argNum
;
if
((
argNum
=
matchIntOrNeg
(
lvlRegex
,
arg
))
!=
-
1
)
{
lvl
=
argNum
;
deleteArg
=
true
;
}
if
((
argNum
=
matchIntOrNeg
(
refineRegex
,
arg
))
!=
-
1
)
{
refinement
=
argNum
;
deleteArg
=
true
;
}
if
((
argNum
=
matchIntOrNeg
(
amountRegex
,
arg
))
!=
-
1
)
{
amount
=
argNum
;
deleteArg
=
true
;
}
if
(
deleteArg
)
{
args
.
remove
(
i
);
}
}
switch
(
args
.
size
())
{
case
4
:
// <itemId|itemName> [amount] [level] [refinement]
try
{
...
...
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