Skip to content

Commit

Permalink
allow post command ticks delay
Browse files Browse the repository at this point in the history
  • Loading branch information
CyR1en committed Nov 5, 2023
1 parent a5c3de5 commit dbf163a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,24 +183,29 @@ private List<String> getPrompts(PromptContext promptContext) {
}

private static final Pattern PCM_INDEX_PATTERN = Pattern.compile("p:\\d+");
private static final Pattern PCM_DELAYED_PATTERN = Pattern.compile("exa:\\d+");

private PromptQueue.PostCommandMeta parsePCM(String prompt) {
plugin.getPluginLogger().debug("Parsing PCM: " + prompt);


var matcher = PCM_INDEX_PATTERN.matcher(prompt);
var count = 0;
while (matcher.find()) count++;

plugin.getPluginLogger().debug("Index Count: " + count);
var indexes = new int[count];

var indexes = new int[count];
matcher.reset();
count = 0;
while (matcher.find()) {
var index = matcher.group().split(":")[1];
indexes[count] = Integer.parseInt(index);
count++;
}
var pcm = new PromptQueue.PostCommandMeta(cleanPrompt(prompt), indexes);

var delayMatcher = PCM_DELAYED_PATTERN.matcher(prompt);
var delay = delayMatcher.find() ? Integer.parseInt(delayMatcher.group().split(":")[1]) : 0;
var pcm = new PromptQueue.PostCommandMeta(cleanPrompt(prompt), indexes, delay);
plugin.getPluginLogger().debug("Parsed PCM: " + pcm);
return pcm;
}
Expand Down Expand Up @@ -237,7 +242,7 @@ public String getCleanKey() {
}

public enum PromptQueueArgument implements Keyable {
POST_COMMAND("-exa");
POST_COMMAND("-exa(:\\d+)?");

private final String key;

Expand Down
14 changes: 12 additions & 2 deletions src/main/java/com/cyr1en/commandprompter/prompt/PromptQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public void addPCM(PostCommandMeta pcm) {
postCommandMetas.add(pcm);
}

public boolean containsPCM() {
return postCommandMetas != null && !postCommandMetas.isEmpty();
}

public String getCommand() {
return command;
}
Expand All @@ -86,7 +90,13 @@ public void dispatch(CommandPrompter plugin, Player sender) {
Dispatcher.dispatchCommand(plugin, (Player) sender, getCompleteCommand());

if (!postCommandMetas.isEmpty())
postCommandMetas.forEach(pcm -> execPCM(pcm, sender));
postCommandMetas.forEach(pcm -> {
if (pcm.delayTicks() > 0)
plugin.getServer().getScheduler().runTaskLater(plugin, () -> execPCM(pcm, sender),
pcm.delayTicks());
else
execPCM(pcm, sender);
});
}

private void execPCM(PostCommandMeta postCommandMeta, Player sender) {
Expand Down Expand Up @@ -115,7 +125,7 @@ private void execPCM(PostCommandMeta postCommandMeta, Player sender) {
* @param promptIndex This will hold the index of the prompt answers to be
* injected in this post command.
*/
public record PostCommandMeta(String command, int[] promptIndex) {
public record PostCommandMeta(String command, int[] promptIndex, int delayTicks) {
@Override
public String toString() {
return "PostCommandMeta{" +
Expand Down

0 comments on commit dbf163a

Please sign in to comment.