Skip to content

Commit

Permalink
allow post command to be dispatched w/o prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
CyR1en committed Nov 5, 2023
1 parent dbf163a commit de790cb
Showing 1 changed file with 62 additions and 41 deletions.
103 changes: 62 additions & 41 deletions src/main/java/com/cyr1en/commandprompter/prompt/PromptManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@
/**
* Class that would manage all prompts.
* <p>
* We need to register a new prompt into this map. And we simply do that by appending a new
* We need to register a new prompt into this map. And we simply do that by
* appending a new
* Prompt class with its optional argument key.
* <p>
* i.e: For chat prompt, the key would just be an empty string, and for an anvil prompt the key
* i.e: For chat prompt, the key would just be an empty string, and for an anvil
* prompt the key
* would be 'a'.
*/
public class PromptManager extends HashMap<String, Class<? extends Prompt>> {
Expand Down Expand Up @@ -91,65 +93,82 @@ public void parse(PromptContext context) {
}

public void sendPrompt(CommandSender sender) {
if (!promptRegistry.containsKey(sender)) return;
if (promptRegistry.get(sender).isEmpty()) return;
if (!promptRegistry.containsKey(sender))
return;

var queue = promptRegistry.get(sender);
if (queue.isEmpty() && !queue.containsPCM())
return;
plugin.getPluginLogger().debug("PromptQueue for %s: %s", sender.getName(), promptRegistry.get(sender));
var prompt = Objects.requireNonNull(promptRegistry.get(sender).peek());
Bukkit.getScheduler().runTaskLater(plugin, prompt::sendPrompt, 2L);
plugin.getPluginLogger().debug("Sent %s to %s", prompt.getClass().getSimpleName(), sender.getName());

if (!queue.isEmpty()) {
var prompt = Objects.requireNonNull(queue.peek());
Bukkit.getScheduler().runTaskLater(plugin, prompt::sendPrompt, 2L);
plugin.getPluginLogger().debug("Sent %s to %s", prompt.getClass().getSimpleName(), sender.getName());
} else if (queue.containsPCM()) {
// This means queue is empty but contains PCM. If it does, we just dispatch it.
dispatchQueue(sender, queue);
}

}

public void processPrompt(PromptContext context) {
var sender = context.getSender();

if (!getPromptRegistry().containsKey(sender)) return;
if (promptRegistry.get(sender).isEmpty()) return;
if (!getPromptRegistry().containsKey(sender))
return;

var promptQueue = promptRegistry.get(sender);
var queue = promptRegistry.get(sender);
if (queue.isEmpty() && !queue.containsPCM())
return;

if (!checkInput(promptQueue, context))
if (!checkInput(queue, context))
return;

getPromptRegistry().get(sender).poll();
getPromptRegistry().get(sender).addCompleted(context.getContent());
plugin.getPluginLogger().debug("PromptQueue for %s: %s", sender.getName(), promptRegistry.get(sender));
if (promptRegistry.get(sender).isEmpty()) {
var queue = promptRegistry.get(sender);

var isCurrentOp = sender.isOp();
plugin.getPluginLogger().debug("Is Currently OP?: " + isCurrentOp);
plugin.getPluginLogger().debug("PromptQueue OP: " + queue.isOp());
if (queue.isOp() && !isCurrentOp) {
sender.setOp(true);
plugin.getPluginLogger().debug("Gave OP status temporarily");
}
plugin.getPluginLogger().debug("Dispatching for %s: %s", sender.getName(), queue.getCompleteCommand());
if (plugin.getConfiguration().showCompleted())
plugin.getMessenger().sendMessage(sender, plugin.getI18N()
.getFormattedProperty("CompletedCommand", queue.getCompleteCommand()));

queue.dispatch(plugin, (Player) sender);

if (!isCurrentOp) {
sender.setOp(false);
plugin.getPluginLogger().debug("Remove OP status");
// Redundancy for de-op
Bukkit.getScheduler().runTaskLater(plugin, () -> {
plugin.getPluginLogger().debug("Remove OP status (redundancy)");
sender.setOp(false);
}, 2L);
}
promptRegistry.unregister(sender);
dispatchQueue((Player) sender, queue);
} else if (sender instanceof Player player)
sendPrompt(player);

}

private void dispatchQueue(CommandSender sender, PromptQueue queue) {
var isCurrentOp = sender.isOp();
plugin.getPluginLogger().debug("Is Currently OP?: " + isCurrentOp);
plugin.getPluginLogger().debug("PromptQueue OP: " + queue.isOp());
if (queue.isOp() && !isCurrentOp) {
sender.setOp(true);
plugin.getPluginLogger().debug("Gave OP status temporarily");
}
plugin.getPluginLogger().debug("Dispatching for %s: %s", sender.getName(), queue.getCompleteCommand());
if (plugin.getConfiguration().showCompleted())
plugin.getMessenger().sendMessage(sender, plugin.getI18N()
.getFormattedProperty("CompletedCommand", queue.getCompleteCommand()));

queue.dispatch(plugin, (Player) sender);

if (!isCurrentOp) {
sender.setOp(false);
plugin.getPluginLogger().debug("Remove OP status");
// Redundancy for de-op
Bukkit.getScheduler().runTaskLater(plugin, () -> {
plugin.getPluginLogger().debug("Remove OP status (redundancy)");
sender.setOp(false);
}, 2L);
}
promptRegistry.unregister(sender);
}

private boolean checkInput(PromptQueue promptQueue, PromptContext context) {
if (promptQueue.peek() == null) return true;
if (promptQueue.peek() == null)
return true;

var prompt = promptQueue.peek();
if (prompt.isValidInput(context.getContent())) return true;
if (prompt.isValidInput(context.getContent()))
return true;

var errMsg = plugin.getPromptConfig().getIVErrMessageWithRegex(prompt.getRegexCheck().pattern());
plugin.getMessenger().sendMessage(context.getSender(), errMsg);
Expand All @@ -166,10 +185,12 @@ public PromptParser getParser() {
}

public void cancel(CommandSender sender, int queueHash) {
if (!promptRegistry.containsKey(sender)) return;
if (!promptRegistry.containsKey(sender))
return;
plugin.getPluginLogger().debug("queueHash: " + queueHash);
plugin.getPluginLogger().debug("registryQueueHash: " + promptRegistry.get(sender).hashCode());
if (queueHash != -1 && queueHash != promptRegistry.get(sender).hashCode()) return;
if (queueHash != -1 && queueHash != promptRegistry.get(sender).hashCode())
return;
promptRegistry.unregister(sender);
plugin.getMessenger().sendMessage(sender, plugin.getI18N().getProperty("PromptCancel"));
plugin.getPluginLogger().debug("Command completion called for: %s", sender.getName());
Expand Down

0 comments on commit de790cb

Please sign in to comment.