5
5
import com .sk89q .minecraft .util .commands .CommandContext ;
6
6
import com .sk89q .minecraft .util .commands .CommandException ;
7
7
import com .sk89q .minecraft .util .commands .CommandPermissions ;
8
+ import com .sk89q .minecraft .util .commands .CommandUsageException ;
8
9
import com .sk89q .minecraft .util .commands .NestedCommand ;
10
+ import java .util .LinkedHashMap ;
9
11
import java .util .List ;
10
12
import java .util .Map ;
11
13
import java .util .Map .Entry ;
15
17
import net .avicus .atlas .core .command .exception .CommandMatchException ;
16
18
import net .avicus .atlas .core .match .Match ;
17
19
import net .avicus .atlas .core .match .registry .MatchRegistry ;
20
+ import net .avicus .atlas .core .module .locales .LocalesModule ;
21
+ import net .avicus .atlas .core .module .locales .LocalizedXmlString ;
18
22
import net .avicus .atlas .core .module .objectives .ObjectivesModule ;
23
+ import net .avicus .atlas .core .module .states .StatesModule ;
19
24
import net .avicus .atlas .sets .competitve .objectives .bridges .ObjectivesBridge ;
20
25
import net .avicus .atlas .sets .competitve .objectives .destroyable .DestroyableObjective ;
21
26
import net .avicus .atlas .sets .competitve .objectives .phases .DestroyablePhase ;
25
30
import net .avicus .compendium .commands .exception .TranslatableCommandErrorException ;
26
31
import net .avicus .compendium .commands .exception .TranslatableCommandWarningException ;
27
32
import net .avicus .compendium .countdown .Countdown ;
33
+ import net .avicus .compendium .countdown .CountdownManager ;
28
34
import net .avicus .compendium .countdown .CountdownTask ;
35
+ import net .avicus .compendium .inventory .MultiMaterialMatcher ;
36
+ import net .avicus .compendium .inventory .SingleMaterialMatcher ;
29
37
import net .avicus .compendium .locale .text .UnlocalizedFormat ;
30
38
import net .avicus .compendium .locale .text .UnlocalizedText ;
31
39
import net .avicus .compendium .plugin .CompendiumPlugin ;
32
40
import net .avicus .compendium .utils .Strings ;
33
41
import net .md_5 .bungee .api .chat .BaseComponent ;
34
42
import net .md_5 .bungee .api .chat .TextComponent ;
35
43
import org .bukkit .ChatColor ;
44
+ import org .bukkit .Material ;
36
45
import org .bukkit .command .CommandSender ;
37
46
import org .joda .time .Duration ;
38
47
@@ -41,6 +50,7 @@ public class PhaseCommands {
41
50
private static final String LINE =
42
51
ChatColor .GOLD + ChatColor .STRIKETHROUGH .toString () + "------" + ChatColor .RESET ;
43
52
private static final String INDENT = " " ;
53
+ private static final String ADD_ARGS = "id|name|success message|delay|find|replace" ;
44
54
45
55
private static Match getMatch (CommandSender sender ) throws CommandException {
46
56
MustBePlayerCommandException .ensurePlayer (sender );
@@ -127,7 +137,7 @@ private static DestroyablePhase getPhase(CommandSender sender, String id)
127
137
return registry .get (DestroyablePhase .class , id , true ).get ();
128
138
}
129
139
130
- @ Command (aliases = "remove" , desc = "Remove a phase by ID" , max = 1 , min = 1 )
140
+ @ Command (aliases = "remove" , desc = "Remove a phase by ID" , max = 1 , min = 1 , usage = "<id>" )
131
141
@ CommandPermissions ("atlas.phases.manage" )
132
142
public static void remove (CommandContext args , CommandSender sender ) throws CommandException {
133
143
String id = args .getString (0 );
@@ -155,7 +165,7 @@ public static void remove(CommandContext args, CommandSender sender) throws Comm
155
165
sender .sendMessage (ChatColor .GREEN + "Phase removed" );
156
166
}
157
167
158
- @ Command (aliases = "modtime" , desc = "Modify a phase's application time" , max = 2 , min = 2 )
168
+ @ Command (aliases = "modtime" , desc = "Modify a phase's application time" , max = 2 , min = 2 , usage = "<id> <time>" )
159
169
@ CommandPermissions ("atlas.phases.manage" )
160
170
public static void modtime (CommandContext args , CommandSender sender ) throws CommandException {
161
171
String id = args .getString (0 );
@@ -182,6 +192,79 @@ public static void modtime(CommandContext args, CommandSender sender) throws Com
182
192
);
183
193
}
184
194
195
+ @ Command (aliases = "add" , desc = "Add a phase" , usage = ADD_ARGS )
196
+ @ CommandPermissions ("atlas.phases.manage" )
197
+ public static void add (CommandContext args , CommandSender sender ) throws CommandException {
198
+ Match match = getMatch (sender );
199
+ ObjectivesBridge bridge = getBridge (sender );
200
+ String [] indArgs = args .getJoinedStrings (0 ).split ("\\ |" );
201
+ if (indArgs .length != 6 ) {
202
+ throw new CommandUsageException ("Wrong number of arguments" , "/phases add " + ADD_ARGS );
203
+ }
204
+ try {
205
+ String id = indArgs [0 ];
206
+ String nameRaw = indArgs [1 ];
207
+ String successRaw = indArgs [2 ];
208
+ Duration delay = Strings .toDuration (indArgs [3 ]);
209
+ String countdownRaw = "{phases.transition}" ;
210
+ String failRaw = "{phases.monument.fail}" ;
211
+ LocalizedXmlString name = match .getRequiredModule (LocalesModule .class ).parse (nameRaw );
212
+ LocalizedXmlString countdown = match .getRequiredModule (LocalesModule .class )
213
+ .parse (countdownRaw );
214
+ LocalizedXmlString success = match .getRequiredModule (LocalesModule .class ).parse (successRaw );
215
+ LocalizedXmlString fail = match .getRequiredModule (LocalesModule .class ).parse (failRaw );
216
+ LinkedHashMap <MultiMaterialMatcher , SingleMaterialMatcher > materials = new LinkedHashMap <>();
217
+ materials .put (parseMultiMatcher (indArgs [4 ]), parseSingleMatcher (indArgs [5 ]));
218
+ DestroyablePhase phase = new DestroyablePhase (match , id , name , countdown , success , fail ,
219
+ materials , delay , Optional .empty (), 0 ,
220
+ Optional .empty (), Optional .empty (), Optional .empty ());
221
+ match .getRegistry ().add (phase );
222
+ CountdownManager manager = CompendiumPlugin .getInstance ().getCountdownManager ();
223
+ if (manager .isRunning (PhaseApplyCountdown .class )) {
224
+ manager .getCountdowns ().keySet ().stream ().filter (c -> c instanceof PhaseApplyCountdown )
225
+ .forEach (c -> {
226
+ ((PhaseApplyCountdown ) c ).getPhase ().addPhase (phase );
227
+ });
228
+ sender .sendMessage (ChatColor .GREEN + "Phase added to end of phase chain" );
229
+ } else {
230
+ for (DestroyableObjective objective : bridge .getLeakables ()) {
231
+ objective .setPhase (Optional .of (phase ));
232
+ }
233
+ bridge .populatePhaseCache ();
234
+ if (match .getRequiredModule (StatesModule .class ).isPlaying ()) {
235
+ bridge .startPhaseCountdowns (match );
236
+ }
237
+ sender .sendMessage (ChatColor .GREEN + "Phase added" );
238
+ }
239
+ } catch (Exception e ) {
240
+ // e.printStackTrace();
241
+ throw new TranslatableCommandErrorException (
242
+ new UnlocalizedFormat ("Failed to add phase: " + e .getMessage ()));
243
+ }
244
+ }
245
+
246
+ private static MultiMaterialMatcher parseMultiMatcher (String raw ) {
247
+ List <SingleMaterialMatcher > matchers = Lists .newArrayList ();
248
+ for (String s : raw .split ("," )) {
249
+ matchers .add (parseSingleMatcher (s ));
250
+ }
251
+ return new MultiMaterialMatcher (matchers );
252
+ }
253
+
254
+ private static SingleMaterialMatcher parseSingleMatcher (String raw ) {
255
+ String [] parts = raw .split (":" );
256
+ Material material = Enum .valueOf (
257
+ Material .class , parts [0 ].toUpperCase ().replace (" " , "_" ).replace ("-" , "_" ));
258
+ Optional <Byte > data = Optional .empty ();
259
+ if (parts .length > 1 ) {
260
+ data = Optional .of (Byte .valueOf (parts [1 ]));
261
+ }
262
+ if (!material .isSolid ()) {
263
+ throw new IllegalArgumentException ("Materials must be solid" );
264
+ }
265
+ return new SingleMaterialMatcher (material , data );
266
+ }
267
+
185
268
public static class Super {
186
269
187
270
@ Command (aliases = "phase" , usage = "<>" , desc = "." , min = 1 )
0 commit comments