-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathCommandRegister.java
172 lines (161 loc) · 8.74 KB
/
CommandRegister.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package com.codehusky.huskycrates.command;
import com.codehusky.huskycrates.HuskyCrates;
import com.google.common.collect.ImmutableMap;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.block.BlockType;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.*;
import org.spongepowered.api.command.spec.CommandSpec;
import org.spongepowered.api.text.Text;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CommandRegister {
public static void register(HuskyCrates plugin) {
CommandSpec mainCommand = CommandSpec.builder()
.child(CommandSpec.builder()
.executor(new BlockCommand())
.arguments(new CrateArgument(Text.of("crate")),
GenericArguments.optionalWeak(GenericArguments.catalogedElement(Text.of("block"),BlockType.class)),
GenericArguments.optionalWeak(GenericArguments.player(Text.of("player"))),
GenericArguments.optional(GenericArguments.integer(Text.of("amount"))),
GenericArguments.optional(GenericArguments.integer(Text.of("damage"))))
.permission("huskycrates.block.base")
.build(),"b","blk","block","chest")
.child(CommandSpec.builder()
.executor(new WandCommand())
.arguments(new CrateArgument(Text.of("crate")),
GenericArguments.optionalWeak(GenericArguments.player(Text.of("player"))))
.permission("huskycrates.wand.base")
.build(),"w","wand")
.child(CommandSpec.builder()
.executor(new KeyCommand())
.arguments(GenericArguments.optionalWeak(GenericArguments.literal(Text.of("virtual"),"v")),
GenericArguments.firstParsing(new CrateArgument(Text.of("crate")), new KeyArgument(Text.of("key"))),
// user to check offline players balances
GenericArguments.optional(GenericArguments.firstParsing(GenericArguments.literal(Text.of("all"),"@a"), GenericArguments.user(Text.of("player")))),
GenericArguments.optionalWeak(GenericArguments.integer(Text.of("amount"))))
.permission("huskycrates.key.base")
.build(),"k","key")
.child(CommandSpec.builder()
.executor(new OpenCommand())
.arguments(
GenericArguments.optionalWeak(GenericArguments.literal(Text.of("keyless"),"nokey")),
new CrateArgument(Text.of("crate")),
GenericArguments.optional(
GenericArguments.firstParsing(
GenericArguments.literal(Text.of("all"),"@a"),
GenericArguments.player(Text.of("player")))))
.permission("huskycrates.open.base")
.build(),"o","open","use")
.child(CommandSpec.builder()
.executor(new BalanceCommand())
// change player to user for offline user
.arguments(GenericArguments.optionalWeak(GenericArguments.user(Text.of("player"))),
GenericArguments.optionalWeak(GenericArguments.uuid(Text.of("uuid"))),
GenericArguments.optionalWeak(GenericArguments.string(Text.of("username"))))
.permission("huskycrates.bal.base")
.build(),"bal","balance")
.child(CommandSpec.builder()
.executor(new ReloadCommand())
.permission("huskycrates.reload")
.build(),"rl","r","reload")
.child(CommandSpec.builder()
.executor(new ItemNBTCommand())
.permission("huskycrates.admin")
.build(),"itemnbt","nbt","in")
.child(CommandSpec.builder()
.executor(new ItemGenerateCommand())
.permission("huskycrates.admin")
.build(),"genitem","generateitem","gitem")
.child(CommandSpec.builder()
.executor(new InventoryGenerateCommand())
.permission("huskycrates.admin")
.build(),"geninvent","generateinventory","ginv")
.child(CommandSpec.builder()
.executor(new CrateGenerateCommand())
.permission("huskycrates.admin")
.arguments(GenericArguments.string(Text.of("name")),
GenericArguments.choices(Text.of("type"), ImmutableMap.<String, String> builder().put("item", "item").put("inventory", "inventory").build()))
.build(),"gencrate","generatecrate","gcrate")
.child(CommandSpec.builder()
.executor(new MainCommand())
.build(),"h","help")
.executor(new MainCommand())
.description(Text.of("Main HuskyCrates Command."))
.extendedDescription(Text.of("For more information on the commands you can use with HuskyCrates, please visit the HuskyCrates documentation.\nYou can find a link to the documentation within /hc help."))
.build();
if(Sponge.getCommandManager().get("crate").isPresent() && HuskyCrates.FORCE_CRATE_CMD) {
Sponge.getCommandManager().removeMapping(Sponge.getCommandManager().get("crate").get());
Sponge.getCommandManager().register(plugin,mainCommand,"crate");
}
Sponge.getCommandManager().register(plugin,mainCommand,"hc","husky","huskycrates");
}
public static class CrateArgument extends CommandElement {
public CrateArgument(@Nullable Text key) {
super(key);
}
@Nullable
@Override
protected Object parseValue(CommandSource source, CommandArgs args) throws ArgumentParseException {
if(HuskyCrates.registry.isCrate(args.peek())){
return HuskyCrates.registry.getCrate(args.next());
}
throw args.createError(Text.of("\"" +args.next() + "\" is not a valid crate."));
}
@Override
public List<String> complete(CommandSource src, CommandArgs args, CommandContext context) {
try {
if(HuskyCrates.registry.isCrate(args.peek())){
return Collections.singletonList(args.next());
}else{
List<String> poss = new ArrayList<>();
for(String crateID : HuskyCrates.registry.getCrates().keySet()){
if(crateID.indexOf(args.peek()) == 0){
poss.add(crateID);
}
}
args.next();
return poss;
}
} catch (ArgumentParseException e) {
e.printStackTrace();
}
return Collections.emptyList();
}
}
public static class KeyArgument extends CommandElement {
public KeyArgument(@Nullable Text key) {
super(key);
}
@Nullable
@Override
protected Object parseValue(CommandSource source, CommandArgs args) throws ArgumentParseException {
if(HuskyCrates.registry.isKey(args.peek())){
return HuskyCrates.registry.getKey(args.next());
}
throw args.createError(Text.of("\"" +args.next() + "\" is not a valid key."));
}
@Override
public List<String> complete(CommandSource src, CommandArgs args, CommandContext context) {
try {
if(HuskyCrates.registry.isKey(args.peek())){
return Collections.singletonList(args.next());
}else{
List<String> poss = new ArrayList<>();
for(String crateID : HuskyCrates.registry.getKeys().keySet()){
if(crateID.indexOf(args.peek()) == 0){
poss.add(crateID);
}
}
args.next();
return poss;
}
} catch (ArgumentParseException e) {
e.printStackTrace();
}
return Collections.emptyList();
}
}
}