Skip to content

Commit 7f75c63

Browse files
committed
Implement on the fly phase creation
Signed-off-by: Austin L Mayes <me@austinlm.me>
1 parent 207b16d commit 7f75c63

File tree

2 files changed

+93
-2
lines changed

2 files changed

+93
-2
lines changed

sets/competitve-objectives/src/main/java/net/avicus/atlas/sets/competitve/objectives/commands/PhaseCommands.java

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import com.sk89q.minecraft.util.commands.CommandContext;
66
import com.sk89q.minecraft.util.commands.CommandException;
77
import com.sk89q.minecraft.util.commands.CommandPermissions;
8+
import com.sk89q.minecraft.util.commands.CommandUsageException;
89
import com.sk89q.minecraft.util.commands.NestedCommand;
10+
import java.util.LinkedHashMap;
911
import java.util.List;
1012
import java.util.Map;
1113
import java.util.Map.Entry;
@@ -15,7 +17,10 @@
1517
import net.avicus.atlas.core.command.exception.CommandMatchException;
1618
import net.avicus.atlas.core.match.Match;
1719
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;
1822
import net.avicus.atlas.core.module.objectives.ObjectivesModule;
23+
import net.avicus.atlas.core.module.states.StatesModule;
1924
import net.avicus.atlas.sets.competitve.objectives.bridges.ObjectivesBridge;
2025
import net.avicus.atlas.sets.competitve.objectives.destroyable.DestroyableObjective;
2126
import net.avicus.atlas.sets.competitve.objectives.phases.DestroyablePhase;
@@ -25,14 +30,18 @@
2530
import net.avicus.compendium.commands.exception.TranslatableCommandErrorException;
2631
import net.avicus.compendium.commands.exception.TranslatableCommandWarningException;
2732
import net.avicus.compendium.countdown.Countdown;
33+
import net.avicus.compendium.countdown.CountdownManager;
2834
import net.avicus.compendium.countdown.CountdownTask;
35+
import net.avicus.compendium.inventory.MultiMaterialMatcher;
36+
import net.avicus.compendium.inventory.SingleMaterialMatcher;
2937
import net.avicus.compendium.locale.text.UnlocalizedFormat;
3038
import net.avicus.compendium.locale.text.UnlocalizedText;
3139
import net.avicus.compendium.plugin.CompendiumPlugin;
3240
import net.avicus.compendium.utils.Strings;
3341
import net.md_5.bungee.api.chat.BaseComponent;
3442
import net.md_5.bungee.api.chat.TextComponent;
3543
import org.bukkit.ChatColor;
44+
import org.bukkit.Material;
3645
import org.bukkit.command.CommandSender;
3746
import org.joda.time.Duration;
3847

@@ -41,6 +50,7 @@ public class PhaseCommands {
4150
private static final String LINE =
4251
ChatColor.GOLD + ChatColor.STRIKETHROUGH.toString() + "------" + ChatColor.RESET;
4352
private static final String INDENT = " ";
53+
private static final String ADD_ARGS = "id|name|success message|delay|find|replace";
4454

4555
private static Match getMatch(CommandSender sender) throws CommandException {
4656
MustBePlayerCommandException.ensurePlayer(sender);
@@ -127,7 +137,7 @@ private static DestroyablePhase getPhase(CommandSender sender, String id)
127137
return registry.get(DestroyablePhase.class, id, true).get();
128138
}
129139

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>")
131141
@CommandPermissions("atlas.phases.manage")
132142
public static void remove(CommandContext args, CommandSender sender) throws CommandException {
133143
String id = args.getString(0);
@@ -155,7 +165,7 @@ public static void remove(CommandContext args, CommandSender sender) throws Comm
155165
sender.sendMessage(ChatColor.GREEN + "Phase removed");
156166
}
157167

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>")
159169
@CommandPermissions("atlas.phases.manage")
160170
public static void modtime(CommandContext args, CommandSender sender) throws CommandException {
161171
String id = args.getString(0);
@@ -182,6 +192,79 @@ public static void modtime(CommandContext args, CommandSender sender) throws Com
182192
);
183193
}
184194

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+
185268
public static class Super {
186269

187270
@Command(aliases = "phase", usage = "<>", desc = ".", min = 1)

sets/competitve-objectives/src/main/java/net/avicus/atlas/sets/competitve/objectives/phases/DestroyablePhase.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,14 @@ public void removePhase(DestroyablePhase phase) {
263263
}
264264
}
265265

266+
public void addPhase(DestroyablePhase phase) {
267+
if (this.passPhase.isPresent()) {
268+
this.passPhase.get().addPhase(phase);
269+
} else {
270+
this.passPhase = Optional.of(phase);
271+
}
272+
}
273+
266274
@Override
267275
public boolean equals(Object o) {
268276
if (this == o) {

0 commit comments

Comments
 (0)